Subversion Repositories vnag

Rev

Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * VNag - Nagios Framework for PHP
5
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
6
 * Licensed under the terms of the Apache 2.0 license
7
 *
79 daniel-mar 8
 * Revision 2023-10-13
2 daniel-mar 9
 */
10
 
11
// TODO: New parameter: alternatively check for the creation timestamp instead of the modification timestamp.
12
// 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
13
 
14
declare(ticks=1);
15
 
16
class FileTimestampCheck extends VNag {
17
        protected $argFiles = null;
18
 
19
        public function __construct() {
20
                parent::__construct();
21
 
22
                $this->registerExpectedStandardArguments('Vhtwcv');
23
 
24
                $this->getHelpManager()->setPluginName('check_file_timestamp');
79 daniel-mar 25
                $this->getHelpManager()->setVersion('2023-10-13');
2 daniel-mar 26
                $this->getHelpManager()->setShortDescription('This plugin checks if local files are within a specific timestamp.');
27
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
28
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-v] -w <warnSeconds>s -c <critSeconds>s -f "[#]<mask>" [-f "[#]<mask>" [...]]');
29
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
30
 
31
                // Individual (non-standard) arguments:
32
                $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.'));
33
 
52 daniel-mar 34
#               $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
35
#               $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
2 daniel-mar 36
        }
37
 
38
        private static function humanFriendlyInterval($secs) {
39
                $out = array();
40
 
41
                $years = floor($secs / 60 / 60 / 24 / 365);
42
                if ($years > 0) $out[] = $years == 1 ? "$years year" : "$years years";
43
 
44
                $days = floor($secs / 60 / 60 / 24) % 365;
45
                if ($days > 0) $out[] = $days == 1 ? "$days day" : "$days days";
46
 
47
                $hours = floor($secs / 60 / 60) % 24;
48
                if ($hours > 0) $out[] = $hours == 1 ? "$hours hour" : "$hours hours";
49
 
50
                $minutes = floor($secs / 60) % 60;
51
                if ($minutes > 0) $out[] = $minutes == 1 ? "$minutes minute" : "$minutes minutes";
52
 
53
                $seconds = $secs % 60;
54
                if ($seconds > 0) $out[] = $seconds == 1 ? "$seconds second" : "$seconds seconds";
55
 
56
                return implode(", ", $out);
57
        }
58
 
59
        protected function cbRun($optional_args=array()) {
60
                $this->argFiles->require();
61
 
62
                $countFilesTotal = 0;
63
                $countFilesCrit = 0;
64
                $countFilesWarn = 0;
65
 
66
                $fileGroupMasks = $this->argFiles->getValue();
67
                if (!is_array($fileGroupMasks)) $fileGroupMasks = array($fileGroupMasks);
68
                foreach ($fileGroupMasks as $fileGroupMask) {
69
                        if (substr($fileGroupMask, 0, 1) === '#') {
70
                                $fileGroupMask = substr($fileGroupMask, 1); // remove #
71
 
72
                                // Mode 1: Only the youngest file of each group is checked.
73
                                // You can use this mode e.g. if you have a folder with downloaded files
74
                                // and you want to check if a downloading-script is still downloading
75
                                // new files regularly.
76
 
77
                                $files = glob($fileGroupMask);
78
                                if (count($files) == 0) continue;
79
 
80
                                $youngestTS = null;
81
                                foreach ($files as $file) {
82
                                        $youngestTS = is_null($youngestTS) ? filemtime($file) : max($youngestTS, filemtime($file));
83
                                }
84
 
85
                                $youngestAge = time() - $youngestTS;
86
                                $countFilesTotal++;
87
                                if ($this->checkAgainstCriticalRange($youngestAge.'s', false, true)) {
88
                                        $countFilesCrit++;
89
                                        $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
90
                                } else if ($this->checkAgainstWarningRange($youngestAge.'s', false, true)) {
91
                                        $countFilesWarn++;
92
                                        $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
93
                                } else {
94
                                        if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
95
                                                $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
96
                                        } else {
97
                                                $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
98
                                        }
99
                                }
100
                        } else {
101
                                // Mode 2: All files of each group are checked.
102
 
103
                                $files = glob($fileGroupMask);
104
                                if (count($files) == 0) continue;
105
 
106
                                foreach ($files as $file) {
107
                                        $age = time() - filemtime($file);
108
                                        $countFilesTotal++;
109
                                        if ($this->checkAgainstCriticalRange($age.'s', false, true)) {
110
                                                $countFilesCrit++;
111
                                                $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
112
                                        } else if ($this->checkAgainstWarningRange($age.'s', false, true)) {
113
                                                $countFilesWarn++;
114
                                                $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
115
                                        } else {
116
                                                if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
117
                                                        $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
118
                                                } else {
119
                                                        $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
120
                                                }
121
                                        }
122
                                }
123
                        }
124
                }
125
 
126
                $msg = array();
127
                $msg[] = "Checked $countFilesTotal files";
128
                if ($this->getArgumentHandler()->getArgumentObj('w')->available()) $msg[] = "$countFilesWarn are in warning time range";
129
                if ($this->getArgumentHandler()->getArgumentObj('c')->available()) $msg[] = "$countFilesCrit are in critical time range";
130
                $msg = implode(", ", $msg);
131
 
132
                $this->setHeadLine($msg);
133
        }
134
}