您的位置:首页 > 编程语言 > Python开发

python zabbix 添加主机

2017-12-28 16:50 183 查看
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 "-----------------\n";
print $response->content->{result} . "\n";

# Check if response was successful
die "Authentication failed\n" unless $response->content->{'result'};

$authID = $response->content->{'result'};
print "Authentication successful. Auth ID: " . $authID . "\n";

# 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\n" unless $response->content->{'result'};

print "List of hosts\n";
foreach my $host ( @{ $response->content->{result} } ) {
print "Host ID: " . $host->{hostid} . " Host: " . $host->{name} . "\n";
}

$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 "----------------------\n";

if ( $response->content->{result}){print "print $host added success\n";
$c->render(text =>  "$host added success" )}
else {print $response->content->{error}->{data};
print "\n";
$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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: