NetSaint -> Nagios: Retention data conversion

Ramiro Morales rm-rpms at gmx.net
Wed Sep 25 19:17:17 CEST 2002


Hi all,

I've just finished putting together (attached) a quick script
to convert the status.sav NetSaint retention data file to the
new format used by Nagios.

This n2n-* script is the third of a set I made for inclusion on 
my Nagios RPM package that helps in migrate from NetSaint
(the other two scripts migrate log files [using the rename command
as suggested by the Nagios doc] and object definition files
[using Ethan's convertcfg tool] respectively).

The script works great for me with both file formats used by
different NetSaint version but I have no previous Perl progra-
mming experience and I'm posting it so people can test/use and
contribute changes and enhancements.

Ethan may even want to include [a variation of it] in the Nagios 
tarball, perhaps filling the value of the $nagios_version
variable with a appropiate Nagios version token at ./configure
time so it keeps synchronized with future software/retention
data file format versions.

For people going to test the script: Dont'forget to stop
NetSaint before using the retention data as the NetSaint
process updates it on exit.

Thanks.

-
Ramiro


-------------- next part --------------
#!/usr/bin/perl -wT

# n2n-retention.pl
#
# Convert NetSaint retention data files to the new Nagios format.
#
# Ramiro Morales <rm-rpms at gmx.net>

no strict 'refs';

sub usage {
print <<EOT;
Usage:

$0 [options...] [<NetSaint-status-retention-file>]

Options:
  --pfp  <program-level failure prediction setting default value>
  --ppp  <program-level process performance data setting default value>

  --hfp  <host-level failure prediction setting default value>
  --hpp  <host-level process performance data setting default value>

  --sfp  <service-level failure prediction setting default value>
  --spp  <service-level process performance data setting default value>
  --soos <service-level obsess over service setting default value>
  --sct  <service-level check type setting default value>
EOT
exit 1;
}

my ($ftype, $ldata, $ltype, @fields, $i, $limit);
$ftype = 0;
$pfp = $ppp = $hfp = $hpp = $sfp = $spp = $soos = $sct = 0;

my $nagios_version = "1.0";

# Process command line options
while (@ARGV && $ARGV[0] =~ /^--(.+)/ && (shift, ($_ = $1), 1)) {
	/^(pfp|ppp|hfp|hpp|sfp|spp|soos|sct)$/ && ($$1 = shift @ARGV);
	usage if (!defined $$1 || $$1 !~ /[01]/);
}

@ARGV = ($ARGV[0]) unless @ARGV <= 1;
unshift (@ARGV, '-') unless @ARGV;

while (<>) {
	chomp;
	
	$ldata = $_;

	# Detect the format of the input file

	#Trying to process an already converted file?
	if ($ldata =~ /#\ Nagios.+Retention\ File/) {
		die "This file is already a Nagios status retention file";
	}
	# Status retention data file, pre NetSaint 0.0.7a5 format
	if ($ldata =~ /#\ NetSaint\ Host\/Service/) {
		$ftype = 1;
	}
	# status retention data file, post NetSaint 0.0.7a5 format
	if ($ldata =~ /#\ NetSaint.+Retention\ File/) {
		$ftype = 2;
	}

	die "Unknown file type" if ($ftype == 0);

	if ($. == 1) {
		print "# Nagios $nagios_version Retention File\n";
		next;
	}

	# If the record has retention data for an entity, detect the type

	if ($ldata =~ /^(PROGRAM|HOST|SERVICE): /) {
		$ltype = $1;
		$ldata =~ s/^$ltype: //;
	}


	# Process records that have retention data for an entity,
	# the rest of the records will fall thru
	if (defined ( $ltype ) ) {

		if ($ltype eq "PROGRAM") {
			@fields = split(';', $ldata, 8);
			# Insert new fields as required by the new format
			if (@fields == 5) {
				push(@fields, 1);
			}
			if (@fields < 8) {
				push(@fields, $pfp, $ppp);
			}
		}

		if ($ltype eq "HOST") {
			$limit = ($ftype == 1)?13:15;

			@fields = split(';', $ldata, $limit);

			# Insert new fields as required by the new format
			if ($ftype == 1) {
				$fields[16] = $fields[12];
				
				$fields[12] = 1;
				$fields[15] = time;
			} else {
				@fields[15,16] = @fields[13,14];
			}
			$fields[13] = $hfp;
			$fields[14] = $hpp;
		}

		if ($ltype eq "SERVICE") {
			$limit = ($ftype == 1)?16:18;

			@fields = split(';', $ldata, $limit);
			
			# Insert new fields as required by the new format
			if ($ftype == 1) {
				$fields[21] = $fields[15];
				@fields[5..15] = @fields[4..14];
				$fields[16] = 1;
				$fields[20] = time;
			} else {
				@fields[20,21] = @fields[16,17];
				@fields[5..16] = @fields[4..15];
			}

			$fields[4] = $sct;
			$fields[17] = $sfp;
			$fields[18] = $spp;
			$fields[19] = $soos;

		}

		# Assemble the new record
		if ( defined ( @fields ) ) {
			$ldata = "$ltype: $fields[0]";
			for ($i = 1; $i < @fields; $i++) {
				$ldata .= ";$fields[$i]";
			}
		}
	}
	undef @fields;
	undef $ltype;

	print "$ldata\n";
	
	close ARGV if eof;
}

#eof


More information about the Users mailing list