2013-11-18 10:59:49 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import os
|
2014-02-11 00:12:10 +01:00
|
|
|
from GlobalRRD import GlobalRRD
|
|
|
|
from NodeRRD import NodeRRD
|
2013-11-18 10:59:49 +01:00
|
|
|
|
|
|
|
class rrd:
|
|
|
|
def __init__( self
|
|
|
|
, databaseDirectory
|
|
|
|
, imagePath
|
|
|
|
, displayTimeGlobal = "7d"
|
|
|
|
, displayTimeNode = "1d"
|
|
|
|
):
|
|
|
|
self.dbPath = databaseDirectory
|
2014-02-11 00:12:10 +01:00
|
|
|
self.globalDb = GlobalRRD(self.dbPath)
|
2013-11-18 10:59:49 +01:00
|
|
|
self.imagePath = imagePath
|
|
|
|
self.displayTimeGlobal = displayTimeGlobal
|
|
|
|
self.displayTimeNode = displayTimeNode
|
2013-11-23 22:16:35 +01:00
|
|
|
|
2013-11-18 10:59:49 +01:00
|
|
|
self.currentTimeInt = (int(time.time())/60)*60
|
|
|
|
self.currentTime = str(self.currentTimeInt)
|
|
|
|
|
2013-11-23 22:16:35 +01:00
|
|
|
try:
|
|
|
|
os.stat(self.imagePath)
|
|
|
|
except:
|
|
|
|
os.mkdir(self.imagePath)
|
|
|
|
|
2013-11-18 10:59:49 +01:00
|
|
|
def update_database(self,db):
|
2014-08-17 19:31:14 +02:00
|
|
|
nodes = db.get_nodes()
|
|
|
|
clientCount = sum(map(lambda d: d.clientcount, nodes))
|
2013-11-18 10:59:49 +01:00
|
|
|
|
2014-09-22 23:34:21 +02:00
|
|
|
curtime = time.time() - 60
|
|
|
|
self.globalDb.update(len(list(filter(lambda x: x.lastseen >= curtime, nodes))), clientCount)
|
2014-08-17 19:31:14 +02:00
|
|
|
for node in nodes:
|
2014-02-11 00:12:10 +01:00
|
|
|
rrd = NodeRRD(
|
|
|
|
os.path.join(self.dbPath, str(node.id).replace(':', '') + '.rrd'),
|
|
|
|
node
|
|
|
|
)
|
|
|
|
rrd.update()
|
2013-11-18 10:59:49 +01:00
|
|
|
|
|
|
|
def update_images(self):
|
2014-02-11 00:12:10 +01:00
|
|
|
""" Creates an image for every rrd file in the database directory.
|
2013-11-18 10:59:49 +01:00
|
|
|
"""
|
|
|
|
|
2014-02-11 00:12:10 +01:00
|
|
|
self.globalDb.graph(os.path.join(self.imagePath, "globalGraph.png"), self.displayTimeGlobal)
|
2013-11-18 10:59:49 +01:00
|
|
|
|
|
|
|
nodeDbFiles = os.listdir(self.dbPath)
|
|
|
|
|
|
|
|
for fileName in nodeDbFiles:
|
2013-11-25 13:17:53 +01:00
|
|
|
if not os.path.isfile(os.path.join(self.dbPath, fileName)):
|
2013-11-23 23:15:49 +01:00
|
|
|
continue
|
|
|
|
|
2013-11-18 10:59:49 +01:00
|
|
|
nodeName = os.path.basename(fileName).split('.')
|
|
|
|
if nodeName[1] == 'rrd' and not nodeName[0] == "nodes":
|
2014-02-11 00:12:10 +01:00
|
|
|
rrd = NodeRRD(os.path.join(self.dbPath, fileName))
|
|
|
|
rrd.graph(self.imagePath, self.displayTimeNode)
|