File: rd.monitor

package info (click to toggle)
mon 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,860 kB
  • ctags: 538
  • sloc: perl: 15,847; ansic: 774; sh: 330; makefile: 46
file content (160 lines) | stat: -rwxr-xr-x 4,864 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl

# readdir.monitor
# Return a list of directories that contains more than given files numbers

# For use with "mon" or stand alone.
#

#
# Usage : my-mailqueue.monitor [options] [dir1[:num1] dir2[:num2] ...]
# 
# --number    n      : the maximum file number allowed
# --regex     string : a regex expression to match
# --debug            : print some debug information (do not use this with mon)
#                      but just in command line to understand everything :-)
# dir1 dir2          : list of directory to check

# Examples:
#
# Do nothing (nothing to check)
# $ ./my-readdir.monitor
#
# Checks if:
# /var/spool/mqueue contains more than 50 files (50 is a default value)
#
# $ ./my-readdir.monitor /var/spool/mqueue/
#
# Checks if:
# /var/spool/mqueue contains more than 14 files
#
# $ ./my-readdir.monitor /var/spool/mqueue/:14
#
# Check if :
# /var/spool/mqueue          contains more than 14 files
# /var/spool/lp/requests     contains more than  7 files
#
# $ ./my-readdir.monitor /var/spool/mqueue/:14 /var/spool/lp/requests:7
#

# Check if :
# /var                       contains more than 34 files
# /var/spool                 contains more than 34 files
# /bin                       contains more than 65 files
#
# $ ./my-readdir.monitor --number=34 /var /var/spool /bin:65
#

# Check if :
# /var/spool/mqueue          contains more than 3 files which name
#                            begins with "df"
#
# $ ./my-readdir.monitor --number=3 --regex "^df" /var/spool/mqueue
#

# The regex can be every perl regex.



# this program exits with the $maxlogNumberFound in log based 2 fashion.
# file number found = 1 * number Allowed,  log = 1
# file number found = 2 * number Allowed,  log = 2
# file number found = 4 * number Allowed,  log = 3
#                   .
#                   .
#                   .
#                  2^n * $numberAllowed, log = n+1
#
# Reverse:
# if the return status is N, that means that the directories
# contains more (or equal) than 2^(N-1) *  allowed files number
#    and   less            than 2^N     *  allowed files number

# The worse situation is return (in case of several directories)

# 
#    Gilles LAMIRAL, lamiral@mail.dotcom.fr
#
#
#    Copyright (C) 1998, Gilles LAMIRAL

#    This program  is free  software;  you can redistribute it  and/or
# modify it   under the terms  of  the GNU  General Public  License as
# published by the Free  Software Foundation; either  version 2 of the
# License, or (at your option) any later version.

#    This program is  distributed in the hope that  it will be useful,
# but  WITHOUT ANY WARRANTY;  without   even the implied  warranty  of
# MERCHANTABILITY  or FITNESS FOR A PARTICULAR  PURPOSE.   See the GNU
# General Public License for more details.

#    You should have received a copy of the GNU General Public License
# along  with this program;   if  not,  write  to  the  Free  Software
# Foundation, Inc., 59 Temple Place,  Suite 330, Boston, MA 02111-1307
# USA #


use Getopt::Long;
use DirHandle;


GetOptions(
	"number:i"	=> \$globalNumberAllowed,
	"debug"		=> \$debug,
	"regex:s"	=> \$regex
	);

$globalNumberAllowed = ($globalNumberAllowed) ? $globalNumberAllowed : "50";

@failures = ();
$maxlogNumberFound = 0;

foreach $dir (@ARGV) {	
	my ($dirHandle, @filesList, @filesListFiltered, $numberFound);
	($dir, $numberAllowed) = split (/:/, $dir, 2);
	$numberAllowed = ($numberAllowed) ? $numberAllowed : $globalNumberAllowed;
	($debug) and print "directory checked      : $dir\n";
	($debug) and print "   number of file allowed : $numberAllowed\n";
	
	$dirHandle = new DirHandle "$dir";
	if (defined $dirHandle) {
		# reads the directory and filters "." and ".." files 
		@filesList = grep !/^\.\.?$/, $dirHandle->read();
		$dirHandle->close();
		if ($regex) {
			foreach $file (@filesList) {
				push(@filesListFiltered, $file) if ($file =~ /$regex/);
			}
			@filesList = @filesListFiltered;
		}
		$numberFound = scalar(@filesList);
		#($debug) and print "@filesList\n";
		($debug) and print "   number of files	  : $numberFound\n";
		if ($numberFound >= $numberAllowed) {
			push (@failures, sprintf ("%s:%s", $dir, $numberFound));
			$logNumberFound = 1+(log($numberFound / $numberAllowed)/log(2));
			($debug) and print "   1+(log($numberFound/$numberAllowed)/log(2)) = ", $logNumberFound,"\n";
			if ($logNumberFound > $maxlogNumberFound) {
				$maxlogNumberFound = $logNumberFound;
			}
	    }
	}else{
		warn "Could not open $dir : $!\, warn at";
		push (@failures, sprintf ("%s:%s", $dir, "COULD_NOT_OPEN"));
	};
}

if (@failures == 0) {
	exit 0;
}
($debug) and print "\nSummary:";

print join (" ", sort @failures), "\n";

if ($maxlogNumberFound >= 1) {
	($debug) and print "maxlogNumberFound (exit status) =$maxlogNumberFound\n";
	$exitStatus = int($maxlogNumberFound);
}else{
	$exitStatus = 1;
}
exit($exitStatus);