Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/zabbix/perl/bin/perl
- use strict;
- use warnings;
- use File::Basename;
- use Data::Dumper;
- use Getopt::Long;
- use LWP::UserAgent;
- use JSON;
- sub send_zabbix_request($);
- sub host_exists($);
- sub get_groupids($);
- sub get_templateids($);
- sub get_proxy_id($);
- sub add_zabbix_host($);
- sub usage($);
- sub list();
- my $groupids;
- my $proxyid=undef;
- my $templateids;
- my $hostid;
- $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
- # Option processing
- my $opts={};
- 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');
- my $zabbix_apiurl=($opts->{'apiurl'}?$opts->{'apiurl'}:$ENV{'ZABBIX_APIURL'});
- usage('no apiurl') if ! $zabbix_apiurl;
- my $debug=($opts->{'debug'}?$opts->{'debug'}:0);
- my $zabbix_authid=($opts->{'authid'}?$opts->{'authid'}:$ENV{'ZABBIX_AUTHID'});
- usage('no authid') if ! $zabbix_authid;
- list() if $opts->{'list'};
- usage('no hostname provided') if ! $opts->{'hostname'};
- usage('no dns name provided') if ! $opts->{'dns'};
- usage('no group provided') if ! $opts->{'group'};
- usage('no template provided') if ! $opts->{'template'};
- usage('no ip provided') if ! $opts->{'ip'};
- usage('no port provided') if ! $opts->{'port'};
- usage('no proxy provided') if ! $opts->{'proxy'};
- # usage('no pocemail provided') if ! $opts->{'pocemail'};
- # usage('no pocname provided') if ! $opts->{'pocname'};
- usage('no monitorby provided') if ! $opts->{'monitorby'};
- usage('monitorby should be "dns" or "ip"') if $opts->{'monitorby'} ne "ip" && $opts->{'monitorby'} ne "dns";
- my $useip;
- $useip=($opts->{'monitorby'} eq "ip"?1:0);
- # Convert all of the passed options to Zabbix IDs
- # Is the host in Zabbix already?
- die "ERROR: $opts->{'hostname'} already exists\n" if(host_exists({hostname => $opts->{'hostname'}}));
- # Group IDs
- $groupids=get_groupids({groups => $opts->{'group'}});
- print Dumper($groupids) if $debug;
- die "ERROR, one or more groups does not exist in Zabbix\n" if (!defined($groupids));
- # Proxy IDs
- $proxyid=get_proxy_id({proxy => $opts->{'proxy'}}) if($opts->{'proxy'});
- print Dumper($proxyid) if $debug;
- die "ERROR, proxy $opts->{'proxy'} does not exist in Zabbix\n" if (!defined($proxyid) && $opts->{'proxy'});
- # Template IDs
- $templateids=get_templateids({template => $opts->{'template'}});
- print Dumper($templateids) if $debug;
- die "ERROR, one or more templates does not exist in Zabbix\n" if (!defined($templateids));
- my $hosthash={
- hostname => $opts->{'hostname'},
- dns => $opts->{'dns'},
- ip => $opts->{'ip'},
- port => $opts->{'port'},
- groups => $groupids,
- proxy => $proxyid,
- templates => $templateids
- };
- $hosthash->{'pocemail'}=$opts->{'pocemail'} if $opts->{'pocemail'};
- $hosthash->{'pocname'}=$opts->{'pocname'} if $opts->{'pocname'};
- # Add the host
- $hostid=add_zabbix_host($hosthash);
- print "Host $opts->{'hostname'} added, hostid=$hostid\n";
- sub add_zabbix_host($) {
- my $args=$_[0];
- my $zabreq;
- my $zabres;
- my $groupid;
- my $templateid;
- $zabreq = {
- "jsonrpc" => "2.0",
- "method" => "host.create",
- "params" => {
- "host" => $args->{'hostname'},
- "interfaces" => [{
- "type" => 1,
- "main" => 1,
- "dns" => $args->{'dns'},
- "ip" => $args->{'ip'},
- "port" => $args->{'port'},
- "useip" => $useip
- }]
- },
- "auth" => $zabbix_authid,
- "id" => 3
- };
- $zabreq->{'params'}->{'inventory'}->{'poc_1_name'}=$args->{'pocname'} if $args->{'pocname'};
- $zabreq->{'params'}->{'inventory'}->{'poc_1_email'}=$args->{'pocemail'} if $args->{'pocemail'};
- foreach $groupid (@{$args->{'groups'}}) {
- push(@{$zabreq->{'params'}->{'groups'}},{"groupid" => $groupid});
- }
- foreach $templateid (@{$args->{'templates'}}) {
- push(@{$zabreq->{'params'}->{'templates'}},{"templateid" => $templateid});
- }
- $zabreq->{'params'}->{'proxy_hostid'}=$proxyid if($args->{'proxy'});
- print "Sending:\n" if $debug;
- print Dumper($zabreq) if $debug;
- print "\n\nReceived:\n" if $debug;
- $zabres=send_zabbix_request($zabreq);
- print Dumper($zabres) if $debug;
- return $zabres->{'result'}->{'hostids'}->[0];
- }
- sub get_templateids($) {
- my $args=$_[0];
- my $zabreq;
- my $zabres;
- my $error;
- my $template;
- my $allfoundtemplates;
- my $templatids;
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "template.get",
- "params" => {
- "output" => "extend",
- "filter" => {
- "host" => $args->{'template'}
- }
- },
- "auth" => $zabbix_authid,
- "id" => 2
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $template (@{$zabres->{'result'}}) {
- push (@$allfoundtemplates,$template->{'host'});
- push (@$templateids,$template->{'templateid'});
- }
- foreach $template (@{$args->{'template'}}) {
- if(!grep(/$template/,@$allfoundtemplates)) {
- print qq /ERROR: Template "$template" not in Zabbix\n/;
- $error=1;
- }
- }
- return undef if $error;
- return $templateids;
- }
- sub get_proxy_id($) {
- my $args=$_[0];
- my $zabreq;
- my $zabres;
- my $proxyname=$args->{'proxy'};
- my $proxy;
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "proxy.get",
- "params" => {
- "output" => "extend",
- },
- "auth" => $zabbix_authid,
- "id" => 2
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $proxy (@{$zabres->{'result'}}) {
- if($proxy->{'host'} eq $proxyname) {
- return $proxy->{'proxyid'};
- }
- }
- return undef;
- }
- sub get_groupids($) {
- my $args=$_[0];
- my $zabreq;
- my $zabres;
- my $error;
- my $group;
- my $allfoundgroups;
- my $groupids;
- # Get the group IDs for the passed groups, exit if the group(s) don't exist
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "hostgroup.get",
- "params" => {
- "output" => "extend",
- "filter" => {
- "name" => $args->{'groups'}
- }
- },
- "auth" => $zabbix_authid,
- "id" => 2
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $group (@{$zabres->{'result'}}) {
- push (@$allfoundgroups,$group->{'name'});
- push (@$groupids,$group->{'groupid'});
- }
- foreach $group (@{$args->{'groups'}}) {
- if(!grep(/$group/,@$allfoundgroups)) {
- print qq /ERROR: Group "$group" not in Zabbix\n/;
- $error=1;
- }
- }
- return undef if $error;
- return $groupids;
- }
- sub send_zabbix_request($) {
- my $request=$_[0];
- my $jsonreq;
- my $ua;
- my $req;
- my $response;
- my $results;
- my $errtype;
- my $json;
- my $api_url=$zabbix_apiurl;
- $json = new JSON;
- $jsonreq = $json->encode($request);
- # Create a new UserAgent Object
- $ua=new LWP::UserAgent;
- # Create a new HTTP POST Request
- # with the right JSON content-type
- # and our JSON request as the content
- $req=HTTP::Request->new(POST => $api_url);
- $req->content_type('application/json-rpc');
- $req->content($jsonreq);
- # Use our User Agent to send a request
- # using the HTTP Request we built out
- $response=$ua->request($req);
- if ($response->is_success) {
- # succesful response, decode the JSON
- $results=$json->decode($response->decoded_content);
- # error?
- if(defined($results->{'error'})) {
- print "ERROR while sending request to zabbix:\n";
- print Dumper($results->{'error'});
- print "\nHappened while sending:\n$jsonreq\n";
- die;
- } else {
- return $results;
- }
- } else {
- die "\"",$response->status_line,"\"";
- }
- }
- sub host_exists($) {
- my $args=$_[0];
- my $hostname=$args->{'hostname'};
- my $zabreq;
- my $zabres;
- ## Does the host already exist in Zabbix?
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "host.get",
- "params" => {
- "output" => "shorten",
- "filter" => {
- "host" => [
- $hostname
- ]
- }
- },
- "auth" => $zabbix_authid,
- "id" => 1
- };
- $zabres=send_zabbix_request($zabreq);
- return scalar(@{$zabres->{'result'}});
- }
- sub usage($) {
- 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";
- print qq/ NOTES: * Either set the authid in ZABBIX_AUTHID or pass
- one via the --authid command line options.
- * Required fields:
- --hostname
- --dns
- --group
- --template
- --ip
- --port
- --monitorby
- --proxy
- --pocname
- --pocemail
- * Multiple --template and --group options
- are accepted
- * --list lists all available groups, proxies, and templates.
- all other options will be ignored
- Adds a host to Zabbix.
- /;
- if($_[0]) {
- print qq /\n/;
- print qq /ERROR: $_[0]/;
- print qq /\n/;
- }
- exit(1);
- }
- sub list() {
- my $zabreq;
- my $zabres;
- my $templategroup="Templates";
- my $template;
- my $allnames;
- my $ingroup;
- my $group;
- my $proxy;
- # Get templates
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "template.get",
- "params" => {
- "output" => "extend",
- "select_groups" => "extend",
- "filter" => {
- }
- },
- "auth" => $zabbix_authid,
- "id" => 1
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $template (@{$zabres->{'result'}}) {
- $ingroup=0;
- foreach $group (@{$template->{'groups'}}) {
- $ingroup=1 if $group->{'name'} eq $templategroup;
- }
- # push(@{$allnames->{'templates'}},$template->{'host'}) if $ingroup;
- push(@{$allnames->{'templates'}},$template->{'host'});
- }
- # Get groups
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "hostgroup.get",
- "params" => {
- "output" => "extend",
- "filter" => {
- }
- },
- "auth" => $zabbix_authid,
- "id" => 2
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $group (@{$zabres->{'result'}}) {
- push(@{$allnames->{'groups'}},$group->{'name'});
- }
- # Get proxies
- $zabreq={
- "jsonrpc" => "2.0",
- "method" => "proxy.get",
- "params" => {
- "output" => "extend",
- },
- "auth" => $zabbix_authid,
- "id" => 2
- };
- $zabres=send_zabbix_request($zabreq);
- foreach $proxy (@{$zabres->{'result'}}) {
- push(@{$allnames->{'proxies'}},$proxy->{'host'});
- }
- # Print Results
- print "Groups:\n";
- foreach $group (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'groups'}})) {
- print qq /\t'$group'\n/;
- }
- print "\n";
- print "Templates:\n";
- foreach $template (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'templates'}})) {
- print qq /\t'$template'\n/;
- }
- print "\n";
- print "Proxies:\n";
- foreach $proxy (sort({ "\L$a" cmp "\L$b" } @{$allnames->{'proxies'}})) {
- print qq /\t'$proxy'\n/;
- }
- print "\n";
- print Dumper($allnames) if $debug;
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement