Rev 1421 | Rev 1424 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1421 | Rev 1422 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | <?php /* <ViaThinkSoftSignature> |
- | |
2 | H8RS2FtzJ6oXxHoX7qyolomkUVb5hPPq71/+xoZIME1BY1QcCif7imp/jeGKoOezw |
- | |
3 | YhYVmKrS0zYZG04mhfGnrMez9NcvMrtqSbBBNXZ3dsGcjkYMYq8VVbvOW9A9MOoaw |
- | |
4 | vVSZydPls7FxszGfPmL1M9ebZmj03TUeGbBfE+kIiw+dAMsO+rjYZmi5khwZPb6nn |
- | |
5 | bP+6DVMqg1blhaESl2k0Eqhe7kX9NWhZ3y0QuxGj7nVm3gFojo02TQubgRr9l2f7z |
- | |
6 | 6QqS7jt7YSwQg2nP+3KVNBJZoXU/hTx3Bo/PaJG/9Dotp0/wmFHb6YqTkUHdP5DEu |
- | |
7 | M9LxOFEKNjrmPbVHPDxNPyTaoF06fI8rXI46XyUULkw1LXOUv8nzaoK3WXJzfO/74 |
- | |
8 | o/GOMEV01Aeq0ooTdyi/ktpS43T4sYA5qod66dKTUKwUTU8IgEp/GcZnugpvlXi0b |
- | |
9 | U1hfF5W32Rp3vbeh37edXCMRkOw+7svvFQle5JzC+xHloW3jbzRJvfDULAd6bUrbl |
- | |
10 | QAy6pOEA26Sjpi5pmo5qRu3LHHXiu9V3eYpPzl2orADnyOyKiqyj8Yvw3U/cvqxwq |
- | |
11 | YSSxXEPYxOQN+Cl/ncUOJeNv8C+AYBU9S5aSXwQWnIl3baVbS9NL4CW9Fy2AM1/Vf |
- | |
12 | uMBVz3NWclUFO6qPMoaCEdVsuUSbpo6/HDXGm5kr4Yzc/lv1Q3lUKQ+a5B3ZMAfhY |
- | |
13 | yMKdSPnUZwQhuCxaAjpMdkVf5s2qECgT/e1psSKCOqLd9fhWQuV1eHa9gxqBL7ClS |
- | |
14 | m1jy8TRbSSZ6v7yQ397GDU8iyKvXCpeBs+g6VXkFd5ZM2Ajgb+uIfzPD3qPfPC+nK |
- | |
15 | bhsMzDwBmu444dvGC9OCNLb3efZXLSxZ6wMG2MoA7c814qrEYFokTKeMiYKCGQkx5 |
- | |
16 | kgamyPtizgOpiis6XSKFs9GXSY0vT+q3rSCo+23xjNHPhaCjLmywDoeoeOX1tmf30 |
- | |
17 | Blh0RrNEXgmlMuVkXaHrKAepU0K2oXhw63bRQUzcZMY1rORoML6Rro6u0PI3mT7VQ |
- | |
18 | 6ZnnyxZgUljxpo6ZdP8XqE8oyR9P6dhmdN6xI3rYyrIDrPWsoRGCMQk/ej8eS8Ve7 |
- | |
19 | Y1Z3hC9njExmmrZrVoymMcJNDK99jBQCs49qXA+OuCRbfgU0B12FwEUy8xDnUlzY9 |
- | |
20 | t6b1fwHL2G25m3wMXzk0CHnQWOoAS+3zG+nlfF1RRbx0U3TC4O3Epy30zC/v/TvV7 |
- | |
21 | 73uEr/LLezls3KBddOI1FRkWC5CoERg9Bi4XoLFiZqpF/eOWsRaYzrwqv0d9sCgnU |
- | |
22 | RFzD9MdFjT3EiQDnlvbf9nTtN3Dpw1IaYFb/u/4AhjugS2p1XFIZpYP4U1J6wL5Vl |
- | |
23 | Q== |
- | |
24 | </ViaThinkSoftSignature> */ ?> |
- | |
25 | <?php |
1 | <?php |
26 | 2 | ||
27 | /* |
3 | /* |
28 | 4 | ||
29 | VNag - Nagios Framework for PHP (C) 2014-2023 |
5 | VNag - Nagios Framework for PHP (C) 2014-2023 |
Line 39... | Line 15... | ||
39 | 15 | ||
40 | */ |
16 | */ |
41 | 17 | ||
42 | /**************************************************************************************************** |
18 | /**************************************************************************************************** |
43 | 19 | ||
44 | Introduction: |
- | |
45 | - | ||
46 | VNag is a small framework for Nagios Plugin Developers who use PHP CLI scripts. |
- | |
47 | The main purpose of VNag is to make the development of plugins as easy as possible, so that |
20 | More information on how to develop your plugin, see doc/Plugin_Development.md |
48 | the developer can concentrate on the actual work. VNag will try to automate as much |
- | |
49 | as possible. |
- | |
50 | - | ||
51 | Please note that your script should include the +x chmod flag: |
- | |
52 | chmod +x myscript.php |
- | |
53 | - | ||
54 | Please see the the demo/ folder for a few examples how to use this framework. |
- | |
55 | - | ||
56 | Arguments: |
- | |
57 | - | ||
58 | Example: |
- | |
59 | $this->addExpectedArgument($argSilent = new VNagArgument('s', 'silent', VNagArgument::VALUE_FORBIDDEN, null, 'Description for the --silent output', $defaultValue)); |
- | |
60 | $this->addExpectedArgument($argHost = new VNagArgument('H', 'host', VNagArgument::VALUE_REQUIRED, 'hostname', 'Description for the --host output', $defaultValue)); |
- | |
61 | - | ||
62 | In the example above, the two argument objects $argSilent and $argHost were created. |
- | |
63 | With these objects of the type VNagArgument, you can query the argument's value, |
- | |
64 | how often the argument was passed and if it is set: |
- | |
65 | - | ||
66 | $argSilent->count(); // 1 if "-s" is passed, 2 if "-s -s" is passed etc. |
- | |
67 | $argSilent->available(); // true if "-s" is passed, false otherwise |
- | |
68 | $argHost->getValue(); // "example.com" if "-h example.com" is passed |
- | |
69 | - | ||
70 | It is recommended that you pass every argument to $this->addExpectedArgument() . |
- | |
71 | Using this way, VNag can generate a --help page for you, which lists all your arguments. |
- | |
72 | Future version of VNag may also require to have a complete list of all valid arguments, |
- | |
73 | since the Nagios Development Guidelines recommend to output the usage information if an illegal |
- | |
74 | argument is passed. Due to PHP's horrible bad implementation of GNU's getopt(), this check for |
- | |
75 | unknown arguments is currently not possible, and the developer of VNag does not want to use |
- | |
76 | dirty hacks/workarounds, which would not match to all argument notation variations/styles. |
- | |
77 | See: https://bugs.php.net/bug.php?id=68806 |
- | |
78 | https://bugs.php.net/bug.php?id=65673 |
- | |
79 | https://bugs.php.net/bug.php?id=26818 |
- | |
80 | - | ||
81 | Setting the status: |
- | |
82 | - | ||
83 | You can set the status with: |
- | |
84 | $this->setStatus(VNag::STATUS_OK); |
- | |
85 | If you don't set a status, the script will return Unknown instead. |
- | |
86 | setStatus($status) will keep the most severe status, e.g. |
- | |
87 | $this->setStatus(VNag::STATUS_CRITICAL); |
- | |
88 | $this->setStatus(VNag::STATUS_OK); |
- | |
89 | will result in a status "Critical". |
- | |
90 | If you want to completely overwrite the status, use $force=true: |
- | |
91 | $this->setStatus(VNag::STATUS_CRITICAL); |
- | |
92 | $this->setStatus(VNag::STATUS_OK, true); |
- | |
93 | The status will now be "OK". |
- | |
94 | - | ||
95 | Possible status codes are: |
- | |
96 | (For service plugins:) |
- | |
97 | VNag::STATUS_OK = 0; |
- | |
98 | VNag::STATUS_WARNING = 1; |
- | |
99 | VNag::STATUS_CRITICAL = 2; |
- | |
100 | VNag::STATUS_UNKNOWN = 3; |
- | |
101 | - | ||
102 | (For host plugins:) |
- | |
103 | VNag::STATUS_UP = 0; |
- | |
104 | VNag::STATUS_DOWN = 1; |
- | |
105 | - | ||
106 | Output: |
- | |
107 | - | ||
108 | After the callback function cbRun() of your job has finished, |
- | |
109 | the framework will automatically output the results in the Nagios console output format, |
- | |
110 | the visual HTML output and/or the invisible HTML output. |
- | |
111 | - | ||
112 | In case of CLI invokation, the Shell exit code will be remembered and |
- | |
113 | automatically returned by the shutdown handler once the script normally |
- | |
114 | terminates. (In case you run different jobs, which is not recommended, the |
- | |
115 | shutdown handler will output the baddest exit code). |
- | |
116 | - | ||
117 | The Shell output format will be: |
- | |
118 | <Service status text>: <Comma separates messages> | <whitespace separated primary performance data> |
- | |
119 | "Verbose information:" |
- | |
120 | <Multiline verbose output> | <Multiline secondary performance data> |
- | |
121 | - | ||
122 | <Service status text> will be automatically created by VNag. |
- | |
123 | - | ||
124 | Verbose information are printed below the first line. Most Nagios clients will only print the first line. |
- | |
125 | If you have important output, use $this->setHeadline() instead. |
- | |
126 | You can add verbose information with following method: |
- | |
127 | $this->addVerboseMessage('foobar', $verbosity); |
- | |
128 | - | ||
129 | Following verbosity levels are defined: |
- | |
130 | VNag::VERBOSITY_SUMMARY = 0; // always printed |
- | |
131 | VNag::VERBOSITY_ADDITIONAL_INFORMATION = 1; // requires at least -v |
- | |
132 | VNag::VERBOSITY_CONFIGURATION_DEBUG = 2; // requiers at least -vv |
- | |
133 | VNag::VERBOSITY_PLUGIN_DEBUG = 3; // requiers at least -vvv |
- | |
134 | - | ||
135 | All STDOUT outputs of your script (e.g. by echo) will be interpreted as "verbose" output |
- | |
136 | and is automatically collected, so |
- | |
137 | echo "foobar"; |
- | |
138 | has the same functionality as |
- | |
139 | $this->addVerboseMessage('foobar', VNag::VERBOSITY_SUMMARY); |
- | |
140 | - | ||
141 | You can set messages (which will be added into the first line, which is preferred for plugin outputs) |
- | |
142 | using |
- | |
143 | $this->setHeadline($msg, $append, $verbosity); |
- | |
144 | Using the flag $append, you can choose if you want to append or replace the message. |
- | |
145 | - | ||
146 | VNag will catch Exceptions of your script and will automatically end the plugin, |
- | |
147 | returning a valid Nagios output. |
- | |
148 | - | ||
149 | Automatic handling of basic arguments: |
- | |
150 | - | ||
151 | VNag will automatic handle of following CLI arguments: |
- | |
152 | -? |
- | |
153 | -V --version |
- | |
154 | -h --help |
- | |
155 | -v --verbose |
- | |
156 | -t --timeout (only works if you set declare(ticks=1) at the beginning of each of your scripts) |
- | |
157 | -w --warning |
- | |
158 | -c --critical |
- | |
159 | - | ||
160 | You can performe range checking by using: |
- | |
161 | $example_value = '10MB'; |
- | |
162 | $this->checkAgainstWarningRange($example_value); |
- | |
163 | this is more or less the same as: |
- | |
164 | $example_value = '10MB'; |
- | |
165 | $wr = $this->getWarningRange(); |
- | |
166 | if (isset($wr) && $wr->checkAlert($example_value)) { |
- | |
167 | $this->setStatus(VNag::STATUS_WARNING); |
- | |
168 | } |
- | |
169 | - | ||
170 | In case that your script allows ranges which can be relative and absolute, you can provide multiple arguments; |
- | |
171 | $wr->checkAlert() will be true, as soon as one of the arguments is in the warning range. |
- | |
172 | The check will be done in this way: |
- | |
173 | $example_values = array('10MB', '5%'); |
- | |
174 | $this->checkAgainstWarningRange($example_values); |
- | |
175 | this is more or less the same as: |
- | |
176 | $example_values = array('10MB', '5%'); |
- | |
177 | $wr = $this->getWarningRange(); |
- | |
178 | if (isset($wr) && $wr->checkAlert($example_values)) { |
- | |
179 | $this->setStatus(VNag::STATUS_WARNING); |
- | |
180 | } |
- | |
181 | - | ||
182 | Note that VNag will automatically detect the UOM (Unit of Measurement) and is also able to convert them, |
- | |
183 | e.g. if you use the range "-w 20MB:40MB", your script will be able to use $wr->checkAlert('3000KB') |
- | |
184 | - | ||
185 | Please note that only following UOMs are accepted (as defined in the Plugin Development Guidelines): |
- | |
186 | - no unit specified: assume a number (int or float) of things (eg, users, processes, load averages) |
- | |
187 | - s, ms, us: seconds |
- | |
188 | - %: percentage |
- | |
189 | - B, KB, MB, TB: bytes // NOTE: GB is not in the official development guidelines,probably due to an error, so I've added them anyway |
- | |
190 | - c: a continous counter (such as bytes transmitted on an interface) |
- | |
191 | - | ||
192 | Multiple warning/critical ranges: |
- | |
193 | - | ||
194 | The arguments -w and -c can have many different values, separated by comma. |
- | |
195 | We can see this feature e.g. with the official plugin /usr/lib/nagios/plugins/check_ping: |
- | |
196 | It has following syntax for the arguments -w and -c: <latency>,<packetloss>% |
- | |
197 | - | ||
198 | When you are using checkAgainstWarningRange, you can set the fourth argument to the range number |
- | |
199 | you would like to check (beginning with 0). |
- | |
200 | - | ||
201 | Example: |
- | |
202 | // -w 1MB:5MB,5%:10% |
- | |
203 | $this->checkAgainstWarningRange('4MB', true, true, 0); // check value 4MB against range "1MB:5MB" (no warning) |
- | |
204 | $this->checkAgainstWarningRange('15%', true, true, 1); // check value 15% gainst range "5%:10%" (gives warning) |
- | |
205 | - | ||
206 | Visual HTTP output: |
- | |
207 | - | ||
208 | Can be enabled/disabled with $this->http_visual_output |
- | |
209 | - | ||
210 | Valid values: |
- | |
211 | - | ||
212 | VNag::OUTPUT_SPECIAL = 1; // illegal usage / help page, version page |
- | |
213 | VNag::OUTPUT_NORMAL = 2; |
- | |
214 | VNag::OUTPUT_EXCEPTION = 4; |
- | |
215 | VNag::OUTPUT_ALWAYS = 7; |
- | |
216 | VNag::OUTPUT_NEVER = 0; |
- | |
217 | - | ||
218 | Encryption and Decryption: |
- | |
219 | - | ||
220 | In case you are emitting machine-readable code in your HTTP output |
- | |
221 | (can be enabled/disabled by $this->http_invisible_output), |
- | |
222 | you can encrypt the machine-readable part of your HTTP output by |
- | |
223 | setting $this->password_out . If you want to read the information, |
- | |
224 | you need to set $this->password_in at the web-reader plugin. |
- | |
225 | The visual output is not encrypted. So, if you want to hide the information, |
- | |
226 | then you must not enable visual HTML output. |
- | |
227 | If you don't want to encrypt the machine-readable output, |
- | |
228 | please set $this->password_out to null or empty string. |
- | |
229 | - | ||
230 | Attention! |
- | |
231 | - Encryption and decryption require the OpenSSL extension in PHP. |
- | |
232 | - | ||
233 | Digital signature: |
- | |
234 | - | ||
235 | You can sign the output by setting $this->privkey with a filename containing |
- | |
236 | a private key created by OpenSSL. If it is encrypted, please also set |
- | |
237 | $this->privkey_password . |
- | |
238 | To check the signature, set $this->pubkey at your web-reader plugin with |
- | |
239 | the filename of the public key file. |
- | |
240 | - | ||
241 | Attention! |
- | |
242 | - Signatures require the OpenSSL extension in PHP. |
- | |
243 | - | ||
244 | Performance data: |
- | |
245 | - | ||
246 | You can add performance data using |
- | |
247 | $this->addPerformanceData(new VNagPerformanceData($label, $value, $warn, $crit, $min, $max)); |
- | |
248 | or by the alternative constructor |
- | |
249 | $this->addPerformanceData(VNagPerformanceData::createByString("'XYZ'=100;120;130;0;500")); |
- | |
250 | $value may contain an UOM, e.g. "10MB". All other parameters may not contain an UOM. |
- | |
251 | - | ||
252 | Guidelines: |
- | |
253 | - | ||
254 | This framework currently supports meets following guidelines: |
- | |
255 | - https://nagios-plugins.org/doc/guidelines.html#PLUGOUTPUT (Plugin Output for Nagios) |
- | |
256 | - https://nagios-plugins.org/doc/guidelines.html#AEN33 (Print only one line of text) |
- | |
257 | - https://nagios-plugins.org/doc/guidelines.html#AEN41 (Verbose output) |
- | |
258 | - https://nagios-plugins.org/doc/guidelines.html#AEN78 (Plugin Return Codes) |
- | |
259 | - https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT (Threshold and ranges) |
- | |
260 | - https://nagios-plugins.org/doc/guidelines.html#AEN200 (Performance data) |
- | |
261 | - https://nagios-plugins.org/doc/guidelines.html#PLUGOPTIONS (Plugin Options) |
- | |
262 | - https://nagios-plugins.org/doc/guidelines.html#AEN302 (Option Processing) |
- | |
263 | Note: The screen output of the help page will (mostly) be limited to 80 characters width; but the max recommended length of 23 lines cannot be guaranteed. |
- | |
264 | - | ||
265 | This framework does currently NOT support following guidelines: |
- | |
266 | - https://nagios-plugins.org/doc/guidelines.html#AEN74 (Screen Output) |
- | |
267 | - https://nagios-plugins.org/doc/guidelines.html#AEN239 (Translations) |
- | |
268 | - https://nagios-plugins.org/doc/guidelines.html#AEN293 (Use DEFAULT_SOCKET_TIMEOUT) |
- | |
269 | - https://nagios-plugins.org/doc/guidelines.html#AEN296 (Add alarms to network plugins) |
- | |
270 | - https://nagios-plugins.org/doc/guidelines.html#AEN245 (Don't execute system commands without specifying their full path) |
- | |
271 | - https://nagios-plugins.org/doc/guidelines.html#AEN249 (Use spopen() if external commands must be executed) |
- | |
272 | - https://nagios-plugins.org/doc/guidelines.html#AEN253 (Don't make temp files unless absolutely required) |
- | |
273 | - https://nagios-plugins.org/doc/guidelines.html#AEN259 (Validate all input) |
- | |
274 | - https://nagios-plugins.org/doc/guidelines.html#AEN317 (Plugins with more than one type of threshold, or with threshold ranges) |
- | |
275 | - | ||
276 | We will intentionally NOT follow the following guidelines: |
- | |
277 | - https://nagios-plugins.org/doc/guidelines.html#AEN256 (Don't be tricked into following symlinks) |
- | |
278 | Reason: We believe that this guideline is contraproductive. |
- | |
279 | Nagios plugins usually run as user 'nagios'. It is the task of the system administrator |
- | |
280 | to ensure that the user 'nagios' must not read/write to files which are not intended |
- | |
281 | for access by the Nagios service. Symlinks, on the other hand, are useful for several tasks. |
- | |
282 | See also https://stackoverflow.com/questions/27112949/nagios-plugins-why-not-following-symlinks |
- | |
283 | - | ||
284 | VNag over HTTP: |
- | |
285 | - | ||
286 | A script that uses the VNag framework can run as CLI script (normal Nagios plugin) or as web site (or both). |
- | |
287 | Having the script run as website, you can include a Nagios information combined with a human friendly HTML output which can |
- | |
288 | include colors, graphics (like charts) etc. |
- | |
289 | - | ||
290 | For example: |
- | |
291 | A script that measures traffic can have a website which shows graphs, |
- | |
292 | and has a hidden Nagios output included, which can be read by a Nagios plugin that |
- | |
293 | converts the hidden information on that website into an output that Nagios can evaluate. |
- | |
294 | - | ||
295 | Here is a comparison of the usage and behavior of VNag in regards to CLI and HTTP calls: |
- | |
296 | - | ||
297 | ------------------------------------------------------------------------------------------ |
- | |
298 | CLI script HTTP script |
- | |
299 | ------------------------------------------------------------------------------------------ |
- | |
300 | * "echo" will be discarded. * "echo" output will be discarded. |
- | |
301 | - | ||
302 | * Exceptions will be handled. * Exceptions will be handled. |
- | |
303 | - | ||
304 | * outputHTML() will be ignored. * outputHTML() will be handled. |
- | |
305 | (This allows you to have the same script |
- | |
306 | running as CLI and HTML) |
- | |
307 | - | ||
308 | * Arguments are passed via CLI. * Arguments are passed via $_REQUEST |
- | |
309 | (i.e. GET or POST) |
- | |
310 | - | ||
311 | * Arguments: "-vvv" * Arguments: GET ?v[]=&v[]=&v[]= or POST |
- | |
312 | - | ||
313 | * When run() has finished, the program * When run() has finished, the program |
- | |
314 | flow continues, although it is not flow continues. |
- | |
315 | recommended that you do anything after it. |
- | |
316 | (The exit code is remembered for the |
- | |
317 | shutdown handler) |
- | |
318 | - | ||
319 | * Exactly 1 job must be called, resulting * You can call as many jobs as you want. |
- | |
320 | in a single output of that job. A website can include more than one |
- | |
321 | Nagios output which are enumerated with |
- | |
322 | a serial number (0,1,2,3,...) or manual ID. |
- | |
323 | ------------------------------------------------------------------------------------------ |
- | |
324 | 21 | ||
325 | ****************************************************************************************************/ |
22 | ****************************************************************************************************/ |
326 | 23 | ||
327 | if (!VNag::is_http_mode()) error_reporting(E_ALL); |
24 | if (!VNag::is_http_mode()) error_reporting(E_ALL); |
328 | 25 |