4
0

check_snmp_agent.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Subagent for testing check_snmp
  4. #
  5. #use strict; # Doesn't work
  6. use NetSNMP::OID qw(:all);
  7. use NetSNMP::agent;
  8. use NetSNMP::ASN qw(ASN_OCTET_STR ASN_COUNTER ASN_COUNTER64 ASN_INTEGER ASN_INTEGER64 ASN_UNSIGNED ASN_UNSIGNED64);
  9. #use Math::Int64 qw(uint64); # Skip that module while we don't need it
  10. sub uint64 { return $_ }
  11. if (!$agent) {
  12. print "This program must run as an embedded NetSNMP agent\n";
  13. exit 1;
  14. }
  15. my $baseoid = '.1.3.6.1.4.1.8072.3.2.67';
  16. my $multiline = 'Cisco Internetwork Operating System Software
  17. IOS (tm) Catalyst 4000 "L3" Switch Software (cat4000-I9K91S-M), Version
  18. 12.2(20)EWA, RELEASE SOFTWARE (fc1)
  19. Technical Support: http://www.cisco.com/techsupport
  20. Copyright (c) 1986-2004 by cisco Systems, Inc.
  21. ';
  22. my $multilin2 = "Kisco Outernetwork Oserating Gystem Totware
  23. Copyleft (c) 2400-2689 by kisco Systrems, Inc.";
  24. my $multilin3 = 'This should not confuse check_snmp "parser"
  25. into thinking there is no 2nd line';
  26. my $multilin4 = 'It\'s getting even harder if the line
  27. ends with with this: C:\\';
  28. my $multilin5 = 'And now have fun with with this: "C:\\"
  29. because we\'re not done yet!';
  30. # Next are arrays of indexes (Type, initial value and increments)
  31. # 0..16 <---- please update comment when adding/removing fields
  32. my @fields = (ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED, ASN_COUNTER, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_OCTET_STR, ASN_INTEGER, ASN_OCTET_STR, ASN_OCTET_STR );
  33. my @values = ($multiline, $multilin2, $multilin3, $multilin4, $multilin5, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)), 64000, "stringtests", "3.5", "87.4startswithnumberbutshouldbestring", '555"I said"', 'CUSTOM CHECK OK: foo is 12345', -2, '-4', '-6.6' );
  34. # undef increments are randomized
  35. my @incrts = (undef, undef, undef, undef, undef, 1000, -500, 1000, 100000, undef, 666, undef, undef, undef, undef, undef, -1, undef, undef );
  36. # Number of elements in our OID
  37. my $oidelts;
  38. {
  39. my @oid = split(/\./, $baseoid);
  40. $oidelts = scalar(@oid);
  41. }
  42. my $regoid = new NetSNMP::OID($baseoid);
  43. $agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
  44. sub my_snmp_handler {
  45. my ($handler, $registration_info, $request_info, $requests) = @_;
  46. for (my $request = $requests; $request; $request = $request->next) {
  47. my $oid = $request->getOID();
  48. my $index;
  49. my $next_index;
  50. # Validate the OID
  51. my @numarray = $oid->to_array();
  52. if (@numarray != $oidelts) {
  53. if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) {
  54. # GETNEXT for the base oid; set it to the first index
  55. push(@numarray, 0);
  56. $next_index = 0;
  57. } else {
  58. # We got a request for the base OID or with extra elements
  59. $request->setError($request_info, SNMP_ERR_NOERROR);
  60. next;
  61. }
  62. }
  63. $index = pop(@numarray);
  64. if ($index >= scalar(@fields)) {
  65. # Index is out of bounds
  66. $request->setError($request_info, SNMP_ERR_NOERROR);
  67. next;
  68. }
  69. # Handle the request
  70. if ($request_info->getMode() == MODE_GETNEXT) {
  71. if (++$index >= scalar(@fields)) {
  72. # Index will grow out of bounds
  73. $request->setError($request_info, SNMP_ERR_NOERROR);
  74. next;
  75. }
  76. $index = (defined($next_index) ? $next_index : $index);
  77. $request->setOID("$baseoid.$index");
  78. } elsif ($request_info->getMode() != MODE_GET) {
  79. # Everything else is write-related modes
  80. $request->setError($request_info, SNMP_ERR_READONLY);
  81. next;
  82. }
  83. # Set the response... setValue is a bit touchy about the data type, but accepts plain strings.
  84. my $value = sprintf("%s", $values[$index]);
  85. $request->setValue($fields[$index], $value);
  86. # And update the value
  87. if (defined($incrts[$index])) {
  88. $values[$index] += $incrts[$index];
  89. } elsif ($fields[$index] != ASN_OCTET_STR) {
  90. my $minus = int(rand(2))*-1;
  91. $minus = 1 unless ($minus);
  92. my $exp = 32;
  93. $exp = 64 if ($fields[$index] == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64);
  94. $values[$index] = int(rand(2**$exp));
  95. }
  96. }
  97. }