A State Examination

You will often want to make a front end that inspects the current state of the machine and generates different HTML output based upon the state. The following (wui/cgi-bin/server/http_logs.cgi) script provides an example of this. It defines a show_log() function which checks for the existance of log files prior to displaying the contents. The show_log() function is then invoked several times for the various log files associated with the httpd daemon.

@bashCgiBegin("$Id: http_logs.cgi,v 1.18 2008/02/18 02:37:40 rwhalb Exp $","2003-10-22","Clear Web Server Logs")

#
# http_logs.cgi

#
# local defines...
@define("exitAnchor","System")

#
# parse any CGI arguments...
set_query_options;

#
# exit text...
qrft="${QUERY_return_from:-"NST WUI Index"}";
qrlt="${QUERY_return_label:-"Exit"}"
exit_hover_text="@jsn("${qrlt}") to page: @jsqh("${qrft}")...";
exit_hover_text_len="$(((${#qrft} * @dtm()) + 130))";

#
# define vars...
LOG_DIR="/var/log/httpd";

QUERY_action="${QUERY_action:-""}";

if [ "${QUERY_action}" != "clear" ]; then

@bashCgiOutBegin()

@wuiHtmlHeader("header","Clear Apache Web Server Log Files")

@p("Use this page to clear \"@stress("All")\" log files associated with the
@apacheWebSiteLink() Web Server. The log files are located within the directory:
\"@bold("@wuiBrowseLink("${LOG_DIR}",,"../server/http_logs.cgi","Clear Apache Web Server Log Files")")\".")

@cautionMessage("
The @apacheWebSiteLink() Web Server will be @stress("restarted") after the
log files have been cleared.
","Caution: Apache Web Server Restart Required")

<center>
  <table@htmlAttr("border","0")>
    <tr>
      <td>
        <form@htmlAttr("action","http_logs.cgi")>
          @wuiInputHidden("action","clear")
          @wuiInputHidden("return","http_logs.cgi")
          @wuiInputHidden("return_from","Clear Apache Web Server Log Files")
          @wuiInputHidden("return_label","Return")
          @wuiInputButton("submit","Clear All Log Files","@toolTipAttr("@jsn("Clear") @jsqh("All") associated @jsdh("Apache Web Server") log files...","360")")
        </form>
      </td>
    </tr>
  </table>
</center>

@verticalGap()

@bashCgiOutEnd()

fi

#
# clear_log FILE 
#
#    Resets the contents of the log files
clear_log() {
  local LFILE="${LOG_DIR}/${1}";

  if [ -f "${LFILE}" ]; then
    /usr/bin/sudo /bin/rm -f "${LFILE}";
  fi

  /usr/bin/sudo /bin/touch "${LFILE}";
  /usr/bin/sudo /bin/chmod 644 "${LFILE}";
}

#
# List of log files maintained by apache
LOG_FILES=(ssl_error_log ssl_access_log ssl_request_log error_log access_log);

#
# See if user wants us to clear the log files access_log
if [ "${QUERY_action:-"show"}" = "clear" ]; then

  cat <<EOF
@wuiHtmlHeader("clearing","Clearing All Web Server Log Files")
EOF

  for l in ${LOG_FILES[*]}; do
    clear_log "${l}";
  done

@bashCgiOutBegin()

@p("The content of the following log files have been @stress("cleared"):
$(for i in ${LOG_FILES[@]}; do printf "\"@bold("${i}")\" "; done)
which are located within the directory:
\"@bold("@wuiBrowseLink("${LOG_DIR}",,"../server/http_logs.cgi","Clear Apache Web Server Log Files")")\".
The \"@apacheWebSiteLink()\" Web Server
will now be \"@bold("reloaded")\" (a <u>slight</u> pause may
occur).")

@bashCgiOutEnd()

#
# set exit label and return if not already set...
QUERY_return="${QUERY_return:-"@topDir()/cgi-bin/server/http_logs.cgi"}";
QUERY_return_from="@jsqh("${QUERY_return_from:-"Clear Apache Web Server Log Files"}")";

@bashCgiEnd()

  /usr/bin/sudo /etc/rc.d/init.d/httpd reload &> /dev/null;

  exit 0;
fi

#
# Show index to log files

@bashCgiOutBegin()

@p("Use these convenient buttons below to view each individual
@apacheWebSiteLink() Web Server log file with the
\"@nst() @bold("File Viewer")\" prior to clearing.")

<center>
  <table@htmlAttr("border","1")
        @htmlAttr("width","90%")>
    <colgroup>
      <col@htmlAttr("width","20%")>
      <col@htmlAttr("width","16%")>
      <col@htmlAttr("width","16%")>
      <col@htmlAttr("width","16%")>
      <col@htmlAttr("width","16%")>
      <col@htmlAttr("width","16%")>
    </colgroup>
    <tr>
      <th@htmlAttr("style","text-align: left;")>View&nbsp;Web&nbsp;Logs</th>

@bashCgiOutEnd()

for l in ${LOG_FILES[*]}; do

@bashCgiOutBegin()

      <td>
        @tailButton("/var/log/httpd/${l}","100","${l}","../server/http_logs.cgi",,,,,,,"@htmlAttr("style","width: 100%")")
      </td>

@bashCgiOutEnd()

done

@bashCgiOutBegin()

    </tr>
  </table>
</center>

@bashCgiOutEnd()

#
# set exit label/return...
QUERY_return_from="@jsqh("${qrft}")";
QUERY_return_label="${qrlt}";

@bashCgiEnd("${exit_hover_text_len}")