aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'clarkzjw.cc/config/bia/www/update_index.py')
-rwxr-xr-xclarkzjw.cc/config/bia/www/update_index.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/clarkzjw.cc/config/bia/www/update_index.py b/clarkzjw.cc/config/bia/www/update_index.py
new file mode 100755
index 0000000..427c147
--- /dev/null
+++ b/clarkzjw.cc/config/bia/www/update_index.py
@@ -0,0 +1,60 @@
1from jinja2 import Environment, FileSystemLoader
2import platform
3import subprocess
4import psutil
5import time
6from datetime import datetime
7
8
9def get_pkg_count():
10 result = subprocess.run(["pkg", "stats", "-ql"], stdout=subprocess.PIPE)
11 for r in result.stdout.decode("utf-8").strip("\n").split("\n\t"):
12 if r.startswith("Installed packages: "):
13 return r.split(": ")[1]
14
15
16def get_uptime():
17 result = subprocess.run(["sysctl", "-a"], stdout=subprocess.PIPE)
18 for r in result.stdout.decode("utf-8").strip("\n").split("\n"):
19 if r.startswith("kern.boottime"):
20 boot = datetime.fromtimestamp(int(r.split(" ")[4].strip(",")))
21 now = datetime.fromtimestamp(time.time())
22 delta = now - boot
23 days = delta.days
24 hour = delta.seconds // 3600
25 seconds = (delta.seconds % 3600) // 60
26 return days, hour, seconds
27
28
29def get_mem_total():
30 mem = dict(psutil.virtual_memory()._asdict())
31 return int(mem["total"] / 1024.0 / 1024.0)
32
33
34def get_mem_used():
35 mem = dict(psutil.virtual_memory()._asdict())
36 return int(mem["used"] / 1024.0 / 1024.0)
37
38
39def parse_template():
40 environment = Environment(loader=FileSystemLoader("/var/www/cgit.jinwei.me/template/"))
41 template = environment.get_template("index.jinja2")
42
43 content = template.render(
44 UP_DAYS = get_uptime()[0],
45 UP_HOUR = get_uptime()[1],
46 UP_MINS = get_uptime()[2],
47 NB_PACKAGES = get_pkg_count(),
48 MEM_IN_USE = get_mem_used(),
49 MEM_IN_TOTAL = get_mem_total(),
50 OS=platform.system(),
51 VERSION=platform.release(),
52 ARCH = platform.machine()
53 )
54 with open("/var/www/cgit.jinwei.me/index.html", mode="w", encoding="utf-8") as f:
55 f.write(content)
56 f.close()
57
58
59if __name__ == "__main__":
60 parse_template()
Powered by cgit v1.2.3 (git 2.41.0)