You’re running Cacti and love it. But sadly you need to count the number of X connected to Y (In my case this happened to be users connected to an access concentrator)
SNMP and indeed Cacti doesn’t actualy have any built in method to do this already for you.
So I went and made one.
Step 1 -=- We need to make sure we have the perl librarys we need (You probably already do)
[root@localhost root]# perl -MCPAN -e 'install Net::SNMP'
*Step 2 -=-* Download or copy this script into a file under {cacti-dir}/scripts/
#!/usr/bin/perl use Net::SNMP;use Getopt::Long;use Data::Dumper;use strict;my ($help, $hostname, $snmp_community, $snmp_port, $snmp_timeout,$snmp_version, $snmp_oid, $preg_match) =(0,'127.0.0.1','public',161,10,1,'','');Getopt::Long::Configure ('bundling');GetOptions('hostname|h=s' => \$hostname,'port|p=i' => \$snmp_port,'version|v=i' => \$snmp_version,'timeout|t=i' => \$snmp_timeout,'community|c=s' => \$snmp_community,'oid|o=s' => \$snmp_oid,'pregmatch|m=s' => \$preg_match,'help' => \$help);&Help if ($help || !($snmp_version eq '1') || ($snmp_oid eq '') || ($preg_match eq ''));my ($session, $error) = Net::SNMP->session(-hostname => $hostname,-port => $snmp_port,-timeout => $snmp_timeout,-version => $snmp_version,-community => $snmp_community);if (!defined($session)) {printf("ERROR: %s.\n", $error);exit 1;}my $result = $session->get_table(-baseoid => $snmp_oid);if (!defined($result)) {printf("ERROR: %s.\n", $session->error);$session->close;exit 1;}$session->close;my $count = 0;foreach my $key (keys(%$result)) {if ($result->{$key} =~ /$preg_match/) { $count ++;}}print $count;sub Help {print <<EOF;count-match-oid.pl - Version 0.05- Written by Shannon Wynter (http://fremnet.net/contact)----------------------------------------------------------------------------Ever needed to count the number of snmp strings that match an expression?Now you can----------------------------------------------------------------------------Options (Those with * are required) -h--hostname : The host to query (default: 127.0.0.1) -p--port : The port to query on (default: 161) -v--version : The SNMP version to use - Only supports 1 atm -t--timeout : SNMP query timeout seconds(default: 10) -c--community : SNMP query (default: public) -o--oid *: Base OID for searching in (Numeric only) -m--pregmatch*: Perl regular expression to search for--help : This message---------------------------------------------------------------------------Example count-match-oid.pl -h 172.16.0.2 -o ".1.3.6.1.2.1.2.2.1.2" -m "^<pppoe-" This will query the interace descriptions on host 172.16.0.2 and return the number that start with <pppoe-EOFexit 0;}Step 3 -=- Install the script into cacti
<cacti> <hash_030010de382638dacacdf9e7c60a3893dff150> <name>SNMP - Count Match OID</name> <type_id>1</type_id> <input_string>perl &lt;path_cacti&gt;/scripts/count-match-oid.pl -h &lt;ip&gt; -o "&lt;oid&gt;" -m "&lt;match&gt;"</input_string> <fields> <hash_070010ca07f2b6d7e6c094cc3e5f427d6d1dc5> <name>IP Address / Hostname</name> <update_rra></update_rra> <regexp_match></regexp_match> <allow_nulls></allow_nulls> <type_code>hostname</type_code> <input_output>in</input_output> <data_name>ip</data_name> </hash_070010ca07f2b6d7e6c094cc3e5f427d6d1dc5> <hash_070010584f637a9cf87562570eda9759b573a6> <name>Base OID (Numeric)</name> <update_rra></update_rra> <regexp_match></regexp_match> <allow_nulls></allow_nulls> <type_code></type_code> <input_output>in</input_output> <data_name>oid</data_name> </hash_070010584f637a9cf87562570eda9759b573a6> <hash_070010480838b7f2b1efda97713f94fb085822> <name>Perl Regecp For Matching</name> <update_rra></update_rra> <regexp_match></regexp_match> <allow_nulls></allow_nulls> <type_code></type_code> <input_output>in</input_output> <data_name>match</data_name> </hash_070010480838b7f2b1efda97713f94fb085822> <hash_0700105707de2afccf71666e1582bf1c216d83> <name>Number of matches</name> <update_rra>on</update_rra> <regexp_match></regexp_match> <allow_nulls></allow_nulls> <type_code></type_code> <input_output>out</input_output> <data_name>out_count</data_name> </hash_0700105707de2afccf71666e1582bf1c216d83> </fields></hash_030010de382638dacacdf9e7c60a3893dff150></cacti>Log into Cacti, go to “Import Templates”
Browse to where you stored your download and select it
Click “Save”
This will create the Data Input Method – SNMP – Count Match OID
Step 4 -=- Create a Data Template
Go to “Data Templates” and click “Add”
Fill in the Data Template Name field…
example: Online - PPPoE Users
Fill in the Data Source Name field…
example: |host_description| - Online - PPPoE Users
Select SNMP – Count Match OID from the Data Source Input Method field.
Fill in the Internal Data Source Name field…
example: pppoe_count
Click Create
Cacti will save your input so far and reload the page with more fields for you…
Fill in the Base OID (Numeric) field…
example: .1.3.6.1.2.1.2.2.1.2
Fill in the Perl Regexp For Matching field…
example: ^<pppoe-
Click Save
Step 4 -=- Create a Graph Template
Well, I’ve taken you this far… The remainder is not so hard to work out…
I might come back later and complete these instructions if people complain (c:
Commenting is closed for this article.