/
daemon.py
67 lines (53 loc) · 1.89 KB
/
daemon.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
from config import Config
import json
import threading
import time
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from qumulo_client import QumuloClient
from qactivity_tables import Iops, Capacity
class Worker(threading.Thread):
def __init__(self, cfg):
threading.Thread.__init__(self)
self.cfg = cfg
self.setDaemon(True)
self.client = QumuloClient(cfg) # only one cluster for now
engine = create_engine('sqlite:///qactivity.sqlite')
DBSession = sessionmaker(bind=engine)
self.session = DBSession()
def get_iops(self, ts):
iops = self.client.get_iops()
for entry in iops:
self.session.add(\
Iops(ts=ts,\
cluster=self.cfg.cluster.hostname,\
path=entry['path'],\
ip=entry['ip'],\
iops = json.dumps(entry)))
self.session.commit()
def get_capacity(self, ts):
for path in self.cfg.paths:
capacity = self.client.get_capacity(path)
# add a Capacity record
self.session.add(Capacity(ts=ts, cluster=self.cfg.cluster.hostname, \
path=path, size= long(capacity['total_capacity'])))
self.session.commit()
def get_cluster_metrics(self):
for path in self.cfg.paths:
ts = int((datetime.now() - datetime(1970, 1, 1)).total_seconds())
self.get_iops(ts)
self.get_capacity(ts)
def run(self):
try:
while True:
print("Getting data....")
time.sleep(10)
self.get_cluster_metrics()
except KeyboardInterrupt:
print "Shutting down"
if __name__ == '__main__':
# see if we can read config
f = file('qactivity.cfg')
cfg = Config(f)
Worker(cfg).run()