Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. #!/usr/bin/perl
  2. ####################################
  3. #
  4. # zload_snmpwalk
  5. # Author: Steven Dossett
  6. # Email: sd at panath.com
  7. #
  8. ####################################
  9. # Disclaimer:
  10. # This script has only been used with ZABBIX 1.1alpha6. It isn't thoroughly
  11. # tested. It isn't very efficient. It may eat your database or cause other
  12. # unintentional harm. Be careful and use at your own risk.
  13. # Please share bug fixes and improvements that you make...
  14. #
  15. # See usage instructions by running: zload_snmpwalk -h
  16. #
  17. # Description:
  18. # Creating new ZABBIX host templates can be time consuming. This script
  19. # automates some of the process by loading snmpwalk data into a ZABBIX
  20. # template. It is very important to use a MIB so that keys and descriptions
  21. # for the data are usable. MIBs can be specified on the command line or
  22. # snmpwalk may find them via your MIBS environment variable. The template
  23. # items will need modification from the ZABBIX UI after bulk loading. The
  24. # script doesn't attempt to manage interesting things like 'units' or 'custom
  25. # multipliers'. The script also has a limited notion of what qualifies as
  26. # 'Character' data and defaults to 'Numeric' for just about everything.
  27. # Update the script to properly identify more types.
  28. #
  29. # An example session might work like the following:
  30. #
  31. # 1 - Create a new host template in the Zabbix UI. Make note of the ID for the
  32. # new template.
  33. # 2 - Use an additional -T option to test the examples in 2a and 2b.
  34. # 2a - Build an initial set of items for the template. In the example below
  35. # the template id 10015 is used:
  36. # zload_snmpwalk -m SOMEVENDOR-MASTER-MIB secretstring dbuser dbpass 10015 somehost .iso.org.dod.internet.private
  37. # 2b - Alternatively, the template can be created from saved snmpwalk data:
  38. # zload_snmpwalk -m SOMEVENDOR-MASTER-MIB secretstring dbuser dbpass 10015 somefile
  39. # For the option above, the data must be in 'snmpwalk -Of' format.
  40. # 3 - Next, the template should be tweaked and tested from the Zabbix UI before
  41. # applying to hosts.
  42. #
  43. use Getopt::Std;
  44. use DBI;
  45.  
  46. use strict;
  47. use vars qw($PROG $VERSION $community $host $mib $usagetxt %OPTS
  48. $dbhost $dbuser $dbpass $zabbix_db $zabbix_id $translate $dbh
  49. $zabbix_port $zabbix_delay $zabbix_history $zabbix_trends
  50. $TEST);
  51.  
  52. $VERSION = "1.0";
  53. $PROG = "zload_snmpwalk";
  54.  
  55. $usagetxt = qq {
  56. USAGE:
  57. $PROG [OPTIONS] <community> <dbuser> <dbpasswd> <zabbix_id> <agent> <oid>
  58. $PROG [OPTIONS] <community> <dbuser> <dbpasswd> <zabbix_id> <file>
  59.  
  60. Version: $VERSION
  61. snmpwalk the target <agent> from the starting <oid> and then store the results
  62. in the ZABBIX template <zabbix_id>. Alternatively, a <file> containing the
  63. output of a previous snmpwalk can be used. The data is expected in the format
  64. generated by 'smpwalk -Of' and should be generated using the appropriate MIB(s)
  65. so that meaningful descriptions can be created for ZABBIX. Without using a
  66. MIB, the attempt will likely fail as the descriptions used for ZABBIX keys will
  67. be duplicated.
  68. Be sure to test your data with the -T option first!
  69.  
  70. BASIC ENVIRONMENT REQUIREMENTS:
  71. Zabbix 1.1alpha6 - Not tested with any other versions.
  72. perl - Tested with 5.8.0
  73. Mysql & perl DBI - Might work with postgres after minor changes.
  74. net-snmp - Tested with net-snmp 5.0.8
  75.  
  76. PRIMARY ARGUMENTS:
  77. <community> SNMP community string
  78. <dbuser> DB user that can update ZABBIX tables
  79. <dbpasswd> DB password for the user
  80. <zabbix_id> Zabbix template id for a template created from the UI
  81.  
  82. DATA SOURCES:
  83. <agent> Target SNMP host/agent
  84. <oid> Beginning oid - example: .iso.org.dod.internet.private
  85. OR
  86. <file> File containing snmpwalk data in '-Of' format to be loaded
  87.  
  88. OPTIONS:
  89. -T Test Mode. No DB updates - just view the new records.
  90. -v 1|2c SNMP version
  91. -m MIB[:...] load given list of MIBs (ALL loads everything)
  92. -p port SNMP port, default is 161
  93.  
  94. Database or ZABBIX data related options:
  95. -r delay SNMP polling delay, default is 60
  96. -h history How long to keep polling history, default is 7
  97. -t trends How long to keep polling trends, default is 365
  98. -s server Database server, default is localhost
  99. -d database Database, default is zabbix
  100. };
  101.  
  102. sub connect_db {
  103. my $db = "DBI:mysql:$zabbix_db:$dbhost";
  104. $dbh = DBI->connect($db, $dbuser, $dbpass) ||
  105. die "Can't connect to DB: $dbh->errstr\n";
  106. $dbh->{AutoCommit} = 0;
  107. }
  108.  
  109. sub get_zabbix_description
  110. {
  111.  
  112. my $raw_oid = shift(@_);
  113. my $pos;
  114.  
  115. # Step back 2 positions for a description string and key
  116. $pos = rindex($raw_oid,'.');
  117. $pos = rindex($raw_oid,'.',$pos - 1);
  118.  
  119. return substr($raw_oid, $pos + 1);
  120. }
  121.  
  122. sub get_zabbix_oid
  123. {
  124. my $raw_oid = shift(@_);
  125. my $zabbix_oid = `$translate $raw_oid`;
  126. chomp($zabbix_oid);
  127. return $zabbix_oid;
  128. }
  129.  
  130. sub get_zabbix_value_type
  131. {
  132. my $snmp_type = shift(@_);
  133. my $zabbix_value_type;
  134.  
  135. # Zabbix types
  136. # 0 = Numeric
  137. # 1 = Character
  138. # Add additional character types as needed. More are needed..
  139. SWITCH:
  140. {
  141. if ($snmp_type eq "STRING")
  142. { $zabbix_value_type = 1; last SWITCH; }
  143. $zabbix_value_type = 0;
  144. }
  145.  
  146. return $zabbix_value_type;
  147. }
  148.  
  149. sub usage
  150. {
  151. print $usagetxt;
  152. exit 0;
  153. }
  154.  
  155. MAIN:
  156. $| = 1; # no print delay
  157.  
  158. my $input;
  159. my $sql;
  160. my $tcounter = 0;
  161.  
  162. if (!getopts('d:h:m:p:r:s:t:u:v:T', \%OPTS))
  163. {
  164. usage();
  165. }
  166.  
  167.  
  168. if (@ARGV == 5 || @ARGV == 6)
  169. {
  170. $TEST = exists($OPTS{T}) ? "1" : "0";
  171.  
  172. $community = $ARGV[0];
  173. $dbuser = $ARGV[1];
  174. $dbpass = $ARGV[2];
  175. $zabbix_id = $ARGV[3];
  176.  
  177. $dbhost = exists($OPTS{s}) ? ($OPTS{s}) : "localhost";
  178. $zabbix_db = exists($OPTS{d}) ? ($OPTS{d}) : "zabbix";
  179.  
  180. $zabbix_port = exists($OPTS{p}) ? ($OPTS{p}) : "161";
  181. $zabbix_delay = exists($OPTS{r}) ? ($OPTS{r}) : "60";
  182. $zabbix_history = exists($OPTS{h}) ? ($OPTS{h}) : "7";
  183. $zabbix_trends = exists($OPTS{t}) ? ($OPTS{t}) : "365";
  184.  
  185. $translate = "snmptranslate -Ofn ";
  186. $translate .= exists($OPTS{m}) ? ("-m $OPTS{m} ") : "";
  187.  
  188. # Input from file by default, might change below
  189. $input = "<$ARGV[4]";
  190. }
  191. else
  192. {
  193. usage();
  194. };
  195.  
  196. # Data from live snmpwalk
  197. if (@ARGV == 6)
  198. {
  199. $host = $ARGV[4];
  200.  
  201. $input = "snmpwalk -c $community ";
  202. $input .= exists($OPTS{m}) ? ("-m $OPTS{m} ") : "";
  203. $input .= exists($OPTS{v}) ? ("-v $OPTS{v} ") : "";
  204. # Host
  205. $input .= "-Of $host";
  206. $input .= exists($OPTS{p}) ? (":$OPTS{p} ") : ":161 ";
  207. # OID
  208. $input .= "$ARGV[5]|";
  209. }
  210.  
  211. connect_db();
  212. open(SWALK,$input) or die "Can't open $input\n";
  213.  
  214. print "Processing Data from $input ";
  215.  
  216. $sql = $dbh->prepare("INSERT INTO items (type, snmp_community, snmp_oid, snmp_port, hostid, description, key_, delay, history, trends, value_type) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
  217.  
  218. while(my $line = <SWALK>)
  219. {
  220. my $zabbix_value_type;
  221. my $zabbix_description;
  222. my $zabbix_oid;
  223.  
  224. next if ($line !~ /(.iso.org.dod.*) = (.*):/ );
  225. #$1 = oid string to translate
  226. #$2 = integer, string or other snmp type
  227. #More snmp "character" types should be added
  228.  
  229. $zabbix_description = get_zabbix_description($1);
  230. $zabbix_oid = get_zabbix_oid($1);
  231. $zabbix_value_type = get_zabbix_value_type($2);
  232.  
  233.  
  234. if ($TEST)
  235. {
  236. if (($tcounter++ % 10) == 0)
  237. {
  238. print "\ntype\tsnmp_community\tsnmp_oid\tsnmp_port\t",
  239. "hostid\tdescription\tkey_\tdelay\t",
  240. "history\ttrends\tvalue_type\n";
  241. }
  242. print "4\t$community\t$zabbix_oid\t$zabbix_port\t",
  243. "$zabbix_id\t$zabbix_description\t",
  244. "$zabbix_description\t$zabbix_delay\t",
  245. "$zabbix_history\t$zabbix_trends\t",
  246. "$zabbix_value_type\n";
  247. }
  248. else
  249. {
  250. print "." if (($tcounter++ % 10) == 0 );
  251. $sql->execute(4, $community, $zabbix_oid, $zabbix_port,
  252. $zabbix_id, $zabbix_description, $zabbix_description,
  253. $zabbix_delay, $zabbix_history, $zabbix_trends,
  254. $zabbix_value_type) || die "Insert Failure: $sql->strerror\n";
  255. }
  256. }
  257. close(SWALK);
  258. $dbh->commit();
  259. $dbh->disconnect();
  260. print "Finished\n";
  261. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement