check_snmp_agent.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 @fields = (ASN_OCTET_STR, ASN_UNSIGNED, ASN_UNSIGNED, ASN_COUNTER, ASN_COUNTER64, ASN_UNSIGNED);
  25. my @values = ($multiline, 4294965296, 1000, 4294965296, uint64("18446744073709351616"), int(rand(2**32)));
  26. my @incrts = (undef, 1000, -500, 1000, 100000, undef);
  27. # Number of elements in our OID
  28. my $oidelts;
  29. {
  30. my @oid = split(/\./, $baseoid);
  31. $oidelts = scalar(@oid);
  32. }
  33. my $regoid = new NetSNMP::OID($baseoid);
  34. $agent->register('check_snmp_agent', $regoid, \&my_snmp_handler);
  35. sub my_snmp_handler {
  36. my ($handler, $registration_info, $request_info, $requests) = @_;
  37. for (my $request = $requests; $request; $request = $request->next) {
  38. my $oid = $request->getOID();
  39. my $index;
  40. my $next_index;
  41. # Validate the OID
  42. my @numarray = $oid->to_array();
  43. if (@numarray != $oidelts) {
  44. if ($request_info->getMode() == MODE_GETNEXT && @numarray == ($oidelts - 1)) {
  45. # GETNEXT for the base oid; set it to the first index
  46. push(@numarray, 0);
  47. $next_index = 0;
  48. } else {
  49. # We got a request for the base OID or with extra elements
  50. $request->setError($request_info, SNMP_ERR_NOERROR);
  51. next;
  52. }
  53. }
  54. $index = pop(@numarray);
  55. if ($index >= scalar(@fields)) {
  56. # Index is out of bounds
  57. $request->setError($request_info, SNMP_ERR_NOERROR);
  58. next;
  59. }
  60. # Handle the request
  61. if ($request_info->getMode() == MODE_GETNEXT) {
  62. if (++$index >= scalar(@fields)) {
  63. # Index will grow out of bounds
  64. $request->setError($request_info, SNMP_ERR_NOERROR);
  65. next;
  66. }
  67. $index = (defined($next_index) ? $next_index : $index);
  68. $request->setOID("$baseoid.$index");
  69. } elsif ($request_info->getMode() != MODE_GET) {
  70. # Everything else is write-related modes
  71. $request->setError($request_info, SNMP_ERR_READONLY);
  72. next;
  73. }
  74. # Set the response... setValue is a bit touchy about the data type, but accepts plain strings.
  75. my $value = sprintf("%s", $values[$index]);
  76. $request->setValue($fields[$index], $value);
  77. # And update the value
  78. if (defined($incrts[$index])) {
  79. $values[$index] += $incrts[$index];
  80. } elsif ($fields[$index] != ASN_OCTET_STR) {
  81. my $minus = int(rand(2))*-1;
  82. $minus = 1 unless ($minus);
  83. my $exp = 32;
  84. $exp = 64 if ($fields[$index] == ASN_COUNTER64 || $fields[$index] == ASN_INTEGER64 || $fields[$index] == ASN_UNSIGNED64);
  85. $values[$index] = int(rand(2**$exp));
  86. }
  87. }
  88. }