tags:

views:

115

answers:

1

Right now I'm working on a program with PHP and Perl to read the computer's system data and we have been using SNMP to collect data (or rather, forced to). After retrieving the data, we were supposed to store the data in a database and then use the data to plot out a line graph.

At the moment, I'm using this perl script to retrieve the CPU/Processor usage of a computer.

  $MIB1 = ".1.3.6.1.2.1.25.3.3.1.2"; #Cpu Processors
  $HOST = shift; #or localhost for testing

  $count = 0;
      #print out all the processors of the computer and their values
      #SNMP is used with perl because of the libraries
      #snmpwalk retrieves information from the computers by the computer name and MIB
      #as there are many values, they are stored in an array to be used
      (@values) = &snmpwalk("$HOST","$MIB1");
      foreach $value (@values)
      {
          $count++;
          if ($value) {
              #fixes the value for presentation
              $goodvalue = &strip_comment($value);
              #prints out the result. $count will count out the processor number
              print "CPU Usage of Processor $count: $goodvalue%\n"; }
              if ($value > 90){
                  #warns user if the processor usage is over 90%
                  print "Warning: CPU Usage over 90%! \n"
              }
          else { warn "No response from host :$HOST:\n"; } #provides error
      }

The code, or rather, SNMP retrieves several individual processors and many should know that there can be many processors on one computer, therefore making storing this data into a database not very practical, (for example if one computer has only 2 processors, the next has 4 and the one across the room has 100.)

So I was wondering if anyone could help better this code or change it so that I could add them all together, and find an average value of the CPU/processor usages and store it into a database. Because, I'm not too sure how to go about it, as the loop scans for only 1 processor at a time, and it might be unknown how many there are.

Thanks in advance.

+3  A: 

Use host and cpu number as a double key, and the a group by to get the average.

Here's an example with sqlite3.

First create the table:

CREATE TABLE cpu (host string, cpu integer, load integer, primary key (host, cpu));

Then insert some testdata (this would be done by your perl script):

INSERT INTO cpu (host, cpu, load) VALUES ("appserv", 1, 25);
INSERT INTO cpu (host, cpu, load) VALUES ("appserv", 2, 15);
INSERT INTO cpu (host, cpu, load) VALUES ("dbserv", 1, 10);
INSERT INTO cpu (host, cpu, load) VALUES ("dbserv", 2, 30);
INSERT INTO cpu (host, cpu, load) VALUES ("dbserv", 3, 5);
INSERT INTO cpu (host, cpu, load) VALUES ("dbserv", 4, 5);

Now you can get the average load for each host, which could be retrieved and plotted using PHP:

SELECT host, avg(load) FROM cpu GROUP BY host;

appserv|20.0
dbserv|12.5

The total average across all hosts:

SELECT avg(load) FROM cpu;

15.0

Or find any hosts with at high load:

SELECT host FROM cpu WHERE load > 25;

dbserv

You would probably want to make a to make a table of all your computers and link it to the cpu table above, exchanging the host string with a computer_id.

All this assumes you're using a relational database (i.e. SQLite, MySQL, Oracle, etc.).

slu