-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_json.php
More file actions
69 lines (57 loc) · 1.3 KB
/
check_json.php
File metadata and controls
69 lines (57 loc) · 1.3 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
60
61
62
63
64
65
66
67
68
69
<?php
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__.'/inc/nagios.php');
// Command line parser
$parser = new Console_CommandLine(array(
'description' => 'Check JSON',
'version' => '0.0.2',
'force_posix' => true
));
$parser->addOption('url', array(
// 'short_name' => '-u',
'long_name' => '--url',
'description' => 'URL',
'action' => 'StoreString'
));
try {
$result = $parser->parse();
$url = $result->options['url'];
if (empty($url)) {
echo 'No URL specified'."\n";
exit(NAGIOS_UNKNOWN);
}
$data = @file_get_contents($url);
if (!$data || empty($data)) {
echo 'Couldn\'t retrieve data from URL'."\n";
exit(NAGIOS_UNKNOWN);
}
$data = json_decode($data, true);
if (!$data || empty($data)) {
echo 'Couldn\'t read JSON from URL'."\n";
exit(NAGIOS_UNKNOWN);
}
if (!isset($data['status']) && !isset($data['message'])) {
echo 'Invalid response from URL'."\n";
exit(NAGIOS_UNKNOWN);
}
$r = NAGIOS_UNKNOWN;
switch ($data['status']) {
case 'OK':
$r = NAGIOS_OK;
break;
case 'WARNING':
$r = NAGIOS_WARNING;
break;
case 'CRITICAL':
$r = NAGIOS_CRITICAL;
break;
default:
$r = NAGIOS_UNKNOWN;
break;
}
echo $data['message']."\n";
exit($r);
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
exit(NAGIOS_UNKNOWN);
}