-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_json.py
More file actions
120 lines (103 loc) · 3.29 KB
/
monitor_json.py
File metadata and controls
120 lines (103 loc) · 3.29 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python3
### USAGE:
# sudo python monitor_json.py
### INSTALL:
#if no pip: command not found, install:
#sudo apt-get install python-dev
#sudo apt-get install python-pip
#sudo pip install psutil
#sudo pip install psutil --upgrade
### IMPORT
import os
import multiprocessing
import psutil #https://code.google.com/p/psutil/wiki/Documentation
import time
import json
# convert bytes to human readable
def bytes2human(n):
symbols = ('KB', 'MB', 'GB', 'TB')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.2f %s' % (value, s)
return '%.2f B' % (n)
# cpu percent usage
def cpu():
return str(psutil.cpu_percent(interval=1)) + "%"
# ram usage
def ram():
return bytes2human(psutil.virtual_memory().available)
# server load
def load():
# print "Load Average: " + str(os.getloadavg()[0])
return str(os.getloadavg()[0])
# cpu count
def cpu_count():
return str(multiprocessing.cpu_count())
# network stats
def network_stats():
pnic_before = psutil.net_io_counters(pernic=True)
# sleep some time (one second)
interval = 1
time.sleep(interval)
pnic_after = psutil.net_io_counters(pernic=True)
# return stats
stats = {}
stats['before'] = pnic_before['eth1']
stats['after'] = pnic_after['eth1']
return stats
# network sent
def network_sent():
stats = network_stats()
bytes_sent = bytes2human(stats['after'].bytes_sent - stats['before'].bytes_sent) + '/s'
return bytes_sent
# network received
def network_received():
stats = network_stats()
bytes_rcvd = bytes2human(stats['after'].bytes_recv - stats['before'].bytes_recv) + '/s'
return bytes_rcvd
# free disk space
def disk_free():
statvfs = os.statvfs('/')
# Size of filesystem in bytes
# disk_size = bytes2human(statvfs.f_frsize * statvfs.f_blocks)
# Actual number of free bytes
# disk_free = bytes2human(statvfs.f_frsize * statvfs.f_bfree)
# Number of free bytes that ordinary users are allowed to use (excl. reserved space)
disk_real_free = bytes2human(statvfs.f_frsize * statvfs.f_bavail)
# print "Disk Usage: " + disk_size
# print "Disk Usage: " + disk_free
return disk_real_free
# disk used as percentage
def disk():
vfs = os.statvfs('/')
usedPercentage = 100.0 * float(vfs.f_blocks - vfs.f_bfree) / float(vfs.f_blocks - vfs.f_bfree + vfs.f_bavail)
return '%.2f% %' % (usedPercentage)
# main
def main():
try:
while True:
# get the monitor data and convert to json
monitor_json = json.dumps({
"cpu": cpu(),
"ram": ram(),
"load": load(),
"cpu_count": cpu_count(),
"network_sent": network_sent(),
"network_received": network_received(),
"disk": disk(),
#"disk_free": disk_free(),
})
# write to json file
with open("monitor.json", "w") as text_file:
text_file.write(monitor_json)
# refresh every 5 seconds
interval = 5
time.sleep(interval)
except (KeyboardInterrupt, SystemExit):
pass
if __name__ == '__main__':
main()