-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_sensor.php
executable file
·154 lines (136 loc) · 3.73 KB
/
check_sensor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/php
<?php
function usage() {
echo "Usage: check_sensor.php --sensor <sensor> --outdated <seconds>\n";
echo "--sensor and --outdated can be abbreviated to -s and -o respectively.\n";
die(3);
}
if($argc != 3 && $argc != 5) {
usage();
}
if($argv[1] != '--sensor' && $argv[1] != '-s') {
usage();
}
$sensor_id = $argv[2];
if(!preg_match('/^[0-9]+/', $sensor_id)) {
echo "Invalid sensor ID: $sensor_id\n";
die(3);
}
$config_file = 'config.properties';
if($argc > 3 && $argv[3] != '--outdated' && $argv[3] != '-o') {
usage();
}
else if($argc > 3) {
$outdated = $argv[4];
if(!preg_match('/^[0-9]+$/', $outdated)) {
usage();
}
}
else {
$outdated = -1;
}
chdir(dirname(__FILE__));
require_once('common.php');
if($outdated == -1) {
$outdated = $config['value_outdated_period'];
}
$query = 'SELECT id, name, format, decimals FROM sensor_values';
$data = db_query($query, array(), 86400);
$value_ids = array();
foreach($data as $row) {
$value_ids[$row['id']] = $row;
}
$state = 0;
$message = '';
$query = 'SELECT sensor, description, display_name FROM sensors WHERE id = ? ORDER BY id DESC LIMIT 0, 1';
$data = db_query($query, array($sensor_id), 86400);
if(count($data) == 0) {
echo "No data for sensor with ID $sensor\n";
die(3);
}
$sensor = $data[0]['sensor'];
if($data[0]['display_name']) {
$sensor_description = $data[0]['display_name'];
}
else if($data[0]['description']) {
$sensor_description = $data[0]['description'];
}
else {
$sensor_description = "Sensor $sensor";
}
$query = 'SELECT value, low_warn, low_crit, high_warn, high_crit FROM sensor_limits WHERE sensor = ?';
$data = db_query($query, array($sensor_id), 86400);
$limits = array();
foreach($data as $row) {
$limits[$row['value']] = $row;
}
$count = count($value_ids);
$query = "SELECT UNIX_TIMESTAMP(timestamp) timestamp, what, value FROM sensor_cache WHERE sensor = ? ORDER BY timestamp DESC LIMIT 0, $count";
$db_data = db_query($query, array($sensor_id));
$data = array();
$timestamps = array();
foreach($db_data as $row) {
$timestamp = $row['timestamp'];
$what = $row['what'];
$value = $row['value'];
if(!isset($data[$what])) {
$data[$what] = $value;
$timestamps[$what] = $timestamp;
}
}
if(count($data) == 0) {
echo "No data for '$sensor_description'.\n";
die(3);
}
if(count($data) != count($limits)) {
$missing = implode(', ', array_map(function($key) use ($value_ids) { return $value_ids[$key]['name']; }, array_keys(array_diff_key($limits, $data))));
echo "Missing data for value(s): $missing\n";
die(3);
}
$timestamp_warning = false;
$max_state = 0;
foreach($timestamps as $timestamp) {
if(time()-$timestamp > $outdated && !$timestamp_warning) {
$timestamp_warning = true;
$message = "$sensor_description - no recent data";
$max_state = 3;
}
}
if(!$timestamp_warning) {
ksort($data);
$message = "$sensor_description - ";
$parts = array();
foreach($data as $what => $item) {
if(!isset($limits[$what])) {
echo "$sensor_description - Missing limits for sensor value $what sensor with ID $sensor_id\n";
die(3);
}
$name = $value_ids[$what]['name'];
$value = str_replace('%s', round($item, $value_ids[$what]['decimals']), $value_ids[$what]['format']);
$state = 0;
if($item < $limits[$what]['low_crit']) {
$state = 2;
}
else if($item < $limits[$what]['low_warn']) {
$state = 1;
}
else if($item > $limits[$what]['high_crit']) {
$state = 2;
}
else if($item > $limits[$what]['high_warn']) {
$state = 1;
}
switch ($state) {
case 0: $state_string = 'OK'; break;
case 1: $state_string = 'WARNING'; break;
case 2: $state_string = 'CRITICAL'; break;
}
$parts[] = "$name: $state_string ($value)";
if ($state > $max_state) {
$max_state = $state;
}
}
$message .= implode('; ', $parts);
}
echo "$message\n";
die($max_state);