This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaster-collect.py
66 lines (52 loc) · 2.8 KB
/
master-collect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import time
import json
import nibbler
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Nibbler collects statistics and metrics from a Mesos master and push them to influxdb')
parser.add_argument('--master', default='localhost:5050', type=str, help='hostname and port for mesos master')
parser.add_argument('--master-state', default='state.json', type=str, help='path to state.json')
parser.add_argument('--master-metrics', default='metrics/snapshot', type=str, help='path to metrics snapshot json')
parser.add_argument('--influxdb-host', default='localhost:8086', type=str,
help='hostname and port for influxdb admin server')
parser.add_argument('--influxdb-name', required=True, type=str, help='Database name to use')
parser.add_argument('--influxdb-user', default='root', type=str, help='user for influxdb admin server')
parser.add_argument('--influxdb-password', default='root', type=str, help='password for influxdb admin server')
args = parser.parse_args()
master_location = args.master
metrics_endpoint = 'http://%s/%s' % (master_location, args.master_metrics)
master_endpoint = 'http://%s/%s' % (master_location, args.master_state)
influx_endpoint = 'http://%s/db/%s/series?u=%s&p=%s' % (
args.influxdb_host, args.influxdb_name, args.influxdb_user, args.influxdb_password)
create_json = '{"name": "%s"}' % args.influxdb_name
create_url = 'http://%s/db?u=%s&p=%s' % (args.influxdb_host, args.influxdb_user, args.influxdb_password)
nibbler.post_json(create_url, create_json)
# One second sample rate.
sample_rate = 1
# Sample loop.
while True:
# TODO(nnielsen): Don't fetch state json every iteration.
# Get slave state object once to tag samples and determine leader.
master_state = nibbler.json_from_url(master_endpoint)
if "leader" in master_state and "pid" in master_state:
if master_state["leader"] != master_state["pid"]:
print "Master is not the current leader - waiting 5 seconds"
time.sleep(5)
continue
master_id = master_state["id"]
influx_samples = []
# Collect the latest metrics (gauges and counters).
metrics = nibbler.json_from_url(metrics_endpoint)
for metric in metrics:
influx_samples.append({
"name": metric,
"columns": ["value", "master_id", "source"],
"points": [[metrics[metric], master_id, "master"]]
})
# Send samples if collected.
if influx_samples is not '':
json_out = json.dumps(influx_samples)
print nibbler.post_json(influx_endpoint, json_out)
print "Sent sample..."
time.sleep(sample_rate)