-
Notifications
You must be signed in to change notification settings - Fork 0
/
parcheck.pl
executable file
·87 lines (75 loc) · 2.49 KB
/
parcheck.pl
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
#!/usr/bin/perl
# Script to check downloads made from the par.pl script. If this checking
# script is periodically run it will detect if a programme has been skipped.
#
# Note that this script currently assumes that programmes are downloaded
# weekly (ie every seven days). With more advanced crontab parsing that
# restriction could be removed.
#
# Copyright (c) Stuart Hickinbottom 2009
use warnings;
use strict;
use Config::Crontab;
use Getopt::Long;
use Mail::Sendmail;
use constant MAXAGE => 7.0;
use constant NOAGE => 10000;
use constant MARKER => '.checkpar';
use constant FROM => 'checkpar@hickinbottom.com';
use constant SMTP => 'post.demon.co.uk';
my $verbose;
my $mailto;
GetOptions('verbose' => \$verbose,
'mailto=s' => \$mailto);
my $ct = new Config::Crontab;
$ct->read;
# Find all the par.pl lines that are active.
for my $line ($ct->select(-command_re => 'par.pl', -active => 1)) {
# Identify the directory that was being recorded to.
my $command = $line->command;
if ($command =~ /--outputdir ?"(.*?)"/) {
my $dir = $1;
print "Processing directory $dir\n" if $verbose;
# Now find all the files (not directories) in those directories and
# process them.
opendir(DIR, $dir);
my @names = readdir(DIR);
my $newest = NOAGE;
my $newest_file;
foreach my $name (@names) {
if (-f "$dir/$name") {
my $age = -M "$dir/$name";
if ($age < $newest) {
$newest = $age;
$newest_file = "$dir/$name";
}
}
}
print "Newest file in $dir is $newest days old\n" if $verbose;
# If the newest is older than the threshold, then a recording might
# be missing.
if (($newest > MAXAGE) and ($newest != NOAGE)) {
print "$dir may be missing a recording\n" if $verbose;
if ((! -f "$dir/" . MARKER) || (-M ("$dir/" . MARKER) > -M $newest_file)) {
# Create/update the marker file. We do this so we don't
# keep reminding the user.
open(FILEHANDLE, ">$dir/" . MARKER) || die('Cannot create file: ' . $!);
close(FILEHANDLE);
# Alert the user.
if ($mailto) {
print "Sending notification to $mailto\n" if $verbose;
my %mail = (
To => $mailto,
From => FROM,
smtp => SMTP,
Subject => "Possible recording missing from $dir",
Message => "There might be a recording missing from '$dir' - the newest recording in that directory is " . int($newest) . " days old"
);
sendmail(%mail) or die $Mail::Sendmail::error;
}
}
} else {
print "But user user has already been warned so not doing so again\n" if $verbose;
}
}
}