-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors_exporter.php
91 lines (84 loc) · 2.67 KB
/
sensors_exporter.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
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Worker("http://0.0.0.0:9102");
$http_worker->count = 1;
$http_worker->onMessage = function (TcpConnection $connection, Request $request) {
switch ($request->path()) {
case '/metrics':
$s = new CrazySensorsExporter();
$connection->close($s->return);
break;
default:
$connection->close(new Response(404, [], '404 not found'));
}
};
class CrazySensorsExporter
{
public $return = '';
private $_template = "# HELP %s TEMP\n# TYPE %s gauge\n";
private $sensors = array();
private function Output()
{
foreach ($this->sensors as $s) {
$label_now = 'crazy_sensor_' . $s['name'];
$this->return .= sprintf($this->_template, $label_now, $label_now);
foreach ($s['temp'] as $s1) {
$this->return .= sprintf(
'%s{name="%s"} %s' . "\n",
$label_now,
$s1['name'],
$s1['temp']
);
}
}
}
private function ReadTemp()
{
$root = '/sys/class/hwmon/';
$list = scandir($root);
unset($list[0], $list[1]);
foreach ($list as $hw) {
$path = $root . $hw;
$this->sensors[$hw]['name'] = $this->TrimRead($path . '/name');
$this->sensors[$hw]['temp'] = [];
$i = 1;
while (true) {
$temp_now = sprintf($path . '/temp%s_input', $i);
$label_now = sprintf($path . '/temp%s_label', $i);
if (is_readable($label_now)) {
$this->sensors[$hw]['temp'][$i] = [
'name' => $this->TrimRead($label_now),
'temp' => $this->TempRead($temp_now),
];
$i++;
} else if (is_readable($temp_now)) {
$this->sensors[$hw]['temp'][$i] = [
'name' => 'temp' . $i,
'temp' => $this->TempRead($temp_now),
];
$i++;
} else {
break;
}
}
}
}
private function TrimRead($file)
{
return trim(file_get_contents($file));
}
private function TempRead($file)
{
return (float) trim(file_get_contents($file)) / 1000;
}
public function __construct()
{
$this->ReadTemp();
$this->Output();
}
}
Worker::runAll();