zbx.inc.php
-
<?php
-
class jsonrpc{
-
-
protected function connect($server, $query){
-
$http = curl_init($server);
-
curl_setopt($http, CURLOPT_CUSTOMREQUEST, 'POST');
-
curl_setopt($http, CURLOPT_POSTFIELDS, $query);
-
curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);
-
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, FALSE);
-
/* curl_setopt($http, CURLOPT_PROXY, 'proxy_url');
-
curl_setopt($http, CURLOPT_PROXYPORT, '3128');
-
curl_setopt($http, CURLOPT_PROXYUSERPWD, 'login:pass'); */
-
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, FALSE);
-
curl_setopt($http, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
-
$response = curl_exec($http);
-
return json_decode($response, true);
-
curl_close($http);
-
}
-
-
}
-
-
-
class zbx extends jsonrpc{
-
-
public $method;
-
public $access_token;
-
public $url;
-
public $query;
-
-
function call(){
-
$data['jsonrpc'] = '2.0';
-
$data['method'] = $this->method;
-
$data['params'] = $this->query;
-
$this->query = '';
-
if(!empty($this->access_token))
-
$data['auth'] = $this->access_token;
-
$data['id'] = rand(1,100);
-
$data = json_encode($data, JSON_PRETTY_PRINT);
-
return $this->connect($this->url, $data);
-
}
-
-
}
import.php
-
#!/opt/lampp/bin/php
-
<?php
-
require __DIR__ . '/zbx.inc.php';
-
-
$zbx = new zbx;
-
-
// 生成token
-
$zbx->url = "";
-
$zbx->method = 'user.login';
-
$zbx->query['user'] = 'Admin';
-
$zbx->query['password'] = 'zabbix';
-
$zbx->access_token = $zbx->call()['result'];
-
-
// 组ID
-
// $zbx->method = 'hostgroup.get';
-
// $zbx->query['output'] = 'extend';
-
// $zbx->query['filter']['name'] = 'Online_SwitchLoopCheck_STU_5';
-
// $groupid = $zbx->call()['result']['0']['groupid'];
-
//echo $groupid."\n";exit;
-
// echo "
";
-
-
// 查看主机是否存在
-
// $zbx->method = 'host.exists';
-
// $zbx->query['host'] = '10.105.0.11';
-
// $if = $zbx->call();
-
// var_dump($if);
-
-
// 模板ID
-
// $zbx->method = 'template.get';
-
// $zbx->query['output'] = 'extend';
-
// $zbx->query['filter']['host'] = 'Switch_H3C-S3100-26C-SI';
-
// $templateid = $zbx->call()['result']['0']['templateid'];
-
// print_r($templateid);
-
-
// 创建主机
-
// $zbx->method = 'host.create';
-
// $zbx->query['host'] = '10.105.0.11';
-
// $zbx->query['interfaces'] = [array(
-
// "type" => 2,
-
// "main" => 1,
-
// "useip" => 1,
-
// "ip" => "10.105.0.11",
-
// "dns" => "",
-
// "port" => "161"
-
// )];
-
// $zbx->query['groups']=[array('groupid' => "$groupid")];
-
// $zbx->query['templates']=[array('templateid' => "$templateid")];
-
// $create = $zbx->call();
-
// print_r($create);
-
-
//
-
// $zbx->method = 'proxy.get';
-
// $zbx->query['output'] = 'extend';
-
// $zbx->query['selectInterfaces'] = 'extend';
-
// $zbx->query['selectHosts'] = 'extend';
-
// $proxy = $zbx->call();
-
// print_r($proxy);
-
-
// $zbx->method = 'proxy.update';
-
// $zbx->query['proxyid'] = '0';
-
// $zbx->query['hosts'] = [array('hosts' => '12369')];
-
// $proxy = $zbx->call();
-
// print_r($proxy);
-
-
// 接收文件外部参数
-
$ip = $argv[1];
-
-
$xml = simplexml_load_file('/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml');
-
$xml->hosts->host->host = $ip;
-
$xml->hosts->host->name = $ip;
-
$xml->hosts->host->interfaces->interface->ip = $ip;
-
$xml->saveXML("/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml");
-
-
$xml ='/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml';
-
$f = fopen ($xml, "r");
-
$source = stream_get_contents($f);
-
fclose ($f);
-
-
$zbx->method = 'configuration.import';
-
$zbx->query['format'] = 'xml';
-
$zbx->query['rules']['groups'] = array(
-
"createMissing"=> true
-
);
-
$zbx->query['rules']['hosts'] = array(
-
"createMissing"=> true,
-
"updateExisting"=> true
-
);
-
$zbx->query['rules']['items'] = array(
-
"createMissing"=> true,
-
"updateExisting"=> true
-
);
-
$zbx->query['rules']['triggers'] = array(
-
"createMissing"=> true,
-
"updateExisting"=> true
-
);
-
$zbx->query['rules']['templateLinkage'] = array(
-
"createMissing"=> true
-
);$zbx->query['rules']['templates'] = array(
-
"createMissing"=> true,
-
"updateExisting"=> true
-
);
-
$zbx->query['source'] = "$source";
-
$import = $zbx->call()['result'];
-
// print_r($import);
-
if($import = 1){
-
echo "$ip host import secuess!\n";
-
}
S3100.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<zabbix_export>
-
<version>2.0</version>
-
<date>2014-11-05T03:51:39Z</date>
-
<groups>
-
<group>
-
<name>Online_SwitchLoopCheck_STU_8</name>
-
</group>
-
</groups>
-
<hosts>
-
<host>
-
<host>10.108.0.43</host>
-
<name>10.108.0.43</name>
-
<description/>
-
<proxy>
-
<name>32.236-Proxy</name>
-
</proxy>
-
<status>0</status>
-
<ipmi_authtype>-1</ipmi_authtype>
-
<ipmi_privilege>2</ipmi_privilege>
-
<ipmi_username/>
-
<ipmi_password/>
-
<templates>
-
<template>
-
<name>Switch_H3C-S3100-26C-SI</name>
-
</template>
-
</templates>
-
<groups>
-
<group>
-
<name>Online_SwitchLoopCheck_STU_8</name>
-
</group>
-
</groups>
-
<interfaces>
-
<interface>
-
<default>1</default>
-
<type>2</type>
-
<useip>1</useip>
-
<ip>10.108.0.43</ip>
-
<dns/>
-
<port>161</port>
-
<bulk>1</bulk>
-
<interface_ref>if1</interface_ref>
-
</interface>
-
</interfaces>
-
<applications/>
-
<items/>
-
<discovery_rules/>
-
<macros/>
-
<inventory/>
-
</host>
-
</hosts>
-
</zabbix_export>
Zabbix API
https://www.zabbix.com/documentation/2.4/manual/api/reference
阅读(2162) | 评论(0) | 转发(0) |