Search This Blog

2013-06-11

Adding Up SNMP values using AWK

In this example I am walking the entire hrProcessorLoad table to find the values for each CPU

snmpwalk -v1 -cpublic (hostname) 1.3.6.1.2.1.25.3.3.1.2
HOST-RESOURCES-MIB::hrProcessorLoad.768 = INTEGER: 69
HOST-RESOURCES-MIB::hrProcessorLoad.769 = INTEGER: 87
HOST-RESOURCES-MIB::hrProcessorLoad.770 = INTEGER: 73
HOST-RESOURCES-MIB::hrProcessorLoad.771 = INTEGER: 91
HOST-RESOURCES-MIB::hrProcessorLoad.772 = INTEGER: 77
HOST-RESOURCES-MIB::hrProcessorLoad.773 = INTEGER: 87
HOST-RESOURCES-MIB::hrProcessorLoad.774 = INTEGER: 72
HOST-RESOURCES-MIB::hrProcessorLoad.775 = INTEGER: 85
HOST-RESOURCES-MIB::hrProcessorLoad.776 = INTEGER: 93
HOST-RESOURCES-MIB::hrProcessorLoad.777 = INTEGER: 92
HOST-RESOURCES-MIB::hrProcessorLoad.778 = INTEGER: 81
HOST-RESOURCES-MIB::hrProcessorLoad.779 = INTEGER: 85
HOST-RESOURCES-MIB::hrProcessorLoad.780 = INTEGER: 75
HOST-RESOURCES-MIB::hrProcessorLoad.781 = INTEGER: 84
HOST-RESOURCES-MIB::hrProcessorLoad.782 = INTEGER: 73
HOST-RESOURCES-MIB::hrProcessorLoad.783 = INTEGER: 79

You can see that we get a bunch of values as there are 16 separate processors each being utilized at a certain capacity. If we want a good overview of total system load we need to get an average of these values.

This is done through either
  1. Awk'ing out the values at the end
  2. Grabbing ONLY the integer values provided by the snmp call

I'm going to use the awk method as it will also provide a pretty easy way of adding up and dividing the numbers in a quick one liner

The below is what gives us the value we want

snmpwalk -v1 -cpublic (hostname) 1.3.6.1.2.1.25.3.3.1.2 | awk '{sum+=$4} END {print sum/16 "\n" sum/16 "\n0\n0\n"}'
78.6875
78.6875
0
0

The result is printed with
Value_1_in
Value_1_out
0
0
(newline)

The reason for this is that mrtg expects 4 values, in and out for each value and a new line at the end.

This can be placed in the target line in any mrtg config and you will get a nice graph of overall processor load.

No comments:

Post a Comment