Thank you monks for your wise and astute observations. I have included in my script that wisdom which fits with its purpose. Other wisdom is still being digested...(yummy). The check_private_dir sub seems twisted and its only purpose it to make sure there is a protected dir to write to, if anyone can enlighten me with more elegant code, I'd be smarter for it (and thankfull). UPDATE: I suppose I could create the private dir in /var/tmp, which on my systems is not cleared upon reboot, and I could remove all but one line from check_private_dir to test for the existance of the directory. However, I'd still be intersted in a cleaner way of executing the current check_private_dir for academic purposes.
#!/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();

In reply to RE: lock files vs. non-predictable file names by RuphSkunk
in thread lock files vs. non-predictable file names by RuphSkunk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.