Chinaunix首页 | 论坛 | 博客
  • 博客访问: 243574
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 414
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-22 11:22
个人简介

.....

文章分类

全部博文(32)

文章存档

2016年(1)

2015年(12)

2014年(19)

我的朋友

分类: 系统运维

2014-11-16 10:05:43

zbx.inc.php 

点击(此处)折叠或打开

  1. <?php
  2. class jsonrpc{
  3.     
  4.     protected function connect($server, $query){
  5.         $http = curl_init($server);
  6.         curl_setopt($http, CURLOPT_CUSTOMREQUEST, 'POST');
  7.         curl_setopt($http, CURLOPT_POSTFIELDS, $query);
  8.         curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);
  9.         curl_setopt($http, CURLOPT_SSL_VERIFYPEER, FALSE);
  10.         /* curl_setopt($http, CURLOPT_PROXY, 'proxy_url');
  11.         curl_setopt($http, CURLOPT_PROXYPORT, '3128');
  12.         curl_setopt($http, CURLOPT_PROXYUSERPWD, 'login:pass'); */
  13.         curl_setopt($http, CURLOPT_SSL_VERIFYHOST, FALSE);
  14.         curl_setopt($http, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  15.         $response = curl_exec($http);
  16.         return json_decode($response, true);
  17.         curl_close($http);
  18.     }
  19.     
  20. }


  21. class zbx extends jsonrpc{

  22.     public $method;
  23.     public $access_token;
  24.     public $url;
  25.     public $query;

  26.     function call(){
  27.         $data['jsonrpc'] = '2.0';
  28.         $data['method'] = $this->method;
  29.         $data['params'] = $this->query;
  30.         $this->query = '';
  31.         if(!empty($this->access_token))
  32.         $data['auth'] = $this->access_token;
  33.         $data['id'] = rand(1,100);
  34.         $data = json_encode($data, JSON_PRETTY_PRINT);
  35.         return $this->connect($this->url, $data);
  36.     }
  37.         
  38. }

import.php

点击(此处)折叠或打开

  1. #!/opt/lampp/bin/php
  2. <?php
  3. require __DIR__ . '/zbx.inc.php';

  4. $zbx = new zbx;

  5. // 生成token
  6. $zbx->url = "";
  7. $zbx->method = 'user.login';
  8. $zbx->query['user'] = 'Admin';
  9. $zbx->query['password'] = 'zabbix';
  10. $zbx->access_token = $zbx->call()['result'];

  11. // 组ID
  12. // $zbx->method = 'hostgroup.get';
  13. // $zbx->query['output'] = 'extend';
  14. // $zbx->query['filter']['name'] = 'Online_SwitchLoopCheck_STU_5';
  15. // $groupid = $zbx->call()['result']['0']['groupid'];
  16. //echo $groupid."\n";exit;
  17. // echo "
    ";


  18. // 查看主机是否存在
  19. // $zbx->method = 'host.exists';
  20. // $zbx->query['host'] = '10.105.0.11';
  21. // $if = $zbx->call();
  22. // var_dump($if);

  23. // 模板ID
  24. // $zbx->method = 'template.get';
  25. // $zbx->query['output'] = 'extend';
  26. // $zbx->query['filter']['host'] = 'Switch_H3C-S3100-26C-SI';
  27. // $templateid = $zbx->call()['result']['0']['templateid'];
  28. // print_r($templateid);

  29. // 创建主机
  30. // $zbx->method = 'host.create';
  31. // $zbx->query['host'] = '10.105.0.11';
  32. // $zbx->query['interfaces'] = [array(
  33.                             // "type" => 2,
  34.                             // "main" => 1,
  35.                             // "useip" => 1,
  36.                             // "ip" => "10.105.0.11",
  37.                             // "dns" => "",
  38.                             // "port" => "161"
  39.                         // )];
  40. // $zbx->query['groups']=[array('groupid' => "$groupid")];
  41. // $zbx->query['templates']=[array('templateid' => "$templateid")];
  42. // $create = $zbx->call();
  43. // print_r($create);

  44. //
  45. // $zbx->method = 'proxy.get';
  46. // $zbx->query['output'] = 'extend';
  47. // $zbx->query['selectInterfaces'] = 'extend';
  48. // $zbx->query['selectHosts'] = 'extend';
  49. // $proxy = $zbx->call();
  50. // print_r($proxy);

  51. // $zbx->method = 'proxy.update';
  52. // $zbx->query['proxyid'] = '0';
  53. // $zbx->query['hosts'] = [array('hosts' => '12369')];
  54. // $proxy = $zbx->call();
  55. // print_r($proxy);

  56. // 接收文件外部参数
  57. $ip = $argv[1];

  58. $xml = simplexml_load_file('/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml');
  59. $xml->hosts->host->host = $ip;
  60. $xml->hosts->host->name = $ip;
  61. $xml->hosts->host->interfaces->interface->ip = $ip;
  62. $xml->saveXML("/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml");

  63. $xml ='/opt/wwwroot2/ZbxImport/PHP/xml/S3100.xml';
  64. $f = fopen ($xml, "r");
  65. $source = stream_get_contents($f);
  66. fclose ($f);

  67. $zbx->method = 'configuration.import';
  68. $zbx->query['format'] = 'xml';
  69. $zbx->query['rules']['groups'] = array(
  70.                 "createMissing"=> true
  71. );
  72. $zbx->query['rules']['hosts'] = array(
  73.                 "createMissing"=> true,
  74.                 "updateExisting"=> true
  75. );
  76. $zbx->query['rules']['items'] = array(
  77.                 "createMissing"=> true,
  78.                 "updateExisting"=> true
  79. );
  80. $zbx->query['rules']['triggers'] = array(
  81.                 "createMissing"=> true,
  82.                 "updateExisting"=> true
  83. );
  84. $zbx->query['rules']['templateLinkage'] = array(
  85.                 "createMissing"=> true
  86. );$zbx->query['rules']['templates'] = array(
  87.                 "createMissing"=> true,
  88.                 "updateExisting"=> true
  89. );
  90. $zbx->query['source'] = "$source";
  91. $import = $zbx->call()['result'];
  92. // print_r($import);
  93. if($import = 1){
  94.     echo "$ip host import secuess!\n";
  95. }


S3100.xml


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <zabbix_export>
  3.     <version>2.0</version>
  4.     <date>2014-11-05T03:51:39Z</date>
  5.     <groups>
  6.         <group>
  7.             <name>Online_SwitchLoopCheck_STU_8</name>
  8.         </group>
  9.     </groups>
  10.     <hosts>
  11.         <host>
  12.             <host>10.108.0.43</host>
  13.             <name>10.108.0.43</name>
  14.             <description/>
  15.             <proxy>
  16.                 <name>32.236-Proxy</name>
  17.             </proxy>
  18.             <status>0</status>
  19.             <ipmi_authtype>-1</ipmi_authtype>
  20.             <ipmi_privilege>2</ipmi_privilege>
  21.             <ipmi_username/>
  22.             <ipmi_password/>
  23.             <templates>
  24.                 <template>
  25.                     <name>Switch_H3C-S3100-26C-SI</name>
  26.                 </template>
  27.             </templates>
  28.             <groups>
  29.                 <group>
  30.                     <name>Online_SwitchLoopCheck_STU_8</name>
  31.                 </group>
  32.             </groups>
  33.             <interfaces>
  34.                 <interface>
  35.                     <default>1</default>
  36.                     <type>2</type>
  37.                     <useip>1</useip>
  38.                     <ip>10.108.0.43</ip>
  39.                     <dns/>
  40.                     <port>161</port>
  41.                     <bulk>1</bulk>
  42.                     <interface_ref>if1</interface_ref>
  43.                 </interface>
  44.             </interfaces>
  45.             <applications/>
  46.             <items/>
  47.             <discovery_rules/>
  48.             <macros/>
  49.             <inventory/>
  50.         </host>
  51.     </hosts>
  52. </zabbix_export>

Zabbix API

https://www.zabbix.com/documentation/2.4/manual/api/reference


阅读(2162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~