in reply to lock files vs. non-predictable file names
#!/usr/bin/perl -w # Run this perl code from cron every 5 minutes # 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /.ping_hosts.pl 2>&1 # use strict; use Net::Ping; my $private_dir = "/tmp/ping_hosts_dir"; sub check_private_dir { unless (-e $private_dir){ mkdir($private_dir, 0700) or die "Can't +mkdir $private_dir\n" } if (-d $private_dir) { my $dec_mode = (stat($private_dir))[2]; my $oct_mode = sprintf "%lo", $dec_mode; if (-O $private_dir and ($oct_mode == 40700)) { return } elsif (-O $private_dir and ($oct_mode != 40700)) { chmod(0700, $private_dir); return; } else { die "We don't own the directory\n" } } else { die "$private_dir exists and is not a directory\n"; } } sub ping_hosts { my $ws; my $timeout = 2; my @hosts = qw(host1 host2 host3 host4); my $ping_obj = Net::Ping->new("icmp"); foreach $ws (@hosts) { unless ($ping_obj->ping($ws, $timeout)) { print "$ws did not respond to ping within the $timeout sec +ond timeout period\n"; notify_admins($ws); } sleep(1); # To prevent flooding } $ping_obj->close(); # Redundant since open network conn is auto-cl +osed when leaving sub } sub notify_admins { # Called from ping_hosts() my $ws = shift(@_); my $nfile = "${ws}.notify"; my $admins = "admin1\@mailhost.com, " . "18005551212\@page.com, " . "admin2\@mailhost.com, " . "19005551212\@page.com"; unless (-e "${private_dir}/${nfile}") { open(NOTIFY, ">>${private_dir}/${nfile}") or die "Can't create + notify file\n"; close(NOTIFY); open(MAIL, "|mailx $admins") or die "Can't fork for mailx: $!\ +n"; print MAIL "$ws doesn't respond to ping\n"; close(MAIL); } } ### MAIN ### check_private_dir(); ping_hosts();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: lock files vs. non-predictable file names
by merlyn (Sage) on Aug 26, 2000 at 08:29 UTC | |
by RuphSkunk (Acolyte) on Aug 28, 2000 at 21:35 UTC | |
by merlyn (Sage) on Aug 28, 2000 at 22:11 UTC |