def __init__(self, directory, server, key, extentions): """ Initialization of a log Collection agent @param directory: path to the directory containing files log to send and watch @param server: address of a collection server where to send logs @param AgentKey: @param AgentCertificat: """ self.server = urljoin('https://%s' % server, 'send/%s' % key) self.http = httplib2.Http(disable_ssl_certificate_validation=True) if extentions: extentions = ['' if x=='noext' else x for x in extentions] self.lw = LogWatcher(directory,self.sendLine, extentions) signal.signal(signal.SIGTERM, self._exitHandler) self.exit = False
class logAgent(): """ This class represent an example of an HTTPS agent """ def __init__(self, directory, server, key, extentions): """ Initialization of a log Collection agent @param directory: path to the directory containing files log to send and watch @param server: address of a collection server where to send logs @param AgentKey: @param AgentCertificat: """ self.server = urljoin('https://%s' % server, 'send/%s' % key) self.http = httplib2.Http(disable_ssl_certificate_validation=True) if extentions: extentions = ['' if x=='noext' else x for x in extentions] self.lw = LogWatcher(directory,self.sendLine, extentions) signal.signal(signal.SIGTERM, self._exitHandler) self.exit = False def _exitHandler(self): """ called when exiting the logAgent """ self.exit = True print "Exiting... please wait..." def run(self): """ start watching the defined directory """ self.lw.loop() def sendLine(self,filename,lines): """ This function is called to send logs line to a log collection server @param filename: the name of the file that contain logsLine to send @param lines: logs data to send to collection server """ print "sending" size = 0 for line in lines: if not self.exit: if line.strip(): print "line: " ,line try: resp, content = self.http.request(self.server, 'POST', line, headers={'Content-Type': 'text/plain'} ) print "resp",resp if not resp['status'] == '200': print "request error" print resp return size size += len(line) except socket.error, msg: if socket.errno.errorcode[111] == 'ECONNREFUSED': print "Socket error: connection refused" return size else: return size return size