• 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;
     
    }
  • 相关阅读:
    List--使用List作为堆栈和队列
    Range的范围
    异常处理
    关于打印输出的一些方法
    关于set的unordered特性
    面向对象
    函数
    Linux中命令备份mysql形成文件
    局域网内Linux下开启ftp服务的“曲折路”和命令复习
    linux下的apache服务自启动的几种方式
  • 原文地址:https://www.cnblogs.com/mcshell/p/5655810.html
Copyright © 2020-2023  润新知