-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphiteSink.php
More file actions
59 lines (50 loc) · 1.21 KB
/
graphiteSink.php
File metadata and controls
59 lines (50 loc) · 1.21 KB
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
<?php
include('secret.php');
error_reporting(0);
$data = trim(file_get_contents('php://input'));
if(empty($data)) {
error_log("no content provided");
echo("no content provided");
http_response_code(400);
exit();
}
$metrics = explode("\n", $data);
$fp = fsockopen("tcp://127.0.0.1", 2003, $errno, $errstr);
if (!empty($errno) || !empty($errstr)) {
error_log(sprintf("%d - %s", $errno, $errstr));
http_response_code(500);
exit();
}
foreach($metrics as $data) {
if (strpos($data, $secretPrefix)===false) {
error_log("Not authorised: " . $data);
http_response_code(401);
exit();
} else {
$data = str_replace($secretPrefix, "", $data);
}
$pieces = explode(" ", trim($data));
if (count($pieces) == 2) {
$data = trim($data)." ".time().PHP_EOL;
} else {
$data = trim($data).PHP_EOL;
}
try {
$bytes = fwrite($fp, $data);
fflush($fp);
if($bytes == strlen($data)) {
echo $data;
} else {
error_log("Size does not match");
http_response_code(500);
exit();
}
} catch (Exception $e) {
error_log("Network error: ".$e->getMessage());
http_response_code(500);
exit();
}
}
fclose($fp);
http_response_code(202);
?>