#!/usr/bin/perl -w # put this in snmpd.conf: # exec .1.3.6.1.4.1.X.Y cpu /path/to/this/script.pl # # X and Y are numbers of your choosing I suppose. I'm still a little new # to snmp. I keep X the same across all my stuff and Y is unique per # service that I'm providing. # you can then 'snmpget hostname .1.3.6.1.4.1.X.Y.101.1' # and will get back something like: u:32,s:15,i:53 use strict; # line by line vmstat output for 2 iterations. # mine looks like: # procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- # r b swpd free buff cache si so bi bo in cs us sy id wa # 0 0 29708 72468 39012 272552 0 0 15 22 12 15 1 0 97 1 # 0 0 29708 72340 39012 272680 0 0 64 0 1092 205 0 0 100 0 my @lines = split /\n/,`vmstat 2 2`; # $lines[1] is our column headers, trim of the leading whitespace # then split them out to an array $lines[1] =~ s/^\s+//; my @head = split /\s+/, $lines[1]; # The last line is the one with useful data. Trim it and split it too. $lines[$#lines] =~ s/^\s+//; my @data = split /\s+/, $lines[$#lines]; # match the headers and data into a hash my %data; for my $x (0 .. $#data) { $data{$head[$x]} = $data[$x]; } print STDOUT "u:${data{'us'}},s:${data{'sy'}},i:${data{'id'}}\n"; exit 0;