check_snmp_agent.pl 3.7 KB

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