Show server processes using apache and gawk - showprocesses (0/1) 
Author Message
 Show server processes using apache and gawk - showprocesses (0/1)

Here's a gawk based CGI script that creates an HTML page from the top command for use as an Apache CGI script.

Sample uses:

        http://servername/cgi-bin/showprocesses

for a one time display or

        http://servername/cgi-bin/showprocesses?refresh=120

for a page that automatically refreshes itself every two minutes.

To use just copy it into the cgi-bin directory, make it executable, and make sure that bash and gawk are in the path.

I wrote this script as part of a collection of pages I can use to remotely check the status of my server. Of course
these pages are SSL and password protected to limit viewing (hopefully) to just those with the need. I opened the
O'Reilly sed & awk book and rolled up my sleeves for a day, and this is the result. I have tested it on two of my
machines and it seems to work fine. One is an AlphaPC running Red Hat Linux 6.1 and the other is a Celeron based PC
running SuSE Linux 6.4.

I have inserted it directly into the message text and attached it as a uuencoded attachment to alleviate the problems
for mail clients that wrap long lines.

Any comments or suggestions from all you awk gurus out there will be accepted with appreciation. This is my first
effort, and I don't claim to have used the features of the language in anything remotely approximating an optimal way.
However, it does seem to work quite well, and that was my primary objective.

.... Bob Kimble

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#! /bin/bash
## showprocesses
## Version 1.0

## Copyright 18 July 2000
## Freely released into the public domain.
## Not guaranteed to do anything useful or even not to harm. YMMV.
## But the code is all laid out before you, so if you spot anything bad, fix it!
#
# Written by the trial and error method. Fixes to be written as problems are observed.
# Assumes top and gawk are in the path.
#
top n 1 b | gawk '
function html_escape (strArg) {
        # This function is necessary for some of the command entries.
        # It takes the input argument and replaces any characters that
        # need to be escaped for the HTML to be correct.

        strRet = ""   # Initialize output variable.

        # The next lines assign the correct escape values to the replacement array,
        # using the character to be escaped as the index.

        arrRep["<"] = "&lt;"
        arrRep[">"] = "&gt;"

        # Add more elements to the array as necessary.
        # Now split the input string into an array of characters, then build the
        # output string one character at a time, replacing problem characters with
        # their equivalent HTML escape sequence.

        n = split( strArg, arrArg, "" )
        for ( i = 1; i <= n; i++ ) {
                if ( arrArg[ i ] in arrRep ) strRet = strRet arrRep[ arrArg[i] ]
                else strRet = strRet arrArg[i]
        }
        return strRet

Quote:
}

BEGIN {
        # Print the HTTP Header for Apache and the initial HTML code.

        OFS=""
        print "Content-Type: text/html"

        # This next line allows for the requester to ask for a page that
        # refreshes at periodic intervals.

        if (ENVIRON["QUERY_STRING"] ~ "^refresh=[0-9]+$") {
                n = split(ENVIRON["QUERY_STRING"],arrRef,"=")
                print "Refresh: " arrRef[2]
                }
        print ""
        print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"
        print "<html><head>"
        print "<title>" ENVIRON["SERVER_NAME"] " Processes</title>"
        print "</head>"
        print "<body bgcolor=\"#c0c0c0\">"
        print "<h1>Processes for " ENVIRON["SERVER_NAME"] "</h1>"
        print "<p>"

Quote:
}

# Lines 1 and 2 are blank.
# Lines 3 through 7 are the summary information provided by top.
NR > 2 && NR < 8 {
        print
        print "<br>"
Quote:
}

# Line 9 is the process table header row.
NR == 9 {
        print "</p>"
        print "<hr><br>"
        print "<table bgcolor=\"#c0c0c0\" border=0 cellpadding=3>"
        print "<tr bgcolor=000000 align=\"center\">"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$1,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$2,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$3,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$4,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$5,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$6,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$7,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$8,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$9,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$10,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$11,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$12,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$13,"</b></font></td>"
        print "</tr>"
Quote:
}

# The remaining lines are the individual process entries.
NR > 9 {
        print "<tr bgcolor=\"#ffffff\">"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$1,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$2,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$3,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$4,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$5,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$6,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$7,"</font></td>"
Quote:
}

# The next two sections handle two separate cases.
# This one is for when the STAT value is one contiguous string.
NR > 9 && $9 ~ "[0-9]+" {
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$8,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$9,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$10,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$11,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$12,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$13
        if(NF>13){print "\t" html_escape($14)}
        print "\t</font></td>"
        print "</tr>"
Quote:
}

# This case handles the situation when the STAT value is two separate characters.
NR > 9 && $9 !~ "[0-9]+" {
        print "<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$8," ",$9,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$10,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$11,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$12,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$13,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$14
        if(NF>14){print "\t" html_escape($15)}
        print "\t</font></td>"
        print "</tr>"
Quote:
}

END {
        print "</table>"
        print "</body>"
        print "</html>"
Quote:
}

'
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


Sat, 04 Jan 2003 03:00:00 GMT  
 Show server processes using apache and gawk - showprocesses (0/1)
Here's a gawk based CGI script that creates an HTML page from the top command for use as an Apache CGI script.

Sample uses:

        http://servername/cgi-bin/showprocesses

for a one time display or

        http://servername/cgi-bin/showprocesses?refresh=120

for a page that automatically refreshes itself every two minutes.

To use just copy it into the cgi-bin directory, make it executable, and make sure that bash and gawk are in the path.

I wrote this script as part of a collection of pages I can use to remotely check the status of my server. Of course
these pages are SSL and password protected to limit viewing (hopefully) to just those with the need. I opened the
O'Reilly sed & awk book and rolled up my sleeves for a day, and this is the result. I have tested it on two of my
machines and it seems to work fine. One is an AlphaPC running Red Hat Linux 6.1 and the other is a Celeron based PC
running SuSE Linux 6.4.

Any comments or suggestions from all you awk gurus out there will be accepted with appreciation. This is my first
effort, and I don't claim to have used the features of the language in anything remotely approximating an optimal way.
However, it does seem to work quite well, and that was my primary objective.

Hopefully the longer lines won't be scrambled by your news reader.

.... Bob Kimble

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#! /bin/bash
## showprocesses
## Version 1.0

## Copyright 18 July 2000
## Freely released into the public domain.
## Not guaranteed to do anything useful or even not to harm. YMMV.
## But the code is all laid out before you, so if you spot anything bad, fix it!
#
# Written by the trial and error method. Fixes to be written as problems are observed.
# Assumes top and gawk are in the path.
#
top n 1 b | gawk '
function html_escape (strArg) {
        # This function is necessary for some of the command entries.
        # It takes the input argument and replaces any characters that
        # need to be escaped for the HTML to be correct.

        strRet = ""   # Initialize output variable.

        # The next lines assign the correct escape values to the replacement array,
        # using the character to be escaped as the index.

        arrRep["<"] = "&lt;"
        arrRep[">"] = "&gt;"

        # Add more elements to the array as necessary.
        # Now split the input string into an array of characters, then build the
        # output string one character at a time, replacing problem characters with
        # their equivalent HTML escape sequence.

        n = split( strArg, arrArg, "" )
        for ( i = 1; i <= n; i++ ) {
                if ( arrArg[ i ] in arrRep ) strRet = strRet arrRep[ arrArg[i] ]
                else strRet = strRet arrArg[i]
        }
        return strRet

Quote:
}

BEGIN {
        # Print the HTTP Header for Apache and the initial HTML code.

        OFS=""
        print "Content-Type: text/html"

        # This next line allows for the requester to ask for a page that
        # refreshes at periodic intervals.

        if (ENVIRON["QUERY_STRING"] ~ "^refresh=[0-9]+$") {
                n = split(ENVIRON["QUERY_STRING"],arrRef,"=")
                print "Refresh: " arrRef[2]
                }
        print ""
        print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"
        print "<html><head>"
        print "<title>" ENVIRON["SERVER_NAME"] " Processes</title>"
        print "</head>"
        print "<body bgcolor=\"#c0c0c0\">"
        print "<h1>Processes for " ENVIRON["SERVER_NAME"] "</h1>"
        print "<p>"

Quote:
}

# Lines 1 and 2 are blank.
# Lines 3 through 7 are the summary information provided by top.
NR > 2 && NR < 8 {
        print
        print "<br>"
Quote:
}

# Line 9 is the process table header row.
NR == 9 {
        print "</p>"
        print "<hr><br>"
        print "<table bgcolor=\"#c0c0c0\" border=0 cellpadding=3>"
        print "<tr bgcolor=000000 align=\"center\">"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$1,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$2,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$3,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$4,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$5,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$6,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$7,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$8,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$9,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$10,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$11,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$12,"</b></font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" color=\"#ffffff\"><b>",$13,"</b></font></td>"
        print "</tr>"
Quote:
}

# The remaining lines are the individual process entries.
NR > 9 {
        print "<tr bgcolor=\"#ffffff\">"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$1,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$2,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$3,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$4,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$5,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$6,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$7,"</font></td>"
Quote:
}

# The next two sections handle two separate cases.
# This one is for when the STAT value is one contiguous string.
NR > 9 && $9 ~ "[0-9]+" {
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$8,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$9,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$10,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$11,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$12,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$13
        if(NF>13){print "\t" html_escape($14)}
        print "\t</font></td>"
        print "</tr>"
Quote:
}

# This case handles the situation when the STAT value is two separate characters.
NR > 9 && $9 !~ "[0-9]+" {
        print "<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$8," ",$9,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$10,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$11,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$12,"</font></td>"
        print "\t<td align=\"right\"><font face=\"Ariel,Helvetica\" size=\"-1\">",$13,"</font></td>"
        print "\t<td><font face=\"Ariel,Helvetica\" size=\"-1\">",$14
        if(NF>14){print "\t" html_escape($15)}
        print "\t</font></td>"
        print "</tr>"
Quote:
}

END {
        print "</table>"
        print "</body>"
        print "</html>"
Quote:
}

'
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


Sat, 04 Jan 2003 03:00:00 GMT  
 Show server processes using apache and gawk - showprocesses (0/1)
Sorry about the multiple posts. I misunderstood the error messages I got back from the news server to mean that my
posting had been rejected.


Sat, 04 Jan 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Show server processes using apache and gawk

2. Using Rexx for CGI in Apache Server

3. awk/gawk stop after processing 40mill lines

4. Apache + PHP not processing php

5. Run Python as separate process with Apache?

6. Inter process communication using Tk/send hangs on suspended processes

7. forking cgi processes in apache

8. Q: Multiple processes writing to a single process using Expect

9. Apache Web Server and ISAPI Broker

10. Apache Web Server

11. Eiffel on Apache Servers?

12. PyApache 4.14 (Python module for Apache web server)

 

 
Powered by phpBB® Forum Software