Cacti (home)ForumsRepositoryDocumentation
Cacti: offical forums and support  

 FAQFAQ   SearchSearch   MemberlistMemberlist    RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in    


Mobile average script: smoother.pl

 
Post new topic   Reply to topic    Cacti Forum Index -> Scripts and Templates
Author Message
nvir



Joined: 17 Jan 2006
Posts: 2

PostPosted: Tue Jan 17, 2006 5:17 pm    Post subject: Mobile average script: smoother.pl Reply with quote

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!!
Back to top
jacauc



Joined: 10 Sep 2006
Posts: 34

PostPosted: Mon Sep 11, 2006 12:51 am    Post subject: Reply with quote

Thanks nvir.

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

Thanks
jacauc
Back to top
nvir



Joined: 17 Jan 2006
Posts: 2

PostPosted: Mon Sep 11, 2006 2:31 am    Post subject: more explanation Reply with quote

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...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cacti Forum Index -> Scripts and Templates All times are GMT - 5 Hours
Page 1 of 1

 



Powered by phpBB © 2001, 2005 phpBB Group