Skip to content

Commit 59d50f1

Browse files
author
Alexis Mousset
committed
Adding source files
1 parent 502e520 commit 59d50f1

5 files changed

+268
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*~
2+

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Zabbix plugin for PHP Network Weathermap
2+
3+
This plugin inegrates Zabbix into PHP Weathermap, using Zabbix API.
4+
5+
## Installation
6+
7+
Copy the content of the `lib` folder into the `lib` folder of your PHP Weathermap installation.
8+
9+
### Configuration of the new datasource for traffic
10+
11+
You have to configure the following parameters in yoour weathermap file :
12+
13+
* `SET zabbix_user username` : Zabbix username
14+
* `SET zabbix_password password` : Zabbix password
15+
* `SET zabbix_url http://zabbix/zabbix/api_jsonrpc.php` : the URL to the Zabbix API
16+
* `SET zabbix_key name` : the attributes you want to use to select items in the configuration
17+
18+
You can connect with a guest account by ommitting the password parameter :
19+
20+
```SET zabbix_user guest```
21+
22+
### Configuration of the Zabbix graphs integration
23+
24+
You have to configure the following parameters in yoour weathermap file :
25+
26+
* `SET post_zabbix_graphs 1` : enable the overlib graphs
27+
* `SET post_zabbix_graph_links 1` : enable the links to the graph page in Zabbix
28+
* `SET post_zabbix_graph_base_url https://zabbix/zabbix/chart2.php` : the URL to the Zabbix frontend
29+
* `SET post_zabbix_graph_width 420` : the graphs width in pixels
30+
* `SET post_zabbix_graph_height 150` : the graph height in pixels
31+
* `SET post_zabbix_graph_period 86400` : the graph period in seconds
32+
33+
## License
34+
35+
This plugins is licensed under

lib/SimpleZabbixApi.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
class SimpleZabbixApi {
3+
4+
private $apiUrl = "";
5+
private $authToken = "";
6+
7+
function __construct($apiUrl, $username, $password) {
8+
$this->apiUrl = $apiUrl;
9+
return $this->auth($username, $password);
10+
}
11+
12+
function isConnected() {
13+
return ($this->authToken != "");
14+
}
15+
16+
function jsonRequest($data) {
17+
18+
$jsonData = json_encode($data);
19+
$ch = curl_init();
20+
curl_setopt($ch, CURLOPT_URL, $this->apiUrl);
21+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
22+
curl_setopt($ch, CURLOPT_POST, 1);
23+
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
24+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
25+
$response = curl_exec($ch);
26+
27+
curl_close($ch);
28+
return json_decode($response, true);
29+
}
30+
31+
function request($method, $params) {
32+
$data = array(
33+
'jsonrpc' => '2.0',
34+
'method' => $method,
35+
'params' => $params,
36+
'id' => '2'
37+
);
38+
39+
if ($this->authToken != "") {
40+
$data['auth'] = $this->authToken;
41+
}
42+
return $this->jsonRequest($data);
43+
}
44+
45+
function auth($username, $password) {
46+
if (is_null($password)) {
47+
$login_params = array(
48+
'user' => $username
49+
);
50+
} else {
51+
$login_params = array(
52+
'user' => $username,
53+
'password' => $password
54+
);
55+
}
56+
57+
$result = $this->request("user.login", $login_params);
58+
59+
if (isset($result['result']) and $result['result'] != "") {
60+
$this->authToken = $result['result'];
61+
return true;
62+
} else {
63+
return false;
64+
}
65+
}
66+
67+
function getItemLastValue($host, $keyname, $key) {
68+
$params = array(
69+
"output"=> "extend",
70+
"filter"=> array(
71+
$keyname => $key
72+
),
73+
"host"=> $host
74+
);
75+
76+
$result = $this->request("item.get", $params);
77+
78+
if (isset($result['result']) and count($result['result']) == 1) {
79+
return $result['result'][0]['lastvalue'];
80+
} else {
81+
return null;
82+
}
83+
}
84+
85+
function getHostId($host) {
86+
$params = array(
87+
"filter"=> array(
88+
'limit' => 1
89+
),
90+
"host"=> $host
91+
);
92+
93+
$result = $this->request("item.get", $params);
94+
95+
if (isset($result['result']) and count($result['result']) > 0) {
96+
return $result['result'][0]['hostid'];
97+
} else {
98+
return null;
99+
}
100+
}
101+
102+
function getGraphId($host, $keyname, $key) {
103+
$params = array(
104+
"output"=> "extend",
105+
"filter"=> array(
106+
$keyname => $key
107+
),
108+
"hostids"=> array(
109+
$this->getHostId($host)
110+
)
111+
);
112+
113+
$result = $this->request("graph.get", $params);
114+
115+
if (isset($result['result']) and count($result['result']) == 1) {
116+
return $result['result'][0]['graphid'];
117+
} else {
118+
return null;
119+
}
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
// Zabbix pluggable datasource for PHP Weathermap 0.9
3+
// - reads a pair of values from the JSON API
4+
5+
// TARGET zabbix:host:in:out
6+
7+
require_once(__DIR__."/../SimpleZabbixApi.php");
8+
9+
class WeatherMapDataSource_zabbix extends WeatherMapDataSource {
10+
11+
private $zabbixApi;
12+
13+
function Init(&$map)
14+
{
15+
$this->zabbixApi = new SimpleZabbixApi($map->get_hint('zabbix_url'), $map->get_hint('zabbix_user'), $map->get_hint('zabbix_password'));
16+
return $this->zabbixApi->isConnected();
17+
}
18+
19+
function Recognise($targetstring)
20+
{
21+
if(preg_match("/^zabbix:([-a-zA-Z0-9_\.\/]+):([-a-zA-Z0-9_\.\/]+):([-a-zA-Z0-9_\.\/]+)$/", $targetstring, $matches))
22+
{
23+
return true;
24+
}
25+
else
26+
{
27+
return false;
28+
}
29+
}
30+
31+
function ReadData($targetstring, &$map, &$item)
32+
{
33+
$data[IN] = null;
34+
$data[OUT] = null;
35+
$data_time = 0;
36+
37+
if(preg_match("/^zabbix:([-a-zA-Z0-9_\.\/]+):([-a-zA-Z0-9_\.\/]+):([-a-zA-Z0-9_\.\/]+)$/", $targetstring, $matches))
38+
{
39+
$zabbix_uri = $map->get_hint('zabbix_uri');
40+
$zabbix_key = $map->get_hint('zabbix_key');
41+
42+
$host = $matches[1];
43+
$in = $matches[2];
44+
$out = $matches[3];
45+
46+
debug ("Zabbix ReadData: Found (".$host.",".$in.",".$out.")\n");
47+
48+
$in_value = $this->zabbixApi->getItemLastValue($host, $zabbix_key, $in);
49+
$out_value = $this->zabbixApi->getItemLastValue($host, $zabbix_key, $out);
50+
51+
if (isset($in_value)) {
52+
$data[IN] = $in_value;
53+
}
54+
if (isset($out_value)) {
55+
$data[OUT] = $out_value;
56+
}
57+
58+
$data_time = time();
59+
}
60+
61+
debug("Zabbix ReadData: Returning (".($data[IN]===null?'null':$data[IN]).",".($data[OUT]===null?'null':$data[IN]).",$data_time)\n");
62+
63+
return (array($data[IN], $data[OUT], $data_time));
64+
}
65+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class WeatherMapPostProcessorZabbix extends WeatherMapPostProcessor
2+
{
3+
function run(&$map)
4+
{
5+
$enable = $map->get_hint("post_zabbix_graphs");
6+
7+
if($enable)
8+
{
9+
$this->zabbixApi = new SimpleZabbixApi($map->get_hint('zabbix_url'), $map->get_hint('zabbix_user'), $map->get_hint('zabbix_password'));
10+
if ($this->zabbixApi->isConnected()) {
11+
$keyname = $map->get_hint('post_zabbix_key');
12+
$baseUrl = $map->get_hint("post_zabbix_graph_base_url");
13+
$addGraphLink = $map->get_hint("post_zabbix_graph_link");
14+
$graphWidth = $map->get_hint("post_zabbix_graph_width");
15+
$graphHeight = $map->get_hint("post_zabbix_graph_height");
16+
$graphPeriod = $map->get_hint("post_zabbix_graph_period");
17+
18+
foreach ($map->links as $link) {
19+
foreach (range(0, 1) as $k) {
20+
$graph = $link->overliburl[$k];
21+
22+
if (count($graph) == 1) {
23+
if(preg_match("/^overliburl:([-a-zA-Z0-9_\.\/]+):([-a-zA-Z0-9_\.\/]+)$/", $graph[0], $matches))
24+
{
25+
$host = $matches[1];
26+
$key = $matches[2];
27+
28+
debug ("Zabbix ReadData: Found (".$host.",".$key.")\n");
29+
30+
$graphId = $this->zabbixApi->getGraphId($host, $keyname, $key);
31+
if (isset($graphId)) {
32+
if ($addGraphLink) {
33+
$link->infourl[$k] = $baseUrl."/charts.php?form_refresh=1&fullscreen=0&graphid=".$graphId;
34+
}
35+
$link->overliburl[$k][0] = $baseUrl."/chart2.php?width=".$graphWidth."&height=".
36+
$graphHeight."&period=".$graphPeriod."&stime=now&graphid=".$graphId;
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)