-
Notifications
You must be signed in to change notification settings - Fork 1
/
ns-to-seriesly.py
executable file
·70 lines (50 loc) · 1.84 KB
/
ns-to-seriesly.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
67
68
69
70
#!/usr/bin/env python
from optparse import OptionParser
import time
import sys
import requests
from seriesly import Seriesly
class NsToSeriesly(object):
def __init__(self, in_host, out_host, database):
self.url = 'http://{0}:8091/'.format(in_host) + \
'pools/default/buckets/default/stats?zoom=minute'
self.database = database
self.seriesly = Seriesly(host=out_host)
if database not in self.seriesly.list_dbs():
self.seriesly.create_db(database)
def collect(self):
r = requests.get(self.url)
all_stats = r.json['op']['samples']
last_stats = dict((k, v[-1]) for k, v in all_stats.iteritems())
self.store(last_stats)
def store(self, data):
self.seriesly[self.database].append(data)
def parse_args():
usage = "usage: %prog [options]\n\n" +\
"Example: %prog -i 127.0.0.1 -o 127.0.0.1 -d ns_db "
parser = OptionParser(usage)
parser.add_option('-i', dest='in_host', default='127.0.0.1',
help='input address', metavar='127.0.0.1')
parser.add_option('-o', dest='out_host', default='127.0.0.1',
help='output address', metavar='127.0.0.1')
parser.add_option('-d', dest='database',
help='database name', metavar='seriesly_db')
options, args = parser.parse_args()
if not options.database:
parser.print_help()
sys.exit()
return options
def main():
options = parse_args()
n2s = NsToSeriesly(in_host=options.in_host,
out_host=options.out_host,
database=options.database)
while True:
try:
print "Collecting and storing data"
n2s.collect()
time.sleep(5)
except KeyboardInterrupt:
break
if __name__ == '__main__':
main()