Mobile average script: smoother.pl

Templates, scripts for templates, scripts and requests for templates.

Moderators: Moderators, Developers

Post Reply
Author
Message
nvir
Posts: 2
Joined: Tue Jan 17, 2006 5:11 pm

Mobile average script: smoother.pl

#1 Post by nvir » Tue Jan 17, 2006 5:17 pm

I never found a built in way of building an averaged version of a value in cacti. If there exists and elegant way of doing this, please tell me!

The solution I found was to use a smoother perl script. Basically, I modify my Data Input Methods to go through the smoother script. Instead of doing cat /tmp/file.log | wc -l as a script command, I just do: perl smoother.pl 5 /tmp/sm_file.db "cat /tmp/file.log | wc -l"

The smoother script just calls the preceding script, averages it for 5 instances and stores in a database the 5 preceding values in /tmp/sm_file.db

Here is the perl code itself:

Code: Select all

#!/usr/bin/perl

# this wrapper computes a mobile average of the value it finds

use strict;

my $length = shift;
my $dbFile = shift;
my $program = shift;
my @array;
my $value;


getValue();
readFile();
addLastValue($value);
removeOldestValue();
#printArray();
writeFile();
my $average = averageArray();
print "$average\n";


sub averageArray {
	my $totalSum=0;
	my $count=0;
	foreach my $item (@array) {
		$totalSum=$totalSum+$item;
		$count++;
	}
	my $ret = $totalSum / $count;
}


sub getValue {
	$value=`$program`;
	chomp $value;
}



sub printArray {
	foreach my $item (@array) {
		print "$item\n";
	}
}



sub removeOldestValue {
	while (@array > $length) {
		shift @array;
	}
}



sub addLastValue {
	my $lastValue = shift;
	push(@array,$lastValue);
}



sub writeFile {
	open FILE, "> $dbFile";
	foreach my $item (@array) {
		print FILE "$item\n";
	}
	close FILE;
}


sub readFile {
	open FILE, $dbFile;
	while (<FILE>) {
		if(/^(\d*)$/) {
			push(@array,$1);
		}
		
	}
	close FILE;
}
Any comments on this are more than welcome!!

jacauc
Posts: 34
Joined: Sun Sep 10, 2006 1:05 am

#2 Post by jacauc » Mon Sep 11, 2006 12:51 am

Thanks nvir.

Could you give me some details on how to implement this?

Thanks
jacauc

nvir
Posts: 2
Joined: Tue Jan 17, 2006 5:11 pm

more explanation

#3 Post by nvir » Mon Sep 11, 2006 2:31 am

As I said in the post, considering the script command you use to gather information is cat /tmp/file.log | wc -l, you can use the script command in cacti: perl smoother.pl 5 /tmp/sm_file.db "cat /tmp/file.log | wc -l"

where smoother.pl contains the perl script given...

please note that this works for data gathered as a command line script, and not for SNMP data... (although you can gather SNMP data through the command line)...

good luck...

Post Reply