Thursday, April 10, 2008

Perl Script To List Out Linux or Unix Crontabs In HTML

Output from crontab HTML Perl script

Hello again,

Today's script is a simple, but useful, one. A while back, we ran a series of posts on graphing out disk, paging, memory and cpu statistics for Unix, with a big roundup for paging, memory and cpu on Linux.

This time, we're going to go in a bit of a different direction and put together an example script that creates an HTML page, also (hopefully) providing some useful information for you, the sysem admin or user.

As with the other scripts, there are a few words of caution that go with putting any sort of script like this "out there." While this doesn't have the destructive potential of the Linux graphing scripts (or the Unix ones), as they were interfacing directly with system commands, you should still be careful whenever you publish information in this way. Since our script goes to the filesystem to read its information from a system output file, it affords access that may not necessarily be granted to a user logged on to that system.

In any event, all "gloom and doom" aside, we're starting out simple with today's "system information" script by just reporting on crontab contents. The script below does need to be run from the command line (unless you expressly allow execution of .pl files in your web server's configuration and modify it somewhat), which creates an interesting cyclical loop if you run it from cron ;) It's also kind enough to remove all comment lines from the crontabs so they're, hopefully, more readable!

You can execute it simply by running it from the command line with no arguments, like so:

host # ./cron.pl

You'll probably need to modify a few variables (like the "chdir" line, to indicate the correct path to your crontabs directory, the output file's destination and the section where I purposefully ignore the basic "system accounts" (including root) and then print to the HTML page that they're "currently unused"), but it should run right out-of-the-box, otherwise.

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# cron.pl --- Easily scan through a server's cron jobs
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

chomp($cur_dir=`pwd`);
chdir("/var/spool/cron/crontabs");
@crons = `ls`;
$filename = "${cur_dir}/cron.html";
$hostname = `hostname`;

foreach $job (@crons) {
chomp $job;
if ( $job =~ /(^adm$|^lp$|^sys$|^uucp$|^root$)/ ) {
next;
} elsif ( $job =~ /\.pl|html|passwd/ ) {
next;
} else {
push(@parcel, $job);
}
}

open(OUTPUT, ">$filename");
print OUTPUT "<html><head><title>$hostname Cron Services</title></head>\n";
print OUTPUT "<body BGCOLOR=\"#000000\" TEXT=\"#00FF00\" LINK=\"#FFFF00\" VLINK=\"#FF0000\" ALINK=\"#BECD8C\">\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">$hostname -- CRON SERVICES:</FONT>\n";
print OUTPUT "<br><hr><br>\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">root:</FONT><br>Currently unused. - System Supplied.<br>\n";
print OUTPUT "<br><hr><br>\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">adm:</FONT><br>Currently unused. - System Supplied.<br>\n";
print OUTPUT "<br><hr><br>\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">lp:</FONT><br>Currently unused. System Supplied.<br>\n";
print OUTPUT "<br><hr><br>\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">sys:</FONT><br>Currently unused. - System Supplied.<br>\n";
print OUTPUT "<br><hr><br>\n";
print OUTPUT "<FONT COLOR=\"FFFF00\">uucp:</FONT><br>Currently unused - System Supplied.<br>\n";
print OUTPUT "<br><hr><br>\n";

foreach $part (@parcel) {
open(PACKAGE, "<$part");
@package = <PACKAGE>;
close(PACKAGE);
print OUTPUT "<FONT COLOR=\"FFFF00\">$part</FONT>\n";
print OUTPUT "<br>\n";

foreach $pretty (@package) {
if ( $pretty =~ /^#/ ) {
next;
} else {
@picture = split(/ /, $pretty);
$frame = join(" ", @picture);
chomp $frame;
print OUTPUT "$frame<br>\n";
}
}
print OUTPUT "<br><hr><br>\n";
}
close(OUTPUT);


, Mike