diff options
author | clarkzjw <[email protected]> | 2023-02-17 11:15:38 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-17 11:15:38 -0800 |
commit | bc0884bea403ed61abb4149633a33071b44fd36f (patch) | |
tree | e48bc800e6bdd93f5032556b66465ae0a60d5334 /clarkzjw.cc/config/bia/www/update_index.py | |
parent | 4a5073589cca64898016209c0a6b0d22d9fe2075 (diff) | |
download | homelab-bc0884bea403ed61abb4149633a33071b44fd36f.tar.gz |
add cgit configcgit
Diffstat (limited to 'clarkzjw.cc/config/bia/www/update_index.py')
-rwxr-xr-x | clarkzjw.cc/config/bia/www/update_index.py | 60 |
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 @@ | |||
1 | from jinja2 import Environment, FileSystemLoader | ||
2 | import platform | ||
3 | import subprocess | ||
4 | import psutil | ||
5 | import time | ||
6 | from datetime import datetime | ||
7 | |||
8 | |||
9 | def 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 | |||
16 | def 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 | |||
29 | def get_mem_total(): | ||
30 | mem = dict(psutil.virtual_memory()._asdict()) | ||
31 | return int(mem["total"] / 1024.0 / 1024.0) | ||
32 | |||
33 | |||
34 | def get_mem_used(): | ||
35 | mem = dict(psutil.virtual_memory()._asdict()) | ||
36 | return int(mem["used"] / 1024.0 / 1024.0) | ||
37 | |||
38 | |||
39 | def 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 | |||
59 | if __name__ == "__main__": | ||
60 | parse_template() | ||