-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_cpu_load.py
More file actions
80 lines (69 loc) · 1.73 KB
/
get_cpu_load.py
File metadata and controls
80 lines (69 loc) · 1.73 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
#!/usr/bin/env python
import sys
import commands
def get_cpu_load (processor_number=0):
"""Calculates the system load."""
try:
f = open("/proc/stat", "r")
tmp = f.readlines(2000)
f.close()
except:
print _("Failed to open /proc/stat")
return None
if processor_number == 0 : sufix = ''
else: sufix = str(processor_number -1)
line = tmp[processor_number]
if line.startswith("cpu%s"% (sufix)):
cuse = float( line.split()[1] )
cn = float( line.split()[2] )
csys = float( line.split()[3])
if sufix == '':
load = cuse + cn
else:
load = cuse + csys + cn
#load = int(load / .update_interval)
return load
return None
def cpu_get_cpu_name():
"""Returns Cpu Name"""
try:
f = open("/proc/cpuinfo", "r")
tmp = f.readlines(500)
f.close()
except:
print _("Failed to open /proc/cpuinfo")
return None
list = []
for line in tmp:
if line.startswith("model name"):
return line.split(':')[1].strip()
return ''
def net_get_updown():
"""Returns upload and download"""
try:
f = open("/proc/net/dev", "r")
data = f.readlines(2000)
f.close()
newNetUp = 0
newNetDown = 0
for i in data:
if i.find(':') != -1 and i.strip().startswith('lo:') == False:
v = i.split(':')[1].split()
newNetUp = float( v[8] )+newNetUp
newNetDown = float( v[0] )+newNetDown
return (newNetUp/1024), (newNetDown/1024)
except:
print(_("Can't open /proc/net/dev"))
return 0,0
def net_get_connections ():
"""This will return the number of connections."""
data = commands.getoutput("netstat -n | grep -c tcp")
return data
def main():
load = get_cpu_load()
name = cpu_get_cpu_name()
print str(name) + " " + str(load)
print net_get_updown()
print net_get_connections()
if __name__ == "__main__":
sys.exit(main())