Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. zGZPz4tVkmDMjrF+v+xuP5dMnqRI4kbdU8ojps0jpGMRwVFzfd6cmeRy1+67gqLA9
  3. 4NEGq7tz9T+gVN8HmmxkW4BiD6dWzrcLrZwtiiVx/C8LS7SB71SKfpVaISx0v2RjH
  4. ek77YzcQQaC6v1D/p1iPFjNJe1W/XITfCdGQjUxODY84f5iyMRFgeCl4/TK1xqeaC
  5. aiBvMbyr/ypo+Zq90TtXDQdYezgF8xQv/yDgP/qYGKTDq7ya3wUjGmCR+DViT6DFQ
  6. 6doi/kAGWTsa9KflYYcuYY9L/MJnDsSXZqFjc30BBqxsAjZaQZcQnZHYLmlmyEIBt
  7. YZn/UOG9Z67E6/tq1OBW8styEyw59z4DFNB+uIqLtx5Ip/JLoNcEfG4jxQVISZ1QC
  8. xzYHQ+LaaTk3yDM5hIQ4Cm5z+L6w9YfQ+JGho0CUGDgo0QL+0f4fKW+YETrcEzwYO
  9. VqX7Ry6DujKgTWIVKkU1yT/7bg3KdPAJKHaHuBWqqHIMCuTE+muhJd/t6jyOXcuay
  10. GKlI3jey33fdrW7CaVpMsUDeFmM7QRMPrvWv+HiY0fu406F6dlQxPSqmMZHTxZ4UH
  11. 1LkREzFBEyJet7vlazVrLKBmWk83l37lwWrlL5Ehw0RtZGanLLLxNqyDfh6iGjRI9
  12. yJDOI+68oMoeF5rnchLJYJ2bMJ2lqJ+0cGy9foFdbCPqd+iPDK/KwmNHXwkZQvuDe
  13. KLuJycCKUt2x2CMoIX6GkF4jHAC860AZ2f0A9n6OFXPHfDgZAwWhdKYK8M6ob0QVS
  14. TICsBiFAMKkQM8t/BM2FnUPgB1a/m9wPaao/4Xx7TFwvvAqZaJe4iQNhwDvji92V/
  15. uRELz831J5lNi84PmY8WAqQLzWf8Uo5L2noZ/+EQiv6xydKig0hGq0WOaxSalbd6E
  16. 9JYqKXRny2gAhEEIjWzhKyd4yUa4z3WOWe2r8wGTF06y6nbtTaOvVtUKWSC/0ZS9v
  17. 6fxdmv37TLxZczJ1XNQpRpCPkOngz11ZYuBV0jR23VZfW//CKyV89YVufp/w6lQ74
  18. V1RCjIYb8tcIS8L8QTQB1JCqFFyUMhgOmFEYHZSF0BZYicXO78i7wK3wbZqjo0WMm
  19. ySw6Yy1v1yYPB2xCFiXRCLSsiQTAPwxAghOWMdCWhx5qXTPmNbvZXOuyKQCbFPxJz
  20. jYpmPGIfI2Zhiu5Fdmx58NjcJBHNFQJ9Km0FpVb1TOpPdTaWvi6Ms6ecivzOPV9Wz
  21. FtSkA5R0Hr5pBcRgGRMVkJqSEyeebJfWKhBcjNSHmTcuwCE3s/WcOAhZRzq9dnYY6
  22. cTBcMfodXMOiICU5nq9Zt5NsLFSNI+5q8bghPmfy5mQ7cobhNge1lJme6hUXV4CRT
  23. w==
  24. </ViaThinkSoftSignature> */ ?>
  25. <?php
  26.  
  27. /*
  28.  * VNag - Nagios Framework for PHP
  29.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  30.  * Licensed under the terms of the Apache 2.0 license
  31.  *
  32.  * Revision 2018-07-15
  33.  */
  34.  
  35. // TODO: New parameter: alternatively check for the creation timestamp instead of the modification timestamp.
  36. // 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
  37.  
  38. declare(ticks=1);
  39.  
  40. class FileTimestampCheck extends VNag {
  41.         protected $argFiles = null;
  42.  
  43.         public function __construct() {
  44.                 parent::__construct();
  45.  
  46.                 $this->registerExpectedStandardArguments('Vhtwcv');
  47.  
  48.                 $this->getHelpManager()->setPluginName('check_file_timestamp');
  49.                 $this->getHelpManager()->setVersion('1.0');
  50.                 $this->getHelpManager()->setShortDescription('This plugin checks if local files are within a specific timestamp.');
  51.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  52.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-v] -w <warnSeconds>s -c <critSeconds>s -f "[#]<mask>" [-f "[#]<mask>" [...]]');
  53.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  54.  
  55.                 // Individual (non-standard) arguments:
  56.                 $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.'));
  57.  
  58.                 // 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:
  59.                 $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  60.                 $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  61.         }
  62.  
  63.         private static function humanFriendlyInterval($secs) {
  64.                 $out = array();
  65.  
  66.                 $years = floor($secs / 60 / 60 / 24 / 365);
  67.                 if ($years > 0) $out[] = $years == 1 ? "$years year" : "$years years";
  68.  
  69.                 $days = floor($secs / 60 / 60 / 24) % 365;
  70.                 if ($days > 0) $out[] = $days == 1 ? "$days day" : "$days days";
  71.  
  72.                 $hours = floor($secs / 60 / 60) % 24;
  73.                 if ($hours > 0) $out[] = $hours == 1 ? "$hours hour" : "$hours hours";
  74.  
  75.                 $minutes = floor($secs / 60) % 60;
  76.                 if ($minutes > 0) $out[] = $minutes == 1 ? "$minutes minute" : "$minutes minutes";
  77.  
  78.                 $seconds = $secs % 60;
  79.                 if ($seconds > 0) $out[] = $seconds == 1 ? "$seconds second" : "$seconds seconds";
  80.  
  81.                 return implode(", ", $out);
  82.         }
  83.  
  84.         protected function cbRun($optional_args=array()) {
  85.                 $this->argFiles->require();
  86.  
  87.                 $countFilesTotal = 0;
  88.                 $countFilesCrit = 0;
  89.                 $countFilesWarn = 0;
  90.  
  91.                 $fileGroupMasks = $this->argFiles->getValue();
  92.                 if (!is_array($fileGroupMasks)) $fileGroupMasks = array($fileGroupMasks);
  93.                 foreach ($fileGroupMasks as $fileGroupMask) {
  94.                         if (substr($fileGroupMask, 0, 1) === '#') {
  95.                                 $fileGroupMask = substr($fileGroupMask, 1); // remove #
  96.  
  97.                                 // Mode 1: Only the youngest file of each group is checked.
  98.                                 // You can use this mode e.g. if you have a folder with downloaded files
  99.                                 // and you want to check if a downloading-script is still downloading
  100.                                 // new files regularly.
  101.  
  102.                                 $files = glob($fileGroupMask);
  103.                                 if (count($files) == 0) continue;
  104.  
  105.                                 $youngestTS = null;
  106.                                 foreach ($files as $file) {
  107.                                         $youngestTS = is_null($youngestTS) ? filemtime($file) : max($youngestTS, filemtime($file));
  108.                                 }
  109.  
  110.                                 $youngestAge = time() - $youngestTS;
  111.                                 $countFilesTotal++;
  112.                                 if ($this->checkAgainstCriticalRange($youngestAge.'s', false, true)) {
  113.                                         $countFilesCrit++;
  114.                                         $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  115.                                 } else if ($this->checkAgainstWarningRange($youngestAge.'s', false, true)) {
  116.                                         $countFilesWarn++;
  117.                                         $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  118.                                 } else {
  119.                                         if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  120.                                                 $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  121.                                         } else {
  122.                                                 $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  123.                                         }
  124.                                 }
  125.                         } else {
  126.                                 // Mode 2: All files of each group are checked.
  127.  
  128.                                 $files = glob($fileGroupMask);
  129.                                 if (count($files) == 0) continue;
  130.  
  131.                                 foreach ($files as $file) {
  132.                                         $age = time() - filemtime($file);
  133.                                         $countFilesTotal++;
  134.                                         if ($this->checkAgainstCriticalRange($age.'s', false, true)) {
  135.                                                 $countFilesCrit++;
  136.                                                 $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
  137.                                         } else if ($this->checkAgainstWarningRange($age.'s', false, true)) {
  138.                                                 $countFilesWarn++;
  139.                                                 $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
  140.                                         } else {
  141.                                                 if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
  142.                                                         $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  143.                                                 } else {
  144.                                                         $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  145.                                                 }
  146.                                         }
  147.                                 }
  148.                         }
  149.                 }
  150.  
  151.                 $msg = array();
  152.                 $msg[] = "Checked $countFilesTotal files";
  153.                 if ($this->getArgumentHandler()->getArgumentObj('w')->available()) $msg[] = "$countFilesWarn are in warning time range";
  154.                 if ($this->getArgumentHandler()->getArgumentObj('c')->available()) $msg[] = "$countFilesCrit are in critical time range";
  155.                 $msg = implode(", ", $msg);
  156.  
  157.                 $this->setHeadLine($msg);
  158.         }
  159. }
  160.