-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
85 lines (61 loc) · 2.52 KB
/
metrics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
Created on Mar 19, 2014
@author: Dario Bonino <[email protected]>
Copyright (c) 2014 Dario Bonino
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
'''
import os,psutil,time,tts
def print_sys_metrics():
'''
Prints some system metric in an os-independent way
'''
#get uname data
uname = os.uname()
# print the operating system information
print "OS Type:%s\nHost:%s\nKernel:%s %s\nArch:%s\n"%uname
#get the current system load average (last min, 5min, 15min)
load = os.getloadavg()
#print the load average
print "load_avg:\n \t%f (1min)\n \t%f (5min)\n \t%f (15min)"%(load)
#get the current virtual memory statistics
virtual_memory = psutil.virtual_memory()
#print total memory
print "Total memory:\n \t%s"%virtual_memory.total
#print available memory
print "Available memory:\n \t%s"%virtual_memory.available
#print free memory
print "Free memory:\n \t%s"%virtual_memory.available
#print cpu usage
print "CPU usage:\n \t%f"%psutil.cpu_percent(None, False)
#get disk counters
disk_io = psutil.disk_io_counters(False)
#print the number of reads and corresponding bytes
print "Reads: %d (%d bytes)"%(disk_io.read_count,disk_io.read_bytes)
#print the number of writes and the corresponding bytes
print "Writes: %d (%d bytes)"%(disk_io.write_count, disk_io.write_bytes)
'''
Monitors the cpu occupation and if raises over a given threshold, calls a specified function
'''
def monitor_cpu(threshold,interval,callback=None):
while(True):
#get the cpu percentage
percent = psutil.cpu_percent()
#check the thrashold
if(percent > threshold):
#callback
callback(percent)
#debug
print "calling callback: %s"%percent
#wait for the given time
time.sleep(interval)
if __name__ == '__main__':
print_sys_metrics()
monitor_cpu(10, 1, lambda x: tts.say("warning, CPU percent raised up to %s"%x))