-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-stats.py
executable file
·137 lines (100 loc) · 2.93 KB
/
server-stats.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import os
import serial
import time
import datetime
import psutil
import logging
import datetime
import json
import socket
from hurry.filesize import size
from subprocess import check_output
# ___________________VARIABLES___________________#
# Linux Variables
sys_disk = "/dev/sda2"
eth_interface = "enp1s0"
com = "/dev/ttyUSB0"
# Windows variables
#sys_disk = "C:/"
#eth_interface = "Ethernet"
#com = "COM20"
# _______________________________________________#
#target = serial.Serial(com, 9600) #commented for developing
def run_cmd(cmd):
return check_output(cmd, shell=True).decode('utf-8')
def cpu_usage():
cpuUsage = psutil.cpu_percent(interval=0, percpu=False)
return cpuUsage
def ram_percent():
cpuPercent = psutil.virtual_memory().percent
return cpuPercent
def ram_total():
ramTotal = size(psutil.virtual_memory().total)
return ramTotal
def ram_used():
ramUsed = size(psutil.virtual_memory().used)
return ramUsed
def sys_disk_total():
sysDiskTotal = size(psutil.disk_usage(sys_disk).total)
return sysDiskTotal
def sys_disk_free():
sysDiskFree = size(psutil.disk_usage(sys_disk).free)
return sysDiskFree
def up_from():
upFrom = datetime.datetime.fromtimestamp(
psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
return upFrom
def uptime():
upTime = datetime.timedelta(
seconds=round(time.time() - psutil.boot_time()))
return upTime
def net_stats():
netStats = psutil.net_io_counters(pernic=True, nowrap=True)[eth_interface]
return netStats
def net_in():
netIn = size(psutil.net_io_counters(
pernic=True, nowrap=True)[eth_interface].bytes_recv)
return netIn
def net_out():
netOut = size(psutil.net_io_counters(
pernic=True, nowrap=True)[eth_interface].bytes_sent)
return netOut
def hostaname():
name = run_cmd("hostname")
return name
def host_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1)) # connect() for UDP doesn't send packets
local_ip = s.getsockname()[0]
return local_ip
def get_data():
data = {
"hostname": str(hostaname()),
"ip": str(host_ip()),
"uptime": str(uptime()),
"cpu_usage": str(cpu_usage()),
"ram_percent": str(ram_percent()),
"ram_total": str(ram_total()),
"ram_used": str(ram_used()),
"sys_disk_total": str(sys_disk_total()),
"sys_disk_free": str(sys_disk_free()),
"up_from": str(up_from()),
"net_in": str(net_in()),
"net_out": str(net_out()),
}
return json.dumps(data)
def main():
while True:
print(datetime.datetime.now())
print(get_data().encode('ascii'))
#target.write(get_data().encode('ascii')) #commented for develop
time.sleep(0.5)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('Exited')