python zabbix 添加主机

   if ($env eq "pro"){
  my $client = new JSON::RPC::Client;
my $url    = 'http://192.168.32.55/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"       => "$host",
        "interfaces" => [
            {
                "type"  => 1,
                "main"  => 1,
                "useip" => 1,
                "ip"    => "$ip",
                "dns"   => "",
                "port"  => "10050"
            }
        ],
        "groups"    => [ { "groupid"    => "$groupid" } ],
        "templates" => [ { "templateid" => "$templateid" } ]
    },
    "auth" => "$authID",
    "id"   => 1
};
$response = $client->call( $url, $json );
use Data::Dumper;

my $str = Dumper($response);
print $str;
print "----------------------
";
   
if ( $response->content->{result}){print "print $host added success
";
$c->render(text =>  "$host added success" )}
else {print $response->content->{error}->{data};
     print "
";
     $c->render(text =>  $response->content->{error}->{data} )}
  
  }



python:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import urllib
import urllib2
import json
import requests
import json
def http_post():
    url = 'http://192.168.137.3/zabbix/api_jsonrpc.php'
    values = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "admin",
        "password": "zabbix"
    },
    "id": 1
    }
    headers={"Content-Type":"application/json"}
    jdata = json.dumps(values)  # 对数据进行JSON格式化编码
    req = urllib2.Request(url, jdata,headers)  # 生成页面请求的完整数据
    response = urllib2.urlopen(req)  # 发送页面请求
    return response.read()  # 获取服务器返回的页面信息
def get_token():
   resp = http_post()
   #print resp
   #print type(resp)
   aa=json.loads(resp)
   #print aa
   #print type(aa)
   token=aa['result']
   return  token
def add_host(host,ip,groupid,templateid):
    host=host
    ip=ip
    groupid=groupid
    templateid=templateid
    token = get_token()
    url = 'http://192.168.137.3/zabbix/api_jsonrpc.php'
    values={
    "jsonrpc" : "2.0",
    "method"  : "host.create",
    "params"  : {
        "host"       : host,
        "interfaces" : [
            {
                "type"  :1,
                "main"  : 1,
                "useip" : 1,
                "ip"    : ip,
                "dns"   : "",
                "port"  : "10050"
            }
        ],
        "groups"    : [ { "groupid"  : groupid } ],
        "templates" : [ { "templateid" : templateid } ]
    },
    "auth" : token,
    "id" : 1
};
    headers = {"Content-Type": "application/json"}
    # jdata = json.dumps(values)  # 对数据进行JSON格式化编码
    # req = urllib2.Request(url, jdata, headers)  # 生成页面请求的完整数据
    # response = urllib2.urlopen(req)  # 发送页面请求
    # return response.read
    request = requests.post(url=url, headers=headers, data=json.dumps(values))
    dict = json.loads(request.content)
    return dict
a=add_host('aa-1.1.1.1','192.168.1.2','2','10001')
print a

原文地址:https://www.cnblogs.com/hzcya1995/p/13349372.html