Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>zGZPz4tVkmDMjrF+v+xuP5dMnqRI4kbdU8ojps0jpGMRwVFzfd6cmeRy1+67gqLA94NEGq7tz9T+gVN8HmmxkW4BiD6dWzrcLrZwtiiVx/C8LS7SB71SKfpVaISx0v2RjHek77YzcQQaC6v1D/p1iPFjNJe1W/XITfCdGQjUxODY84f5iyMRFgeCl4/TK1xqeaCaiBvMbyr/ypo+Zq90TtXDQdYezgF8xQv/yDgP/qYGKTDq7ya3wUjGmCR+DViT6DFQ6doi/kAGWTsa9KflYYcuYY9L/MJnDsSXZqFjc30BBqxsAjZaQZcQnZHYLmlmyEIBtYZn/UOG9Z67E6/tq1OBW8styEyw59z4DFNB+uIqLtx5Ip/JLoNcEfG4jxQVISZ1QCxzYHQ+LaaTk3yDM5hIQ4Cm5z+L6w9YfQ+JGho0CUGDgo0QL+0f4fKW+YETrcEzwYOVqX7Ry6DujKgTWIVKkU1yT/7bg3KdPAJKHaHuBWqqHIMCuTE+muhJd/t6jyOXcuayGKlI3jey33fdrW7CaVpMsUDeFmM7QRMPrvWv+HiY0fu406F6dlQxPSqmMZHTxZ4UH1LkREzFBEyJet7vlazVrLKBmWk83l37lwWrlL5Ehw0RtZGanLLLxNqyDfh6iGjRI9yJDOI+68oMoeF5rnchLJYJ2bMJ2lqJ+0cGy9foFdbCPqd+iPDK/KwmNHXwkZQvuDeKLuJycCKUt2x2CMoIX6GkF4jHAC860AZ2f0A9n6OFXPHfDgZAwWhdKYK8M6ob0QVSTICsBiFAMKkQM8t/BM2FnUPgB1a/m9wPaao/4Xx7TFwvvAqZaJe4iQNhwDvji92V/uRELz831J5lNi84PmY8WAqQLzWf8Uo5L2noZ/+EQiv6xydKig0hGq0WOaxSalbd6E9JYqKXRny2gAhEEIjWzhKyd4yUa4z3WOWe2r8wGTF06y6nbtTaOvVtUKWSC/0ZS9v6fxdmv37TLxZczJ1XNQpRpCPkOngz11ZYuBV0jR23VZfW//CKyV89YVufp/w6lQ74V1RCjIYb8tcIS8L8QTQB1JCqFFyUMhgOmFEYHZSF0BZYicXO78i7wK3wbZqjo0WMmySw6Yy1v1yYPB2xCFiXRCLSsiQTAPwxAghOWMdCWhx5qXTPmNbvZXOuyKQCbFPxJzjYpmPGIfI2Zhiu5Fdmx58NjcJBHNFQJ9Km0FpVb1TOpPdTaWvi6Ms6ecivzOPV9WzFtSkA5R0Hr5pBcRgGRMVkJqSEyeebJfWKhBcjNSHmTcuwCE3s/WcOAhZRzq9dnYY6cTBcMfodXMOiICU5nq9Zt5NsLFSNI+5q8bghPmfy5mQ7cobhNge1lJme6hUXV4CRTw==</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-15
  10.  */
  11.  
  12. // TODO: New parameter: alternatively check for the creation timestamp instead of the modification timestamp.
  13. // TODO: vnag soll der timestamp monitor auch warnen, wenn gewisse dateien nicht gefunden werden? im moment macht er nur einen glob() und ignoriert damit leere ergebnisse
  14.  
  15. declare(ticks=1);
  16.  
  17. class FileTimestampCheck extends VNag {
  18.         protected $argFiles = null;
  19.  
  20.         public function __construct() {
  21.                 parent::__construct();
  22.  
  23.                 $this->registerExpectedStandardArguments('Vhtwcv');
  24.  
  25.                 $this->getHelpManager()->setPluginName('check_file_timestamp');
  26.                 $this->getHelpManager()->setVersion('1.0');
  27.                 $this->getHelpManager()->setShortDescription('This plugin checks if local files are within a specific timestamp.');
  28.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  29.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-v] -w <warnSeconds>s -c <critSeconds>s -f "[#]<mask>" [-f "[#]<mask>" [...]]');
  30.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  31.  
  32.                 // Individual (non-standard) arguments:
  33.                 $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 youngest file of each group will be checked (use this mode e.g. if you want to check if a downloader is regularly downloading files into a download directory). Otherwise, all files of the file group are checked.'));
  34.  
  35.                 // In this context, when the user writes "-w 60s" then he actually means "-w @60s:~" or "-w 0s:60s", so these commands allow this notation:
  36.                 $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  37.                 $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  38.         }
  39.  
  40.         private static function humanFriendlyInterval($secs) {
  41.                 $out = array();
  42.  
  43.                 $years = floor($secs / 60 / 60 / 24 / 365);
  44.                 if ($years > 0) $out[] = $years == 1 ? "$years year" : "$years years";
  45.  
  46.                 $days = floor($secs / 60 / 60 / 24) % 365;
  47.                 if ($days > 0) $out[] = $days == 1 ? "$days day" : "$days days";
  48.  
  49.                 $hours = floor($secs / 60 / 60) % 24;
  50.                 if ($hours > 0) $out[] = $hours == 1 ? "$hours hour" : "$hours hours";
  51.  
  52.                 $minutes = floor($secs / 60) % 60;
  53.                 if ($minutes > 0) $out[] = $minutes == 1 ? "$minutes minute" : "$minutes minutes";
  54.  
  55.                 $seconds = $secs % 60;
  56.                 if ($seconds > 0) $out[] = $seconds == 1 ? "$seconds second" : "$seconds seconds";
  57.  
  58.                 return implode(", ", $out);
  59.         }
  60.  
  61.         protected function cbRun($optional_args=array()) {
  62.                 $this->argFiles->require();
  63.  
  64.                 $countFilesTotal = 0;
  65.                 $countFilesCrit = 0;
  66.                 $countFilesWarn = 0;
  67.  
  68.                 $fileGroupMasks = $this->argFiles->getValue();
  69.                 if (!is_array($fileGroupMasks)) $fileGroupMasks = array($fileGroupMasks);
  70.                 foreach ($fileGroupMasks as $fileGroupMask) {
  71.                         if (substr($fileGroupMask, 0, 1) === '#') {
  72.                                 $fileGroupMask = substr($fileGroupMask, 1); // remove #
  73.  
  74.                                 // Mode 1: Only the youngest file of each group is checked.
  75.                                 // You can use this mode e.g. if you have a folder with downloaded files
  76.                                 // and you want to check if a downloading-script is still downloading
  77.                                 // new files regularly.
  78.  
  79.                                 $files = glob($fileGroupMask);
  80.                                 if (count($files) == 0) continue;
  81.  
  82.                                 $youngestTS = null;
  83.                                 foreach ($files as $file) {
  84.                                         $youngestTS = is_null($youngestTS) ? filemtime($file) : max($youngestTS, filemtime($file));
  85.                                 }
  86.  
  87.                                 $youngestAge = time() - $youngestTS;
  88.                                 $countFilesTotal++;
  89.                                 if ($this->checkAgainstCriticalRange($youngestAge.'s', false, true)) {
  90.                                         $countFilesCrit++;
  91.                                         $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  92.                                 } else if ($this->checkAgainstWarningRange($youngestAge.'s', false, true)) {
  93.                                         $countFilesWarn++;
  94.                                         $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  95.                                 } else {
  96.                                         if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  97.                                                 $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  98.                                         } else {
  99.                                                 $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  100.                                         }
  101.                                 }
  102.                         } else {
  103.                                 // Mode 2: All files of each group are checked.
  104.  
  105.                                 $files = glob($fileGroupMask);
  106.                                 if (count($files) == 0) continue;
  107.  
  108.                                 foreach ($files as $file) {
  109.                                         $age = time() - filemtime($file);
  110.                                         $countFilesTotal++;
  111.                                         if ($this->checkAgainstCriticalRange($age.'s', false, true)) {
  112.                                                 $countFilesCrit++;
  113.                                                 $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  114.                                         } else if ($this->checkAgainstWarningRange($age.'s', false, true)) {
  115.                                                 $countFilesWarn++;
  116.                                                 $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  117.                                         } else {
  118.                                                 if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  119.                                                         $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  120.                                                 } else {
  121.                                                         $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  122.                                                 }
  123.                                         }
  124.                                 }
  125.                         }
  126.                 }
  127.  
  128.                 $msg = array();
  129.                 $msg[] = "Checked $countFilesTotal files";
  130.                 if ($this->getArgumentHandler()->getArgumentObj('w')->available()) $msg[] = "$countFilesWarn are in warning time range";
  131.                 if ($this->getArgumentHandler()->getArgumentObj('c')->available()) $msg[] = "$countFilesCrit are in critical time range";
  132.                 $msg = implode(", ", $msg);
  133.  
  134.                 $this->setHeadLine($msg);
  135.         }
  136. }
  137.