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
相关文章推荐
- python调用zabbix的api接口添加主机、查询组、主机、模板
- python调用zabbix的api接口添加主机、查询组、主机、模板
- python调用zabbix api 实现主机添加等功能
- Python调用zabbix API批量添加主机 (读取Excel)
- zabbix自动添加删除主机的python脚本
- Zabbix添加主机监控及自定义item监控
- zabbix如何添加主机监控
- 企业级监控软件zabbix搭建部署之zabbix Services添加主机
- Python一键添加Zabbix监控脚本
- zabbix添加windows系统主机
- python 一键添加 zabbix 监控脚本 推荐
- zabbix 添加被监控主机
- Zabbix添加监控主机 Zabbix
- Zabbix(二)之添加主机、item、triggers、Graph、Medias
- Linux监控平台——搭建zabbix监控平台,zabbix监控主动模式、被动模式、添加监控主机、添加自定义模板、处理图形乱码、自动发现
- zabbix 添加监控主机和相关配置
- zabbix监控主动模式、被动模式、添加监控主机、添加自定义模板、处理图形乱码、自动发现
- Zabbix添加windows服务器&主机
- zabbix的使用自动发现和api添加主机
- Python结合Zabbix模板的自动发现功能监控RabbitMQ队列是 5b4 否正常运行并自动添加触发器