-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
executable file
·93 lines (85 loc) · 2.95 KB
/
monitor.py
File metadata and controls
executable file
·93 lines (85 loc) · 2.95 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import psutil, alarm, yaml, json, time, os, sys, signal
#check parameters
if "-h" in sys.argv:
print("\n--- Python Monitoring ---")
print("Author: Florian Patzwall\n")
print("Everything is configured inside of config.yml\n")
print("This script is able to:")
print(" 1. Display current system information")
print(" 2. Setup alarming with predefined values (config.yml)")
print(" 3. Visually alert the user looking at the script")
print(" 4. Only be run as singe module (Monitor/Alarm/Timeseries)")
print(" 5. Insert current / alarming data into a timeseries database")
print(" 6. Notify user about alarms via eMail\n")
sys.exit(0)
#create "graceful" exit (CTRL + C)
def signal_handler(sig, frame):
print("\n\nStopped")
time.sleep(1)
cls()
sys.exit(0)
#register exit handler
signal.signal(signal.SIGINT, signal_handler)
#######################
#Make config avaliable in monitor and alarm
def loadConf():
f=open("config.yml", "r")
cfg = yaml.load(f.read())
f.close()
return cfg
#clear screen function for linux & windows
def cls():os.system('cls' if os.name=='nt' else 'clear')
cfg = loadConf()
alarm.setConf(cfg)
#######################
#Get stats from host
def getStat(stat):
#check if requested stat is enabled in config
if stat not in cfg['modules']['enabled']:
if stat not in cfg['modules']['disabled']:
alarm.dprint(f"Stat: '{stat}' not found!")
return False
else:
alarm.dprint(f"Stat: '{stat}' is disabled!")
return False
#return stat if enabled
else:
if stat == "cpu_stat":
return psutil.cpu_stats()
elif stat == "cpu_util":
return psutil.cpu_percent()
elif stat == "mem_stat":
return psutil.virtual_memory()
elif stat == "disk_IO":
return psutil.disk_io_counters()
elif stat == "disk_space":
return psutil.disk_usage(cfg['disk_usage_fs'])[3]
elif stat == "network_io":
return psutil.net_io_counters()
elif stat == "network_io":
return psutil.net_if_addrs()
elif stat == "processes":
return len(psutil.pids())
elif stat == "cpu_clock":
return psutil.cpu_freq()
#init logging timestamp
timestamp_logging=time.time()
#Main loop
while True:
if cfg['mode'] == 'cli': cls()
alarm.dprint("#"*10)
values = {}
#loop over enabled modules
for stat in cfg['modules']['enabled']:
values[stat] = getStat(stat)
alarm.dprint(f"Stat: {stat} / {values[stat]}")
#pass values to alarming
alarm.checkStat(values)
alarm.dprint("#"*10)
ts_stat = stat
if cfg['timeseries']['logging'] == True:
if time.time() >= timestamp_logging + cfg['timeseries']['refreshrate']:
alarm.timeseries.insertData(values)
timestamp_logging=time.time()
#define "tickrate"
time.sleep(1/cfg['tickrate'])