-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward.php
61 lines (50 loc) · 1.61 KB
/
forward.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
#!/usr/bin/env php
<?php
/**
* Usage: php forward.php host port [namespace]
*/
require __DIR__ . '/vendor/autoload.php';
if (empty($argv[1]) || empty($argv[2])) {
echo 'Destination host and port are not provided' . PHP_EOL;
exit(1);
}
$host = $argv[1];
$port = $argv[2];
$namespace = empty($argv[3]) ? '' : $argv[3];
msg("forward started, destination: {$host}:{$port}, namespace: {$namespace}");
$stdin = file_get_contents('php://stdin');
//$stdin = 'counts.foo|11.000000|1458126040';
$stdinLines = array_filter(explode(PHP_EOL, trim($stdin)));
if ($stdinLines) {
msg("metrics to send:" . count($stdinLines));
$connection = new \Domnikl\Statsd\Connection\TcpSocket($host, $port);
$statsd = new \Domnikl\Statsd\Client($connection, $namespace);
$statsd->startBatch();
foreach ($stdinLines as $line) {
list($typeKey, $value, $timestamp) = explode('|', $line);
list($type, $key) = explode('.', $typeKey, 2);
switch ($type) {
case 'gauges':
$value = (float)$value;
$statsd->gauge($key, $value);
msg("gauge {$key}={$value}" . PHP_EOL);
break;
case 'counts':
$value = (float)$value;
$statsd->count($key, $value);
msg("count {$key}={$value}");
break;
default:
//for now we support only gauges and counters
}
}
$statsd->endBatch();
msg("process finished successfully");
} else {
msg("nothing to send");
}
exit(0);
function msg($msg)
{
echo date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
}