UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / examples / index.php
blob721fface7e498fad842f2928f717b6971a2f06f7
1 <html>
2 <body link="$0000ff" vlink="$0000ff">
3 <?php
4 /* apcupsd monitoring script
5 Copyright April 25, 2005, Rob Kroll, rob@killerbob.ca
7 Because I don't think I can keep anybody from doing what they want with it, nor would I want to, I'm releasing this script
8 to the world without restrictions. There's also no warranty, implied or otherwise. If this script somehow manages to make
9 your computer blow up, you're on your own. That said, I sincerely doubt that's even possible.
11 User-Configurable variables below:
12 $statusfile_location This is the location of the apcupsd.status file, as found in /etc/apcupsd/apcupsd.conf
13 If this file exists, the graphical parse will be displayed.
14 $logfile_location This is the location of the apcupsd.events file, as found in /etc/apcupsd/apcupsd.conf
15 If this file exists, it will be displayed with most recent entries first.
16 $showloglines By default, this script does not display the entire .events file. This is the number of
17 lines to display by default. The script generates a link that enables displaying the
18 entire log.
19 $goodcolour Default: "lightgreen". This is the HTML colour for the background of cells where things
20 are good.
21 $warncolour Default: "yellow". This is the HTML colour for the background of cells where things are
22 in need of attention.
23 $badcolour Default: "red". This is the HTML colour for the background of cells where things are bad.
28 $statusfile_location = "/var/log/apcupsd.status";
29 $logfile_location = "/var/log/apcupsd.events";
31 $showloglines = 5;
33 $goodcolour = "lightgreen";
34 $warncolour = "yellow";
35 $badcolour = "red";
39 There are no more user-configurable variables beyond this point. You're welcome to browse the code, and submit any bugfixes you
40 find. If you don't know much about coding, it can be educational, but you probably don't need to read further.
44 // There's a logic problem with the display of a set number of log lines. I'm lazy, so rather than fix the logic, I'm just going
45 // to increase the number of log lines to show. The only problem this should cause is with a very short log.
47 // $showloglines++;
50 $showlog = $_GET["showlog"];
53 echo "<table border=2 width=\"100%\">\n";
54 echo "<tr><td colspan=2 align=center><b>UPS Status</b></td></tr><tr>\n";
55 if (file_exists($statusfile_location)) {
56 // Open the Status file.
57 $statusfile = file($statusfile_location);
59 // Get the word status ("ONLINE", "ON-BATT", etc.)
60 $dumbvar = split(":", $statusfile[10]);
61 $status = $dumbvar[1];
62 if (eregi('online', $status)) {
63 $statusbg = $goodcolour;
64 } else $statusbg = $warncolour;
66 // Get the current UPS load
67 $dumbvar = split(":", $statusfile[12]);
68 $load = floatval($dumbvar[1]);
69 if ($load > 75) {
70 $loadbg = $badcolour;
71 } else if ($load > 25) {
72 $loadbg = $warncolour;
73 } else $loadbg = $goodcolour;
75 // Get the battery charge level
76 $dumbvar = split(":", $statusfile[13]);
77 $charge = floatval($dumbvar[1]);
78 if ($charge > 60) {
79 $chargebg = $goodcolour;
80 } else if ($charge > 25) {
81 $chargebg = $warncolour;
82 } else $chargebg = $badcolour;
84 // Get the estimated time remaining
85 $dumbvar = split(":", $statusfile[14]);
86 $etl = $dumbvar[1];
88 // Draw the table entry.
89 echo "<td valign=center>\n";
90 echo "<table border=0 width=\"100%\"><tr><td>\n";
91 echo "<b>Battery Status:</b></td><td bgcolor=" . $statusbg . " align=center>" . $status . "</td></tr><tr><td>\n";
92 echo "<b>UPS Load:</b></td><td bgcolor=" . $loadbg . " align=center>" . $load . "</td></tr><tr><td>\n";
93 echo "<b>Battery Charge:</b></td><td bgcolor=" . $chargebg . " align=center>". $charge . "</td></tr><tr><td>\n";
94 echo "<b>Estimated time left:</b></td><td align=center>" . $etl . "</td></tr></table></td>\n";
97 if (file_exists($logfile_location)) {
98 // Open the log file.
99 $logfile = file($logfile_location);
100 $loglines = count($logfile);
101 if ($loglines < $showloglines) $showloglines = $loglines;
103 // Draw the table
104 echo "<td>";
105 if ($showlog == "yes") { // If the user has clicked the "Show entire log" link, show the whole shebang in reverse order.
106 echo "<table border=0 width=\"100%\">";
107 for ($i = 0; $i <= $loglines; $i++) {
108 $newline = $logfile[($loglines - $i - 1)];
109 $notifycolour = "white";
110 if (eregi("power is back", $newline)) {
111 $notifycolour = $goodcolour;
112 } else if (eregi("power failure", $newline)) {
113 $notifycolour = $badcolour;
114 } else if (eregi("startup succeeded", $newline)) {
115 $notifycolour = $goodcolour;
116 } else if (eregi("exiting", $newline)) {
117 $notifycolour = $warncolour;
118 } else if (eregi("shutdown succeeded", $newline)) {
119 $notifycolour = $goodcolour;
120 } else if (eregi("ups batteries", $newline)) {
121 $notifycolour = $warncolour;
122 } else if (eregi("exhausted", $newline)) {
123 $notifycolour = $badcolour;
124 } else {
125 $notifycolour = $warncolour;
127 echo "<tr><td bgcolor=\"". $notifycolour ."\" width=15></td><td>";
128 echo $newline;
129 echo "</td></tr>\n";
131 echo "</table>\n";
132 echo "</td></tr>\n";
133 echo "<tr><td colspan=2 align=right>\n";
135 // Add a link so that the user can switch this view off.
136 echo "<a href=\"" . $_SERVER["PHP_SELF"] . "\">Hide old log entries</a></td></tr>\n";
137 } else { // By default, just show the last X lines. Save some screen real estate.
138 echo "<table border=0 width=\"100%\">";
139 for ($i = 0; $i < $showloglines; $i++) {
140 $newline = $logfile[($loglines - $i - 1)];
141 $notifycolour = "white";
142 if (eregi("power is back", $newline)) {
143 $notifycolour = $goodcolour;
144 } else if (eregi("power failure", $newline)) {
145 $notifycolour = $badcolour;
146 } else if (eregi("startup succeeded", $newline)) {
147 $notifycolour = $goodcolour;
148 } else if (eregi("exiting", $newline)) {
149 $notifycolour = $warncolour;
150 } else if (eregi("shutdown succeeded", $newline)) {
151 $notifycolour = $goodcolour;
152 } else if (eregi("ups batteries", $newline)) {
153 $notifycolour = $warncolour;
154 } else if (eregi("exhausted", $newline)) {
155 $notifycolour = $badcolour;
156 } else {
157 $notifycolour = $warncolour;
159 echo "<tr><td bgcolor=\"". $notifycolour ."\" width=15></td><td>";
160 echo $newline;
161 echo "</td></tr>\n";
163 echo "</table>\n";
164 echo "</td></tr>\n";
165 echo "<tr><td colspan=2 align=right>\n";
167 // Add a link so the user can see the entire log from the beginning.
168 echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?showlog=yes\">Show entire log</a></td>\n";
171 echo "</tr></table>";
173 </body>
174 </html>