api.zabbix.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Class ZabbixAPI
  4. *
  5. * Zabbix 3.xx API implementation
  6. */
  7. class ZabbixAPI {
  8. /**
  9. * Placeholder for $this->ubConfig object
  10. *
  11. * @var null
  12. */
  13. protected $ubConfig = null;
  14. /**
  15. * Zabbix host URL/IP
  16. *
  17. * @var string
  18. */
  19. protected $apiHostURL = '';
  20. /**
  21. * Zabbix login
  22. *
  23. * @var string
  24. */
  25. protected $authLogin = '';
  26. /**
  27. * Zabbix password
  28. *
  29. * @var string
  30. */
  31. protected $authPasswd = '';
  32. /**
  33. * Zabbix connection token for communication after successful auth
  34. *
  35. * @var
  36. */
  37. protected $authToken = '';
  38. public function __construct() {
  39. global $ubillingConfig;
  40. $this->ubConfig = $ubillingConfig;
  41. $this->apiHostURL = ($this->ubConfig->getAlterParam('ZABBIX_HOST_URL')) ? rtrim($this->ubConfig->getAlterParam('ZABBIX_HOST_URL'), '/') . '/api_jsonrpc.php' : '';
  42. $this->authLogin = ($this->ubConfig->getAlterParam('ZABBIX_LOGIN')) ? $this->ubConfig->getAlterParam('ZABBIX_LOGIN') : '';
  43. $this->authPasswd = ($this->ubConfig->getAlterParam('ZABBIX_PASSWD')) ? $this->ubConfig->getAlterParam('ZABBIX_PASSWD') : '';
  44. $this->getConnectionToken();
  45. }
  46. /**
  47. * Connects to Zabbix host and returns a token string if successful
  48. *
  49. * @return string
  50. */
  51. public function getConnectionToken() {
  52. if (!empty($this->apiHostURL) and !empty($this->authLogin) and !empty($this->authPasswd)) {
  53. $authJSON = '{
  54. "jsonrpc": "2.0",
  55. "method": "user.login",
  56. "params": {
  57. "user": "' . $this->authLogin . '",
  58. "password": "' . $this->authPasswd . '"
  59. },
  60. "id": 0,
  61. "auth": null
  62. }';
  63. $curl = curl_init();
  64. curl_setopt($curl, CURLOPT_POST, true);
  65. curl_setopt($curl, CURLOPT_URL, $this->apiHostURL);
  66. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json-rpc; charset=utf-8", "Cache-Control: no-cache"));
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, $authJSON);
  69. $curlResult = curl_exec($curl);
  70. curl_close($curl);
  71. if (!empty($curlResult)) {
  72. $authArr = json_decode($curlResult, true);
  73. $this->authToken = $authArr['result'];
  74. }
  75. }
  76. return ($this->authToken);
  77. }
  78. /**
  79. * Runs a data query to Zabbix host and returns result as a JSON string
  80. *
  81. * @param $apiMethod
  82. * @param array|string $methodParams
  83. * @param string $authToken
  84. * @param int $reqID
  85. *
  86. * @return string
  87. */
  88. public function runQuery($apiMethod, $methodParams = array(), $authToken = '', $reqID = 0) {
  89. $reqResult = '';
  90. $reqParams = (is_array($methodParams)) ? json_encode($methodParams) : $methodParams;
  91. $authToken = (empty($authToken)) ? $this->authToken : $authToken;
  92. $requestJSON = '{
  93. "jsonrpc": "2.0",
  94. "method": "' . $apiMethod . '",
  95. "params": ' . $reqParams . ',
  96. "auth": "' . $authToken . '",
  97. "id": ' . $reqID . '
  98. }';
  99. $curl = curl_init();
  100. curl_setopt($curl, CURLOPT_POST, true);
  101. curl_setopt($curl, CURLOPT_URL, $this->apiHostURL);
  102. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json-rpc; charset=utf-8", "Cache-Control: no-cache"));
  103. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  104. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestJSON);
  105. $curlResult = curl_exec($curl);
  106. curl_close($curl);
  107. if (!empty($curlResult)) {
  108. $reqResult = $curlResult;
  109. }
  110. return ($reqResult);
  111. }
  112. /**
  113. * Auth token getter
  114. *
  115. * @return string
  116. */
  117. public function getAuthToken() {
  118. return ($this->authToken);
  119. }
  120. }