-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix.php
110 lines (88 loc) · 3.54 KB
/
zabbix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Usage: php zabbix.php executable zabbixHost zabbixPort metricsHostname zabbixApiUrl metricsNamespace
*/
require __DIR__ . '/vendor/autoload.php';
if (empty($argv[1]) || empty($argv[2]) || empty($argv[3]) || empty($argv[4]) || empty($argv[5]) || empty($argv[6])) {
echo 'Required arguments not provided' . PHP_EOL;
echo 'Example: php statsite-sink-zabbix.phar /usr/bin/zabbix_sender zabbixHost 10051 metricsHostname http://user:password@zabbixhost/api metricsNamespace' . PHP_EOL;
exit(1);
}
$executable = $argv[1];
$host = $argv[2];
$port = $argv[3];
$metricsHostname = $argv[4];
$zabbixApiUrl = $argv[5];
$namespace = $argv[6];
$debugMode = in_array('--debug', $argv, true);
//start
msg("Push to zabbix started, destination: {$executable} {$host}:{$port}, metrics hostname: {$metricsHostname}, " .
"namespace: {$namespace}, API endpoint: {$zabbixApiUrl}");
//normalize API url
//$zabbixApiUrl = 'http://admin:zabbix:192.168.99.100:32779/zabbix/api_jsonrpc.php';
$zabbixApiUrlParts = parse_url($zabbixApiUrl);
$zabbixApiWrapper = new \StatsiteSinks\ZabbixApiWrapper(
combineUrlWithoutAuth($zabbixApiUrlParts),
$zabbixApiUrlParts['user'],
$zabbixApiUrlParts['pass']
);
if ($debugMode) {
$stdin = 'counts.test99|' . (int)rand(1,100) . '|1458126040';
} else {
$stdin = file_get_contents('php://stdin');
}
$stdinLines = array_filter(explode(PHP_EOL, trim($stdin)));
if ($stdinLines) {
msg("Metrics to send:" . count($stdinLines));
$zabbixFileContent = '';
foreach ($stdinLines as $line) {
list($typeKey, $value, $timestamp) = explode('|', $line);
list($type, $key) = explode('.', $typeKey, 2);
switch ($type) {
case 'gauges':
case 'counts':
$zabbixKey = "{$namespace}.{$key}.{$type}";
$zabbixApiWrapper->ensureZabbixItem($metricsHostname, $zabbixKey);
$value = (float)$value;
$line = "{$metricsHostname} {$zabbixKey} {$value}";
$zabbixFileContent .= $line . PHP_EOL;
msg($line);
break;
default:
//for now we support only gauges and counters
}
}
$zabbixFilePath = sys_get_temp_dir() . '/zabbix.txt';
file_put_contents($zabbixFilePath, $zabbixFileContent);
msg('Metrics to send: ' . str_replace(PHP_EOL, ';', $zabbixFileContent));
$cmd = "{$executable} -vv --zabbix-server {$host} --port {$port} --input-file {$zabbixFilePath}";
msg("Executing: {$cmd}");
exec($cmd);
$pipes = array();
$proc = proc_open($cmd, array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
$stdout = trim(stream_get_contents($pipes[1]));
fclose($pipes[1]);
$stderr = trim(stream_get_contents($pipes[2]));
fclose($pipes[2]);
proc_close($proc);
msg("stdout: {$stdout}");
msg("stderr: {$stderr}");
msg("Process finished successfully");
} else {
msg("No metrics to send");
}
exit(0);
function msg($msg)
{
echo date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
}
function combineUrlWithoutAuth(array $parsedUrl)
{
$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : '';
$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
$port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
$query = isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '';
$fragment = isset($parsedUrl['fragment']) ? '#' . $parsedUrl['fragment'] : '';
return "$scheme$host$port$path$query$fragment";
}