Index: monshow.cgi
===================================================================
RCS file: /home/dpavlin/private/cvsroot/mon-modules/monshow.cgi,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- monshow.cgi	10 Aug 2002 16:33:09 -0000	1.1
+++ monshow.cgi	10 Aug 2002 16:44:38 -0000	1.2
@@ -4,6 +4,13 @@
 #
 # Jim Trocki, trockij@transmeta.com
 #
+# Midified to produce WAP (wml) output by
+# Dobrica Pavlinusic <dpavlin@rot13.org>
+# http://www.rot13.org/~dpavlin/sysadm.html
+#
+# Since it users <b>...</b> tags in wml outout, this script will NOT WORK
+# on Nokia 7110.
+#
 # $Id: monshow 1.7 Sun, 24 Jun 2001 22:41:40 -0400 trockij $
 #
 #    Copyright (C) 1998, Jim Trocki
@@ -82,6 +89,8 @@
 
 @ARGV == 0 || usage "No non-switch args expected\n";
 
+my $WAP = 1;	# force wap output
+
 my $CGI;
 my %QUERY_ARGS;
 if (defined $ENV{"REQUEST_METHOD"})
@@ -213,7 +222,14 @@
 
 compose_trailer;
 
-if ($CGI)
+if ($WAP) {
+	print <<EOF;
+Content-type: text/vnd.wap.wml
+
+$OUT_BUF
+EOF
+}
+elsif ($CGI)
 {
     print <<EOF;
 Content-type: text/html
@@ -569,7 +585,22 @@
 {
     my $msg = shift;
 
-    if ($CGI)
+    if ($WAP) {
+print <<EOF;
+Content-type: text/vnd.wap.wml
+
+<?xml version="1.0"?>
+<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
+<wml>
+<card id="error" title="Error">
+<p>
+$msg
+</p>
+</card>
+</wml>
+EOF
+    }
+    elsif ($CGI)
     {
 print <<EOF;
 Content-type: text/html
@@ -607,11 +638,12 @@
 {
     if ($CGI)
     {
-    	$OUT_BUF .= <<EOF;
+    	$OUT_BUF .= <<EOF if (! $WAP);
 <h2> All systems OK </h2>
 <p>
 
 EOF
+    	$OUT_BUF .= "<b> All systems OK </b>" if ($WAP);
     }
 
     else
@@ -828,31 +860,37 @@
     #
     if ($CGI)
     {
-	$OUT_BUF = <<EOF;
+	$OUT_BUF = <<EOF if (! $WAP);
 <html>
 <head>
 <title> Operational Status </title>
 EOF
 
+	$OUT_BUF = <<EOF if ($WAP);
+<?xml version="1.0"?>
+<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
+<wml>
+EOF
+
 	if ($CF->{"refresh"})
 	{
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
 <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
 <META HTTP-EQUIV="Refresh" CONTENT=$CF->{refresh}>
 EOF
 	}
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 </head>
 <body bgcolor="#$CF->{'bg'}">
 EOF
 
 	if ($CF->{"html-header"} ne "")
 	{
-	    $OUT_BUF .= $CF->{"html-header"};
+	    $OUT_BUF .= $CF->{"html-header"} if (! $WAP);
 	}
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 <h1><FONT FACE="sans-serif">Operational Status</font></h1>
 
 <table width="100%">
@@ -873,17 +911,33 @@
 		</tr>
 EOF
 
+	# skip overview card if detail page is used or next page navigation
+	$OUT_BUF .= <<EOF if ($WAP && !$QUERY_ARGS{"detail"} && !$QUERY_ARGS{"p"});
+<card id="status" title="Operational Status" ontimer="#display">
+ <timer value="10"/>
+<p>
+<b>Server:</b> $CF->{host}
+<br/><b>Time:</b> $t
+<br/><b>State:</b> $state 
+</p>
+<p><a href="#display">Display status</a></p>
+</card>
+EOF
+
 	if ($GLOBAL->{"view-name"} ne "")
 	{
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
 		<tr>
 		    <td align=right><b>View:</b></td>
 		    <td><b>$GLOBAL->{"view-name"}</b></td>
 		</tr>
 EOF
+	    $OUT_BUF .= <<EOF if (! $WAP);
+<b>View:</b> $GLOBAL->{"view-name"}
+EOF
 	}
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 	    </table>
 	</td>
 
@@ -908,6 +962,7 @@
 <p>
 <hr>
 EOF
+
     }
 
     else
@@ -1053,7 +1108,7 @@
     #
     if ($CGI)
     {
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 
 <table cellpadding=2 cellspacing=1 bgcolor="#$CF->{'table-color'}" width="100%">
     <tr>
@@ -1071,6 +1126,8 @@
 EOF
     }
 
+    my @wml;
+
     foreach my $l (@{$rows})
     {
 	my ($depstate, $group, $service) = @{$l};
@@ -1171,6 +1228,8 @@
 
 	else
 	{
+	    if (! $WAP)
+	    {
 	    if ($SUMMARY =~ /^[\s\n]*$/)
 	    {
 		$SUMMARY = "<pre> </pre>";
@@ -1185,7 +1244,8 @@
 	    {
 	    	$bgcolor = "bgcolor=\"#$bgcolor\"";
 	    }
-	    $OUT_BUF .= <<EOF;
+	    }
+	    $OUT_BUF .= <<EOF if (! $WAP);
 
 <tr $bgcolor>
    <td> $DEP </td>
@@ -1200,15 +1260,62 @@
 </tr>
 EOF
 
+	    # dump just failuers to wap mobile
+	    if ($WAP) {
+	    	if ($sref->{"opstatus"} == $OPSTAT{"fail"}) {
+		    push @wml,<<EOF;
+<p>
+ <b>$GROUP
+ <a href="$ENV{SCRIPT_NAME}?detail=$group,$service">$SERVICE</a></b>
+ $TIME/$NEXT
+ <b>$STATUS</b>
+ $SUMMARY 
+</p>
+EOF
+		} else {
+		    push @wml,<<EOF;
+<p>
+ $GROUP
+ <a href="$ENV{SCRIPT_NAME}?detail=$group,$service">$SERVICE</a>
+</p>
+EOF
+
+		}
+	    }
+
 	}
     }
 
     if ($CGI)
     {
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 </table>
 
 EOF
+	# generate page-by-page output
+	my $on_page=10;
+	my $max_page=int(($#wml + $on_page -1) / $on_page);
+
+	if ($WAP) {
+		my $p = $QUERY_ARGS{"p"} || 1;	# current page
+
+		$OUT_BUF .= "<card id=\"display\" title=\"Monitoring $p/$max_page\">";
+		for (my $i = 0; $i < $on_page; $i++) {
+			$OUT_BUF .= $wml[$i + ($p-1) * $on_page];
+		}
+		# navigation
+		$OUT_BUF .= "<p>";
+		for (my $i = 1; $i < $max_page; $i++) {
+			if ($i == $p) {
+				$OUT_BUF .= "<b>$i</b> ";
+			} else {
+				$OUT_BUF .= "<a href=\"$ENV{SCRIPT_NAME}?p=$i\">$i</a> ";
+			}
+		}
+		$OUT_BUF .= "</p>";
+
+		$OUT_BUF .= "</card>";
+	}
     }
 }
 
@@ -1223,10 +1330,10 @@
     	if ($CGI)
 	{
 	    $OUT_BUF .= <<EOF;
-<br>
+<br/>
 Nothing is disabled.
-<p>
 EOF
+	    $OUT_BUF .= "<p>" if (! $WAP);
 	}
 
 	else
@@ -1241,11 +1348,14 @@
     {
 	if ($CGI)
 	{
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
 
 <h2> Disabled Watches </h2>
 
 EOF
+
+	    $OUT_BUF .= "<b> Disabled Watches </b>" if ($WAP);
+
 	}
 
 	else
@@ -1257,7 +1367,7 @@
 	{
 	    if ($CGI)
 	    {
-	    	$OUT_BUF .= "$watch <br>\n";
+	    	$OUT_BUF .= "$watch <br/>\n";
 	    }
 
 	    else
@@ -1285,6 +1395,9 @@
 <h2> Disabled Services </h2>
 
 EOF
+
+	    $OUT_BUF .= "<b> Disabled Services </b>" if ($WAP);
+
 	}
 
 	else
@@ -1295,7 +1408,7 @@
 	{
 	    if ($CGI)
 	    {
-	    	$OUT_BUF .= "$_ <br>\n";
+	    	$OUT_BUF .= "$_ <br/>\n";
 	    }
 
 	    else
@@ -1309,11 +1422,16 @@
     {
 	if ($CGI)
 	{
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
 
 <h2> Disabled Hosts </h2>
 
 EOF
+	    $OUT_BUF .= <<EOF if ($WAP);
+
+<p><b> Disabled Hosts </b></p>
+
+EOF
 	}
 
 	else
@@ -1329,7 +1447,7 @@
 	    }
 	    if ($CGI)
 	    {
-		$OUT_BUF .= sprintf ("%-15s %s <br>\n", $group, "@HOSTS");
+		$OUT_BUF .= sprintf ("%-15s %s <br/>\n", $group, "@HOSTS");
 	    }
 
 	    else
@@ -1344,10 +1462,13 @@
 sub compose_trailer {
     if ($CGI)
     {
-    	$OUT_BUF .= <<EOF;
+    	$OUT_BUF .= <<EOF if (! $WAP);
 </body>
 </html>
 EOF
+	$OUT_BUF .= <<EOF if ($WAP);
+</wml>
+EOF
     }
 }
 
@@ -1355,7 +1476,7 @@
 sub compose_detail {
     my ($args, $st) = @_;
 
-    my ($group, $service) = split (/,/, $args, 2);
+    my ($group, $service, $optarg) = split (/,/, $args, 3);
 
     if (!defined ($st->{"opstatus"}->{$group}->{$service}))
     {
@@ -1381,7 +1502,7 @@
 
     foreach my $k (qw (opstatus exitval last_check timer ack ackcomment))
     {
-    	if ($CGI && $sref->{$k} =~ /^\s*$/)
+    	if ($CGI && !$WAP && $sref->{$k} =~ /^\s*$/)
 	{
 	    $d->{$k} = "<pre> </pre>";
 	}
@@ -1420,7 +1541,7 @@
 	my $descr = pre_pad_if_empty ($st->{"desc"}->{$group}->{$service});
 	my $hosts = pre_pad_if_empty ("@{$st->{groups}->{$group}}");
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 
 <h2><FONT FACE="sans-serif">Detail for $group/$service</font></h2>
 
@@ -1439,11 +1560,28 @@
 	</pre>
 </table>
 EOF
+		my $wml = <<EOF;
+<p><b>Description:</b> $descr</p>
+<p><b>Summary:</b> $sum</p>
+<p><b>Hosts:</b> $hosts</p>
+<p><b>Detail: </b>
+EOF
+		my $wml_detail = "no detail -- bug!";
+		if ($WAP) {
+			if ($optarg eq "full") {
+				$wml_detail = $sref->{last_detail};
+			} elsif (length $sref->{last_detail} > 200) {
+	    			$wml .= substr($sref->{last_detail},0,200) . "... [<a href=\"$ENV{SCRIPT_NAME}?detail=$group,$service,full\">more</a>]";
+			} else {
+				$wml .= $sref->{last_detail};
+			}
+		}
+		$wml .= "</p>";
 
 	if ($d->{"ack"}) {
 	    my $comment = pre_pad_if_empty ($d->{"ackcomment"});
 
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
 <table width="100%">
     <tr>
     	<td align=right width="15%"> <h2>Acknowledgment of failure</h2> </td>
@@ -1452,11 +1590,17 @@
 </table>
 
 EOF
+	    $wml .= <<EOF if ($WAP);
+<p><b>Acknowledgment of failure</b>: $comment</p>
+EOF
 	}
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 <table cellpadding=2 cellspacing=1 bgcolor="#CCCCCC" width="100%">
 EOF
+	$wml .= <<EOF if ($WAP);
+<p>
+EOF
 
 	#
 	# 0 = nothing special
@@ -1493,16 +1637,19 @@
 	    next if ($k->[2] == 1 && $$v == 0);
 	    next if ($k->[2] == 2 && $$v eq "");
 
-	    $OUT_BUF .= <<EOF;
+	    $OUT_BUF .= <<EOF if (! $WAP);
     <tr>
     	<td align=right width="15%"><b>$k->[1]:</b></td>
 	<td> $$v </td>
 EOF
+	    $wml .= "<br/><b>$k->[1]:</b> $$v" if ($WAP);
 	}
 
-	$OUT_BUF .= <<EOF;
+	$OUT_BUF .= <<EOF if (! $WAP);
 </table>
-
+EOF
+	$wml .= <<EOF if ($WAP);
+</p>
 EOF
 
 	#
@@ -1512,30 +1659,54 @@
 	{
 	    if (defined ($CF->{"links"}->{$group}->{$service}->{"link-text"}))
 	    {
-	    	$OUT_BUF .= <<EOF;
+	    	$OUT_BUF .= <<EOF if (! $WAP);
 <p>
 <h2><a href=\"$CF->{links}->{$group}->{$service}->{link}\">More Information</a></h2>
 $CF->{links}->{$group}->{$service}->{'link-text'}
 
 EOF
+	    	$wml .= <<EOF if ($WAP);
+<p>
+<b><a href=\"$CF->{links}->{$group}->{$service}->{link}\">More Information</a></b>
+$CF->{links}->{$group}->{$service}->{'link-text'}
+</p>
+EOF
 	    }
 
 	    else
 	    {
-	    	$OUT_BUF .= <<EOF;
+	    	$OUT_BUF .= <<EOF if (! $WAP);
+<p>
+<a href="$CF->{links}->{$group}->{$service}->{link}">$CF->{links}->{$group}->{$service}->{link}</a>
+
+EOF
+	    	$wml .= <<EOF if ($WAP);
 <p>
 <a href="$CF->{links}->{$group}->{$service}->{link}">$CF->{links}->{$group}->{$service}->{link}</a>
+</p>
 
 EOF
 	    }
 	}
 
-    	$OUT_BUF .= <<EOF;
+    	$OUT_BUF .= <<EOF if (! $WAP);
 <p>
 <a href="$ENV{SCRIPT_NAME}">Back to summary table</a>
 <p>
 
 EOF
+	# dump out wml
+	if ($optarg eq "full") {
+		$wml_detail =~ s/[\n\r]+/<br\/>/g;
+		$wml = "<p>$wml_detail</p>";
+	}
+
+    	$OUT_BUF .= <<EOF if ($WAP);
+<card id="display" title="Detail for $group/$service">
+$wml
+<do type="prev" label="Back to summary table"><prev/></do>
+</card>
+EOF
     }
 
     #
@@ -1649,6 +1820,6 @@
 {
     my $l = shift;
 
-    return "<pre> </pre>" if ($l =~ /^\s*$/);
+    return "<pre> </pre>" if ($l =~ /^\s*$/ && !$WAP);
     $l;
 }
