/
update_kindle.py
103 lines (86 loc) · 3.26 KB
/
update_kindle.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
import codecs
from esxisnmp import EsxiSnmp
from nginxstats import NginxStats
from secrets import SNMP_HOST, SNMP_USER, SNMP_AUTHKEY, SNMP_PRIVKEY, SNMP_NIC, NGINX_HOST
import pickle
import time
from datetime import datetime, date
import subprocess
import os
import pathlib
NET_STATS = 'esxi_net_stats.pkl'
os.chdir(pathlib.Path(__file__).parent.absolute())
def human_bytes(num, is_kibi=True, suffix='B'):
if is_kibi:
units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']
byte_scale = 1024.0
else:
units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
byte_scale = 1000.0
for unit in units:
if abs(num) < byte_scale:
return "%3.1f%s%s%s" % (num, unit, suffix, 'ps')
num /= byte_scale
return "%.1f%s%s%s" % (num, 'Y', suffix, 'ps')
def usbnet_ready():
for netdev in os.listdir("/sys/class/net/"):
if netdev == "usb0":
return True
return False
if __name__ == "__main__":
if not usbnet_ready():
raise RuntimeException("The usbnet device is not ready")
esxi = EsxiSnmp(SNMP_HOST, SNMP_USER, SNMP_AUTHKEY, SNMP_PRIVKEY)
try:
cpu = str(esxi.get_cpu()) + "%"
except Exception as e:
print(e)
cpu = "N/A"
try:
mem = str(esxi.get_mem()) + "%"
except Exception as e:
print(e)
mem = "N/A"
if os.path.isfile(NET_STATS):
with open(NET_STATS, 'rb') as f:
last_stats = pickle.load(f)
else:
last_stats = None
try:
cur_net_in = esxi.get_nic_in(SNMP_NIC)
cur_net_out = esxi.get_nic_out(SNMP_NIC)
now = time.mktime(datetime.today().timetuple())
stats = {"now": now, "in": cur_net_in, "out": cur_net_out}
with open(NET_STATS, 'wb') as f:
pickle.dump(stats, f)
if last_stats and last_stats["now"] != now:
net_in = float(cur_net_in - last_stats["in"]) / float(now - last_stats["now"])
net_out = float(cur_net_out - last_stats["out"]) / float(now - last_stats["now"])
net_in_pretty = human_bytes(net_in)
net_out_pretty = human_bytes(net_out)
else:
net_in_pretty = "N/A"
net_out_pretty = "N/A"
except Exception as e:
print(e)
net_in_pretty = "N/A"
net_out_pretty = "N/A"
nginx = NginxStats(NGINX_HOST)
try:
connections = nginx.get_active_connections()
except Exception as e:
print(e)
connections = "N/A"
template = codecs.open('template.svg', 'r', encoding='utf-8').read()
template = template.replace("S1", cpu)
template = template.replace("S2", mem)
template = template.replace("S3", net_out_pretty)
template = template.replace("S4", net_in_pretty)
template = template.replace("S5", connections)
template = template.replace("S6", time.strftime("%Y-%m-%d %H:%M"))
codecs.open('working.svg', 'w', encoding='utf-8').write(template)
subprocess.call("rsvg-convert --background-color=white -o working.png working.svg", shell=True)
subprocess.call("pngcrush -force -c 0 working.png image.png", shell=True)
subprocess.call('sshpass -p "" scp -l 15 image.png root@192.168.2.2:/mnt/us/timelit/server_image/image.png', shell=True)
os.remove("working.png")
os.remove("working.svg")