-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_sys.py
More file actions
101 lines (90 loc) · 3.07 KB
/
Copy paththread_sys.py
File metadata and controls
101 lines (90 loc) · 3.07 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
from __future__ import division
import time
import threading
import config
import os
class ThreadSys(object):
def __init__(self, interval=1):
""" Constructor
:type interval: int
:param interval: Check sys params interval, in seconds
"""
self.interval = interval
self.tx_prev = 0
self.rx_prev = 0
self.tx_speed = 0
self.rx_speed = 0
self.cpu_used = 0
self.memory_total = 0
self.memory_used = 0
self.last_worktime = 0
self.last_idletime = 0
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
@staticmethod
def bytes_to_mb(bts):
mb = bts / 1048576
return float("{0:.2f}".format(mb))
@staticmethod
def bytes_to_gib(bts):
gib = bts / 1073741824
return float("{0:0.1f}".format(gib))
@staticmethod
def mb_to_gib(mb):
gib = mb / 1024
return float("{0:0.1f}".format(gib))
@staticmethod
def get_network_bytes(interface):
for line in open('/proc/net/dev', 'r'):
if interface in line:
data = line.split('%s:' % interface)[1].split()
rx_bytes, tx_bytes = (data[0], data[8])
return (int(rx_bytes), int(tx_bytes))
def get_cpu(self):
f = open("/proc/stat", "r")
line = ""
while not "cpu " in line: line = f.readline()
f.close()
spl = line.split(" ")
worktime = int(spl[2]) + int(spl[3]) + int(spl[4])
idletime = int(spl[5])
dworktime = (worktime - self.last_worktime)
didletime = (idletime - self.last_idletime)
rate = float(dworktime) / (didletime + dworktime)
self.last_worktime = worktime
self.last_idletime = idletime
if (self.last_worktime == 0):
result = 0
else:
result = float("%.2f" % round(rate,2))*100
return result
def run(self):
""" Get sys info with 1s period """
while True:
try:
rx, tx = self.get_network_bytes('eth0')
except:
rx = 0
tx = 0
tot_m, used_m, free_m = map(int, os.popen('free -t -m').readlines()[-1].split()[1:])
self.cpu_used = self.get_cpu()
self.memory_total = self.mb_to_gib(tot_m)
self.memory_used = self.mb_to_gib(used_m)
if int(self.tx_prev) > 0:
self.tx_speed = self.bytes_to_mb(tx - self.tx_prev)
if int(self.rx_prev) > 0:
self.rx_speed = self.bytes_to_mb(rx - self.rx_prev)
time.sleep(self.interval)
self.tx_prev = tx
self.rx_prev = rx
def get_info(self):
return {
'memory_total': self.memory_total,
'memory_used': self.memory_used,
'cpu_used': self.cpu_used,
'rx_speed': self.rx_speed,
'tx_speed': self.tx_speed,
'server_name': config.SERVER_NAME,
'time': time.strftime("%H:%M:%S", time.gmtime())
}