-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime.pl
More file actions
80 lines (65 loc) · 1.64 KB
/
Copy pathrealtime.pl
File metadata and controls
80 lines (65 loc) · 1.64 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Digest::file qw(digest_file_hex);
my $DB_Name = "/usr/lib/nagios/plugins/save.ini";
my %map_digest;
my @fileList;
my $foldername = $ARGV[0];
#my $foldername = "/usr/lib/nagios/plugins/ParentFolder";
my $is_first = 1;
#print "Phong ", $foldername; exit(1);
monitorFolder($foldername);
sub monitorFolder {
my $directory = shift(@_);
ReadDB();
opendir (DIR, $directory) or print "Could not open file '$directory' $!";
find(\&getFile, $directory);
# print %map_digest;
# print @fileList;
foreach my $filename (@fileList){
# New file
if (!scalar($map_digest{$filename})) { doPrint($filename, "Created."); }
# Change File
elsif ($map_digest{$filename} ne digest_file_hex($filename, "MD5")) {
my $text = "Changed ";
&doPrint($filename, $text);
}
}
closedir(DIR);
foreach my $filename (keys %map_digest) {
if (!-e $filename) {
&doPrint($filename, "Deleted.");
}
}
if($is_first){
print "All file is OK";
exit(0);
}else{
exit(1);
}
}
sub ReadDB {
open(my $fh, '<', $DB_Name) or print "Could not open file '$DB_Name' $!";
while(my $line = <$fh>){
chomp($line);
my ($key,$digest) = split(':', $line);
$map_digest{$key} = $digest;
}
close($fh);
}
sub getFile {
if (-d){
}else{
push @fileList, $File::Find::name;
}
}
# Print out a file change in a consistent format
sub doPrint {
my $fn = shift(@_);
my $t = shift(@_);
print $fn."\t\t".$t."\n";
$is_first = 0;
}