Remove backup pf. It's part of my std lib now.
[kiosk.git] / health_renderer.py
1 #!/usr/bin/env python3
2
3 import os
4 import time
5 from typing import Dict, List
6
7 import constants
8 import file_writer
9 import renderer
10 import utils
11
12
13 class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
14     def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
15         super(periodic_health_renderer, self).__init__(name_to_timeout_dict, False)
16
17     def debug_prefix(self) -> str:
18         return "health"
19
20     def periodic_render(self, key: str) -> bool:
21         with file_writer.file_writer("periodic-health_6_300.html") as f:
22             timestamps = "/timestamps/"
23             days = constants.seconds_per_day
24             hours = constants.seconds_per_hour
25             mins = constants.seconds_per_minute
26             minutes = mins
27             limits = {
28                 timestamps + "last_http_probe_wannabe_house": mins * 10,
29                 timestamps + "last_http_probe_meerkat_cabin": mins * 10,
30                 timestamps + "last_http_probe_dns_house": mins * 10,
31                 timestamps + "last_http_probe_rpi_cabin": mins * 10,
32                 timestamps + "last_http_probe_rpi_house": mins * 10,
33                 timestamps + "last_http_probe_therm_house": mins * 10,
34                 timestamps + "last_rsnapshot_hourly": hours * 24,
35                 timestamps + "last_rsnapshot_daily": days * 3,
36                 timestamps + "last_rsnapshot_weekly": days * 14,
37                 timestamps + "last_rsnapshot_monthly": days * 70,
38                 timestamps + "last_zfssnapshot_hourly": hours * 5,
39                 timestamps + "last_zfssnapshot_daily": hours * 36,
40                 timestamps + "last_zfssnapshot_weekly": days * 9,
41                 timestamps + "last_zfssnapshot_monthly": days * 70,
42                 timestamps + "last_zfssnapshot_cleanup": hours * 24,
43                 timestamps + "last_zfs_scrub": days * 9,
44                 timestamps + "last_backup_zfs_scrub": days * 9,
45                 timestamps + "last_cabin_zfs_scrub": days * 9,
46                 timestamps + "last_zfsxfer_backup.house": hours * 36,
47                 timestamps + "last_zfsxfer_ski.dyn.guru.org": days * 7,
48                 timestamps + "last_photos_sync": hours * 8,
49                 timestamps + "last_disk_selftest_short": days * 14,
50                 timestamps + "last_disk_selftest_long": days * 31,
51                 timestamps + "last_backup_disk_selftest_short": days * 14,
52                 timestamps + "last_backup_disk_selftest_long": days * 31,
53                 timestamps + "last_cabin_disk_selftest_short": days * 14,
54                 timestamps + "last_cabin_disk_selftest_long": days * 31,
55                 timestamps + "last_cabin_rpi_ping": mins * 20,
56                 timestamps + "last_healthy_wifi": mins * 10,
57                 timestamps + "last_healthy_network": mins * 10,
58                 timestamps + "last_scott_sync": days * 2,
59             }
60             self.write_header(f)
61
62             now = time.time()
63             n = 0
64             for filepath, limit_sec in sorted(limits.items()):
65                 ts = os.stat(filepath).st_mtime
66                 age = now - ts
67                 self.debug_print(f"{filepath} -- age: {age}, limit {limit_sec}")
68                 if age < limits[filepath]:
69                     # OK
70                     f.write(
71                         '<TD BGCOLOR="#007010" HEIGHT=100 WIDTH=33% STYLE="text-size:70%; vertical-align: middle;">\n'
72                     )
73                     txt_color="#ffffff"
74                 else:
75                     # BAD!
76                     f.write(
77                         '<TD BGCOLOR="#990000" HEIGHT=100 WIDTH=33% CLASS="invalid" STYLE="text-size:70%; vertical-align:middle;">\n'
78                     )
79                     txt_color="#000000"
80                 f.write(f"  <CENTER><FONT SIZE=-1 COLOR={txt_color}>\n")
81
82                 name = filepath.replace(timestamps, "")
83                 name = name.replace("last_", "")
84                 name = name.replace("_", "&nbsp;")
85                 duration = utils.describe_duration_briefly(int(age))
86
87                 self.debug_print(f"{name} is {duration} old.")
88                 f.write(f"{name}<BR>\n<B>{duration}</B> old.\n")
89                 f.write("</FONT></CENTER>\n</TD>\n\n")
90                 n += 1
91                 if n % 3 == 0:
92                     f.write("</TR>\n<TR>\n<!-- ------------------- -->\n")
93             self.write_footer(f)
94         return True
95
96     def write_header(self, f: file_writer.file_writer) -> None:
97         f.write(
98             """
99 <HTML>
100 <HEAD>
101 <STYLE>
102 @-webkit-keyframes invalid {
103   from { background-color: #ff6400; }
104   to { background-color: #ff0000; }
105   padding-right: 25px;
106   padding-left: 25px;
107 }
108 @-moz-keyframes invalid {
109   from { background-color: #ff6400; }
110   to { background-color: #ff0000; }
111   padding-right: 25px;
112   padding-left: 25px;
113 }
114 @-o-keyframes invalid {
115   from { background-color: #ff6400; }
116   to { background-color: #ff0000; }
117   padding-right: 25px;
118   padding-left: 25px;
119 }
120 @keyframes invalid {
121   from { background-color: #ff6400; }
122   to { background-color: #ff0000; }
123   padding-right: 25px;
124   padding-left: 25px;
125 }
126 .invalid {
127   -webkit-animation: invalid 1s infinite; /* Safari 4+ */
128   -moz-animation:    invalid 1s infinite; /* Fx 5+ */
129   -o-animation:      invalid 1s infinite; /* Opera 12+ */
130   animation:         invalid 1s infinite; /* IE 10+ */
131 }
132 </STYLE>
133 <meta http-equiv="cache-control" content="max-age=0" />
134 <meta http-equiv="cache-control" content="no-cache" />
135 <meta http-equiv="expires" content="0" />
136 <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
137 <meta http-equiv="pragma" content="no-cache" />
138 </HEAD>
139 <BODY>
140 <H1>Periodic Cronjob Health Report</H1>
141 <HR>
142 <CENTER>
143 <TABLE BORDER=0 WIDTH=99% style="font-size:16pt">
144 <TR>
145 """
146         )
147
148     def write_footer(self, f: file_writer.file_writer) -> None:
149         f.write(
150             """
151 </TR>
152 </TABLE>
153 </BODY>
154 </HTML>"""
155         )
156
157
158 #test = periodic_health_renderer({"Test", 123})
159 #test.periodic_render("Test")