diff --git a/chkhosts-sample.d/conf/gen-webstat.sh b/chkhosts-sample.d/conf/gen-webstat.sh index e1d41b9..1d833f2 100755 --- a/chkhosts-sample.d/conf/gen-webstat.sh +++ b/chkhosts-sample.d/conf/gen-webstat.sh @@ -1,9 +1,20 @@ #!/bin/bash # -# Script to generate host status php web page. +# Script to generate host status php web page and +# the comment update form page. # # usage: gen-webstat.sh chkhosts_directory + +# Variables to customize the output page +_CHKHOSTS_TITLE_="Chkhosts System Status" +_CHKHOSTS_FORMTITLE_="Chkhosts System Comment Update" +_CHKHOSTS_HOSTNAME_="chkhost.somwhere.com" +_CHKHOSTS_POLL_INTERVAL_="5 minute" +_CHKHOSTS_CONTACTNAME_="John Doe" +_CHKHOSTS_CONTACTEMAIL_="john.doe@somewhere.com" +_CHKHOSTS_TABLE_COLS_="3" + # Announce ourselves. echo "Gen-webstat.sh MY_VERSION_STRING" @@ -26,7 +37,124 @@ DOWNHOSTSTATUSDIR=$WORKDIR/status-down CHKHOSTLOGDIR=$WORKDIR/log CHKHOSTLOG=$CHKHOSTLOGDIR/chkhosts.log WEBSTATDIR=$WORKDIR/webstat +WEBDESCRIPTIONDIR=$WEBSTATDIR/system-description +WEBCOMMENTDIR=$WEBSTATDIR/system-comment WEBPAGE=$WEBSTATDIR/status.php +FORMPAGE=$WEBSTATDIR/update-comment.php + +# Calculate the number of hosts we're monitoring +NUMSYSTEMS=$(grep -v -e '^#' $HOSTLISTFILE | wc -l) + + +# +# Create the Comment Form page first +#################################### + +echo '' >$FORMPAGE +echo '' >>$FORMPAGE +echo '' >>$FORMPAGE +echo ' ' >>$FORMPAGE +echo ' ' >>$FORMPAGE +echo ' ' \ + >>$FORMPAGE +echo ' ' >>$FORMPAGE +echo " ${_CHKHOSTS_FORMTITLE_}" >>$FORMPAGE +echo '' >>$FORMPAGE +echo ' ' >>$FORMPAGE + +echo '' >>$FORMPAGE + +# insert the php POST function and showstatus functions +cat >>$FORMPAGE << "SUBMIT_FUNCTION_SECTION" + +SUBMIT_FUNCTION_SECTION + +# insert the comment section +cat >>$FORMPAGE << "COMMENT_SECTION_1" + +

+To help coordinate access to the systems, you can change the comment +to a host's status cell by using the following form: +

+
+ + + + +
Host:   + +
Comment:   +
+
+COMMENT_SECTION_2 + +# +# Close out FORMPAGE document +############################# + +echo '' >>$FORMPAGE +echo '' >>$FORMPAGE + +# now customize the Comment Form page... +sed -i "s/_CHKHOSTS_FORMTITLE_/${_CHKHOSTS_FORMTITLE_}/g" $FORMPAGE +sed -i "s/_CHKHOSTS_HOSTNAME_/${_CHKHOSTS_HOSTNAME_}/g" $FORMPAGE +sed -i "s/_CHKHOSTS_POLL_INTERVAL_/${_CHKHOSTS_POLL_INTERVAL_}/g" $FORMPAGE +sed -i "s/_CHKHOSTS_CONTACTNAME_/${_CHKHOSTS_CONTACTNAME_}/g" $FORMPAGE +sed -i "s/_CHKHOSTS_CONTACTEMAIL_/${_CHKHOSTS_CONTACTEMAIL_}/g" $FORMPAGE + + # # Generate the HTML header @@ -40,7 +168,7 @@ echo ' ' >>$WEBPAGE echo ' ' \ >>$WEBPAGE echo ' ' >>$WEBPAGE -echo ' System Status' >>$WEBPAGE +echo " ${_CHKHOSTS_TITLE_}" >>$WEBPAGE echo '' >>$WEBPAGE echo ' ' >>$WEBPAGE @@ -49,74 +177,167 @@ echo ' ' >>$WEBPAGE ######################## echo '' >>$WEBPAGE -echo '' >>$WEBPAGE +# insert the php POST function and showstatus functions +cat >>$WEBPAGE << "PHP_FUNCTIONS_SECTION" +'; + echo "$hostname
"; + echo "$pingname
"; + if (file_exists("system-description/$hostname.txt")) { + $description=rtrim(file_get_contents( + "system-description/$hostname.txt")); + echo $description; + echo "
"; + } + if (file_exists("system-comment/$hostname.txt")) { + $comment=rtrim(file_get_contents( + "system-comment/$hostname.txt")); + echo $comment; + echo "
"; + } + echo strftime("%Y-%m-%d at %H:%M %Z", + filemtime("../status-up/$pingname")); + echo ''; + } else { + echo ''; + echo "$hostname
"; + echo "$pingname
"; + if (file_exists("system-description/$hostname.txt")) { + $description=rtrim(file_get_contents( + "system-description/$hostname.txt")); + echo $description; + echo "
"; + } + if (file_exists("system-comment/$hostname.txt")) { + $comment=rtrim(file_get_contents( + "system-comment/$hostname.txt")); + echo $comment; + echo "
"; + } + if (file_exists("../status-down/$pingname")) { + echo strftime("%Y-%m-%d at %H:%M %Z", + filemtime("../status-down/$pingname")); + } else { + echo "pinging halted"; + } + echo ''; + } + } +?> +PHP_FUNCTIONS_SECTION -# -# Close out body/document -######################### +# insert the header and intro section template +cat >>$WEBPAGE << "HEADER_INTRO_SECTION" -echo '' >>$WEBPAGE -echo '' >>$WEBPAGE +
+

_CHKHOSTS_TITLE_

+

-exit 0 +

+The date and time in the bottom of each cell in the tables below is the +time the host last responded to a network ping.   +The ping script runs on _CHKHOSTS_HOSTNAME_ +at a _CHKHOSTS_POLL_INTERVAL_ interval +and sends text/SMS and e-mail notifications when systems first +go down or come back up.   +Contact _CHKHOSTS_CONTACTNAME_ +if you'd like to be added to the SMS or e-mail +notification lists. +

-# -# Check on hosts in our HOSTLISTFILE... -# +HEADER_INTRO_SECTION + +# insert the status table +echo "

System Status Table ($NUMSYSTEMS systems)

" >>$WEBPAGE +echo '

' >>$WEBPAGE + +HOSTCOUNTER=0 for i in $( grep -v -e '^#' $HOSTLISTFILE ); do # create short host name... - IPADDR="`echo $i | grep -e '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'`" - if [[ "$IPADDR" == "" ]]; then - SHORTNAME="`echo $i | awk -F . '{ print $1}'`" - else - SHORTNAME="$IPADDR" - fi - - # ping the host with one packet... - ping -q -c 1 $i >/dev/null 2>&1 + IPADDR="`echo $i | grep -e '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'`" + if [[ "$IPADDR" == "" ]]; then + SHORTNAME="`echo $i | awk -F . '{ print $1}'`" + else + SHORTNAME="$IPADDR" + fi - # do a single re-ping if no answer to avoid false positive - # on flaky network, really busy host, etc. - if [[ "$?" != "0" ]]; then - echo "Host $i didn't respond to first ping. Re-pinging." - ping -q -c 1 $i >/dev/null 2>&1 + # start a new table row + if [[ "$((HOSTCOUNTER % $_CHKHOSTS_TABLE_COLS_))" == "0" ]]; then + echo '' >>$WEBPAGE fi + echo "" >>$WEBPAGE - # now act on ping results - if [[ "$?" != "0" ]]; then - # - # Ping was not returned - dead host? - # - if [ -e "$DOWNHOSTSTATUSDIR/$i" ]; then - echo "Host $i is still down or unreachable." - rm -f "$UPHOSTSTATUSDIR/$i" - else - echo "Host $i is down or unreachable." - _log "Host $i is down or unreachable." - touch "$DOWNHOSTSTATUSDIR/$i" - rm -f "$UPHOSTSTATUSDIR/$i" - _notify_sms "$SHORTNAME" "$i" "down" - _notify_email "$SHORTNAME" "$i" "down" - fi - else - # - # Ping was returned - host is alive - # - if [ -e "$UPHOSTSTATUSDIR/$i" ]; then - echo "Host $i is still up." - touch "$UPHOSTSTATUSDIR/$i" - else - echo "Host $i just came back up." - _log "Host $i came back up." - touch "$UPHOSTSTATUSDIR/$i" - rm -f "$DOWNHOSTSTATUSDIR/$i" - _notify_sms "$SHORTNAME" "$i" "up" - _notify_email "$SHORTNAME" "$i" "up" - fi - fi + let "HOSTCOUNTER += 1" done +echo '

' >>$WEBPAGE + + +# insert the Log links section +cat >>$WEBPAGE << "LOG_SECTION" +

+Click here to update the +comment for a host.   +The comment is line four in the host's status cell in the +system status table above. +

+

+Status change log file. +

+

+Comment change log file.   +

+

+Windows users: The log file links above render best in Google +Chrome or Firefox; +Internet Explorer reportedly garbles or doesn't display the +log file at all. +

+LOG_SECTION + + +# insert the footer +cat >>$WEBPAGE << "FOOTER_SECTION" +
+ + +FOOTER_SECTION + +# +# Close out document +#################### + +echo '' >>$WEBPAGE +echo '' >>$WEBPAGE + + +# now customize it... +sed -i "s/_CHKHOSTS_TITLE_/${_CHKHOSTS_TITLE_}/g" $WEBPAGE +sed -i "s/_CHKHOSTS_HOSTNAME_/${_CHKHOSTS_HOSTNAME_}/g" $WEBPAGE +sed -i "s/_CHKHOSTS_POLL_INTERVAL_/${_CHKHOSTS_POLL_INTERVAL_}/g" $WEBPAGE +sed -i "s/_CHKHOSTS_CONTACTNAME_/${_CHKHOSTS_CONTACTNAME_}/g" $WEBPAGE +sed -i "s/_CHKHOSTS_CONTACTEMAIL_/${_CHKHOSTS_CONTACTEMAIL_}/g" $WEBPAGE + +# all done! exit 0