William Shotts SysInfo.html Creator Script

After reading a lot of Wil Shotts free TLCL.pdf here:

TLCL-13.07.pdf

I finally found a working (but incomplete) version of the script that produces an html page of the system status for the parameters he programmed in this BASH script lesson from his book and blog page here:
http://linuxcommand.org/lc3_wss0120.php
It looks like this:

Screenshot from 2014-04-21 16:55:47.png

The slightly amended script -(I changed the output dir to my /home Documents folder, as it didn't seems to appear in /root) - that produced it is here:
----------------------------------------------------------
#!/bin/bash

# sysinfo_page - A script to produce a system information HTML file

##### Constants

TITLE="System Information for $HOSTNAME"
RIGHT_NOW=$(date +"%x %r %Z")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"

##### Functions

system_info()
{
echo "

System release info

"
echo "

Function not yet implemented

"

} # end of system_info

show_uptime()
{
echo "

System uptime

"
echo "

"
    uptime
    echo "

"

} # end of show_uptime

drive_space()
{
echo "

Filesystem space

"
echo "

"
    df
    echo "

"

} # end of drive_space

home_space()
{
# Only the superuser can get this information

if [ "$(id -u)" = "0" ]; then
echo "

Home directory space by user

"
echo "

"
        echo "Bytes Directory"
        du -s /home/* | sort -nr
        echo "

"
fi

} # end of home_space

write_page()
{
cat < <- _EOF_

$TITLE

$TITLE

$TIME_STAMP

$(system_info)
$(show_uptime)
$(drive_space)
$(home_space)

_EOF_

}

usage()
{
echo "usage: sysinfo_page [[[-f file ] [-i]] | [-h]]"
}

##### Main

interactive=
filename=/home/stevee/Documents/sysinfo_page.html

while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done

# Test code to verify command line processing

if [ "$interactive" = "1" ]; then
echo "interactive is on"
else
echo "interactive is off"
fi
echo "output file = $filename"

# Write page (comment out until testing is complete)

write_page > $filename

-----------------------------------------------------------

You can continue to add functionality (and fix Sys Release section with uname -a) if you can follow his lesson, if you are a budding programmer of course...
He didn't end the book with a final finished, tested script to just copy and try, so I lost the plot with it all in the end.

 

------------------------------------------------------------------------------------------------------------

A little later...

I added some more commands to this script for 2 different PCs which was interesting because of different hard drives and ports open.

Add a suitable command, but remember to add each new command section's function at the bottom also. I added drive temp, open ports and users functions to it like this:

--------------------------------------------------------------------------------------

drive_temp_Celsius()
{
echo "<h2>Hard Drive Temps sda, sdb</h2>"
echo "<pre>"
smartctl -x /dev/sda | grep Celsius; smartctl -x /dev/sdb | grep Celsius
echo "</pre>"

} # end of drive_temp_Celsius

open_ports()
{
echo "<h2>Open Ports</h2>"
echo "<pre>"
nmap 127.0.0.1
echo "</pre>"

} # end of open_ports

active_users()
{
echo "<h2>Active Users</h2>"
echo "<pre>"
w
echo "</pre>"

} # end of active_users

--------------------------------------------------------------

$(drive_temp_Celsius)
$(open_ports)
$(active_users)

Screenshot from 2014-04-21 19:40:26.png

Screenshot from 2014-04-21 19:42:03.png

A useful exercise for a beginner script writer I guess.

Lets sort out that finished badly formatted script above - here's a nearly complete version that works in Mint - with both Will and my additions:

*************************************************************************

#!/bin/bash

# sysinfo_page - A script to produce a system information HTML file

##### Constants

HOSTNAME=$(/bin/hostname) #Mint addition
TITLE="System Information for $HOSTNAME"
RIGHT_NOW=$(date +"%x %r %Z")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"

 

##### Functions

system_info()
{
echo "<h2>System release info</h2>"
echo "<pre>

uname -a

</pre>"

} # end of system_info
show_uptime()
{
echo "<h2>System uptime</h2>"
echo "<pre>

uptime

echo "</pre>"

} # end of show_uptime

drive_space()
{
echo "<h2>Filesystem space</h2>"
echo "<pre>"
df
echo "</pre>"

} # end of drive_space

drive_temp_Celsius()
{
echo "<h2>Hard Drive Temps</h2>"
echo "<pre>"
hddtemp /dev/sda ; hddtemp /dev/sdb
echo "</pre>"

} # end of drive_temp_Celsius

open_ports()
{
echo "<h2>Open Ports</h2>"
echo "<pre>"
nmap 127.0.0.1
echo "</pre>"

} # end of open_ports

active_users()
{
echo "<h2>Active Users</h2>"
echo "<pre>"
w
echo "</pre>"

} # end of active_users

home_space()
{
# Only the superuser can get this information so script has to be run as root

if [ "$(id -u)" = "0" ]; then
echo "<h2>Home directory space by user</h2>"
echo "<pre>"
echo "Bytes Directory";
du -csBG /home/* | sort -nr
echo "</pre>"
fi

} # end of home_space

write_page()

{
cat <<- _EOF_
<html>
<head>
<title>$TITLE</title>
</head>
<body>
<h1>$TITLE</h1>
<p>$TIME_STAMP</p>
$(system_info)
$(show_uptime)
$(drive_space)
$(home_space)
$(drive_temp_Celsius)
$(open_ports)
$(active_users)
</body>
</html>
_EOF_

}

usage()
{
echo "usage: sysinfo_page [[[-f file ] [-i]] | [-h]]"
}
##### Main

interactive=
filename=/home/stevee/Documents/sysinfo_page.html

while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# Test code to verify command line processing

if [ "$interactive" = "1" ]; then
echo "interactive is on"
else
echo "interactive is off"
fi
echo "output file = $filename"
# Write page (comment out until testing is complete)

write_page > $filename

--------------------------------------------------

Of course, the main problem with this is that it is a static "snapshot" - so to see ongoing system changes you have to re-run the shell script each time to re-write the html page, then F5 refresh your browser also.

sysinfo.png

Hmm...You could write/add a for loop to re-run the script it every 10 secs say? then add:

?refresh=10

appended to the browser window....

I got round this by running a crontab every hour for root:

00 * * * * sh /var/www/sysinfo/sysinfo.sh

Also, just noticed that $HOSTNAME doesn't work in Mint without being defined first; and hddtemp is quicker than installing smartmontools:

Constants
RIGHT_NOW=$(date +"%x %r %Z")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
HOSTNAME=$(/bin/hostname)
TITLE="System Information for $HOSTNAME"

OK, after research, you would have to have this file as an index.html type/server page in Apache, and point the file creation directory at the same place to re-write the file, say in /var/www/html, because ?refresh=x in a browser does not work for a samba file share, only for a web page. As root, you can use:

watch -n10 sh sysinfo.sh 

to re-write the file every 10 secs...to e.g.:

mkdir /var/www/sysinfo

chown -R www-data:root /var/www/sysinfo

change this to:

 interactive=
filename=/var/www/sysinfo/sysinfo_page.html

Add another alias to the apache default.000:

<VirtualHost>

Alias /sysinfo /var/www/sysinfo/sysinfo_page.html
<Directory /var/www/sysinfo>
AuthType basic
AuthName "stevee"
AuthUserFile /etc/apache2/users.basic
Require valid-user
</Directory>
</VirtualHost>

If I have lost you, it's because I'm adding to this Post 2 years later, so read the WEBDAV Post if the above Alias section is confusing...anyway..still won't refresh...only with F5. I guess if you are just occasionally monitoring the server you could set a root cron job every hour...? You can also look at colourising the headings e.g. "<font color=red>System release info</font></h2>"

sysinfocolor.png

Another little range addition as a /dev[x-x] catchall for any PC as if not present it is ignored with STDERR from going to the page:

sudo hddtemp /dev/sd[a-h]
/dev/sda: ST3160212ACE: 32°C
/dev/sdb: Generic USB SD Reader: S.M.A.R.T. not available
/dev/sdc: Generic USB CF Reader: S.M.A.R.T. not available
/dev/sdd: Generic USB SM Reader: S.M.A.R.T. not available
/dev/sde: Generic USB MS Reader: S.M.A.R.T. not available
/dev/sdf: WDC WD3200AAKS-75VYA0: 41°C
/dev/sdg: ST3500312CS: 36°C

 

Comments are closed.

Post Navigation