def recordSpamMetrics(messages): if CONFIG.graphiteHost is not None: gc = GraphiteClient(prefix='spam', graphite_server=CONFIG.graphiteHost, graphite_port=CONFIG.graphitePort, system_name='') durations = [] for m in messages: if m.date: durations.append(('duration.detect_minutes', m.ageMinutes())) gc.send('daily.by_hour.%d.count' % m.date.hour, 1) gc.send_list(durations)
def cmd_hack(): #account = CONFIG.accounts[0] #imap = connectIMAP(account) #try: # spamFolder = account.spamFolder # info('check if %s exists:' % spamFolder) # exists = imap.folder_exists(spamFolder) # info('?', exists) # if not exists: # info('create folder:', imap.create_folder(spamFolder)) #finally: # imap.logout() #data = """spam-rescore moves for the last 24 hours: #[email protected]: 0 message(s) #[email protected]: 16 message(s) #[email protected]: 17 message(s)""" #data = "[email protected]: 2 message(s)" #m = METRICS_PAT.findall(data) #info(m) # for s in data.split('\n'): # info('try "%s":' % s) # gc = GraphiteClient(prefix='test', graphite_server=CONFIG.graphiteHost, graphite_port=CONFIG.graphitePort, system_name='') # for i in range(10): # ts = time.time() - 1000 * i - i # info('metric: %d' % i) # gc.send('foo', i, ts) # log = logging.getLogger('hack') # log.info('hack running, conf is %s', str(CONFIG)) # counts = Counter() # counts['*****@*****.**'] += 77 # counts['*****@*****.**'] += 88 # recordDailyMetrics(log, counts) gc = GraphiteClient(prefix='test.by_minute', graphite_server=CONFIG.graphiteHost, graphite_port=CONFIG.graphitePort, system_name='') for i in range(1, 11): info('%d ' % i) gc.send('counters.a.count', i) gc.send('counters.b.count', i * 2) gc.send('counters.c.count', i * 3) time.sleep(1)
from graphitesend import GraphiteClient from json import loads from kafka import KafkaConsumer # Graphite host GRAPHITE_HOSTNAME = "graphite" # Kafka broker address and topic names definition SERVER_URI = "<KAFKA_SERVER_URI>" SERVER_PORT = "9092" PREDICTION_TOPIC = "RAW_AND_PREDICTED" # Kafka consumer setup consumer = KafkaConsumer(f"{PREDICTION_TOPIC}", bootstrap_servers=f"{SERVER_URI}:{SERVER_PORT}", value_deserializer=lambda m: loads(m.decode('utf-8'))) # Graphite client setup client = GraphiteClient(GRAPHITE_HOSTNAME) # For every new message, send data to Graphite for message in consumer: key = message.key.decode('utf-8') payload = message.value client.send("predicted", float(payload['PREDICTED'])) client.send("real", float(payload['SILICA_CONCENTRATE']))
import sys import json from time import sleep from graphitesend import GraphiteClient, TemplateFormatter, GraphiteSendException client = GraphiteClient('localhost', 2003, formatter=TemplateFormatter("{name}")) for line in sys.stdin: data = json.loads(line) stat = data['name'] if 'username' not in data['tags']: continue username = data['tags']['username'] metric = 'grid.{}.{}'.format(stat, username) print(metric) for pt in data['datapoints']: (timestamp_ms, value, _) = pt timestamp = int(timestamp_ms / 1000) while True: try: client.send(metric, value, timestamp=timestamp) break except GraphiteSendException: sleep(60)
async def write_stage_test(self, graphite: GraphiteClient, app_id, test_result, completion_time, job_group_alias): key = f"app.{app_id}.job_group.{job_group_alias}.test.{self.test_name}" graphite.send(key, test_result, completion_time.timestamp())