def sendToKafka(self, message): # try: # log.ni("KafkaConnector * Send to kafka: %s", (message,), INFO=2) # self._getKafkaProducer().produce(message) # except Exception as e: # log.ni("KafkaConnector * didn't send %s", (e,), WARN=4) if self.kafkaProducer: self.kafkaProducer.produce(message) else: log.ni("KafkaConnector: sending %s without producer", (message,), ERR=2)
def sendToKafka(self, message): # try: # log.ni("KafkaConnector * Send to kafka: %s", (message,), INFO=2) # self._getKafkaProducer().produce(message) # except Exception as e: # log.ni("KafkaConnector * didn't send %s", (e,), WARN=4) if self.kafkaProducer: self.kafkaProducer.produce(message) else: log.ni("KafkaConnector: sending %s without producer", (message, ), ERR=2)
def transformLoggerLevel(level): lvlMap = {'DEBUG': ('DBG', 3), 'WARNING': ('WARN', 3), 'ERROR': ('ERR', 4), 'INFO': ('INFO', 3), 'EXCEPTION': ('ERR', 4), 'Level 5': ('INFO', 2) } if level in lvlMap: return lvlMap[level] log.ni("Unknown log level %s", level, INFO=3) return 'ERR', 3
def transformLoggerLevel(level): lvlMap = { 'DEBUG': ('DBG', 3), 'WARNING': ('WARN', 3), 'ERROR': ('ERR', 4), 'INFO': ('INFO', 3), 'EXCEPTION': ('ERR', 4), 'Level 5': ('INFO', 2) } if level in lvlMap: return lvlMap[level] log.ni("Unknown log level %s", level, INFO=3) return 'ERR', 3
def request( self, url='/getMonitors', paramList={} ): """ * Autorizovany request na pozadovana data * ID pripadnych chyb: http://uptimerobot.com/api * * @param url URL pro data * @param paramList parametry requestu * @return slovnik s navratovym stavem + daty """ ret = { 'statusCode' : 200, 'statusMessage' : 'OK' } parList = { 'apiKey' : self.__apiKey, 'format' : 'json', 'noJsonCallback': 1 } parList.update(paramList) conUrl = "%s?%s" % (url, urllib.urlencode(parList) ) log.ni("UptimeRobot - url: %s", (conUrl,), INFO=3) connection = httplib.HTTPConnection(self.__apiDomain) connection.request('GET', conUrl) response = connection.getresponse() status = response.status data = response.read().decode('utf-8') if status == 200: data = json.loads(data) # dotaz probehl v poradku if data['stat'] == 'ok': log.ni("UptimeRobot - response - status: %s, data: %s", (status, data), INFO=3) ret['data'] = data else: log.ni("UptimeRobot - response - stat: %s, id: %s, message: %s", ( data['stat'], data['id'], data['message'] ), ERR=4) ret['statusCode'] = 500 ret['statusMessage'] = "stat: %s, id: %s, message: %s" % ( data['stat'], data['id'], data['message'] ) else: log.ni("uptimeRobot - response - status: %s, data: %s", (status, data), ERR=4) ret['statusCode'] = status ret['statusMessage'] = data return ret
def request( self, url='/monitors', paramList={}, method='GET' ): """ * Autorizovany request na pozadovana data * * @param url URL pro data * @param paramList parametry requestu * @param method POST/GET/PUT, POST pro setovani, GET pro ziskani dat, PUT pro aktualizaci dat * @return slovnik s navratovym stavem + daty """ ret = { 'statusCode' : 200, 'statusMessage' : 'OK' } parList = urllib.urlencode(paramList) header = { "Authorization" : "Zoho-authtoken %s" % self.__authToken, "Accept" : "application/json; version=2.0" } conUrl = "%s%s" % (self.__apiPath, url) log.ni("Site24x7 - url: %s, param: %s, header: %s", (conUrl, parList, header), INFO=3) connection = httplib.HTTPSConnection(self.__apiDomain) connection.request(method, conUrl, parList, header) response = connection.getresponse() status = response.status data = response.read() if status == 200: data = json.loads(data) # dotaz probehl v poradku, code != 0 indikuje chybu if data['code'] == 0: log.ni("Site24x7 - response - status: %s, data: %s", (status, data), INFO=3) ret['data'] = data['data'] else: log.ni("Site24x7 - response - code: %s, message: %s", ( data['code'], data['message'] ), ERR=4) ret['statusCode'] = 500 ret['statusMessage'] = "code: %s - %s" % ( data['code'], data['message'] ) else: log.ni("Site24x7 - response - status: %s, data: %s", (status, data), ERR=4) ret['statusCode'] = status ret['statusMessage'] = data return ret
def wrapper(*args, **kwargs): self.thread = Thread(target=self.work, args=args, kwargs=kwargs) self.thread.start() # if not self.timeout_step: # self.timeout_step = self.timeout / 20.0 # # while self.thread and self.thread.is_alive() and not self.finished and self.timer < self.timeout: # time.sleep(self.timeout_step) # self.timer += self.timeout_step # if not self.finished: # log.ni("TimeoutError - Connecting to kafka", ERR=4) # #raise TimeoutError() self.thread.join(self.timeout) if self.thread.is_alive(): log.ni("TimeoutError - Connecting to kafka", ERR=4) raise TimeoutError() return self.result
def _connectKafka(self): log.ni("KafkaConnector: Connecting to kafka at %s ...", (self.config.get("kafka", "zk_hosts"), ), WARN=4) # debugging only - fake connection latency # sleep = 1 + random.random()*1 # print "sleep ", sleep # time.sleep(sleep) try: self.kafkaClient = KafkaClient( zookeeper_hosts=self.config.get("kafka", "zk_hosts"), socket_timeout_ms=500, offsets_channel_socket_timeout_ms=10 * 500) self.kafkaTopic = self.kafkaClient.topics[self.config.get( "kafka", "topic")] self.kafkaProducer = self.kafkaTopic.get_producer( linger_ms=int(self.config.get("kafka", "lingerTimeMs")), min_queued_messages=int( self.config.get("kafka", "minBatchSize"))) log.ni("KafkaConnector: got one", INFO=1) except Exception as e: log.ni("KafkaConnector: didn't find one %s", (traceback.print_exc(), ), WARN=4)
def _getKafkaProducer(self): if not self.kafkaProducer: log.ni("KafkaConnector: no kafka producer", INFO=1) try: self._connectKafka() except Exception as e: log.ni("KafkaConnector: didn't get producer %s", (traceback.print_exc(),), WARN=4) if not self.kafkaProducer: # raise KafkaProducerUnavailable return None log.ni("KafkaConnector: got kafka producer", INFO=1) return self.kafkaProducer
def _connectKafka(self): log.ni("KafkaConnector: Connecting to kafka at %s ...", (self.config.get("kafka", "zk_hosts"),), WARN=4) # debugging only - fake connection latency # sleep = 1 + random.random()*1 # print "sleep ", sleep # time.sleep(sleep) try: self.kafkaClient = KafkaClient(zookeeper_hosts=self.config.get("kafka", "zk_hosts"), socket_timeout_ms=500, offsets_channel_socket_timeout_ms=10 * 500) self.kafkaTopic = self.kafkaClient.topics[self.config.get("kafka", "topic")] self.kafkaProducer = self.kafkaTopic.get_producer(linger_ms=int(self.config.get("kafka", "lingerTimeMs")), min_queued_messages=int(self.config.get("kafka", "minBatchSize"))) log.ni("KafkaConnector: got one", INFO=1) except Exception as e: log.ni("KafkaConnector: didn't find one %s", (traceback.print_exc(),), WARN=4)
def _getKafkaProducer(self): if not self.kafkaProducer: log.ni("KafkaConnector: no kafka producer", INFO=1) try: self._connectKafka() except Exception as e: log.ni("KafkaConnector: didn't get producer %s", (traceback.print_exc(), ), WARN=4) if not self.kafkaProducer: # raise KafkaProducerUnavailable return None log.ni("KafkaConnector: got kafka producer", INFO=1) return self.kafkaProducer
#!/usr/bin/python # -*- coding: utf-8 -*- import urllib, httplib, datetime, time, pprint from logni import log try: import json log.ni( 'use python module json', DBG=1 ) except ImportError: import simplejson as json log.ni( 'use python module simplejson as json', DBG=1 ) class UptimeRobot: def __init__(self, apiKey): """ * Inicializace objektu pro praci s uptimeRobot API * * @param apiKey autorizacni retezec """ self.__apiKey = apiKey self.__apiDomain = 'api.uptimerobot.com'
def loggingLogniAdapter(level, msg, *args, **kwargs): lvlName, lvlVal = transformLoggerLevel(logging.getLevelName(level)) kwargs[lvlName] = lvlVal log.ni("%s%s" % (module, msg), *args, offset=3, **kwargs)
def request( self, url='/checks', paramList={}, method='GET' ): """ * Autorizovany request na pozadovana data * * @param url URL pro data * @param paramList parametry requestu * @param method POST/GET/PUT, POST pro setovani, GET pro ziskani dat, PUT pro aktualizaci dat * @return slovnik s navratovym stavem + daty """ ret = { 'statusCode' : 200, 'statusMessage' : 'OK' } parList = urllib.urlencode(paramList) base64str = base64.encodestring( '%s:%s' % (self.__username, self.__userPasswd) ).replace('\n', '') header = { "App-Key" : self.__apiKey, "Authorization" : "Basic %s" % base64str } conUrl = "%s%s" % (self.__apiPath, url) log.ni("Pingdom - url: %s, param: %s, header: %s", (conUrl, parList, header), INFO=3) connection = httplib.HTTPSConnection(self.__apiDomain) connection.request(method, conUrl, parList, header) response = connection.getresponse() status = response.status data = response.read() if status == 200: data = json.loads(data) log.ni("Site24x7 - response - status: %s, data: %s", (status, data), INFO=3) ret['data'] = data elif data: data = json.loads(data) data = data['error'] log.ni("Pingdom - response - code: %s, desc: %s, message: %s", ( data['statuscode'], data['statusdesc'], data['errormessage'] ), ERR=4) ret['statusCode'] = data['statuscode'] ret['statusMessage'] = "%s - %s" % ( data['statusdesc'], data['errormessage'] ) else: log.ni("Pingdom - response - status: %s, data: %s", (status, data), ERR=4) ret['statusCode'] = status ret['statusMessage'] = data return ret