PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/utils.php

https://github.com/appmode/nagiosplugins-php
PHP | 490 lines | 192 code | 51 blank | 247 comment | 25 complexity | d13d00015595947e15cd530bfbae5bd6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. //----------------------------------------------------------------------------//
  3. // nagiosPluginPHP (c) copyright 2008 CYKO Pty Ltd
  4. //----------------------------------------------------------------------------//
  5. //----------------------------------------------------------------------------//
  6. // THIS SOFTWARE IS GPL LICENSED
  7. //----------------------------------------------------------------------------//
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License (version 2) as
  10. // published by the Free Software Foundation.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Library General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //----------------------------------------------------------------------------//
  21. //--------------------------------------------------------------------//
  22. // NOTES
  23. //--------------------------------------------------------------------//
  24. //
  25. // This file should be installed in your nagios libexec folder
  26. // eg. /usr/local/nagios/libexec/
  27. //
  28. // see check_skel for usage example
  29. //
  30. // see http://nagios.sourceforge.net/docs/3_0/pluginapi.html
  31. // and http://nagiosplug.sourceforge.net/developer-guidelines.html
  32. // for details on Nagios Plugins
  33. //--------------------------------------------------------------------//
  34. // utils.php
  35. //--------------------------------------------------------------------//
  36. // turn off error reporting
  37. error_reporting(0);
  38. //--------------------------------------------------------------------//
  39. // DEFINE CONSTANTS
  40. //--------------------------------------------------------------------//
  41. define('STATE_OK', 0);
  42. define('STATE_WARNING', 1);
  43. define('STATE_CRITICAL', 2);
  44. define('STATE_UNKNOWN', 3);
  45. define('STATE_DEPENDENT', 4);
  46. //--------------------------------------------------------------------//
  47. // NAGIOS PLUGIN CLASS
  48. //--------------------------------------------------------------------//
  49. class nagiosPlugin
  50. {
  51. private $_arrLongText = Array();
  52. private $_arrPerformanceData = Array();
  53. public $strVersion = "(nagiosPluginsPHP 1.0)";
  54. public $strRevision = "1.0.4";
  55. public $strUsage = "";
  56. public $strHelp = "";
  57. public $arrOptions = Array();
  58. public $strName = "";
  59. //------------------------------------------------------------------------//
  60. // __construct
  61. //------------------------------------------------------------------------//
  62. /**
  63. * __construct()
  64. *
  65. * class constructor
  66. *
  67. * stores variables, gets command line options and checks for help requests (-h)
  68. *
  69. * @param string $strRevision optional revision number of the plugin Default = NULL
  70. * @param string $strUsage optional usage example for the plugin Default = NULL
  71. * @param string $strHelp optional help message for the plugin Default = NULL
  72. * @param array $arrOptions optional array of command line options
  73. * to be parsed for the plugin. See the
  74. * getOpt method for more details. Default = NULL
  75. *
  76. * @return void
  77. */
  78. function __construct($strRevision=NULL, $strUsage=NULL, $strHelp=NULL, $arrOptions=NULL)
  79. {
  80. // store params
  81. if ($strRevision)
  82. {
  83. $this->strRevision = $strRevision;
  84. }
  85. if ($strUsage)
  86. {
  87. $this->strUsage = $strUsage;
  88. }
  89. if ($strHelp)
  90. {
  91. $this->strHelp = $strHelp;
  92. }
  93. // get options
  94. $this->arrOptions = $this->getOpt($arrOptions);
  95. // get script name
  96. $this->strName = basename($_SERVER['argv'][0]);
  97. // check for help request
  98. if ($this->arrOptions['help'])
  99. {
  100. // output help
  101. $this->printHelp();
  102. }
  103. // turn error reporting on for very verbose requests (-vvv)
  104. if ($this->arrOptions['verbose'] > 2)
  105. {
  106. error_reporting(E_ALL);
  107. }
  108. }
  109. //------------------------------------------------------------------------//
  110. // output
  111. //------------------------------------------------------------------------//
  112. /**
  113. * output()
  114. *
  115. * prints plugin output
  116. *
  117. * prints formated plugin output and terminates execution.
  118. * This should be the last method your plugin calls.
  119. *
  120. * @param string $strMessage text output to be returned to Nagios
  121. * @param int $intStatus return code, must be one of the
  122. * defined STATE_* constants
  123. *
  124. * @return void (terminates execution)
  125. */
  126. function output($strMessage, $intStatus)
  127. {
  128. echo $this->_buildOutput($strMessage);
  129. exit($intStatus);
  130. }
  131. //------------------------------------------------------------------------//
  132. // printHelp
  133. //------------------------------------------------------------------------//
  134. /**
  135. * printHelp()
  136. *
  137. * prints a help message
  138. *
  139. * prints a help message and by default terminates execution.
  140. * uses the $strRevision, $strUsage and $strHelp strings passed to
  141. * the class constructor.
  142. *
  143. * @param bool $bolExit optional terminate execution Default = TRUE
  144. *
  145. * @return void
  146. */
  147. function printHelp($bolExit=TRUE)
  148. {
  149. $this->printRevision(FALSE);
  150. echo "\n";
  151. $this->printUsage(FALSE);
  152. echo "\n";
  153. echo str_replace("<name>", $this->strName, $this->strHelp)."\n";
  154. if ($bolExit)
  155. {
  156. exit(STATE_UNKNOWN);
  157. }
  158. }
  159. //------------------------------------------------------------------------//
  160. // printRevision
  161. //------------------------------------------------------------------------//
  162. /**
  163. * printRevision()
  164. *
  165. * prints the plugin revision
  166. *
  167. * prints the plugin revision and by default terminates execution.
  168. * uses the $strRevision string passed to the class constructor.
  169. *
  170. * @param bool $bolExit optional terminate execution Default = TRUE
  171. *
  172. * @return void
  173. */
  174. function printRevision($bolExit=TRUE)
  175. {
  176. echo $this->strName." ";
  177. echo $this->strVersion." ";
  178. echo $this->strRevision."\n";
  179. if ($bolExit)
  180. {
  181. exit(STATE_UNKNOWN);
  182. }
  183. }
  184. //------------------------------------------------------------------------//
  185. // printUsage
  186. //------------------------------------------------------------------------//
  187. /**
  188. * printUsage()
  189. *
  190. * prints a usage message
  191. *
  192. * prints a usage message and by default terminates execution.
  193. * uses the $strUsage string passed to the class constructor.
  194. *
  195. * @param bool $bolExit optional terminate execution Default = TRUE
  196. *
  197. * @return void
  198. */
  199. function printUsage($bolExit=TRUE)
  200. {
  201. echo str_replace("<name>", $this->strName, $this->strUsage)."\n";
  202. if ($bolExit)
  203. {
  204. exit(STATE_UNKNOWN);
  205. }
  206. }
  207. //------------------------------------------------------------------------//
  208. // addLongText
  209. //------------------------------------------------------------------------//
  210. /**
  211. * addLongText()
  212. *
  213. * adds a long text line to be output
  214. *
  215. * adds a long text line to be output by the output method
  216. *
  217. * @param string $strValue long line to be output
  218. *
  219. * @return void
  220. */
  221. function addLongText($strValue)
  222. {
  223. $this->_arrLongText[] = $strValue;
  224. }
  225. //------------------------------------------------------------------------//
  226. // addPerformanceData
  227. //------------------------------------------------------------------------//
  228. /**
  229. * addPerformanceData()
  230. *
  231. * adds a performance data line to be output
  232. *
  233. * adds a performance data line to be output by the output method
  234. *
  235. * @param string $strValue performance data line to be output
  236. *
  237. * @return void
  238. */
  239. function addPerformanceData($strValue)
  240. {
  241. $this->_arrPerformanceData[] = $strValue;
  242. }
  243. //------------------------------------------------------------------------//
  244. // addVerbose
  245. //------------------------------------------------------------------------//
  246. /**
  247. * addVerbose()
  248. *
  249. * adds a verbose line to the plugin output
  250. *
  251. * adds a verbose line to the plugin output if the plugin is called with
  252. * a verbosity level equal to or higher than $intLevel
  253. *
  254. * @param string $strValue verbose line to be output
  255. * @param int $intLevel optional verbosity level Default = 1
  256. *
  257. * @return void
  258. */
  259. function addVerbose($strValue, $intLevel=1)
  260. {
  261. if ($this->arrOptions['verbose'] >= $intLevel)
  262. {
  263. echo $strValue."\n";
  264. }
  265. }
  266. //------------------------------------------------------------------------//
  267. // _buildOutput
  268. //------------------------------------------------------------------------//
  269. /**
  270. * _buildOutput()
  271. *
  272. * build output
  273. *
  274. * build output from text output, long text lines and performance data
  275. *
  276. * @param string $strMessage text output to be returned to Nagios
  277. *
  278. * @return string
  279. *
  280. * @private
  281. */
  282. private function _buildOutput($strMessage)
  283. {
  284. // add text output
  285. $strOutput = $strMessage;
  286. // add first line of performance data
  287. if (count($this->_arrPerformanceData))
  288. {
  289. $strOutput .= "|". array_shift($this->_arrPerformanceData);
  290. }
  291. $strOutput .= "\n";
  292. // add long text lines
  293. $strOutput .= implode("\n", $this->_arrLongText);
  294. // add additional lines of performance data
  295. if (count($this->_arrPerformanceData))
  296. {
  297. $strOutput .= "|";
  298. foreach ($this->_arrPerformanceData AS $strLine)
  299. {
  300. $strOutput .= $strLine ."\n";
  301. }
  302. }
  303. // add a trailing \n
  304. if (substr($strOutput, -1) != "\n")
  305. {
  306. $strOutput .= "\n";
  307. }
  308. // return output
  309. return $strOutput;
  310. }
  311. //------------------------------------------------------------------------//
  312. // getOpt
  313. //------------------------------------------------------------------------//
  314. /**
  315. * getOpt()
  316. *
  317. * gets options from the command line argument list
  318. *
  319. * gets standard Nagios plugin options from the command line argument list.
  320. * also accepts an array of custom options in the form;
  321. * $arrCustomOptions[$chrKey] = $strValue
  322. * where $chrKey is a single character option passed to the script
  323. * with a single hyphen (-)
  324. * and $strValue is the key in the return array the option is to be
  325. * mapped to, optionally followed by a single colon (parameter
  326. * requires value) or two colons (optional value)
  327. *
  328. * For Example, a script called with;
  329. * check_mine -m myValue
  330. * with a custom options array containing;
  331. * $arrCustomOptions['m'] = 'myOption:'
  332. * would result in a return array containing;
  333. * $arrReturn['myOption'] = 'myValue'
  334. *
  335. * this method can be easily modified to allow the use of long options
  336. * passed to the script with two hyphens (--) if used with php 5.3.0
  337. *
  338. * see the php getopt function for more details
  339. *
  340. * @param array $arrCustomOptions optional array of command line options
  341. * to be parsed for the plugin. Default = NULL
  342. *
  343. * @return array
  344. */
  345. function getOpt($arrCustomOptions=NULL)
  346. {
  347. $arrOptions = Array();
  348. $arrLongOptions = Array();
  349. $strOptions = "";
  350. // reserved options
  351. $arrOptions['V'] = "version";
  352. $arrOptions['h'] = "help";
  353. $arrOptions['?'] = "help";
  354. $arrOptions['t'] = "timeout:";
  355. $arrOptions['w'] = "warning:";
  356. $arrOptions['c'] = "critical:";
  357. $arrOptions['H'] = "hostname:";
  358. $arrOptions['v'] = "verbose::";
  359. // standard options
  360. $arrOptions['C'] = "comunity:";
  361. $arrOptions['a'] = "authentication:";
  362. $arrOptions['l'] = "logname:";
  363. // standard options with multiple meanings
  364. $arrOptions['p'] = "password:";
  365. //$arrOptions['p'] = "port:";
  366. //$arrOptions['p'] = "passwd:";
  367. $arrOptions['u'] = "username:";
  368. //$arrOptions['u'] = "url:";
  369. // custom options
  370. if (is_array($arrCustomOptions))
  371. {
  372. $arrOptions = array_merge($arrOptions, $arrCustomOptions);
  373. }
  374. // process options
  375. foreach($arrOptions AS $strOption=>$strLongOption)
  376. {
  377. // short options
  378. if (!(int)$strOption)
  379. {
  380. if (substr($strLongOption, -2) == '::')
  381. {
  382. $strRequired = "::";
  383. }
  384. elseif(substr($strLongOption, -1) == ':')
  385. {
  386. $strRequired = ":";
  387. }
  388. else
  389. {
  390. $strRequired = "";
  391. }
  392. $strOptions .= $strOption.$strRequired;
  393. }
  394. // long options
  395. if ($strLongOption)
  396. {
  397. $arrLongOptions[] = $strLongOption;
  398. }
  399. }
  400. // get options
  401. //$arrReturnOptions = getopt($strOptions, array_unique($arrLongOptions)); //PHP 5.3 or better only
  402. $arrReturnOptions = getopt($strOptions);
  403. $arrOutputOptions = Array();
  404. // build output array
  405. foreach ($arrReturnOptions AS $strOption=>$mixValue)
  406. {
  407. // set non-value options to TRUE
  408. if ($mixValue === FALSE)
  409. {
  410. $mixValue = TRUE;
  411. }
  412. if (strlen($strOption) == 1 && $strLongOption = trim($arrOptions[$strOption], ':'))
  413. {
  414. // convert short options to long options
  415. $arrOutputOptions[$strLongOption] = $mixValue;
  416. }
  417. else
  418. {
  419. // add in long options
  420. $arrOutputOptions[$strOption] = $mixValue;
  421. }
  422. }
  423. // calculate verbosity level
  424. if ($arrOutputOptions['verbose'])
  425. {
  426. if (($arrOutputOptions['verbose']) === TRUE)
  427. {
  428. $arrOutputOptions['verbose'] = 1;
  429. }
  430. elseif(!(int)$arrOutputOptions['verbose'])
  431. {
  432. $arrOutputOptions['verbose'] = substr_count($arrOutputOptions['verbose'], 'v') + 1;
  433. }
  434. }
  435. // return options
  436. return $arrOutputOptions;
  437. }
  438. }
  439. ?>