-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindCore.pl
More file actions
88 lines (80 loc) · 1.91 KB
/
Copy pathfindCore.pl
File metadata and controls
88 lines (80 loc) · 1.91 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
81
82
83
84
85
86
87
88
#!/usr/bin/perl -w
$logfile = "message.txt";
if (-e $logfile) {
unlink $logfile;
}
open(LOG, ">>$logfile") or die "could not access $logfile: $!";
@coreFiles = &findCore;
if (@coreFiles) {
foreach $cfile (@coreFiles) {
chomp $cfile;
next if (&chkCore($cfile) != 0);
@ctime = &chkCTime($cfile);
&destroy($cfile, $ctime[0]);
}
close(LOG);
@message = &getMessage("$logfile");
&sendMail;
unlink $logfile;
}
sub destroy {
($rmfile, $time) = @_;
if ($time >= 4) {
writeLog(0, "erasing $rmfile...");
unlink $rmfile;
writeLog($?, "done\n");
}
}
sub chkCTime {
$fcore = shift;
$num_seconds = time - (stat($fcore))[9];
$diff = $num_seconds;
$seconds = $diff % 60;
$diff = ($diff - $seconds) / 60;
$minutes = $diff % 60;
$diff = ($diff - $minutes) / 60;
$hours = $diff % 24;
$days = ($diff - $hours) / 24;
writeLog(0, "The file is $days days and $hours:$minutes:$seconds old");
return($days, $hours, $minutes, $seconds);
}
sub chkCore {
$corefile = shift;
open(CORE, "file $corefile|") or die "could not execute $corefile: $!";
while (<CORE>) {
chomp;
writeLog(0, "$_");
if ($_ =~ /core/) {
return 0;
}
}
return 1;
}
sub findCore {
return `find / -name core -type f`;
}
sub getMessage {
$MESSAGE=shift;
open(MESSAGE);
@message=<MESSAGE>;
close(MESSAGE);
return @message;
}
sub writeLog {
my $code = $_[0];
my $msg = $_[1];
my @codeMap = ('', 'INFO', 'ACTN', 'UNDF', 'CRIT');
my $msgType = $codeMap[$code];
print LOG "$msgType $msg\n";
}
sub sendMail {
$host = `hostname`;
my $sendmail = "/usr/sbin/sendmail -t";
my $subject = "Subject: Core files on $host\n";
#$send_to = "UnixStructure\@dbs.co.il";
my $send_to = "dorond\@moia.gov.il\n";
open(SENDMAIL, "|$sendmail $send_to") or die "Cannot open $sendmail: $!";
print SENDMAIL $subject;
print SENDMAIL @message;
close(SENDMAIL);
}