File: mon.cgi

package info (click to toggle)
mon 0.38.15-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 708 kB
  • ctags: 238
  • sloc: perl: 6,458; ansic: 774; sh: 332; makefile: 112
file content (557 lines) | stat: -rwxr-xr-x 16,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/perl -T

# ----------------------------------------------------------------------
# Web interface for Mon.
# Arthur K. Chan <artchan@althem.com>
#   Based on the Mon program by Jim Trocki <trockij@transmeta.com>. 
#   http://www.kernel.org/software/mon/
# ----------------------------------------------------------------------
# $Id: mon.cgi,v 1.10.2.1 1999/08/23 13:05:39 trockij Exp $
# ----------------------------------------------------------------------

# Instructions ---------------------------------------------------------
# 
# Install this cgi script to whereever your cgi-bin directory sits
# on your mon server. If you don't have a web server installed, try
# http://www.apache.org.
# 
# Modify the "Configurable Parameters" section below to customize it
# to your site's settings. Send comment about this web interface to
# Art Chan at the address above.
#
# This script will require the CGI perl module. Available at any
# perl CPAN site. See http://www.perl.org for details.


# Configurable Parameters ----------------------------------------------

$organization = "";				# Organization name.
$monadmin = "";					# Your e-mail address.
$logo = "";					# Company or mon logo.
$reload="180";					# Seconds for page reload.
$monhost="localhost";				# Mon server hostname.
$BGCOLOR = "white";				# Background color
$BUTTONCOLOR = "tan";				# Button bar color

# You shouldn't need to change these.
$url = $ENV{SCRIPT_NAME};			# URL of this script.

# General Declarations -------------------------------------------------

use CGI;				       # Use the cgi module.
use Mon::Client;			       # mon client interface

%OPSTAT = %Mon::Client::OPSTAT;

$webpage = new CGI;			       # Declare instance of mod.

$time = time;				       # This will be used for 
$localtime = localtime(time);		       # the time on the page.


# Reverse sort ---------------------------------------------------------
sub backwards {
    $b cmp $a;
}

# Return untainted host or group name if safe, undef otherwise ---------
sub validate_name {
    return $_[0] =~ /^([\w.\-_]+)$/ ? $1 : undef;
}


# Setup the html doc headers and such ----------------------------------
sub setup_page {
    my ($title) = @_;
    print $webpage->header;
    print $webpage->start_html(-title=>"MON - $title",
                             -BGCOLOR=>$BGCOLOR);
    print $webpage->h1("MON $title");
    if ($logo){
	$webpage->print("\n<img src=\"$logo\"><br><br>\n");
    }
    if ($organization){
	print $webpage->h3("$organization");
    }
    &print_bar;

    $qtime = time;
    @time = localtime($qtime);
    my $ttime = sprintf ("%.2d:%.2d:%.2d", $time[2],$time[1],$time[0]);
    print $webpage->center
	("\nThis information was presented at: $ttime. ");
} 


# Print the button bar -------------------------------------------------
sub print_bar {
    $button = "INPUT TYPE=\"submit\" NAME=\"command\"";
    $webpage->print("\n\n<FORM METHOD=\"GET\">\n");
    $webpage->print("<table bgcolor=$BUTTONCOLOR width=75% border=1 align=center>\n");
    $webpage->print("<tr>\n");
    $webpage->print("\t<td width=3 align=center>Show Operation Status</td>\n");
    $webpage->print("\t<td width=3 align=center>Show Alert History</td>\n");
    $webpage->print("\t<td width=3 align=center>Disable/Enable monitoring.</td>\n");
    $webpage->print("\t<td width=3 align=center>List Disabled Hosts</td>\n");
    $webpage->print("\t<td width=3 align=center>List PIDs for mon processes.</td>\n");
    $webpage->print("\t<td width=3 align=center>Restart Mon</td>\n");
    $webpage->print("</tr>\n");
    $webpage->print("\t<tr><td width=3 align=center><$button VALUE=\"opstatus\"></td>\n");
    $webpage->print("\t<td width=3 align=center><$button VALUE=\"alerthist\"></td>\n");
    $webpage->print("\t<td width=3 align=center><$button VALUE=\"disable\"></td>\n");
    $webpage->print("\t<td width=3 align=center><$button VALUE=\"disabled\"></td>\n");
    $webpage->print("\t<td width=3 align=center><$button VALUE=\"pids\"></td>\n");
    $webpage->print("\t<td width=3 align=center><$button VALUE=\"reset\"></td>\n");
    $webpage->print("</tr></table>");

    $webpage->print("</form>\n");
}


# query the server operational status ----------------------------------
sub query_opstatus {

    print $webpage->center
	("This page will reload every $reload seconds.<br><br>\n");
    $webpage->print("\n<META HTTP-EQUIV=\"Refresh\"");
    $webpage->print("CONTENT=\"$reload\", URL=\"$url\">\n");

    my $c = new Mon::Client (
    	host => $monhost,
    );

    $c->connect();

    if ($c->error ne "") {
    	# ignore the error for now :(
    }

    my %op = $c->list_opstatus();

    if ($c->error ne "") {
    	# ignore the error for now :(
    }

    $c->disconnect();

    $webpage->print("<table align=center border=1 width=35%>");
    $webpage->print
	("<tr><td><font color=black>Service color legend:</font></td>\n");
    $webpage->print("<td><font color=black>Unchecked</font></td>\n");
    $webpage->print("<td><font color=green>Good</font></td>\n ");
    $webpage->print("<td><font color=red>Error</font></td></tr>\n");
    $webpage->print("</table>");    

    $webpage->print
	("<table align=center width=80% border=1>\n");
    $webpage->print
	("<tr><th>Host Group</th><th>Service</th>\n");
    $webpage->print
	("<th>Last Checked</th><th>Est. Next Check</th></tr>\n");

    foreach my $group (sort keys %op) {
	foreach my $service (sort keys %{$op{$group}}) {

	    my $s = \%{$op{$group}->{$service}};

	    $webpage->print("<tr><td>\n");
	    $webpage->print
		("<a href=\"$url?command=group&args=$group\">");
	    $webpage->print("$group</a></td>\n");

	    if ($s->{"opstatus"} == $OPSTAT{"untested"}) {
		$webpage->print("<td><font color=black>\n");
		$webpage->print("$service</font></td>");

	    } elsif ($s->{"opstatus"} == $OPSTAT{"fail"}) {
		$webpage->print("<td><blink>");
		$webpage->print
		    ("<a href=\"$url?command=alert&args=$group,$service\">");
		$webpage->print
		    ("<font color=red>$service</font></a></blink></td>\n");

	    } elsif ($s->{"opstatus"} == $OPSTAT{"ok"}) {
		$webpage->print("<td><font color=green>");
		$webpage->print("$service</font></td>\n");

	    } else {
		my $txt = "";
		for (keys %OPSTAT) {
		    $txt = $_ if ($s->{"opstatus"} == $OPSTAT{$_});
		}
		$webpage->print("<td><font color=purple>");
		$webpage->print("$service ($txt)</font></td>\n");
	    }

	    if ($s->{"opstatus"} == $OPSTAT{"untested"}) {
		$webpage->print("<td>-</td>");

	    } else {
		my @time = localtime ($s->{"last_check"});
		$webpage->print(
		    sprintf ("<td>%.2d:%.2d:%.2d</td>\n", @time[2, 1, 0])
		);
	    }

	    my @time = localtime ($qtime+$s->{"next"});
	    $webpage->print(
		sprintf ("<td>%.2d:%.2d:%.2d</td>\n", @time[2, 1, 0])
	    );
	    $webpage->print("</tr>");
	}
    }
    $webpage->print("</table>\n");    
}


# Extract the list of hosts associated with the group ---------------
sub query_group {

    my $group = &validate_name ($args);

    if (!defined $group) {
	$webpage->print("Invalid host group\n");

    } else {
	my $c = new Mon::Client (
	    host => $monhost,
	);

	$c->connect();

	if ($c->error ne "") {
		# ignore error for now
	}

	@hosts = $c->list_group ($group);

	if ($c->error ne "") {
	    my $e = $c->error;
	    $webpage->print (<<"EOF"
<pre>
Could not list groups: $e
</pre>
EOF
	    );
	    return undef;
	}

	$c->disconnect();

	$webpage->print("<table align=center width=50% border=1>");
	$webpage->print("<th>Members of group \"<em>$group</em>\".</th>") ;    

	foreach my $host (sort @hosts) {
	    $webpage->print("<tr><td>$host</td></tr>") ;
	}
	$webpage->print("</table>");
    }
}


# End the document -----------------------------------------------------
sub end_page {
    &print_bar;
    if ($monadmin) {
	print $webpage->center("For questions about this server. Contact:");
	print $webpage->center
	    ("<a href=\"mailto:$monadmin\">$monadmin</a><br>");
    }
    print $webpage->end_html;
}


# Get the params from the form -----------------------------------------
sub get_params {
    $command = $webpage->param('command');
    $args = $webpage->param('args');
    $enable_host = $webpage->param('enablehost');
    $enable_group = $webpage->param('enablegroup');
    $enable_service = $webpage->param('enableservice');
    $enable_watch = $webpage->param('enablewatch');

    $disable_host = $webpage->param('disablehost');
    $disable_group = $webpage->param('disablegroup');
    $disable_service = $webpage->param('disableservice');
    $disable_watch = $webpage->param('disablewatch');
}


# What to do if the view alert details is depressed --------------------
sub alert {
    local ($arg) = @_;

    my ($group, $service) = split (/\,/, $arg);

    print $webpage->hr;
    $webpage->print
	("<h2>Failure detail for group <font color=red>$group</font> ");
    $webpage->print
	("and service <i>$service</i> test:</h2>");

    my $c = new Mon::Client (
    	host => $monhost,
    );

    $c->connect();

    if ($c->error ne "") {
    	# ignore errors for now
    }

    my %op = $c->list_opstatus();

    if ($c->error ne "") {
    	# ignore errors for now
    }

    $c->disconnect();

    foreach my $g (keys %op) {
	next if ($g ne $group);
    	foreach my $s (keys %{$op{$g}}) {
	    next if ($s ne $service);
	    $webpage->print("$op{$g}->{$s}->{last_summary}\n");
	}
    }

    print $webpage->hr;
}


# List alert history --------------------------------------------------
sub alerthist {
    print $webpage->hr;
    print $webpage->h2("Alert History:");

    my $c = new Mon::Client (
    	host => $monhost,
    );

    $c->connect();

    if ($c->error ne "") {
    	# ignore error
    }

    my @l = $c->list_alerthist();

    $c->disconnect();

    $webpage->print("<table border=1 width=80% align=center>\n");

    $webpage->print("<tr><th>Group</th><th>Service</th>\n");
    $webpage->print("<th>Type</th><th>Time</th><th>Alert</th>\n");
    $webpage->print("<th>Args</th><th>Summary</th>\n");
    $webpage->print("</tr>\n");	

    foreach my $line (reverse sort {$a->{"time"} <=> $b->{"time"}} (@l)) {
	my $localtime = localtime ($line->{"time"});

	$webpage->print("<tr><td><a href=\"$url?command=group&");
	$webpage->print("args=$line->{group}\">$line->{group}</a></td>");

        $webpage->print("<td>$line->{service}</td>\n");
	$webpage->print("<td>$line->{type}</td>\n");
	$webpage->print("<td>$localtime</td>\n");

	$line->{"alert"} =~ s{^.*\/([^/]*)$}{$1};

	$webpage->print("<td>$line->{alert}</td>\n");

	my $args = "-";
	if ($line->{"args"} !~ /^\s*$/) {
	    $args = $line->{"args"};
	}

	$webpage->print("<td>$args</td>\n");
	$webpage->print("<td>$line->{summary}</td>");

	$webpage->print("</tr>\n");
    }

    $webpage->print("</table>\n");	
    print $webpage->hr;    
}


# Generic button function ---------------------------------------------
sub button {
    local ($title, $command) = @_;

    print $webpage->hr;
    print $webpage->h2("$title");
    print "(command not implemented in this client)\n";
    print $webpage->hr;
}


# Form to get item to disable -------------------------------------------
sub disable {
    print $webpage->hr;
    print $webpage->h2("$title");

    $webpage->print ("(command not implemented in this client)\n");

    print $webpage->hr;

    return;

    my ($desc, $cmd, @arg);
    if ($disable_host) {
	$desc = "Attempting to disable a host";
	$cmd = "disable host";
	push @arg, $disable_host;
    } elsif (($disable_group) && ($disable_service)) {
	$desc = "Attempting to disable a service";
    	$cmd = "disable service";
	push @arg, $disable_group, $disable_service;
    } elsif ($disable_watch) {
	$desc = "Disable a watch";
    	$cmd = "disable watch";
	push @arg, $disable_watch;
    } elsif ($enable_host) {
	$desc = "Attempting to enable a host";
	$cmd = "enable host";
	push @arg, $enable_host;
    } elsif (($enable_group) && ($enable_service)) {
	$desc = "Attempting to enable a service";
	$cmd = "enable service";
	push @arg, $enable_group, $enable_service;
    } elsif ($enable_watch) {
	$desc = "Enable a watch";
	$cmd = "enable watch";
	push @arg, $enable_watch;
    }
    if ($desc) {
	my $bad = 0;
	for (@arg) {
	    $_ = &validate_name($_);
	    $bad = 1 if !defined $_;
	}
	if ($bad) {
	    # Don't try to print out the invalid arg, it wouldn't be
	    # safe.
	    print "Invalid argument for $cmd.\n";
	}
	else {
	    print "$desc: @arg<br>\n";
	    for (@arg) {
		$cmd .= " \Q$_";
	    }
	    open (MON, "$moncmd -s $monhost $cmd|");
	    while (<MON>) {
		print "$_<BR>\n";
	    }
	    close MON;
	}
	print $webpage->hr;
    }

    print "<table width=100%>";
    print "<tr><td>";
    print $webpage->startform(GET);
    print "Disable a host.<br>\n";
    print $webpage->textfield('disablehost');
    print " Hostname<br>";
    print $webpage->submit(-name=>'command',-value=>'disable');
    print $webpage->endform;
    print "</td><td>";
    print $webpage->startform(GET);
    print "Enable a host.<br>\n";
    print $webpage->textfield('enablehost');
    print " Hostname<br>";
    print $webpage->submit(-name=>'command',-value=>'enable');
    print $webpage->endform;
    print "</td></table>";
    print $webpage->hr;

    print "<table width=100%>";
    print "<tr><td>";
    print $webpage->startform(GET);
    print "Disable a service for a group.<br>\n";
    print $webpage->textfield('disablegroup');
    print " Group<br>";
    print $webpage->textfield('disableservice');
    print " Service<br>";
    print $webpage->submit(-name=>'command',-value=>'disable');
    print $webpage->endform;
    print "</td><td>";
    print $webpage->startform(GET);
    print "Enable a service for a group.<br>\n";
    print $webpage->textfield('enablegroup');
    print " Group<br>";
    print $webpage->textfield('enableservice');
    print " Service<br>";
    print $webpage->submit(-name=>'command',-value=>'enable');
    print $webpage->endform;
    print "</td></table>";
    print $webpage->hr;

    print "<table width=100%>";
    print "<tr><td>";
    print $webpage->startform(GET);
    print "Disable a watch.<br>\n";
    print $webpage->textfield('disablewatch');
    print " Watch<br>";
    print $webpage->submit(-name=>'command',-value=>'disable');
    print $webpage->endform;
    print "</td><td>";
    print $webpage->startform(GET);
    print "Enable a watch.<br>\n";
    print $webpage->textfield('enablewatch');
    print " Watch<br>";
    print $webpage->submit(-name=>'command',-value=>'enable');
    print $webpage->endform;
    print "</td></table>";
    print $webpage->hr;
}



# Main program ---------------------------------------------------------

&get_params;				       # Read the args.

if ($command =~ "group" ){		       # Expand hostgroup.
    &setup_page("Group Expansion");
    &query_group;
}
elsif ($command =~ "alerthist"){	       # Alert history button.
    &setup_page("List the alert history");
    &alerthist;
}
elsif ($command =~ "alert"){		       # View alert details.
    &setup_page("Alert Details");
    &alert($args);
}
elsif ($command =~ "disabled"){		       # Disabled hosts button.
    &setup_page("List disabled hosts");
    &button("Disabled hosts:","list disabled");
}
elsif ($command =~ "disable"){		       # Disabled 
    &setup_page("Disable/Enable alert for host, group. or service");
    &disable;
}
elsif ($command =~ "enable"){		       # Disabled 
    &setup_page("Disable/Enable alert for host, group. or service");
    &disable;
}
elsif ($command =~ "pids"){		       # View pid button.
    &setup_page("List pids of server, alerts and monitors.");
    &button("List pids for all mon processes:", "list pids");
}
elsif ($command =~ "reset"){		       # Reset mon button.
    &setup_page("Restart Mon.");
    &button("Attempting to reset mon...","reset");
}
# Button "opstatus" will fall through to else.
else {					       # All else.
    &setup_page("Operation Status");
    &query_opstatus;
}

&end_page;


# That's it! ----------------------------------------------------