zoukankan      html  css  js  c++  java
  • perl 批量添加zabbix

    <pre name="code" class="python">#!/usr/bin/perl
    use JSON::RPC::Client;
    use Data::Dumper;
    
    # Authenticate yourself
    if ( $#ARGV < 1 ) {
        print "please input your host  and ip  !
    ";
        exit(-1);
    }
    my $client = new JSON::RPC::Client;
    my $url    = 'http://192.168.32.83/zabbix/api_jsonrpc.php';
    my $authID;
    my $response;
    
    my $json = {
        jsonrpc => "2.0",
        method  => "user.login",
        params  => {
            user     => "admin",
            password => "zabbix"
        },
        id => 1
    };
    
    $response = $client->call( $url, $json );
    print "-----------------
    ";
    print $response->content->{result} . "
    ";
    
    # Check if response was successful
    die "Authentication failed
    " unless $response->content->{'result'};
    
    $authID = $response->content->{'result'};
    print "Authentication successful. Auth ID: " . $authID . "
    ";
    
    # Get list of all hosts using authID
    
    $json = {
        jsonrpc => '2.0',
        method  => 'host.get',
        params  => {
            output => [ 'hostid', 'name' ],    # get only host id and host name
            sortfield => 'name',               # sort by host name
        },
        id   => 2,
        auth => "$authID",
    };
    $response = $client->call( $url, $json );
    
    # Check if response was successful
    die "host.get failed
    " unless $response->content->{'result'};
    
    print "List of hosts
    ———————–
    ";
    foreach my $host ( @{ $response->content->{result} } ) {
        print "Host ID: " . $host->{hostid} . " Host: " . $host->{name} . "
    ";
    }
    
    $json = {
        "jsonrpc" => "2.0",
        "method"  => "host.create",
        "params"  => {
            "host"       => "$ARGV[0]",
            "interfaces" => [
                {
                    "type"  => 1,
                    "main"  => 1,
                    "useip" => 1,
                    "ip"    => "$ARGV[1]",
                    "dns"   => "",
                    "port"  => "10050"
                }
            ],
            "groups"    => [ { "groupid"    => "11" } ],
            "templates" => [ { "templateid" => "10001" } ]
        },
        "auth" => "$authID",
        "id"   => 1
    };
    $response = $client->call( $url, $json );
    my $var = $response->content->{'result'};
    
    #print $response->content->{result};
    print "$var is $var
    ";
    die "host.get failed
    " unless $response->content->{result};


    
    
    
    
    
                                        
    
  • 相关阅读:
    【代码笔记】Web-CSS-CSS Display
    【代码笔记】Web-CSS-CSS 分组和嵌套
    【代码笔记】Web-CSS-CSS Padding(填充)
    【代码笔记】Web-CSS-CSS Margin(外边距)
    【代码笔记】Web-CSS-CSS Border(边框)
    【代码笔记】Web-CSS-CSS盒子模型
    【代码笔记】Web-CSS-CSS Table(表格)
    【代码笔记】Web-CSS-CSS样式列表(url)
    【代码笔记】Web-CSS-CSS 链接(link)
    【代码笔记】Web-CSS-CSS Fonts(字体)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351112.html
Copyright © 2011-2022 走看看