#!/usr/bin/env ruby # coding: utf-8 # # Logentries Ruby monitoring agent # Copyright 2010,2011 Logentries, Jlizard # Mark Lacomber # module Le module Host class HTTPS class TCPSOCKET attr_accessor :sock, :conn, :key, :location def initialize(key, location) @key = key @location = location begin createSocket(key, location) rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e $stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}" end end def createSocket(key, location) addr = sprintf('/%s/hosts/%s/?realtime=1', key, location) # Open the TCP connection to the Logentries Server @sock = TCPSocket.new('api.logentries.com', 443) @conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new()) @conn.sync_close = true @conn.connect # Set up connection with Logentries API to receive messages in chunks, i.e, logs request = sprintf("PUT %s HTTP/1.1\r\n", addr) @conn.print(request) @conn.print("Accept-Encoding: identity\r\n") @conn.print("Transfer_Encoding: chunked\r\n\r\n") end def deliver(message) # Sends the log to the Logentries Server begin @conn.print(message + "\r\n") rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ENOTCONN, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e $stderr.puts "WARNING: #{e.class} sending log #{e.message}" begin createSocket(@key, @location) rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e $stderr.puts "WARNING: #{e.class} creating the connection to Logentries. #{e.message}" end end end end end end end