def start_listener(self): """Listener""" self.start_timer() while True: try: data, addr = self.sock.recvfrom(UDPServer.BUFSIZE) except KeyboardInterrupt: pystats_log.print_msg("KeyboardInterrupt: Terminate Server!") sys.exit() try: jdata = json.loads(data) except ValueError: pystats_log.print_msg("Data is not correct json string") continue self.metricsmgr.add_metric(jdata)
def __init__(self, common_queue): self.queue = common_queue self.forwarders = {} self.cfg = pystat_config.PyStatConfig() self.debug_mode = self.cfg.parsedyaml.get('debug_mode', True) pystats_log.print_msg("StatsForwarder Initialized!") for forwarder in self.cfg.parsedyaml['forwarders'].keys(): fwobj = self.cfg.parsedyaml['forwarders'][forwarder] mod = __import__(StatsForwarder.FORWARDERS[forwarder]['module']) classobj = getattr( mod, StatsForwarder.FORWARDERS[forwarder]['classname']) if forwarder == "kafka": kafka_broker = fwobj['kafka_broker'] kafka_apikey = fwobj['kafka_apikey'] kafka_tenant_id = fwobj['kafka_tenant_id'] kafka_topic = fwobj['kafka_topic'] self.forwarders[forwarder] = classobj(kafka_broker, kafka_apikey, kafka_tenant_id, kafka_topic)
def publish_to_kafka_broker(self, metric_name, kafka_topic, value, tags, debug=True): """ Generate a payload and publish the data to kafka broker. """ now = datetime.datetime.utcnow() timestamp = now.strftime('%Y-%m-%dT%H:%M:%S.000Z') metric = {} metric['apikey'] = self.kafka_apikey metric['tenant_id'] = self.kafka_tenant_id #host should be passed by the caller. statsd might not be # running on the same host as the caller. #metric['host'] = socket.gethostname() metric['name'] = metric_name metric['value'] = value metric['@version'] = '1' metric['@timestamp'] = timestamp for tag in tags: metric[tag] = tags[tag] if debug: msg = "DEBUG-ON: Kafka metrics tobe sent: %s" % (metric) pystats_log.print_msg(msg) else: kafka = KafkaClient(self.kafka_broker) try: producer = SimpleProducer(kafka) result = producer.send_messages(kafka_topic, json.dumps(metric)) msg = "Kafka Metrics Pushed: [%s] [%s]" % (metric, str(result)) pystats_log.print_msg(msg) except socket.gaierror as gaierror: msg = "Publish metric [%s] failed. [%s]" % \ (metric, str(gaierror)) pystats_log.print_msg(msg)
def run(self): pystats_log.print_msg("Timer Monitor start") while True: time.sleep(self.sleep_interval) pystats_log.print_msg("TimerMonitor Wokeup") self.metricsmgr.forward_metrics()
def start_timer(self): pystats_log.print_msg("Start Timer Monitor") self.timermonitor = TimerMonitor(10, self.metricsmgr) self.timermonitor.start()