首页 > Windows开发 > 详细

zabbix api 获取所有主机名、IP

时间:2016-07-09 14:58:31      阅读:371      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!