• zabbix api 获取所有主机名、IP


    主要添加了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
    " unless $response->content->{'result'};
     
    $authID = $response->content->{'result'};
    print "Authentication successful. Auth ID: " . $authID . "
    ";
     
    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
    " unless $response->content->{result};
        my $hostID;
        foreach my $host ( @{ $response->content->{result} } ) {
     for my $num ( @{$host->{interfaces}} ) {
     
     
     $hostID->{ $host->{host} }
     = $num->{ip};
     
     }
     
        }
        return $hostID;
     
    }
  • 相关阅读:
    href 和src 的区别
    一道返回不重复数组的测试题
    使用Node.js+Socket.IO搭建WebSocket实时应用
    WebSocket 是什么原理?为什么可以实现持久连接?
    图片异步加载
    30分钟新手git教程
    通过ajax异步调用返回值
    [JS] 让人犯晕的JavaScript变量赋值
    javaScript字符串操作
    (String),toString(),String.valueOf()
  • 原文地址:https://www.cnblogs.com/mcshell/p/5655810.html
Copyright © 2020-2023  润新知