Cacti (home)ForumsDocumentation
Cacti: offical forums and support
It is currently Fri Apr 19, 2019 1:37 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Mobile average script: smoother.pl
PostPosted: Tue Jan 17, 2006 5:17 pm 
Offline

Joined: Tue Jan 17, 2006 5:11 pm
Posts: 2
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:
#!/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!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 12:51 am 
Offline

Joined: Sun Sep 10, 2006 1:05 am
Posts: 34
Thanks nvir.

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

Thanks
jacauc


Top
 Profile  
 
 Post subject: more explanation
PostPosted: Mon Sep 11, 2006 2:31 am 
Offline

Joined: Tue Jan 17, 2006 5:11 pm
Posts: 2
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Protected by Anti-Spam ACP Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group