Skip to content

Instantly share code, notes, and snippets.

@changsijay
Last active January 16, 2018 11:44
Show Gist options
  • Save changsijay/d0969abc51e7afeb56ae65e9330cb2bf to your computer and use it in GitHub Desktop.
Save changsijay/d0969abc51e7afeb56ae65e9330cb2bf to your computer and use it in GitHub Desktop.
prometheus exporter
from prometheus_client import start_http_server, Metric, REGISTRY
class MyCollector(object):
def collect(self):
metric = Metric('current_devices', 'current device resource', 'gauge')
for someloop:
# it taks about 3 minutes to finish the loop
metric.add_sample('current_device_count', value=count, labels=labels)
yield metric
if __name__ == '__main__':
start_http_server(int(sys.argv[1])) # port number
REGISTRY.register(MyCollector())
while True:
# every 30 minutes to generate metrics
time.sleep(1800)
+07 - 2017/12/17, 17:01:29 -> +07 - 2018/01/16, 17:01:29: OK
+07 - 2017/12/17, 17:02:29 -> +07 - 2018/01/16, 17:00:29: OK
+07 - 2017/12/17, 17:03:29 -> +07 - 2018/01/16, 16:59:29: OK
+07 - 2017/12/17, 17:04:29 -> +07 - 2018/01/16, 16:58:29: OK
+07 - 2017/12/17, 17:30:29 -> +07 - 2018/01/16, 16:32:29: OK
+07 - 2017/12/17, 17:31:29 -> +07 - 2018/01/16, 16:31:29: OK
+07 - 2017/12/17, 17:32:29 -> +07 - 2018/01/16, 16:30:29: OK
+07 - 2017/12/17, 17:33:29 -> +07 - 2018/01/16, 16:29:29: OK
+07 - 2017/12/17, 17:34:29 -> +07 - 2018/01/16, 16:28:29: OK
+07 - 2017/12/17, 18:00:29 -> +07 - 2018/01/16, 16:02:29: OK
+07 - 2017/12/17, 18:01:29 -> +07 - 2018/01/16, 16:01:29: OK
+07 - 2017/12/17, 18:02:29 -> +07 - 2018/01/16, 16:00:29: OK
+07 - 2017/12/17, 18:03:29 -> +07 - 2018/01/16, 15:59:29: OK
+07 - 2017/12/17, 18:04:29 -> +07 - 2018/01/16, 15:58:29: OK
+07 - 2017/12/17, 18:30:29 -> +07 - 2018/01/16, 15:32:29: OK
+07 - 2017/12/17, 18:31:29 -> +07 - 2018/01/16, 15:31:29: OK
+07 - 2017/12/17, 18:32:29 -> +07 - 2018/01/16, 15:30:29: OK
+07 - 2017/12/17, 18:33:29 -> +07 - 2018/01/16, 15:29:29: OK
+07 - 2017/12/17, 18:34:29 -> +07 - 2018/01/16, 15:28:29: OK
+07 - 2017/12/17, 19:00:29 -> +07 - 2018/01/16, 15:02:29: OK
import requests
import time
import os
start = 1513504889
end = 1516096889
session = requests.Session()
session.trust_env = False
session.verify = False
for i in range(0, 7200, 60):
new_start = start + i
new_end = end - i
start_human = time.strftime("%Z - %Y/%m/%d, %H:%M:%S", time.localtime(new_start))
end_human = time.strftime("%Z - %Y/%m/%d, %H:%M:%S", time.localtime(new_end))
url = 'http://my-host:9090/api/v1/query_range?query=sum(current_device_count)&start=%s&end=%s&step=86400' % (new_start, new_end)
r = session.get(url)
if len(r.text) > 100:
print('%s -> %s: OK' % (start_human, end_human))
else:
pass
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment