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