forked from vicariousdrama/nodeyez
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvicariousstat.py
More file actions
159 lines (144 loc) Β· 5.63 KB
/
Copy pathvicariousstat.py
File metadata and controls
159 lines (144 loc) Β· 5.63 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# import packages
import psutil
import subprocess
# gets hottest temperature (cpu) in degrees C, * 1000
def getcputemp():
cmd = "grep . /sys/class/hwmon/*/* /sys/class/thermal/*/* 2>/dev/null | grep \"temp\" | grep \"input\" | awk '{split($0,a,\":\"); print a[2]}' | sort -r | head -n 1"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return (float(cmdoutput)/1000.0)
except subprocess.CalledProcessError as e:
print(e)
return 0
def getcputempwarnlevel():
h = 60 # based on raspberry pi, which doesnt report high or critical
s = psutil.sensors_temperatures()
for k in s.keys():
v = s[k][0]
if v.high is not None:
if v.high > h: h = v.high
return h
def getcputempdangerlevel():
h = 75 # based on raspberry pi, which doesnt report high or critical
s = psutil.sensors_temperatures()
for k in s.keys():
v = s[k][0]
if v.critical is not None:
if v.critical > h: h = v.critical
return h
# gets available memory, in MB
def getmemoryavailable():
cmd = "free --mebi | grep Mem | awk '{ print $7 }'"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return int(cmdoutput)
except subprocess.CalledProcessError as e:
print(e)
return 0
def getmemoryinfo(memtype="Mem"):
cmd = f"free --mebi | grep {memtype}"
info = {"label":memtype,"total":0,"used":0,"percentused":100}
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
parts = cmdoutput.split()
info["label"] = memtype
info["total"] = parts[1]
info["used"] = parts[2]
info["percentused"] = int((float(info["used"])/float(info["total"]))*100)
except subprocess.CalledProcessError as e:
print(e)
return info
# gets network transmitted
def getnetworktx():
cmd = "ip -j -s link show | jq '.[] | [(select(.ifname!=\"lo\") | .stats64.tx.bytes)//0] | add' | awk -v OFMT='%.0f' '{sum+=$0} END{print sum}' | numfmt --to=iec | head -n 1"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return cmdoutput
except subprocess.CalledProcessError as e:
print(e)
return "?"
# gets network received
def getnetworkrx():
cmd = "ip -j -s link show | jq '.[] | [(select(.ifname!=\"lo\") | .stats64.rx.bytes)//0] | add' | awk -v OFMT='%.0f' '{sum+=$0} END{print sum}' | numfmt --to=iec | head -n 1"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return cmdoutput
except subprocess.CalledProcessError as e:
print(e)
return "?"
# gets uptime (days)
def getuptime():
cmd = "w|head -1|sed -E 's/.*up (.*),.*user.*/\\1/'|sed -E 's/([0-9]* days).*/\\1/'"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return cmdoutput
except subprocess.CalledProcessError as e:
print(e)
return "?"
# gets load (1, 5, 15 minute periods)
def getload():
cmd = "w|head -1|sed -E 's/.*load average: (.*)/\\1/'"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
v = cmdoutput.split(",")
return float(v[0]),float(v[1]),float(v[2])
except subprocess.CalledProcessError as e:
print(e)
return (0.00, 0.00, 0.00)
def getcpucount():
cmd = "cat /proc/cpuinfo | grep processor | wc -l"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return int(cmdoutput)
except subprocess.CalledProcessError as e:
print(e)
return 1
# get drive free
def getdrivefreespace(path="/$"):
cmd = "printf \"%s\" \"$(df -h|grep '" + path + "'|sed -n 1p|awk '{print $4}')\" 2>/dev/null"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return cmdoutput
except subprocess.CalledProcessError as e:
print(e)
return "?"
# get drive free percent
def getdrivefreepercent(path="/$"):
#cmd = "printf \"%.0f\" \"$(df | grep '" + path + "'|sed -n 1p|awk '{print $4/$2*100 }')\" 2>/dev/null"
cmd = "printf \"%.0f\" \"$(df | grep '" + path + "'|sed -n 1p|awk '{print $4/($3+$4)*100 }')\" 2>/dev/null"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return cmdoutput
except subprocess.CalledProcessError as e:
print(e)
return "?"
# get drive2 path
def getdrive2path():
cmd = "df -t ext4 | grep / | awk '{print $6}' | grep -v /boot | sort | wc -l"
try:
cmdoutput = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
if len(cmdoutput) > 0:
drivecount = int(cmdoutput)
if drivecount > 1:
cmd = "df -t ext4 | grep / | awk '{print $6}' | grep -v /boot | sort | sed -n 2p"
drive2path = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
return drive2path
except subprocess.CalledProcessError as e:
print(e)
return "?"
return None
# get drive1 info
def getdrive1info():
drive1path = "/"
drivefreespace = getdrivefreespace()
drivefreepercent = getdrivefreepercent()
return drive1path, drivefreespace, drivefreepercent
# get drive2 info
def getdrive2info():
drive2path = getdrive2path()
if drive2path is None or drive2path == "?":
return "None", 0, 0
else:
drivefreespace = getdrivefreespace(drive2path)
drivefreepercent = getdrivefreepercent(drive2path)
return drive2path, drivefreespace, drivefreepercent