码迷,mamicode.com
首页 > Windows程序 > 详细

zabbix api 获取所有主机名、IP

时间:2016-07-09 15:01:14      阅读:1216      评论:0      收藏:0      [点我收藏+]

标签:

主要添加了1条selectInterfaces记录,本质就是对于数据库就是join这张表。perl代码如下:

#!/usr/bin/perl
use strict;
use warnings;
use JSON::RPC::Client;
use Data::Dumper;
use JSON;
use utf8;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(20);    
#定义开启进程数
$| = 1;
binmode( STDIN,  :encoding(utf8) );
binmode( STDOUT, :encoding(utf8) );       
#三行
binmode( STDERR, :encoding(utf8) );       
#用来正常输出中文
 
# Authenticate yourself
my $client = new JSON::RPC::Client;
my $url    = http://zabbix.mcshell.org/api_jsonrpc.php;
my $authID;
my $response;
 
my $json = {
 
    jsonrpc => "2.0",
    method  => "user.login",
    params  => {
        user     => "user",
        password => "password"
    },
    id => 1
};
 
$response = $client->call( $url, $json );
 
# Check if response was successful
die "Authentication failed\n" unless $response->content->{result};
 
$authID = $response->content->{result};
print "Authentication successful. Auth ID: " . $authID . "\n";
 
print Dumper \get_all_host_and_ip();
 
sub get_all_host_and_ip {
    my $json = {
        jsonrpc => 2.0,
        method  => host.get,
        params  => {
            "output" => [ name, "host" ],    
#可以进行模糊匹配
            "selectInterfaces" => [ "interfaces", "ip" ]    
#过滤 ip
        },
        id   => 1,
        auth => "$authID",
    };
    my $response = $client->call( $url, $json );
    die "host.get failed\n" unless $response->content->{result};
    my $hostID;
    foreach my $host ( @{ $response->content->{result} } ) {
 for my $num ( @{$host->{interfaces}} ) {
 
 
 $hostID->{ $host->{host} }
 = $num->{ip};
 
 }
 
    }
    return $hostID;
 
}

 

zabbix api 获取所有主机名、IP

标签:

原文地址:http://www.cnblogs.com/mcshell/p/5655810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!