~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to html/ops/sample_server_status.php

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
// This file is part of BOINC.
3
 
// http://boinc.berkeley.edu
4
 
// Copyright (C) 2008 University of California
5
 
//
6
 
// BOINC is free software; you can redistribute it and/or modify it
7
 
// under the terms of the GNU Lesser General Public License
8
 
// as published by the Free Software Foundation,
9
 
// either version 3 of the License, or (at your option) any later version.
10
 
//
11
 
// BOINC is distributed in the hope that it will be useful,
12
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
// See the GNU Lesser General Public License for more details.
15
 
//
16
 
// You should have received a copy of the GNU Lesser General Public License
17
 
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
// server_status.php [-f xml_output_filename]
20
 
//   (or server_status.php?xml=1)
21
 
//
22
 
// outputs general information about BOINC server status gathered from
23
 
// config.xml or mysql database queries. If you are running all your
24
 
// services on one machine, and the database isn't so large, this should
25
 
// work right out of the box. Otherwise see configureables below.
26
 
//
27
 
// Daemons in config.xml are checked to see if they are running by ssh'ing
28
 
// into the respective hosts and searching for active pids. Passwordless
29
 
// logins must be in effect if there are multiple hosts involved.
30
 
//
31
 
// The database queries may be very slow. You might consider running these
32
 
// queries elsewhere via cronjob, outputing numbers into a readable file,
33
 
// and then getting the latest values with a `/bin/tail -1 data_file`.
34
 
// See commented example in the code.
35
 
//
36
 
// You can get an xml version of the stats via the web when the url has the
37
 
// optional "?xml=1" tag at the end, i.e 
38
 
//   http://yourboincproject.edu/server_status.php?xml=1
39
 
//
40
 
// If running as a standalone program there is an optional -f flag where
41
 
// you can generate xml server status output to the filename you provide
42
 
// (this will output both html to stdout and xml to the filename given).
43
 
// Some may prefer to do this if takes so long to dredge up the stats for
44
 
// the html, you won't have to do it again to generate the xml.
45
 
//
46
 
// It is highly recommended that you run this program every 10 minutes and
47
 
// send its stdout to an .html file, rather than having the values get
48
 
// regenerated every time the page is accessed. Or use the available
49
 
// web page cache utilities.
50
 
//
51
 
// You should edit the following variables in config.xml to suit your needs:
52
 
//
53
 
// <www_host>    hostname of web server (default: same as <host>)
54
 
// <sched_host>  hostname of scheduling server (default: same as <host>)
55
 
// <uldl_host>   hostname of upload/download server (default: same as <host>)
56
 
// <uldl_pid>    pid file of upload/download server httpd.conf
57
 
//               (default: /etc/httpd/run/httpd.pid)
58
 
// <ssh_exe>     path to ssh (default: /usr/bin/ssh)
59
 
// <ps_exe>      path to ps (which supports "w" flag) (default: /bin/ps)
60
 
 
61
 
///////////////////////////////////////////////////////////////////////////////
62
 
 
63
 
require_once("../inc/util.inc");
64
 
require_once("../inc/db.inc");
65
 
require_once("../inc/xml.inc");
66
 
require_once("../inc/cache.inc");
67
 
 
68
 
$xml = get_int("xml", true);
69
 
 
70
 
$cache_args = "";
71
 
if ($xml) $cache_args = "xml=1";
72
 
$cache_period = 600;
73
 
start_cache($cache_period, $cache_args);
74
 
 
75
 
$xmlout = "";
76
 
if ($argc>0 && $argv[1] == "-f") {
77
 
    $xmlout = $argv[2];
78
 
    $xmloutfile = fopen($xmlout,"w+");
79
 
    if (!$xmloutfile) {
80
 
        die( "failed to open file: $xmlout");
81
 
    }
82
 
}
83
 
 
84
 
// daemon status outputs: 1 (running) 0 (not running) or -1 (disabled)
85
 
//
86
 
function daemon_status($host, $pidname, $progname, $disabled) {
87
 
    global $ssh_exe, $ps_exe, $project_host;
88
 
    $path = "../../pid_$host/$pidname.pid";
89
 
    $running = 0;
90
 
    if (is_file($path)) {
91
 
        $pid = file_get_contents($path);
92
 
        if ($pid) {
93
 
            $pid = trim($pid);
94
 
            $command = "$ps_exe ww $pid";
95
 
            if ($host != $project_host) {
96
 
                $command = "$ssh_exe $host " . $command;
97
 
            }
98
 
            $foo = exec($command);
99
 
            if ($foo) {
100
 
                if (strstr($foo, (string)$pid)) $running = 1;
101
 
            }
102
 
        }
103
 
    }
104
 
    if ($disabled == 1) $running = -1;
105
 
    return $running;
106
 
}
107
 
 
108
 
function show_status($host, $function, $running) {
109
 
    global $xml,$xmlout,$xmloutfile;
110
 
    $xmlstring = "    <daemon>\n      <host>$host</host>\n      <command>$function</command>\n";
111
 
    $htmlstring = "<tr><td>$function</td><td>$host</td>";
112
 
    if ($running == 1) {
113
 
        $xmlstring .= "      <status>running</status>\n";
114
 
        $htmlstring .= "<td class=\"running\">Running</td>\n";
115
 
    } elseif ($running == 0) {
116
 
        $xmlstring .= "      <status>not running</status>\n";
117
 
        $htmlstring .= "<td class=\"notrunning\">Not Running</td>\n";
118
 
    } else {
119
 
        $xmlstring .= "      <status>disabled</status>\n";
120
 
        $htmlstring .= "<td class=\"disabled\">Disabled</td>\n";
121
 
    }
122
 
    $xmlstring .= "    </daemon>\n";
123
 
    $htmlstring .= "</tr>\n";
124
 
    if ($xml) {
125
 
        echo $xmlstring; return 0;
126
 
    }
127
 
    if ($xmlout) {
128
 
        fwrite($xmloutfile, $xmlstring);
129
 
    }
130
 
    echo $htmlstring;
131
 
    return 0;
132
 
}
133
 
 
134
 
function show_daemon_status($host, $pidname, $progname, $disabled) {
135
 
    $running = daemon_status($host, $pidname, $progname, $disabled);
136
 
    show_status($host, $pidname, $running);
137
 
}
138
 
 
139
 
function show_counts($key, $xmlkey, $value) {
140
 
    global $xml,$xmlout,$xmloutfile;
141
 
    $formattedvalue = number_format($value);
142
 
    $xmlstring = "    <$xmlkey>$value</$xmlkey>\n";
143
 
    if ($xml) {
144
 
        echo $xmlstring;
145
 
        return 0;
146
 
    }
147
 
    if ($xmlout) {
148
 
        fwrite($xmloutfile,$xmlstring);
149
 
    }
150
 
    echo "<tr><td>$key</td><td>$formattedvalue</td></tr>";
151
 
    return 0;
152
 
}
153
 
 
154
 
function get_mysql_count ($query) {
155
 
    $result = mysql_query("select count(*) as count from " . $query);
156
 
    $count = mysql_fetch_object($result);
157
 
    mysql_free_result($result);
158
 
    return $count->count;
159
 
}
160
 
 
161
 
$config_xml = get_config();
162
 
$config_vars = parse_element($config_xml,"<config>");
163
 
$project_host = parse_element($config_vars,"<host>");
164
 
$www_host = parse_element($config_vars,"<www_host>");
165
 
if ($www_host == "") {
166
 
    $www_host = $project_host;
167
 
}
168
 
$sched_host = parse_element($config_vars,"<sched_host>");
169
 
if ($sched_host == "") {
170
 
    $sched_host = $project_host;
171
 
}
172
 
$uldl_pid = parse_element($config_vars,"<uldl_pid>");
173
 
if ($uldl_pid == "") {
174
 
    $uldl_pid = "/etc/httpd/run/httpd.pid";
175
 
}
176
 
$uldl_host = parse_element($config_vars,"<uldl_host>");
177
 
if ($uldl_host == "") {
178
 
    $uldl_host = $project_host;
179
 
}
180
 
$ssh_exe = parse_element($config_vars,"<ssh_exe>");
181
 
if ($ssh_exe == "") {
182
 
    $ssh_exe = "/usr/bin/ssh";
183
 
184
 
$ps_exe = parse_element($config_vars,"<ps_exe>");
185
 
if ($ps_exe == "") {
186
 
    $ps_exe = "/bin/ps";
187
 
}
188
 
 
189
 
$version = null;
190
 
if (file_exists("../../local.revision")) {
191
 
    $version = trim(file_get_contents("../../local.revision"));
192
 
}
193
 
$now = time();
194
 
 
195
 
$xmlstring = "<server_status>
196
 
  <update_time>$now</update_time>
197
 
";
198
 
if ($version) {
199
 
    $xmlstring .= "<software_version>$version</software_version>\n";
200
 
}
201
 
$xmlstring .= "  <daemon_status>\n";
202
 
if ($xml) {
203
 
    xml_header();
204
 
    echo $xmlstring;
205
 
} else {
206
 
    if ($xmlout) {
207
 
        fwrite($xmloutfile,$xmlstring);
208
 
    }
209
 
    page_head("Server status page");
210
 
    if ($version) {
211
 
        echo "Server software version: $version<p>\n";
212
 
    }
213
 
    echo time_str(time()), "
214
 
        <table width=100%>
215
 
        <tr>
216
 
        <td width=40% valign=top>
217
 
        <h2>Server status</h2>
218
 
        <table border=0 cellpadding=4>
219
 
        <tr><th>Program</th><th>Host</th><th>Status</th></tr>
220
 
    ";
221
 
}
222
 
;
223
 
// Are the data-driven web sites running? Check for existence of stop_web.
224
 
// If it is there, set $web_running to -1 for "disabled",
225
 
// otherwise it will be already set to 1 for "enabled."
226
 
// Set $www_host to the name of server hosting WWW site.
227
 
//
228
 
$web_running = !file_exists("../../stop_web");
229
 
if ($web_running == 0) $web_running = -1;
230
 
show_status($www_host, "data-driven web pages", $web_running);
231
 
 
232
 
// Check for httpd.pid file of upload/download server.
233
 
//
234
 
$uldl_running = file_exists($uldl_pid);
235
 
if ($uldl_running == 0) $uldl_running = -1;
236
 
show_status($uldl_host, "upload/download server", $uldl_running);
237
 
 
238
 
$sched_running = !file_exists("../../stop_sched");
239
 
show_status($sched_host, "scheduler", $sched_running);
240
 
 
241
 
// parse through config.xml to get all daemons running
242
 
//
243
 
$cursor = 0;
244
 
while ($thisxml = trim(parse_next_element($config_xml,"<daemon>",$cursor))) {
245
 
    $host = parse_element($thisxml,"<host>");
246
 
    if ($host == "") {
247
 
        $host = $project_host;
248
 
    }
249
 
    $cmd = parse_element($thisxml,"<cmd>");
250
 
    list($ncmd) = explode(" ",$cmd);
251
 
    $log = parse_element($thisxml,"<output>");
252
 
    if (!$log) {
253
 
        $log = $ncmd . ".log";
254
 
    }
255
 
    list($nlog) = explode(".log",$log);
256
 
    $pid = parse_element($thisxml,"<pid_file>");
257
 
    if (!$pid) {
258
 
        $pid = $ncmd . ".pid";
259
 
    }
260
 
    $disabled = parse_element($thisxml,"<disabled>");
261
 
    show_daemon_status($host, $nlog, $ncmd, $disabled);
262
 
}
263
 
 
264
 
$xmlstring = "  </daemon_status>\n  <database_file_states>\n";
265
 
if ($xml) {
266
 
    echo $xmlstring;
267
 
} else {
268
 
    if ($xmlout) {
269
 
        fwrite($xmloutfile,$xmlstring);
270
 
    }
271
 
    echo "
272
 
        <tr><td align=right><b>Running:</b></td>
273
 
        <td colspan=2>Program is operating normally</td></tr>
274
 
        <tr><td align=right><b>Not Running:</b></td>
275
 
        <td colspan=2>Program failed or ran out of work<br>
276
 
           (or the project is down)</td></tr>
277
 
        <tr><td align=right><b>Disabled:</b></td>
278
 
        <td colspan=2>Program has been disabled by staff<br>
279
 
           (for debugging/maintenance)</td></tr>
280
 
        </table>
281
 
        </td>
282
 
        <td width=40% valign=top>
283
 
        <h2>Database/file status</h2>
284
 
    ";
285
 
}
286
 
 
287
 
$retval = db_init_aux();
288
 
if ($retval) {
289
 
    echo "The database server is not accessible";
290
 
} else {
291
 
    if (!$xml) {
292
 
        echo "
293
 
            <table border=0 cellpadding=4>
294
 
            <tr><th>State</th><th>#</th></tr>
295
 
        ";
296
 
    }
297
 
 
298
 
    // If you are reading these values from a file rather than
299
 
    // making live queries to the database, do something like this:
300
 
    //
301
 
    // $sendfile = "/home/boincadm/server_status_data/count_results_unsent.out";
302
 
    // $n = `/bin/tail -1 $sendfile`;
303
 
    // show_counts("Results ready to send","results_ready_to_send",$n);
304
 
 
305
 
    show_counts(
306
 
        "Results ready to send",
307
 
        "results_ready_to_send",
308
 
        get_mysql_count("result where server_state = 2")
309
 
    );
310
 
    show_counts(
311
 
        "Results in progress",
312
 
        "results_in_progress",
313
 
        get_mysql_count("result where server_state = 4")
314
 
    );
315
 
    show_counts(
316
 
        "Workunits waiting for validation",
317
 
        "workunits_waiting_for_validation",
318
 
        get_mysql_count("workunit where need_validate=1")
319
 
    );
320
 
    show_counts(
321
 
        "Workunits waiting for assimilation",
322
 
        "workunits_waiting_for_assimilation",
323
 
        get_mysql_count("workunit where assimilate_state=1")
324
 
    );
325
 
    show_counts(
326
 
        "Workunits waiting for deletion",
327
 
        "workunits_waiting_for_deletion",
328
 
        get_mysql_count("workunit where file_delete_state=1")
329
 
    );
330
 
    show_counts(
331
 
        "Results waiting for deletion",
332
 
        "results_waiting_for_deletion",
333
 
        get_mysql_count("result where file_delete_state=1")
334
 
    );
335
 
 
336
 
    $result = mysql_query("select MIN(transition_time) as min from workunit");
337
 
    $min = mysql_fetch_object($result);
338
 
    mysql_free_result($result);
339
 
    $gap = (time() - $min->min)/3600;
340
 
    if (($gap < 0) || ($min->min == 0)) {
341
 
        $gap = 0;
342
 
    }
343
 
    show_counts(
344
 
        "Transitioner backlog (hours)",
345
 
        "transitioner_backlog_hours",
346
 
        $gap
347
 
    );
348
 
    if (!$xml) {
349
 
        echo "</table>";
350
 
    }
351
 
}
352
 
 
353
 
$xmlstring = "  </database_file_states>\n</server_status>\n";
354
 
if ($xml) {
355
 
    echo $xmlstring;
356
 
} else {
357
 
    if ($xmlout) {
358
 
        fwrite($xmloutfile, $xmlstring);
359
 
    }
360
 
    echo "
361
 
        </td>
362
 
        <td>&nbsp;</td>
363
 
        </tr>
364
 
        </table>
365
 
    ";
366
 
    page_tail();
367
 
}
368
 
 
369
 
end_cache($cache_period, $cache_args);
370
 
?>