-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_apc
executable file
·174 lines (163 loc) · 4.97 KB
/
check_apc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/php -q
<?php
/**
* Copyright: 2012 Joel Koglin
* apcmon is distributed under the terms of the GPLv3
* This file is part of apcmon
*/
//TODO include server and client versions and report them to the user on errors
//TODO add more debug code
//TODO verify file_get_contents follows 302's and other redirects
//TODO use config array instead of variables
//TODO add a verbosity level to debugs instead of just all or none.
$options = getopt("H:P:C:s::p:w:c:d::");
#Defaults
$host = '127.0.0.1';
$check = 'expunges';
$ssl = 0;
$path = '/apc_mon/';
$port = 80;
$warning = -1;
$critical = -1;
$debug = 0;
#return code : 0=OK, 1=WARN, 2=CRIT, 3=UNKNOWN
$return_code = 3;
$version = '0.3';
$output = 'UNKNOWN';
if (count($argv) === 1 || $argv[1] == "help" || $argv[1] == "--help" || $argv[1] == "-h") {
print("usage: check_apc [-H hostname] [-P path] [-C check] [-s] [-p int] -w int -c int [-d]\n");
print(" -H (hostname): used to build a URI with the other options. default 127.0.0.1\n");
print(" -P (path): used to build a URI with the other options. default /apc_mon/\n");
print(" -C (check): any defined plugin. Anything invalid will return a list of valid keys. default expunges\n");
print(" -s (ssl): enables SSL. It will automatically change the port to 443 however if one is specified that one will be used instead. default disabled\n");
print(" -p (port): the port to use, default 80\n");
print(" -w (warning): low level of cache use that will raise a warning. default 30\n");
print(" -c (critical): low level of cache use that will raise a critical. default 10\n");
print(" -d: enables debug output\n");
print("\n");
exit(3);
}
if (isset($options["H"]) && !is_null($options["H"])) {
$host = $options["H"];
}
if (isset($options["C"]) && !is_null($options["C"])) {
$check = $options["C"];
}
if (isset($options["P"]) && !is_null($options["P"])) {
$path = $options["P"];
}
if (isset($options["s"])) {
$ssl = 1;
$port = 443;
}
if (isset($options["p"]) && !is_null($options["p"])) {
$port = $options["p"];
}
if (isset($options["w"]) && !is_null($options["w"])) {
$warning = $options["w"];
}
if (isset($options["c"]) && !is_null($options["c"])) {
$critical = $options["c"];
}
if (isset($options["d"])) {
$debug = 1;
}
if ($debug) {
ini_set('display_errors', 1);
error_reporting(E_ALL);
} else {
ini_set('display_errors', 0);
}
if ($warning == '-1' || $critical == '-1') {
print "Error: warning and critical must be set.\n";
exit(3);
}
//determine appropriate comparison operator
if ($critical > $warning) {
$op = '>';
} else if ($critical < $warning) {
$op = '<';
} else {
print "Error: warning and critical being set to the same value is not currently supported.\n";
exit(3);
}
//query apc_mon server side for requested variable
$protocol = $ssl ? 'https://' : 'http://';
$uri = $protocol . $host . ':' . $port . $path . '?check=' . $check;
if ($debug) {
print "Connecting to: $uri\n";
}
$results = file_get_contents($uri);
//make sure we got something back
if ($results) {
$result = unserialize($results);
//if its an error, let the user know and exit
if ($result['error']) {
print "${result['error_message']}\n";
exit(3);
} else {
if ($debug) {
print_r($result);
print "\n";
}
$count = count($result['data']);
if ($debug) {
print "Count: $count\n";
}
if ($count === 1) {
$data = array_shift($result['data']);
} else {
die("Multiple aggregated request are not implemented yet. Give me a use case and let me know you want them.\n");
}
if ($debug) {
print "Data:\n";
print_r($data);
print "\n";
}
//prefix output
//todo let plugins implement this?
$output = "$check ";
//TODO don't build output incrementally, use separate variables and concatenate at the end
//TODO figure out output status by return code value instead of setting it here.
if ($op == '>') {
if ($data >= $critical) {
$output .= "CRITICAL -";
$return_code = 2;
} else if ($data >= $warning) {
$output .= "WARN -";
$return_code = 1;
} else if ($data < $warning) {
$output .= "OK -";
$return_code = 0;
}
} else if ($op == '<') {
if ($data <= $critical) {
$output .= "CRITICAL -";
$return_code = 2;
} else if ($data <= $warning) {
$output .= "WARN -";
$return_code = 1;
} else if ($data > $warning) {
$output .= "OK -";
$return_code = 0;
}
}
//in all cases add result data to the output
//TODO make it so each plugin on the server can specify a name to use
//deprecated will change soon
switch ($check) {
case 'hit_percent':
$output .= " HITS ${data}%";
break;
default:
$output .= " $check=$data\n";
break;
}
}
} else {
//if there was no response from the remote server
print $debug ? "$uri did not send back a response\n" : "No Response";
exit(3);
}
print "$output\n";
exit($return_code);