Subversion Repositories vnag

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php /* <ViaThinkSoftSignature>JvUe2EqrSTpzUAa69aGVuvQPVhC4dS9OgSJw+lNJLLFBnB10I8C8EVE5DcrC1/bQ+uF7qNulGCHJktHv8GEUhS79S7VzsuwFo8N1GXxRuanRlk3uCrPtOx8kvgdUesL8dmfCKQAxGHauSogngnj3qyN6PvcKbcBpp+oFzzQvOJrlb3osfKMcaAtJP1k8UYaNvpdT+Osi7jEe6sxRIH4c8iosSumIMHC763S0ngYMFk6i4tzHiyXr3QsfH9afKiVCX+8F/Q9zlj83VHNRsJYFk/z83R74nK8ZEmOWqUtlC3WaHN5ieNwOfwgjCQQP6QBOk/EihVvBuT/RXlZxEpzkzkpevkGfPK0c21XFHFmni6zhMx950oKY3lUXhvjesORZ+wVGN7CVi/ZjjoP0OAqT0dkl9X9t8LA2zIcN8qcq7kfd1T4ySbu88trlmoM4TM8/JE75B7XH/UwsqxX+aG1pqwy9Ea9IkvCRTD1y80bp4HMrrEhStw70j0xPJMB/IxRRQjVoCajN4ABpPpVk5cibj3/KLsWDSSu0PkXqkWBi1i1D+PZvDW/pqPTHLkd4wxj+dLp76kAgaOZ/xKJ6WNXS+bFd95RjURZaOoVYtwSION0tqqtNXTugmnDcrofSAFz+0+ccdLZKXXjlqLHs042ZGZCHW76hjvneraHqpM9svGorm2i3ka88nCG72ssNIaGfEGiVIzneSNiDHzmL5Qb9YLMa2bSH6CEgYmsaVtYmnwxEAPe2fq73/7pAdLXxRznuu5FbVGkjXXF3MjOMEX+EKmIrtaVWvL6ny+EvAI0BYymuuk2H5fvDMzOIGtsAt3whjR2khF/FF1xUls5WgvE7lM6PQftgI11EHNjgjV5EPstThMHuLWbl8GlMCktG/gS8PMJM2arN7VL1NPjDvJ3wath3kEUBZxfP0k1C7FhrL12I3PLy931uPMRtFH7em7zmLe76sYmgludGkR1W5SZCRXwjk2T8q/fOGV3Ra334cr4ix2gw/EjTK1Ojtlidt3qYeKde1N/WK+n2kihOeMjNj28r6l/0ysF5AYL42ZujJnMyyIiOIDEerYNKZKg9+Q5/6fCrTmItuIp0x2SqW29b4fwtMFoPXN6a+XB/f09NTjnZ56tsG3EY7AJHGKZ9MjTvedrJ/cch1wr6LuD6eTaHlf3zFK3Rgzyp/ixZqjVA4zjSrT/xQnoymLp7PjTK8tKcCOFxm2eJ+u3tsdg/UkaXylt+XPr0/QEXhKheAjug2Ylcqf1Aj4LH/WYVrZcUGAFIH/4Bzy7JEI+KRiN9MO3SgSlzEzaQwfXnvm9rMhGawsgbapsRMpAbNDr9qfMO8iSgKy0SBi+16ao0H4bNxPgM6w==</ViaThinkSoftSignature> */ ?>
  2. <?php
  3.  
  4. /*
  5.  * VNag - Nagios Framework for PHP
  6.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  7.  * Licensed under the terms of the Apache 2.0 license
  8.  *
  9.  * Revision 2018-07-16
  10.  */
  11.  
  12. declare(ticks=1);
  13.  
  14. class X509ExpireCheck extends VNag {
  15.         protected $argFiles = null;
  16.  
  17.         public function __construct() {
  18.                 parent::__construct();
  19.  
  20.                 $this->registerExpectedStandardArguments('Vhtwcv');
  21.  
  22.                 $this->getHelpManager()->setPluginName('check_x509_expire');
  23.                 $this->getHelpManager()->setVersion('1.0');
  24.                 $this->getHelpManager()->setShortDescription('This plugin checks X.509 (PEM) files and warns if certificates are about to expire.');
  25.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  26.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-v] -w <warnSeconds>s -c <critSeconds>s -f "[#]<mask>" [-f "[#]<mask>" [...]]');
  27.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  28.  
  29.                 // Individual (non-standard) arguments:
  30.                 $this->addExpectedArgument($this->argFiles = new VNagArgument('f', 'file', VNagArgument::VALUE_REQUIRED, 'mask', 'The files to be checked. This argument can be used multiple times. Wilcards may be used but MUST be passed as string only (not resolved by the Shell). There are two possible checking modes: If you put a # in front of the file mask, only the oldest file of each group will be checked (use this mode e.g. if you have a directory which contains old backups of certificates beside the current working certificate). Otherwise, all files of the file group are checked.'));
  31.  
  32.                 // In this context, when the user writes "-w 60s" then he actually means "-w @~:60s" or "-w 60s:~", so these commands allow this notation:
  33.                 $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_LT_X_BAD;
  34.                 $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_LT_X_BAD;
  35.         }
  36.  
  37.         private static function humanFriendlyTimeLeft($secs) {
  38.                 $out = array();
  39.  
  40.                 if ($expired = $secs < 0) $secs *= -1;
  41.  
  42.                 $years = floor($secs / 60 / 60 / 24 / 365);
  43.                 if ($years > 0) $out[] = $years == 1 ? "$years year" : "$years years";
  44.  
  45.                 $days = floor($secs / 60 / 60 / 24) % 365;
  46.                 if ($days > 0) $out[] = $days == 1 ? "$days day" : "$days days";
  47.  
  48.                 $hours = floor($secs / 60 / 60) % 24;
  49.                 if ($hours > 0) $out[] = $hours == 1 ? "$hours hour" : "$hours hours";
  50.  
  51.                 $minutes = floor($secs / 60) % 60;
  52.                 if ($minutes > 0) $out[] = $minutes == 1 ? "$minutes minute" : "$minutes minutes";
  53.  
  54.                 $seconds = $secs % 60;
  55.                 if ($seconds > 0) $out[] = $seconds == 1 ? "$seconds second" : "$seconds seconds";
  56.  
  57.                 return ($expired ? 'EXPIRED SINCE ' : '').implode(", ", $out).($expired ? '' : ' left');
  58.         }
  59.  
  60.         private static function timeLeft($pemFile) {
  61.                 $out = array();
  62.  
  63.                 // TODO: Call PHP's openssl functions instead
  64.                 exec("openssl x509 -in ".escapeshellarg($pemFile)." -noout -text | grep \"Not After\" | cut -d ':' -f 2-", $out, $code); // TODO: check $code
  65.                 if ($code != 0) {
  66.                         throw new VNagException("Error calling openssl!");
  67.                 }
  68.  
  69.                 $tim = strtotime($out[0]);
  70.                 return $tim - time();
  71.         }
  72.  
  73.         protected function cbRun($optional_args=array()) {
  74.                 $this->argFiles->require();
  75.  
  76.                 $countFilesTotal = 0;
  77.                 $countFilesCrit = 0;
  78.                 $countFilesWarn = 0;
  79.  
  80.                 $fileGroupMasks = $this->argFiles->getValue();
  81.                 if (!is_array($fileGroupMasks)) $fileGroupMasks = array($fileGroupMasks);
  82.                 foreach ($fileGroupMasks as $fileGroupMask) {
  83.                         if (substr($fileGroupMask, 0, 1) === '#') {
  84.                                 $fileGroupMask = substr($fileGroupMask, 1); // remove #
  85.  
  86.                                 // Mode 1: Only the youngest file of each group is checked.
  87.                                 // You can use this mode e.g. if you have a folder with downloaded files
  88.                                 // and you want to check if a downloading-script is still downloading
  89.                                 // new files regularly.
  90.  
  91.                                 $files = glob($fileGroupMask);
  92.                                 if (count($files) == 0) continue;
  93.  
  94.                                 $minTimeLeft = null;
  95.                                 foreach ($files as $file) {
  96.                                         $minTimeLeft = is_null($minTimeLeft) ? filemtime($file) : min($minTimeLeft, self::timeLeft($file));
  97.                                 }
  98.  
  99.                                 $countFilesTotal++;
  100.                                 if ($this->checkAgainstCriticalRange($minTimeLeft.'s', false, true)) {
  101.                                         $countFilesCrit++;
  102.                                         $this->addVerboseMessage("File group '$fileGroupMask' oldest file: ".self::humanFriendlyTimeLeft($minTimeLeft)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  103.                                 } else if ($this->checkAgainstWarningRange($minTimeLeft.'s', false, true)) {
  104.                                         $countFilesWarn++;
  105.                                         $this->addVerboseMessage("File group '$fileGroupMask' oldest file: ".self::humanFriendlyTimeLeft($minTimeLeft)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  106.                                 } else {
  107.                                         if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  108.                                                 $this->addVerboseMessage("File group '$fileGroupMask' oldest file: ".self::humanFriendlyTimeLeft($minTimeLeft)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  109.                                         } else {
  110.                                                 $this->addVerboseMessage("File group '$fileGroupMask' oldest file: ".self::humanFriendlyTimeLeft($minTimeLeft)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  111.                                         }
  112.                                 }
  113.                         } else {
  114.                                 // Mode 2: All files of each group are checked.
  115.  
  116.                                 $files = glob($fileGroupMask);
  117.                                 if (count($files) == 0) continue;
  118.  
  119.                                 foreach ($files as $file) {
  120.                                         $timeLeft = self::timeLeft($file);
  121.                                         $countFilesTotal++;
  122.                                         if ($this->checkAgainstCriticalRange($timeLeft.'s', false, true)) {
  123.                                                 $countFilesCrit++;
  124.                                                 $this->addVerboseMessage("File $file: ".self::humanFriendlyTimeLeft($timeLeft)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  125.                                         } else if ($this->checkAgainstWarningRange($timeLeft.'s', false, true)) {
  126.                                                 $countFilesWarn++;
  127.                                                 $this->addVerboseMessage("File $file: ".self::humanFriendlyTimeLeft($timeLeft)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  128.                                         } else {
  129.                                                 if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  130.                                                         $this->addVerboseMessage("File $file: ".self::humanFriendlyTimeLeft($timeLeft)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  131.                                                 } else {
  132.                                                         $this->addVerboseMessage("File $file: ".self::humanFriendlyTimeLeft($timeLeft)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  133.                                                 }
  134.                                         }
  135.                                 }
  136.                         }
  137.                 }
  138.  
  139.                 $msg = array();
  140.                 $msg[] = "Checked $countFilesTotal certificates";
  141.                 if ($this->getArgumentHandler()->getArgumentObj('w')->available()) $msg[] = "$countFilesWarn are in warning time range";
  142.                 if ($this->getArgumentHandler()->getArgumentObj('c')->available()) $msg[] = "$countFilesCrit are in critical time range";
  143.                 $msg = implode(", ", $msg);
  144.  
  145.                 $this->setHeadLine($msg);
  146.         }
  147. }
  148.