nagiosplugins-php /utils.php

Language PHP Lines 491
MD5 Hash d13d00015595947e15cd530bfbae5bd6
Repository https://github.com/appmode/nagiosplugins-php.git View Raw File
  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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
//----------------------------------------------------------------------------//
// nagiosPluginPHP (c) copyright 2008 CYKO Pty Ltd
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
// THIS SOFTWARE IS GPL LICENSED
//----------------------------------------------------------------------------//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License (version 2) as 
//  published by the Free Software Foundation.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//----------------------------------------------------------------------------//

//--------------------------------------------------------------------//
// NOTES
//--------------------------------------------------------------------//
// 
// This file should be installed in your nagios libexec folder
// eg. /usr/local/nagios/libexec/
//
// see check_skel for usage example
//
// see http://nagios.sourceforge.net/docs/3_0/pluginapi.html
// and http://nagiosplug.sourceforge.net/developer-guidelines.html
// for details on Nagios Plugins


//--------------------------------------------------------------------//
// utils.php
//--------------------------------------------------------------------//

// turn off error reporting
error_reporting(0);

//--------------------------------------------------------------------//
// DEFINE CONSTANTS
//--------------------------------------------------------------------//
define('STATE_OK', 				0);
define('STATE_WARNING', 		1);
define('STATE_CRITICAL', 		2);
define('STATE_UNKNOWN', 		3);
define('STATE_DEPENDENT', 		4);

//--------------------------------------------------------------------//
// NAGIOS PLUGIN CLASS
//--------------------------------------------------------------------//
class nagiosPlugin
{
	private $_arrLongText			= Array();
	private $_arrPerformanceData	= Array();
	public 	$strVersion 			= "(nagiosPluginsPHP 1.0)";
	public 	$strRevision 			= "1.0.4";
	public 	$strUsage				= "";
	public 	$strHelp				= "";
	public	$arrOptions				= Array();
	public	$strName				= "";
	
	//------------------------------------------------------------------------//
	// __construct
	//------------------------------------------------------------------------//
	/**
	 * __construct()
	 *
	 * class constructor
	 *
	 * stores variables, gets command line options and checks for help requests (-h)
	 *
	 * @param		string 		$strRevision	optional revision number of the plugin	  Default = NULL
	 * @param		string 		$strUsage		optional usage example for the plugin	  Default = NULL
	 * @param		string 		$strHelp		optional help message for the plugin	  Default = NULL
	 * @param		array 		$arrOptions		optional array of command line options
	 * 											to be parsed for the plugin. See the
	 * 											getOpt method for more details.			  Default = NULL
	 * 											
	 * @return  	void
	 */
	function __construct($strRevision=NULL, $strUsage=NULL, $strHelp=NULL, $arrOptions=NULL)
	{
		// store params
		if ($strRevision)
		{
			$this->strRevision = $strRevision;
		}
		if ($strUsage)
		{
			$this->strUsage = $strUsage;
		}
		if ($strHelp)
		{
			$this->strHelp	= $strHelp;
		}
		
		// get options
		$this->arrOptions = $this->getOpt($arrOptions);
		
		// get script name
		$this->strName = basename($_SERVER['argv'][0]);

		// check for help request
		if ($this->arrOptions['help'])
		{
			// output help
			$this->printHelp();
		}
		
		// turn error reporting on for very verbose requests (-vvv)
		if ($this->arrOptions['verbose'] > 2)
		{
			error_reporting(E_ALL);
		}
	}
	
	
	//------------------------------------------------------------------------//
	// output
	//------------------------------------------------------------------------//
	/**
	 * output()
	 *
	 * prints plugin output
	 *
	 * prints formated plugin output and terminates execution. 
	 * This should be the last method your plugin calls.
	 *
	 * @param		string 		$strMessage		text output to be returned to Nagios
	 * @param		int 		$intStatus		return code, must be one of the 
	 * 											defined STATE_* constants
	 *
	 * @return  	void (terminates execution)
	 */
	function output($strMessage, $intStatus)
	{
		echo $this->_buildOutput($strMessage);
		exit($intStatus);
	}
	
	
	//------------------------------------------------------------------------//
	// printHelp
	//------------------------------------------------------------------------//
	/**
	 * printHelp()
	 *
	 * prints a help message
	 *
	 * prints a help message and by default terminates execution.
	 * uses the $strRevision, $strUsage and $strHelp strings passed to
	 * the class constructor.
	 *
	 * @param		bool 		$bolExit		optional terminate execution	  Default = TRUE
	 *
	 * @return  	void
	 */
	function printHelp($bolExit=TRUE)
	{
		$this->printRevision(FALSE);
		echo "\n";
		$this->printUsage(FALSE);
		echo "\n";
		echo str_replace("<name>", $this->strName, $this->strHelp)."\n";

		if ($bolExit)
		{
			exit(STATE_UNKNOWN);
		}
	}
	
	
	//------------------------------------------------------------------------//
	// printRevision
	//------------------------------------------------------------------------//
	/**
	 * printRevision()
	 *
	 * prints the plugin revision
	 *
	 * prints the plugin revision and by default terminates execution.
	 * uses the $strRevision string passed to the class constructor.
	 *
	 * @param		bool 		$bolExit		optional terminate execution	  Default = TRUE
	 *
	 * @return  	void
	 */
	function printRevision($bolExit=TRUE)
	{
		echo $this->strName."  ";
		echo $this->strVersion."  ";
		echo $this->strRevision."\n";
		
		if ($bolExit)
		{
			exit(STATE_UNKNOWN);
		}
	}
	
	
	//------------------------------------------------------------------------//
	// printUsage
	//------------------------------------------------------------------------//
	/**
	 * printUsage()
	 *
	 * prints a usage message
	 *
	 * prints a usage message and by default terminates execution.
	 * uses the $strUsage string passed to the class constructor.
	 *
	 * @param		bool 		$bolExit		optional terminate execution	  Default = TRUE
	 *
	 * @return  	void
	 */
	function printUsage($bolExit=TRUE)
	{
		echo str_replace("<name>", $this->strName, $this->strUsage)."\n";
		
		if ($bolExit)
		{
			exit(STATE_UNKNOWN);
		}
	}

	
	//------------------------------------------------------------------------//
	// addLongText
	//------------------------------------------------------------------------//
	/**
	 * addLongText()
	 *
	 * adds a long text line to be output
	 *
	 * adds a long text line to be output by the output method
	 *
	 * @param		string 		$strValue		long line to be output
	 *
	 * @return  	void
	 */
	function addLongText($strValue)
	{
		$this->_arrLongText[] 			= $strValue;
	}
	
	
	//------------------------------------------------------------------------//
	// addPerformanceData
	//------------------------------------------------------------------------//
	/**
	 * addPerformanceData()
	 *
	 * adds a performance data line to be output
	 *
	 * adds a performance data line to be output by the output method
	 *
	 * @param		string 		$strValue		performance data line to be output
	 *
	 * @return  	void
	 */
	function addPerformanceData($strValue)
	{
		$this->_arrPerformanceData[] 	= $strValue;
	}
	
	
	//------------------------------------------------------------------------//
	// addVerbose
	//------------------------------------------------------------------------//
	/**
	 * addVerbose()
	 *
	 * adds a verbose line to the plugin output
	 *
	 * adds a verbose line to the plugin output if the plugin is called with
	 * a verbosity level equal to or higher than $intLevel
	 *
	 * @param		string 		$strValue		verbose line to be output
	 * @param		int 		$intLevel		optional verbosity level	  Default = 1
	 *
	 * @return  	void
	 */
	function addVerbose($strValue, $intLevel=1)
	{
		if ($this->arrOptions['verbose'] >= $intLevel)
		{
			echo $strValue."\n";
		}
	}
	
	
	//------------------------------------------------------------------------//
	// _buildOutput
	//------------------------------------------------------------------------//
	/**
	 * _buildOutput()
	 *
	 * build output
	 *
	 * build output from text output, long text lines and performance data
	 *
	 * @param		string 		$strMessage		text output to be returned to Nagios
	 *
	 * @return  	string
	 *
	 * @private
	 */
	private function _buildOutput($strMessage)
	{
		// add text output
		$strOutput = $strMessage;
		
		// add first line of performance data
		if (count($this->_arrPerformanceData))
		{
			$strOutput .= "|". array_shift($this->_arrPerformanceData);	
		}
		$strOutput .= "\n";
		
		// add long text lines
		$strOutput .= implode("\n", $this->_arrLongText);
			
		// add additional lines of performance data
		if (count($this->_arrPerformanceData))
		{
			$strOutput .= "|";
			foreach ($this->_arrPerformanceData AS $strLine)
			{
				$strOutput .= $strLine ."\n";
			}	
		}
		
		// add a trailing \n
		if (substr($strOutput, -1) != "\n")
		{
			$strOutput	.= "\n";	
		}
		
		// return output
		return $strOutput;
	}
	
	
	//------------------------------------------------------------------------//
	// getOpt
	//------------------------------------------------------------------------//
	/**
	 * getOpt()
	 *
	 * gets options from the command line argument list
	 *
	 * gets standard Nagios plugin options from the command line argument list.
	 * also accepts an array of custom options in the form;
	 * 		$arrCustomOptions[$chrKey] = $strValue
	 *  	where $chrKey is a single character option passed to the script 
	 * 			with a single hyphen (-)
	 * 		and $strValue is the key in the return array the option is to be 
	 * 			mapped to, optionally followed by a single colon (parameter 
	 * 			requires value) or two colons (optional value)
	 * 
	 * 		For Example, a script called with;
	 * 			check_mine -m myValue
	 * 		with a custom options array containing;
	 * 			$arrCustomOptions['m'] = 'myOption:'
	 * 		would result in a return array containing;
	 * 			$arrReturn['myOption'] = 'myValue'
	 * 
	 * this method can be easily modified to allow the use of long options 
	 * passed to the script with two hyphens (--) if used with php 5.3.0
	 * 
	 * see the php getopt function for more details
	 *
	 * @param		array 		$arrCustomOptions	optional array of command line options
	 * 												to be parsed for the plugin.			  Default = NULL
	 *
	 * @return  	array
	 */
	function getOpt($arrCustomOptions=NULL)
	{
		$arrOptions 	= Array();
		$arrLongOptions	= Array();
		$strOptions		= "";
		
		// reserved options
		$arrOptions['V']	= "version";
		$arrOptions['h']	= "help";
		$arrOptions['?']	= "help";
		$arrOptions['t']	= "timeout:";
		$arrOptions['w']	= "warning:";
		$arrOptions['c']	= "critical:";
		$arrOptions['H']	= "hostname:";
		$arrOptions['v']	= "verbose::";
		
		// standard options
		$arrOptions['C']	= "comunity:";
		$arrOptions['a']	= "authentication:";
		$arrOptions['l']	= "logname:";
		
		// standard options with multiple meanings
		$arrOptions['p']	= "password:";
		//$arrOptions['p']	= "port:";
		//$arrOptions['p']	= "passwd:";
		$arrOptions['u']	= "username:";
		//$arrOptions['u']	= "url:";

		// custom options
		if (is_array($arrCustomOptions))
		{
			$arrOptions = array_merge($arrOptions, $arrCustomOptions);
		}

		// process options
		foreach($arrOptions AS $strOption=>$strLongOption)
		{
			// short options
			if (!(int)$strOption)
			{
				if (substr($strLongOption, -2) == '::')
				{
					$strRequired = "::";
				}
				elseif(substr($strLongOption, -1) == ':')
				{
					$strRequired = ":";
				}
				else
				{
					$strRequired = "";
				}
				$strOptions .= $strOption.$strRequired;
			}
			
			// long options
			if ($strLongOption)
			{
				$arrLongOptions[] = $strLongOption;
			}
		}
		
		// get options
		//$arrReturnOptions = getopt($strOptions, array_unique($arrLongOptions)); //PHP 5.3 or better only
		$arrReturnOptions = getopt($strOptions);
		$arrOutputOptions = Array();
		
		// build output array
		foreach ($arrReturnOptions AS $strOption=>$mixValue)
		{
			// set non-value options to TRUE
			if ($mixValue === FALSE)
			{
				$mixValue = TRUE;
			}
			
			if (strlen($strOption) == 1 && $strLongOption = trim($arrOptions[$strOption], ':'))
			{
				// convert short options to long options
				$arrOutputOptions[$strLongOption] = $mixValue;
			}
			else
			{
				// add in long options
				$arrOutputOptions[$strOption] = $mixValue;
			}
		}
		
		// calculate verbosity level
		if ($arrOutputOptions['verbose'])
		{
			if (($arrOutputOptions['verbose']) === TRUE)
			{
				$arrOutputOptions['verbose'] = 1;	
			}
			elseif(!(int)$arrOutputOptions['verbose'])
			{
				$arrOutputOptions['verbose'] = substr_count($arrOutputOptions['verbose'], 'v') + 1;
			}
		}
		
		// return options
		return $arrOutputOptions;
	}
}


?>
Back to Top