hello all. I was hoping someone may have an idea or two.
I'm throwing together a shell script that does a lot of application and system level data collection. The idea is is you run it before/after restarting an application for later analysis. A lot of stuff is collected... resource stats, logs, db status, mirroring, thread dumps, pstacks, and so forth. Currently the output is being rolled up into a tar ball.
The question is, can anyone point me in the right direction on how to format the output in HTML for ease of review?
many thanks.
Outputting HTML is easy. HTML is text.
#!/bin/sh
echo "<html><body>This is HTML</body></html>"
PHP is a language specifically designed to present results in HTML. It has the power of a full scripting language, and you can intermix raw HTML and PHP with ease.
<html>
<body>
<?php $dspec=array( 1 => array("pipe", "w") );
$proc = proc_open("ps ux", $dspec, $pipes );
($proc) || die("Couldn't run 'ps ux'"); ?>
<table width='100%'>
<tr>
<?php $line=fgets($pipes[1]);
$titles=preg_split("/[\s]+/",$line);
for($n=0; $n<sizeof($titles); $n++)
{ ?>
<th><?=$titles[$n]?></th>
<?php } ?>
</tr>
<?php while($line=fgets($pipes[1]))
{
$titles=preg_split("/[\s]+/",$line);
?>
<tr>
<?php for($n=0; $n<sizeof($titles); $n++)
{ ?>
<td><?=$titles[$n]?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php proc_close($proc); ?>
This will produce an HTML table like so:
<html>
<body>
<table width='100%'>
<tr>
<th>USER</th>
<th>PID</th>
<th>%CPU</th>
<th>%MEM</th>
<th>VSZ</th>
<th>RSS</th>
<th>TTY</th>
<th>STAT</th>
<th>START</th>
<th>TIME</th>
<th>COMMAND</th>
<th></th>
</tr>
<tr>
<td>apache</td>
<td>13019</td>
<td>0.0</td>
<td>3.6</td>
<td>16120</td>
<td>8096</td>
<td>?</td>
<td>S</td>
<td>Jul30</td>
<td>0:00</td>
<td>/usr/sbin/apache2</td>
<td>-D</td>
<td>USERDIR</td>
<td>-D</td>
<td>DEFAULT_VHOST</td>
<td>-D</td>
<td>SSL</td>
<td>-D</td>
<td>PHP5</td>
<td>-d</td>
<td>/usr/lib/apache2</td>
<td>-f</td>
<td>/etc/apache2/httpd.conf</td>
<td>-k</td>
<td>start</td>
<td></td>
</tr>
<snip for brevity>
</table>
</body>
</html>
...obviously not perfect, since PS doesn't use tabs for seperators(for reasons I can't imagine), but hopefully shows the idea.
PHP can be run on a web server -- the UNIX forums run on it -- in which case the code is executed and the results presented to you. You can also run it from the command prompt directly via 'php file.php'.
I appreciate the idea. The thing is, this tool is being written for machines that are deployed w/ out php. What I'm looking to do is have the output in a single file, FAQ-like form with a table of contents at the top. I have multpile functions defined in a ksh script and each will need it's own table of contents entry which takes you to the desired output. Right now each function is redirected to an output file and all output files are rolled into the tarball at the end. Here's an abbreviated version to get the idea across...
#!/bin/ksh
# global var
DATE=`date '+%m%d%y_%H%M'`
# define logging variables
logs_define () {
export TANKDIR=/var/tmp/dataCollect_tmp_.$DATE
export VERLOG=$TANKDIR/rpm_version.log
export METALOG=$TANKDIR/metadevices.log
export PROCLOG=$TANKDIR/allprocs.log
}
# create the directory where output will be stored
holding_tank_mk () {
mkdir $TANKDIR
echo "All output files are being written into "$TANKDIR"/"
echo "This directory will be removed and it's contents placed in a tarball at the end of the run."
}
# grab the rpm version info
get_rpmversion () {
echo ""
echo "Gathering rpm version information..."
rpm -qa|sort >> $VERLOG
}
# get metastat output
get_metastat () {
echo "Gathering disk mirroring information..."
echo "##### metastat output #####" >> $METALOG
metastat >> $METALOG
echo "" >> $METALOG
echo "##### metadb output #####" >> $METALOG
echo "" >> $METALOG
metadb -i >> $METALOG >> $METALOG
}
# snapshot of all system processes running
get_allprocs () {
echo "Gathering 'ps -ef' output..."
/usr/ucb/ps -auxwww >> $PROCLOG
}
# tar and zip contents of output diirectoy
bundle () {
export TARBALL=/var/tmp/dataCollect_.$DATE.tar
tar cvf $TARBALL $TANKDIR
gzip $TARBALL
echo ""
echo "Information has been collected and placed in the following file..."
echo "\t"$TARBALL.gz
echo ""
echo ""
}
# delete the output directory
holding_tank_rm () {
exec rm -rf $TANKDIR
}
# execute
logs_define
holding_tank_mk
get_rpmversion
get_metastat
get_allprocs
bundle
holding_tank_rm
No PHP? Hardly the end of the world, since anything that prints text can make HTML. You might do something like this:
#!/bin/sh
DATE=`date '+%m%d%y_%H%M'`
# Print beginning of webpage
function html_header
{
cat <<END
<html>
<head><title>${1}</title></head>
<body>
<h3>${1}</h3>
<!-- Table of Contents links -->
<p>
<a href='#rpmversion'>RPM Versions</a>
<a href='#processes'>Processes</a>
</p>
END
}
function html_footer
{
cat <<END
</body>
</html>
END
}
function html_title
{
echo "<h3><a name='#${2}'>$1</a></h3>"
}
function rpm_versions
{
html_title "RPM Versions" "rpmversion"
echo "<pre>"
rpm -qa|sort
echo "</pre>"
}
function get_allprocs
{
html_title "All Running Processes" "processes"
/usr/ucb/ps -auxwww
}
html_header "Report Summary for ${DATE}"
rpm_versions
get_allprocs
html_footer
produces output like:
<html>
<head><title>Report Summary for 080306_0836</title></head>
<body>
<h3>Report Summary for 080306_0836</h3>
<!-- Table of Contents links -->
<p>
<a href='#rpmversion'>RPM Versions</a>
<a href='#processes'>Processes</a>
</p>
<h3><a name='#rpmversion'>RPM Versions</a></h3>
<pre>
(stuff)
</pre>
<h3><a name='#processes'>All Running Processes</a></h3>
<pre>
(stuff)
</pre>
</body>
</html>