IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Support me

count-match-oid.pl · Mar 12, 22:26 by Shannon Wynter

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/

  1. #!/usr/bin/perl  
  2.  
  3. use Net::SNMP;
  4. use Getopt::Long;
  5. use Data::Dumper;
  6.  
  7. use strict;
  8.  
  9. my ($help, $hostname, $snmp_community, $snmp_port, $snmp_timeout,
  10. $snmp_version, $snmp_oid, $preg_match) =
  11. (0,'127.0.0.1','public',161,10,1,'','');
  12.  
  13. Getopt::Long::Configure ('bundling');
  14. GetOptions(
  15. 'hostname|h=s' => \$hostname,
  16. 'port|p=i' => \$snmp_port,
  17. 'version|v=i' => \$snmp_version,
  18. 'timeout|t=i' => \$snmp_timeout,
  19. 'community|c=s' => \$snmp_community,
  20. 'oid|o=s' => \$snmp_oid,
  21. 'pregmatch|m=s' => \$preg_match,
  22. 'help' => \$help
  23. );
  24.  
  25. &Help if ($help || !($snmp_version eq '1') || ($snmp_oid eq '') || ($preg_match eq ''));
  26.  
  27. my ($session, $error) = Net::SNMP->session(
  28. -hostname => $hostname,
  29. -port => $snmp_port,
  30. -timeout =>  $snmp_timeout,
  31. -version => $snmp_version,
  32. -community => $snmp_community
  33. );
  34.  
  35. if (!defined($session)) {
  36. printf("ERROR: %s.\n", $error);
  37. exit 1;
  38. }
  39.  
  40. my $result = $session->get_table(
  41. -baseoid => $snmp_oid
  42. );
  43.  
  44. if (!defined($result)) {
  45. printf("ERROR: %s.\n", $session->error);
  46. $session->close;
  47. exit 1;
  48. }
  49.  
  50. $session->close;
  51.  
  52. my $count = 0;
  53. foreach my $key (keys(%$result)) {
  54. if ($result->{$key} =~ /$preg_match/) {
  55.   $count ++;
  56. }
  57. }
  58.  
  59. print $count;
  60.  
  61.  
  62. sub Help {
  63. print <<EOF;
  64. count-match-oid.pl - Version 0.05
  65. - Written by Shannon Wynter (http://fremnet.net/contact)
  66. ----------------------------------------------------------------------------
  67. Ever needed to count the number of snmp strings that match an expression?
  68.  
  69. Now you can
  70. ----------------------------------------------------------------------------
  71. Options (Those with * are required)
  72.     -h--hostname  : The host to query  (default: 127.0.0.1)
  73.     -p--port   : The port to query on  (default: 161)
  74.     -v--version  : The SNMP version to use - Only supports 1 atm
  75.     -t--timeout  : SNMP query timeout seconds(default: 10)
  76.     -c--community  : SNMP query  (default: public)
  77.     -o--oid  *: Base OID for searching in  (Numeric only)
  78.     -m--pregmatch*: Perl regular expression to search for
  79. --help   : This message
  80. ---------------------------------------------------------------------------
  81. Example
  82.     count-match-oid.pl -h 172.16.0.2 -o ".1.3.6.1.2.1.2.2.1.2" -m "^<pppoe-"
  83.  
  84.     This will query the interace descriptions on host 172.16.0.2 and return
  85.     the number that start with <pppoe-
  86.  
  87. EOF
  88. exit 0;
  89. }
  90. Download this code: count-match-oid.pl (Downloaded 230 time(s))

Step 3 -=- Install the script into cacti

  1. <cacti>  
  2. <hash_030010de382638dacacdf9e7c60a3893dff150>
  3.   <name>SNMP - Count Match OID</name>
  4.   <type_id>1</type_id>
  5.   <input_string>perl &amp;lt;path_cacti&amp;gt;/scripts/count-match-oid.pl -h &amp;lt;ip&amp;gt; -o "&amp;lt;oid&amp;gt;" -m "&amp;lt;match&amp;gt;"</input_string>
  6.   <fields>
  7.   <hash_070010ca07f2b6d7e6c094cc3e5f427d6d1dc5>
  8.     <name>IP Address / Hostname</name>
  9.     <update_rra></update_rra>
  10.     <regexp_match></regexp_match>
  11.     <allow_nulls></allow_nulls>
  12.     <type_code>hostname</type_code>
  13.     <input_output>in</input_output>
  14.     <data_name>ip</data_name>
  15.   </hash_070010ca07f2b6d7e6c094cc3e5f427d6d1dc5>
  16.   <hash_070010584f637a9cf87562570eda9759b573a6>
  17.     <name>Base OID (Numeric)</name>
  18.     <update_rra></update_rra>
  19.     <regexp_match></regexp_match>
  20.     <allow_nulls></allow_nulls>
  21.     <type_code></type_code>
  22.     <input_output>in</input_output>
  23.     <data_name>oid</data_name>
  24.   </hash_070010584f637a9cf87562570eda9759b573a6>
  25.   <hash_070010480838b7f2b1efda97713f94fb085822>
  26.     <name>Perl Regecp For Matching</name>
  27.     <update_rra></update_rra>
  28.     <regexp_match></regexp_match>
  29.     <allow_nulls></allow_nulls>
  30.     <type_code></type_code>
  31.     <input_output>in</input_output>
  32.     <data_name>match</data_name>
  33.   </hash_070010480838b7f2b1efda97713f94fb085822>
  34.   <hash_0700105707de2afccf71666e1582bf1c216d83>
  35.     <name>Number of matches</name>
  36.     <update_rra>on</update_rra>
  37.     <regexp_match></regexp_match>
  38.     <allow_nulls></allow_nulls>
  39.     <type_code></type_code>
  40.     <input_output>out</input_output>
  41.     <data_name>out_count</data_name>
  42.   </hash_0700105707de2afccf71666e1582bf1c216d83>
  43.   </fields>
  44. </hash_030010de382638dacacdf9e7c60a3893dff150>
  45. </cacti>
  46. Download this code: cacti_count_oid.xml (Downloaded 394 time(s))

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:

Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==---