Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.47 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. # script: advsnmp.discovery
  4. # version: 2.0
  5. # author: Simon Kowallik <sk simonkowallik.com>
  6. # description: Zabbix low level discovery script for advanced index discovery
  7. # license: GPL2
  8.  
  9. #USAGE:
  10. #advsnmp.discovery[{HOST.IP},"-v2c -cpublic",.1.2.3.4.5.6,offset.length,offset.length,offset.lentgh
  11. #                      ^            ^             ^              ^             ^            ^
  12. #                      |            |             |              |             |            |
  13. #ARGV                  0            1             2              3             4            5
  14. #                      |            |             |              |             |            |
  15. #                 IP/DNS Name       |             |              |             |            |
  16. #                             SNMP Settings    SNMP OID       INDEX_offset.INDEX_length
  17. #
  18. #OID: .0.0.0.0.0.0.1.0.2.1.1.4.4.0
  19. #will be cut to:  .1.0.2.1.1.4.4.0
  20. #
  21. #     .     1 .     0 .     2 .     1 .     1 .     4 .     4 .     0
  22. #0=>'', 1=>'1', 2=>'0', 3=>'2', 4=>'1', 5=>'1', 6=>'4', 7=>'4', 8=>'0'
  23. #
  24. #         $_offset     $_len
  25. #                 \   /
  26. #config: INDEX1 -> 1.1
  27. #        INDEX2 -> 3.2
  28. #        INDEX3 -> 5.3
  29. #
  30. #   INDEX1   INDEX2   INDEX3
  31. #      \       |       /
  32. #     .[1].0.[2.1].[1.4.4].0
  33. #
  34. #
  35. #EXAMPLE:
  36. # OIDs to query:
  37. # .0.0.0.0.0.0.1.0.2.1.1.4.4.0
  38. # .0.0.0.0.0.0.2.0.3.6.1.4.4.0
  39. # .0.0.0.0.0.0.3.0.4.5.1.4.4.0
  40. # .0.0.0.0.0.0.1.0.2.4.5.3.4.0
  41. # .0.0.0.0.0.0.2.0.2.2.5.3.4.0
  42. # .0.0.0.0.0.0.3.0.2.9.5.3.4.0
  43. #
  44. # Discovery Rule with "external check"
  45. # Key: advsnmp.discovery[{HOST.IP},"-v2c -cpublic",.0.0.0.0.0.0,1.1,3.2,5.3]
  46. #
  47. # Idexes will be:
  48. # ADVSNMPINDEX1 ADVSNMPINDEX2 ADVSNMPINDEX3
  49. #       1            2.1          1.4.4
  50. #       2            3.6          1.4.4
  51. #       3            4.5          1.4.4
  52. #       1            2.4          5.3.4
  53. #       2            2.2          5.3.4
  54. #       3            2.9          5.3.4
  55.  
  56.  
  57. # global options
  58. my $GLOB_SNMPW = "-OQn";
  59. my $SNMPWALK_BIN = "/usr/bin/snmpwalk";
  60.  
  61. # never modify these values, as we have to pass them back to Zabbix within the JSON element!
  62. my $OPT_HOST = $ARGV[0];
  63. my $OPT_SNMPW = $ARGV[1];
  64. my $OPT_OID = $ARGV[2];
  65.  
  66. # build config
  67. my %OPT_CONFIG;
  68. my $OPT_CONFIG_STRING;
  69. for (my $cnt = 3; $cnt < scalar(@ARGV); $cnt++) {
  70.   my ($_offset, $_len) = split(/\./, $ARGV[$cnt]);
  71.   $OPT_CONFIG{$_offset} = $_len;
  72.   $OPT_CONFIG_STRING = $OPT_CONFIG_STRING . ',' . $ARGV[$cnt];
  73. }
  74.  
  75. #variables
  76. my %INDEXES;
  77.  
  78. # assign OPT_OID to opt_oid_escape
  79. my $opt_oid_escape = $OPT_OID;
  80. # prepend a dot (.) to opt_oid_escape in case OPT_OID was specified without one
  81. if($opt_oid_escape !~ m/^\./) {
  82.   $opt_oid_escape = '.' . $opt_oid_escape;
  83. }
  84. # v1.1: cut off trailing dot, if it exists
  85. if($opt_oid_escape =~ m/\.$/) {
  86.   chop($opt_oid_escape);
  87. }
  88. # create snmpw_oid from sanitized OPT_OID
  89. my $snmpw_oid = $opt_oid_escape;
  90.  
  91. # escape all dots in opt_oid_escape for future regexes
  92. $opt_oid_escape =~ s/\./\\./g;
  93.  
  94.  
  95. foreach my $snmpw_line (`$SNMPWALK_BIN $GLOB_SNMPW $OPT_SNMPW $OPT_HOST $snmpw_oid`)
  96. {
  97.     # remove newline
  98.     chomp($snmpw_line);
  99.  
  100.     # split OID = VALUE
  101.     my ($oid, $value) = split(/ = /, $snmpw_line);
  102.  
  103.     # sanitize $value, remove start/end quotes
  104.     $value =~ s/^"//;
  105.    $value =~ s/"$//;
  106.     $value =~ s/"/\\"/g;
  107.    $value =~ s/\\/\\\\/g;
  108.  
  109.  
  110.    # remove OPT_OID part from $oid
  111.    # and we will get the trailing OID part, where we will extract the INDEX
  112.    #
  113.    # before: .0.0.0.0.0.0.1.0.2.1.0
  114.    $oid =~ s/$opt_oid_escape//;
  115.    # after: .1.0.2.1.0
  116.  
  117.  
  118.    # split rest of oid into array
  119.    # index N ($OPT_INDEX) is ARRAY[N] -> ARRAY[$OPT_INDEX]
  120.    my @oid_arr = split(/\./, $oid);
  121.    #contains: 0=>'', 1=>'1', 2=>'0', 3=>'2', 4=>'1', 5=>'0'
  122.  
  123.    # put indexes into @%indexes
  124.    my $line_index;
  125.    # fetch offset from OPT_CONFIG
  126.    foreach my $_offset (sort keys %OPT_CONFIG) {
  127.      # set $index to index (OID part)
  128.      my $index = $oid_arr[$_offset];
  129.      # when complete $index by looking at len (stored in $OPT_CONFIG{$_offset})
  130.      for (my $i = 1; $i < $OPT_CONFIG{$_offset}; $i++) {
  131.        #v1.1: next if oid_arr element is empty. this can happen for dymanic length indexes
  132.        next if($oid_arr[$_offset+$i] =~ m/^$/);
  133.        # attach next OID part for len of index
  134.        $index = $index .'.'. $oid_arr[$_offset+$i];
  135.      }
  136.      $line_index = $line_index . ";$index";
  137.    }
  138.    # assign value to INDEXES Hash. INDEX => VALUE
  139.    $INDEXES{$line_index} = $value;
  140. }
  141. #
  142. # print JSON object
  143. #
  144.  
  145. print "{\n";
  146. # from Zabbix 2.0.0rc1 the array name has changed to 'data'
  147. print "\t\"data\":[\n";
  148.  
  149. my $first_line = 1;
  150. #for (my $i = 0; $i < $#INDEXES; $i++) {
  151. foreach my $line_index (sort keys %INDEXES) {
  152.  
  153.    # print if it is not the first line
  154.    if($first_line) {
  155.      # we are at first line
  156.      $first_line = 0;
  157.    } else {
  158.      # we are not at first line
  159.      print "\t,\n";
  160.    }
  161.  
  162.    print "\t\t{\n";
  163.  
  164.    # print all INDEXES
  165.    my $ctr = 1;
  166.    foreach my $index ( split(/;/, $line_index) ) {
  167.      next if (!defined($index));
  168.      print "\t\t\"{#ADVSNMPINDEX$ctr}\": \"$index\",\n";
  169.       $ctr++
  170.     }
  171.     # print value if there are indexes (ctr would be 2 for one index)
  172.     if ($ctr > 1) {
  173.       print "\t\t\"{#ADVSNMPVALUE}\":\"$INDEXES{$line_index}\"\n";
  174.       print "\t\t}";
  175.     } else {
  176.       # if there is no index, close JSON
  177.       print "\t\t}";
  178.     }
  179.  
  180. } #for
  181.  
  182. print "\n\t]\n";
  183. print "}\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement