How to get an average processor / processor using a computer?

I am currently working on a program with PHP and Perl to read computer system data, and we used SNMP to collect data (or rather, forcibly). After extracting the data, we had to store the data in a database, and then use the data to build a line graph.

I am currently using this perl script to fetch a cpu / cpu to 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 separate processors, and many should know that there can be many processors on one computer, so storing this data in a database is not very practical (for example, if one computer has only 2 processors, the next 4, and one in the room - 100.)

, - , , / . , , 1 , , .

.

+3
1

, - .

sqlite3.

:

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

( 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);

, PHP:

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

appserv|20.0
dbserv|12.5

:

SELECT avg(load) FROM cpu;

15.0

:

SELECT host FROM cpu WHERE load > 25;

dbserv

, , a, cpu , computer_id.

, (, SQLite, MySQL, Oracle ..).

+3

Source: https://habr.com/ru/post/1760680/


All Articles