-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
65 lines (57 loc) · 2.17 KB
/
monitor.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
import web
import ConfigParser
import os.path
import pkgutil
import schedule
import time
import traceback
#Helper files
import modules
from helper import logEntry
def monitor(nodeId, database):
try:
#declare config file
configFileDir = 'config.cfg'
config = ConfigParser.ConfigParser()
config.read('config.cfg')
#Database
db = database #web.database(dbn=config.get('Database', 'dbn'), host=config.get('Database', 'host'), port=int(config.get('Database', 'port')), user=config.get('Database', 'user'), pw=config.get('Database', 'pw'), db=config.get('Database', 'db'))
if not(os.path.isfile(configFileDir)):
logEntry(nodeId, os.path.basename(__file__), "Error", "Edit the config file for first use", db)
quit()
else:
config.read(configFileDir)
if nodeId > 0:
logEntry(nodeId, os.path.basename(__file__), "Log", "Starting monitoring for node.", db)
functions = []
#if __name__ == '__main__':
for loader, name, is_pkg in pkgutil.walk_packages(modules.__path__):
module = loader.find_module(name).load_module(name)
if 'run' in dir(module):
functions.append(module)
if config.has_option(name, 'schedule'):
interval = int(config.get(name,'schedule'))
else:
interval = module.setupConfig(name)
logEntry(nodeId, os.path.basename(__file__), "Log", "Scheduling " + name + " every " + str(interval) + " seconds.", db)
#Actually run the module before scheduling it
module.run(db, nodeId)
schedule.every(interval).seconds.do(module.run, db, nodeId)
logEntry(nodeId, os.path.basename(__file__), "Log", "Loaded " + str(len(functions)) + " modules for node.", db)
# Process each function
#for module in functions:
#print("executing")
#module.run(db, nodeId)
#CPU temp block
#possibly support multiple CPUs
#usage = psutil.cpu_percent(interval=1, percpu=True)
#usage = usage[0]
#db.insert('CPU_Usage', value=usage, nodeId=nodeId)
#print(usage)
else:
#We create the new entry
logEntry(-1, os.path.basename(__file__), "Error", "No Node selected... exiting", db)
quit()
except Exception as e:
logEntry(nodeId, os.path.basename(__file__), "Error", traceback.format_exc(), db)
quit()