Subversion Repositories vnag

Rev

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

Rev Author Line No. Line
4 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
52 daniel-mar 2
Pvh08nOB4+wOw/RYGUvi9TRprIwSRd60uk+/5xYr4kuU9aC8i3NojUOlHc1kY3o1x
3
m8zV1VPbyrXggHd/2gGxEP+JjQrrrFTiTfoOaZXnsqNnTkKak5hXvm7KedBcwMjDG
4
ph8jucKz7z4BovNE5AQ8A/qUSuur1AqsFPXOtsW+CS4MDnzYq/54k49V1wN7qHZ0b
5
+Y2pyNq0wf5LmQdPZSB39v8lOeKQRIlDtxVxMKlrkJF1g01PvE/GUSy5A5A7UDaYg
6
/UCDzq/nEtq5Catg3S4m+M+ftIpfw6PktcMboDNXtQnyyj3shWUmn3e2lEt2WzLqC
7
pMtNi0w1KOfO6hAg5oox/H97VbumMb24kxv4GaJSXmU38qjfmIA1S/OyBVuxRcPXO
8
gm0LTlAihLWg/nPKs3+819w0JkuxG2GJj+q84iStMcI8bYOE3N/EI3s+QbAltL/xN
9
4q87ZgS3WRKyY7swTD9HVLwEi/whfraYgfs3y93wV3Leh4u0fD5tuVQ2xSuROxKAc
10
9mwiqllxuBj/6Pp7XKhWWlqmmCuSW+bULzOKVwh8KaHtoOSDRYeVY0c3nFneODZBa
11
1malWf4MfeEvGMDiaXnvhXhA5dpPU6NPKTxngnz6v+nVpArc/OWYH9qAD3vImKy2j
12
bOrja4pR5oIeYT+bfbCxQ7l7vagOFswApiom+oFx1Gelmsh90SfMhmlN16lpgYLo4
13
+zJYbY82Oy9fhaTmn/yA3PPT0XhpJHjLAhjSH0FN9wUC+e9Xh9/BSB9lJP18OP/u9
14
jCvW803IuhAHiZwuGLIs55utuY2DITTpce14+R48YR6/ArsRcGodBrDko7Rbqw54g
15
WreaJCKAzuKngOJ7o7X9ET+Cl0aJV02b0W9UfLIljlHqMTXOsVzAQ5fla22pahToC
16
zU5QuiCcczEfGTtNKudK7ABhgYa5slr+p7q+nu8e0q8xFkHtT+rcfDshdg/hGNNuK
17
lYIX/1XBJ+2K+Jrx+a4jh2kt0k4VTcwmIYKHlL0s66HlHTfiyjdeUkBS8GfAkLmj4
18
J4/SSNb98XH/si+xoSsAUcJzgjXUhZcJLnS9icqLctPr6lARsyHE7sEyt+GcfxFxK
19
wG2ya3rMarVhmLW3XTdr0qLYE0Eb96PTsU0VUkxK+lNa6aNNqQarmdwSmRJ09dKDw
20
4wB6rmYO5Yk7v5Pkqtc6ulTqSBALsyHBv/lqIDM4htUOPfpGDUxtXumd9fM0uDAXW
21
/grWyFmIRTVw2csDEHpb4aZA1tOtlT1luw8QmWg0tbmWA06RgUNV6v/VuDWgJLXOI
22
OrR4n1izC/9ierFHNCyRxdzE2kVOhewObOa+2UJP+7HuChHdAa+cyVgmQe5F0615X
4 daniel-mar 23
w==
24
</ViaThinkSoftSignature> */ ?>
2 daniel-mar 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
 
52 daniel-mar 58
#               $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
59
#               $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
2 daniel-mar 60
        }
61
 
62
        private static function humanFriendlyInterval($secs) {
63
                $out = array();
64
 
65
                $years = floor($secs / 60 / 60 / 24 / 365);
66
                if ($years > 0) $out[] = $years == 1 ? "$years year" : "$years years";
67
 
68
                $days = floor($secs / 60 / 60 / 24) % 365;
69
                if ($days > 0) $out[] = $days == 1 ? "$days day" : "$days days";
70
 
71
                $hours = floor($secs / 60 / 60) % 24;
72
                if ($hours > 0) $out[] = $hours == 1 ? "$hours hour" : "$hours hours";
73
 
74
                $minutes = floor($secs / 60) % 60;
75
                if ($minutes > 0) $out[] = $minutes == 1 ? "$minutes minute" : "$minutes minutes";
76
 
77
                $seconds = $secs % 60;
78
                if ($seconds > 0) $out[] = $seconds == 1 ? "$seconds second" : "$seconds seconds";
79
 
80
                return implode(", ", $out);
81
        }
82
 
83
        protected function cbRun($optional_args=array()) {
84
                $this->argFiles->require();
85
 
86
                $countFilesTotal = 0;
87
                $countFilesCrit = 0;
88
                $countFilesWarn = 0;
89
 
90
                $fileGroupMasks = $this->argFiles->getValue();
91
                if (!is_array($fileGroupMasks)) $fileGroupMasks = array($fileGroupMasks);
92
                foreach ($fileGroupMasks as $fileGroupMask) {
93
                        if (substr($fileGroupMask, 0, 1) === '#') {
94
                                $fileGroupMask = substr($fileGroupMask, 1); // remove #
95
 
96
                                // Mode 1: Only the youngest file of each group is checked.
97
                                // You can use this mode e.g. if you have a folder with downloaded files
98
                                // and you want to check if a downloading-script is still downloading
99
                                // new files regularly.
100
 
101
                                $files = glob($fileGroupMask);
102
                                if (count($files) == 0) continue;
103
 
104
                                $youngestTS = null;
105
                                foreach ($files as $file) {
106
                                        $youngestTS = is_null($youngestTS) ? filemtime($file) : max($youngestTS, filemtime($file));
107
                                }
108
 
109
                                $youngestAge = time() - $youngestTS;
110
                                $countFilesTotal++;
111
                                if ($this->checkAgainstCriticalRange($youngestAge.'s', false, true)) {
112
                                        $countFilesCrit++;
113
                                        $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
114
                                } else if ($this->checkAgainstWarningRange($youngestAge.'s', false, true)) {
115
                                        $countFilesWarn++;
116
                                        $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
117
                                } else {
118
                                        if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
119
                                                $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
120
                                        } else {
121
                                                $this->addVerboseMessage("File group '$fileGroupMask': Youngest file's age: ".self::humanFriendlyInterval($youngestAge)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
122
                                        }
123
                                }
124
                        } else {
125
                                // Mode 2: All files of each group are checked.
126
 
127
                                $files = glob($fileGroupMask);
128
                                if (count($files) == 0) continue;
129
 
130
                                foreach ($files as $file) {
131
                                        $age = time() - filemtime($file);
132
                                        $countFilesTotal++;
133
                                        if ($this->checkAgainstCriticalRange($age.'s', false, true)) {
134
                                                $countFilesCrit++;
135
                                                $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Critical)\n", VNag::VERBOSITY_SUMMARY);
136
                                        } else if ($this->checkAgainstWarningRange($age.'s', false, true)) {
137
                                                $countFilesWarn++;
138
                                                $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (Warning)\n", VNag::VERBOSITY_SUMMARY);
139
                                        } else {
140
                                                if (($this->getArgumentHandler()->getArgumentObj('w')->available()) || ($this->getArgumentHandler()->getArgumentObj('c')->available())) {
141
                                                        $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)." (OK)\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
142
                                                } else {
143
                                                        $this->addVerboseMessage("File $file age ".self::humanFriendlyInterval($age)."\n", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
144
                                                }
145
                                        }
146
                                }
147
                        }
148
                }
149
 
150
                $msg = array();
151
                $msg[] = "Checked $countFilesTotal files";
152
                if ($this->getArgumentHandler()->getArgumentObj('w')->available()) $msg[] = "$countFilesWarn are in warning time range";
153
                if ($this->getArgumentHandler()->getArgumentObj('c')->available()) $msg[] = "$countFilesCrit are in critical time range";
154
                $msg = implode(", ", $msg);
155
 
156
                $this->setHeadLine($msg);
157
        }
158
}