-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
118 lines (91 loc) · 2.51 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
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
import socket
import time
import os
import uuid
def get_cpu_usage():
total = 0
times = 0
lines = os.popen('mpstat -P ALL 1 3').readlines()
for line in lines:
if line.find("all") < 0:
continue
str = line.split(' ')
cnt = len(str)
try:
total = total + float(str[cnt-1])
except:
return -1
times = times + 1
if times == 0:
return -1
cpu_usage = total/times
return cpu_usage
def get_net_usage():
totalin = 0
totalout = 0
times = 0
lines = os.popen('sar -n DEV 1 3').readlines()
for line in lines:
if line.find("eth") < 0:
continue
str = line.split(' ')
item = []
for i in str:
if i != '':
item.append(i)
totalin = totalin + float(item[5])
totalout = totalout + float(item[6])
times = times + 1
speed_in = totalin/times
speed_out = totalout/times
return (speed_in, speed_out)
def get_disk_usage():
total = 0
times = 0
lines = os.popen('iostat -x 1 3').readlines()
for line in lines:
if line.find("sd") < 0:
continue
str = line.split(' ')
try:
if str[0].find("sd") < 0:
continue
except:
return -1
cnt = len(str)
try:
total = total + float(str[cnt-1])
except:
return -1
times = times + 1
if times == 0:
return -1
disk_usage = total/times
return disk_usage
count = 0
ave_cpu = 0.0
ave_disk = 0.0
ave_net_in = 0.0
ave_net_out = 0.0
while True:
try:
cpu_usage = get_cpu_usage( )
ave_cpu += cpu_usage
cpu_info = "%s:%.2f; " % ("cpu", cpu_usage)
speed_in, speed_out = get_net_usage( )
ave_net_in += speed_in
ave_net_out += speed_out
net_info = "%s:%.2f; %s:%.2f; " % ("net_speed_in", speed_in, "net_speed_out", speed_out)
disk_usage = get_disk_usage( )
ave_disk += disk_usage
disk_info = "%s:%.2f; " % ("disk_usage", disk_usage)
msg = cpu_info + net_info + disk_info
print msg
count += 1
if count > 0:
print 'ave cpu:%.2f' % (ave_cpu / count)
print 'ave disk:%.2f' % (ave_disk / count)
print 'ave net in:%.2f' % (ave_net_in / count)
print 'ave net out:%.2f' % (ave_net_out / count)
except:
continue