2013-11-18 10:59:49 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import time
|
|
|
|
import os
|
2015-03-24 22:10:54 +01:00
|
|
|
|
|
|
|
from lib.GlobalRRD import GlobalRRD
|
|
|
|
from lib.NodeRRD import NodeRRD
|
2013-11-18 10:59:49 +01:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
|
|
|
|
class RRD(object):
|
|
|
|
def __init__(self,
|
|
|
|
database_directory,
|
|
|
|
image_path,
|
|
|
|
display_time_global="7d",
|
|
|
|
display_time_node="1d"):
|
|
|
|
|
|
|
|
self.dbPath = database_directory
|
|
|
|
self.globalDb = GlobalRRD(self.dbPath)
|
|
|
|
self.imagePath = image_path
|
|
|
|
self.displayTimeGlobal = display_time_global
|
|
|
|
self.displayTimeNode = display_time_node
|
|
|
|
|
2015-04-01 17:34:35 +02:00
|
|
|
self.currentTimeInt = (int(time.time()) / 60) * 60
|
2015-03-24 16:49:37 +01:00
|
|
|
self.currentTime = str(self.currentTimeInt)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.stat(self.imagePath)
|
|
|
|
except OSError:
|
|
|
|
os.mkdir(self.imagePath)
|
|
|
|
|
|
|
|
def update_database(self, nodes):
|
2015-09-02 23:16:06 +02:00
|
|
|
node_count = 0
|
|
|
|
client_count = 0
|
|
|
|
for node in nodes:
|
2015-09-08 21:34:41 +02:00
|
|
|
try:
|
|
|
|
if node['flags']['online']:
|
|
|
|
node_count += 1
|
|
|
|
client_count += node['statistics']['clients']
|
2015-09-02 23:16:06 +02:00
|
|
|
rrd = NodeRRD(os.path.join(self.dbPath, node['nodeinfo']['node_id'] + '.rrd'), node)
|
|
|
|
rrd.update()
|
2015-09-08 21:34:41 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2015-09-02 23:16:06 +02:00
|
|
|
self.globalDb.update(node_count, client_count)
|
2015-03-24 16:49:37 +01:00
|
|
|
|
|
|
|
def update_images(self):
|
2015-03-25 14:33:54 +01:00
|
|
|
self.globalDb.graph(os.path.join(self.imagePath, "globalGraph.png"),
|
|
|
|
self.displayTimeGlobal)
|
2015-03-24 16:49:37 +01:00
|
|
|
|
|
|
|
nodedb_files = os.listdir(self.dbPath)
|
|
|
|
|
|
|
|
for file_name in nodedb_files:
|
|
|
|
if not os.path.isfile(os.path.join(self.dbPath, file_name)):
|
|
|
|
continue
|
|
|
|
|
|
|
|
node_name = os.path.basename(file_name).split('.')
|
|
|
|
if node_name[1] == 'rrd' and not node_name[0] == "nodes":
|
|
|
|
rrd = NodeRRD(os.path.join(self.dbPath, file_name))
|
|
|
|
rrd.graph(self.imagePath, self.displayTimeNode)
|