Archive for October, 2009

Numa: Using Perl to stat a file like in Linux

Saturday, October 31st, 2009

Ever run into the problem when you need to clean up a directory? You need to remove files that are only a couple of hours old and the system does not have the stat command. You are forced to use the find command with the mtime flag. Not any more!

If you have every considered taking classes on Perl this provides another reason to use the language. If you are one of the foolish people that think Perl is only useful for CGI scripting and database functions, you are way off base. Here is a simple script that is run from a crontab to clean up files every two hours:

use strict;
use File::Find;
use File::stat;
use File::Copy;

my $wdirectory;
my $test_direc = "/u";
my $mount_tab = "/etc/mnttab";
my $dayofweek = (localtime) [6];
my $flag;
my $core_files = "/tmp/core_file_log";
my $offset="-4";
my $time=time+($offset*3600);
my $monthdate=localtime($time);

#function to test for file system in mtab to insure it exists and is mounted
open(FILE, "<$mount_tab");
while (){
$flag = 1 if /$test_direc/;
}
close(FILE);

#rotate the log file /tmp/core_file_log to keep 3 days worth of logs
if (-w $core_files){
      move("$core_files.2", "/dev/null");
      move("$core_files.1", "$core_files.2");
      move("$core_files","$core_files.1");
      copy("/dev/null", "$core_files");
}
#For testing purposes I have created a log file
open(FH,">>$core_files") or die "Could not open log file: $!n";

#Print the date at the top of each section
printf FH "nn %snn",(scalar (localtime));

#If it is Sunday or Monday we only want store core files
#if ($dayofweek == 0 || $dayofweek == 1) {
if ($dayofweek == 0) {
$wdirectory = "/u/";
}else{
$wdirectory = "/u/";
}

#If statement to see if the directory is writeable and mounted
if (-w $wdirectory && $flag == 1){
chdir "$wdirectory" or die "$wdirectory is not writeable: $!n";
find(&wanted,$wdirectory ); #function to find what we are looking for
}
else {
die "$wdirectory is not writable or cannot be found in mtab: $!n";
}
sub wanted {
     if ($dayofweek == 0 || $dayofweek == 1) {
#If it is removing store core files only allow 1 hour since last touched
$offset="-1";
$time=time+($offset*3600);
$monthdate=localtime($time);
if($_ =~ /^core$/ && -f $_){
     my $sb = (stat($File::Find::name)) ;
     if ((scalar $sb->mtime < $time)){
		printf FH "File name $File::Find::name nDate last modified:%sn
Size:%sn Removedn", (scalar localtime $sb->mtime),($sb->size);
		`rm $File::Find::name`;
		     }
		}
	}
	$monthdate=localtime($time);
	if($_ =~ /^core$/ && -f $_){
	     my $sb = (stat($File::Find::name)) ;
	     if ((scalar $sb->mtime < $time)){
		printf FH "File name $File::Find::name nDate last modified:%sn
Size:%sn Removedn", (scalar localtime $sb->mtime),($sb->size);
		`rm $File::Find::name`;
		     }
		}
	}
	else {
		if($_ =~ m/^core$/ && -f $_){
		   my $sb = (stat($File::Find::name)) [0];
		   if((scalar $sb->mtime < $time)){
			printf FH "File name $File::Find::name nDate last Modif
ied:%snSize:%sn Removedn", (scalar localtime $sb->mtime),($sb->size);
			`rm $File::Find::name`;
			}
		}
	}
}
close(FH);
exit;

It is a very simple script. The key elements are “use File::stat;” finding the time, log rotation, and that all the libraries exist within the basic Perl installation. Perl comes with all versions on Linux and Unix. Windows you will need to install Perl and change the directory structure to make the script work. So you can use this script on any operating system with minimal modifications.

Perl is not just for CGI and database work, but for System Administrators to accomplish task within a heterogeneous environment.

Enjoy,

Mike Kniaziewicz, MIS

Nagios: How to add a custom diagram to Nagvis

Saturday, October 31st, 2009

Nagios is a great monitoring tool. Basically you can take the raw product and have it up and running within the hour. You need to remember the scope of your viewers. Having a dashboard with number is not going to fly with management. Management needs information now and cannot afford the luxury of digging through your nagios dashboard to obtain information (do you really want non-IT people looking through an application that shows the status of every server and service in your environment?).

Nagvis is the tool you need to add meaning to your nagios installation. Organizations have diagrams pertaining to various systems, so you need to add meaning to those systems for management. I have found the perfect solution to creating those diagrams. Simply export the diagram in either “tiff” or “png” format and use it as your background.

Nagvis Diagram

You will gain instant user acceptance because these are the diagrams management has already accepted to represent the system. Remember, when you add a service or host to the diagram do not include a label. Let the diagram server as your labelling.