Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 10.53 KB | None | 0 0
  1. #!/usr/local/zabbix/perl/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use File::Basename;
  6. use Data::Dumper;
  7. use Getopt::Long;
  8. use LWP::UserAgent;
  9. use JSON;
  10.  
  11. sub send_zabbix_request($);
  12. sub host_exists($);
  13. sub get_groupids($);
  14. sub get_templateids($);
  15. sub get_proxy_id($);
  16. sub add_zabbix_host($);
  17. sub usage($);
  18. sub list();
  19.  
  20. my $groupids;
  21. my $proxyid=undef;
  22. my $templateids;
  23. my $hostid;
  24.  
  25. $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
  26.  
  27. # Option processing
  28. my $opts={};
  29. GetOptions($opts,'hostname=s','group=s@','proxy=s','dns=s','template=s@','ip=s','port=s','authid=s','list','debug','apiurl=s','monitorby=s','pocemail=s','pocname=s');
  30. my $zabbix_apiurl=($opts->{'apiurl'}?$opts->{'apiurl'}:$ENV{'ZABBIX_APIURL'});
  31. usage('no apiurl') if ! $zabbix_apiurl;
  32. my $debug=($opts->{'debug'}?$opts->{'debug'}:0);
  33. my $zabbix_authid=($opts->{'authid'}?$opts->{'authid'}:$ENV{'ZABBIX_AUTHID'});
  34. usage('no authid') if ! $zabbix_authid;
  35. list() if $opts->{'list'};
  36. usage('no hostname provided') if ! $opts->{'hostname'};
  37. usage('no dns name provided') if ! $opts->{'dns'};
  38. usage('no group provided') if ! $opts->{'group'};
  39. usage('no template provided') if ! $opts->{'template'};
  40. usage('no ip provided') if ! $opts->{'ip'};
  41. usage('no port provided') if ! $opts->{'port'};
  42. usage('no proxy provided') if ! $opts->{'proxy'};
  43. # usage('no pocemail provided') if ! $opts->{'pocemail'};
  44. # usage('no pocname provided') if ! $opts->{'pocname'};
  45. usage('no monitorby provided') if ! $opts->{'monitorby'};
  46. usage('monitorby should be "dns" or "ip"') if $opts->{'monitorby'} ne "ip" && $opts->{'monitorby'} ne "dns";
  47.  
  48. my $useip;
  49. $useip=($opts->{'monitorby'} eq "ip"?1:0);
  50.  
  51. # Convert all of the passed options to Zabbix IDs
  52.  
  53. # Is the host in Zabbix already?
  54. die "ERROR: $opts->{'hostname'} already exists\n" if(host_exists({hostname => $opts->{'hostname'}}));
  55.  
  56. # Group IDs
  57. $groupids=get_groupids({groups => $opts->{'group'}});
  58. print Dumper($groupids) if $debug;
  59. die "ERROR, one or more groups does not exist in Zabbix\n" if (!defined($groupids));
  60.  
  61. # Proxy IDs
  62. $proxyid=get_proxy_id({proxy => $opts->{'proxy'}}) if($opts->{'proxy'});
  63. print Dumper($proxyid) if $debug;
  64. die "ERROR, proxy $opts->{'proxy'} does not exist in Zabbix\n" if (!defined($proxyid) && $opts->{'proxy'});
  65.  
  66. # Template IDs
  67. $templateids=get_templateids({template => $opts->{'template'}});
  68. print Dumper($templateids) if $debug;
  69. die "ERROR, one or more templates does not exist in Zabbix\n" if (!defined($templateids));
  70.  
  71. my $hosthash={
  72.     hostname => $opts->{'hostname'},
  73.     dns => $opts->{'dns'},
  74.     ip => $opts->{'ip'},
  75.     port => $opts->{'port'},
  76.     groups => $groupids,
  77.     proxy => $proxyid,
  78.     templates => $templateids
  79. };
  80. $hosthash->{'pocemail'}=$opts->{'pocemail'} if $opts->{'pocemail'};
  81. $hosthash->{'pocname'}=$opts->{'pocname'} if $opts->{'pocname'};
  82.  
  83. # Add the host
  84. $hostid=add_zabbix_host($hosthash);
  85.  
  86. print "Host $opts->{'hostname'} added, hostid=$hostid\n";
  87.  
  88. sub add_zabbix_host($) {
  89.     my $args=$_[0];
  90.     my $zabreq;
  91.     my $zabres;
  92.     my $groupid;
  93.     my $templateid;
  94.  
  95.     $zabreq = {
  96.         "jsonrpc" => "2.0",
  97.         "method" => "host.create",
  98.         "params" => {
  99.             "host" => $args->{'hostname'},
  100.             "interfaces" => [{
  101.                 "type" => 1,
  102.                 "main" => 1,
  103.                 "dns" => $args->{'dns'},
  104.                 "ip" => $args->{'ip'},
  105.                 "port" => $args->{'port'},
  106.                 "useip" => $useip
  107.             }]
  108.         },
  109.         "auth" => $zabbix_authid,
  110.         "id" => 3
  111.     };
  112.     $zabreq->{'params'}->{'inventory'}->{'poc_1_name'}=$args->{'pocname'} if $args->{'pocname'};
  113.     $zabreq->{'params'}->{'inventory'}->{'poc_1_email'}=$args->{'pocemail'} if $args->{'pocemail'};
  114.  
  115.     foreach $groupid (@{$args->{'groups'}}) {
  116.         push(@{$zabreq->{'params'}->{'groups'}},{"groupid" => $groupid});
  117.     }
  118.  
  119.     foreach $templateid (@{$args->{'templates'}}) {
  120.         push(@{$zabreq->{'params'}->{'templates'}},{"templateid" => $templateid});
  121.     }
  122.  
  123.     $zabreq->{'params'}->{'proxy_hostid'}=$proxyid if($args->{'proxy'});
  124.  
  125.     print "Sending:\n" if $debug;
  126.     print Dumper($zabreq) if $debug;
  127.     print "\n\nReceived:\n" if $debug;
  128.     $zabres=send_zabbix_request($zabreq);
  129.     print Dumper($zabres) if $debug;
  130.     return $zabres->{'result'}->{'hostids'}->[0];
  131. }
  132.  
  133. sub get_templateids($) {
  134.     my $args=$_[0];
  135.     my $zabreq;
  136.     my $zabres;
  137.     my $error;
  138.     my $template;
  139.     my $allfoundtemplates;
  140.     my $templatids;
  141.  
  142.     $zabreq={
  143.         "jsonrpc" => "2.0",
  144.         "method" => "template.get",
  145.         "params" => {
  146.             "output" => "extend",
  147.             "filter" => {
  148.                 "host" => $args->{'template'}
  149.             }
  150.         },
  151.         "auth" => $zabbix_authid,
  152.         "id" => 2
  153.     };
  154.     $zabres=send_zabbix_request($zabreq);
  155.     foreach $template (@{$zabres->{'result'}}) {
  156.         push (@$allfoundtemplates,$template->{'host'});
  157.         push (@$templateids,$template->{'templateid'});
  158.     }
  159.     foreach $template (@{$args->{'template'}}) {
  160.         if(!grep(/$template/,@$allfoundtemplates)) {
  161.             print qq /ERROR: Template "$template" not in Zabbix\n/;
  162.             $error=1;
  163.         }
  164.     }
  165.     return undef if $error;
  166.     return $templateids;
  167. }
  168.  
  169. sub get_proxy_id($) {
  170.     my $args=$_[0];
  171.     my $zabreq;
  172.     my $zabres;
  173.     my $proxyname=$args->{'proxy'};
  174.     my $proxy;
  175.  
  176.     $zabreq={
  177.         "jsonrpc" => "2.0",
  178.         "method" => "proxy.get",
  179.         "params" => {
  180.             "output" => "extend",
  181.         },
  182.         "auth" => $zabbix_authid,
  183.         "id" => 2
  184.     };
  185.  
  186.     $zabres=send_zabbix_request($zabreq);
  187.     foreach $proxy (@{$zabres->{'result'}}) {
  188.         if($proxy->{'host'} eq $proxyname) {
  189.             return $proxy->{'proxyid'};
  190.         }
  191.     }
  192.     return undef;
  193. }
  194.  
  195. sub get_groupids($) {
  196.     my $args=$_[0];
  197.     my $zabreq;
  198.     my $zabres;
  199.     my $error;
  200.     my $group;
  201.     my $allfoundgroups;
  202.     my $groupids;
  203.  
  204.     # Get the group IDs for the passed groups, exit if the group(s) don't exist
  205.     $zabreq={
  206.         "jsonrpc" => "2.0",
  207.         "method" => "hostgroup.get",
  208.         "params" => {
  209.             "output" =>  "extend",
  210.             "filter" => {
  211.                 "name" =>  $args->{'groups'}
  212.             }
  213.         },
  214.         "auth" => $zabbix_authid,
  215.         "id" => 2
  216.     };
  217.     $zabres=send_zabbix_request($zabreq);
  218.     foreach $group (@{$zabres->{'result'}}) {
  219.         push (@$allfoundgroups,$group->{'name'});
  220.         push (@$groupids,$group->{'groupid'});
  221.     }
  222.     foreach $group (@{$args->{'groups'}}) {
  223.         if(!grep(/$group/,@$allfoundgroups)) {
  224.             print qq /ERROR: Group "$group" not in Zabbix\n/;
  225.             $error=1;
  226.         }
  227.     }
  228.     return undef if $error;
  229.     return $groupids;
  230. }
  231.  
  232. sub send_zabbix_request($) {
  233.     my $request=$_[0];
  234.  
  235.     my $jsonreq;
  236.     my $ua;
  237.     my $req;
  238.     my $response;
  239.     my $results;
  240.     my $errtype;
  241.     my $json;
  242.     my $api_url=$zabbix_apiurl;
  243.  
  244.     $json = new JSON;
  245.     $jsonreq = $json->encode($request);
  246.  
  247.     # Create a new UserAgent Object
  248.     $ua=new LWP::UserAgent;
  249.  
  250.     # Create a new HTTP POST Request
  251.     # with the right JSON content-type
  252.     # and our JSON request as the content
  253.     $req=HTTP::Request->new(POST => $api_url);
  254.     $req->content_type('application/json-rpc');
  255.     $req->content($jsonreq);
  256.  
  257.     # Use our User Agent to send a request
  258.     # using the HTTP Request we built out
  259.     $response=$ua->request($req);
  260.  
  261.     if ($response->is_success) {
  262.         # succesful response, decode the JSON
  263.         $results=$json->decode($response->decoded_content);
  264.         # error?
  265.         if(defined($results->{'error'})) {
  266.             print "ERROR while sending request to zabbix:\n";
  267.             print Dumper($results->{'error'});
  268.             print "\nHappened while sending:\n$jsonreq\n";
  269.             die;
  270.         } else {
  271.             return $results;
  272.         }
  273.     } else {
  274.         die "\"",$response->status_line,"\"";
  275.     }
  276. }
  277.  
  278. sub host_exists($) {
  279.     my $args=$_[0];
  280.     my $hostname=$args->{'hostname'};
  281.     my $zabreq;
  282.     my $zabres;
  283.  
  284.     ## Does the host already exist in Zabbix?
  285.     $zabreq={
  286.         "jsonrpc" => "2.0",
  287.         "method" => "host.get",
  288.         "params" => {
  289.             "output" => "shorten",
  290.             "filter" => {
  291.                     "host" => [
  292.                         $hostname
  293.                     ]
  294.                 }
  295.         },
  296.         "auth" => $zabbix_authid,
  297.         "id" => 1
  298.     };
  299.     $zabres=send_zabbix_request($zabreq);
  300.     return scalar(@{$zabres->{'result'}});
  301. }
  302.  
  303. sub usage($) {
  304.     print "Usage: ",basename($0)," --hostname <hostname> --dns <FQDN> --group <group> --proxy <proxy> --template <template> --ip <IP Address> --port <port> --monitorby <dns|ip> --pocname <full name of POC> --pocemail <e-mail address of POC> --list\n\n";
  305.     print qq/        NOTES: * Either set the authid in ZABBIX_AUTHID or pass
  306.                  one via the --authid command line options.
  307.                * Required fields:
  308.                    --hostname
  309.                    --dns
  310.                    --group
  311.                    --template
  312.                    --ip
  313.                    --port
  314.                    --monitorby
  315.                    --proxy
  316.                    --pocname
  317.                    --pocemail
  318.                * Multiple --template and --group options
  319.                  are accepted
  320.                * --list lists all available groups, proxies, and templates.
  321.                  all other options will be ignored
  322.  
  323.         Adds a host to Zabbix.
  324. /;
  325.     if($_[0]) {
  326.         print qq /\n/;
  327.         print qq /ERROR: $_[0]/;
  328.         print qq /\n/;
  329.     }
  330.     exit(1);
  331. }
  332.  
  333. sub list() {
  334.     my $zabreq;
  335.     my $zabres;
  336.     my $templategroup="Templates";
  337.     my $template;
  338.     my $allnames;
  339.     my $ingroup;
  340.     my $group;
  341.     my $proxy;
  342.  
  343.     # Get templates
  344.     $zabreq={
  345.         "jsonrpc" => "2.0",
  346.         "method" => "template.get",
  347.         "params" => {
  348.             "output" => "extend",
  349.             "select_groups" => "extend",
  350.             "filter" => {
  351.             }
  352.         },
  353.         "auth" => $zabbix_authid,
  354.         "id" => 1
  355.     };
  356.     $zabres=send_zabbix_request($zabreq);
  357.     foreach $template (@{$zabres->{'result'}}) {
  358.         $ingroup=0;
  359.         foreach $group (@{$template->{'groups'}}) {
  360.             $ingroup=1 if $group->{'name'} eq $templategroup;
  361.         }
  362.         # push(@{$allnames->{'templates'}},$template->{'host'}) if $ingroup;
  363.         push(@{$allnames->{'templates'}},$template->{'host'});
  364.     }
  365.  
  366.     # Get groups
  367.     $zabreq={
  368.         "jsonrpc" => "2.0",
  369.         "method" => "hostgroup.get",
  370.         "params" => {
  371.             "output" =>  "extend",
  372.             "filter" => {
  373.             }
  374.         },
  375.         "auth" => $zabbix_authid,
  376.         "id" => 2
  377.     };
  378.     $zabres=send_zabbix_request($zabreq);
  379.     foreach $group (@{$zabres->{'result'}}) {
  380.         push(@{$allnames->{'groups'}},$group->{'name'});
  381.     }
  382.  
  383.     # Get proxies
  384.     $zabreq={
  385.         "jsonrpc" => "2.0",
  386.         "method" => "proxy.get",
  387.         "params" => {
  388.             "output" => "extend",
  389.         },
  390.         "auth" => $zabbix_authid,
  391.         "id" => 2
  392.     };
  393.     $zabres=send_zabbix_request($zabreq);
  394.     foreach $proxy (@{$zabres->{'result'}}) {
  395.         push(@{$allnames->{'proxies'}},$proxy->{'host'});
  396.     }
  397.  
  398.     # Print Results
  399.     print "Groups:\n";
  400.     foreach $group (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'groups'}})) {
  401.         print qq /\t'$group'\n/;
  402.     }
  403.     print "\n";
  404.  
  405.     print "Templates:\n";
  406.     foreach $template (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'templates'}})) {
  407.         print qq /\t'$template'\n/;
  408.     }
  409.     print "\n";
  410.  
  411.     print "Proxies:\n";
  412.     foreach $proxy (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'proxies'}})) {
  413.         print qq /\t'$proxy'\n/;
  414.     }
  415.     print "\n";
  416.  
  417.     print Dumper($allnames) if $debug;
  418.  
  419.     exit(0);
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement