13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 11 ■ TRACKING DOWN BUGSAn example is if you’re concerned about the response rate of one of your servers, such as your mainNFS server, which anecdotally appears to be slow to respond from time to time. To check this out, youcan set up a short script to ping it at intervals and get the response rate.■ Note This is only the start of an investigation. You’d also want to check the response to a real NFS request,among other things.01 #!/usr/bin/perl -w02 # JK 10.01.20080304 use strict;05 use Net::Ping;06 use Net::SMTP;07 use Time::HiRes;08 use POSIX qw(stfrtime);0910 my $server = "nfsserver";11 my $file = "/home/jkemp/sysadmin/nfs_response.log";1213 # Ping server and record time and response time14 my $ping = Net::Ping->new();15 $ping->hires();16 my ($ret, $duration, $ip) = $ping->ping($server, 5.5);17 open FILE, ">>$file";18 print FILE ((strftime "%b-%d-%H:%M:%S", localtime), " ");19 printf FILE ("%.3f\n", 1000 * $duration);20 close FILE;This should be fairly self-explanatory. It’s being run on a local network, so there’s no need to specifya domain in line 10. The use of Time::HiRes (and then setting the ping response as hires in line 15—thisis a feature of the Net::Ping module) enables the data to be recorded with millisecond values rather thanjust in seconds. In line 16, $ret is the success flag (1 for reachable, 0 for not), which we don’t use. We alsodiscard $ip, which is the IP address that the pinged host (here nfsserver) resolved to, and keep only$duration, which is the elapsed time of the ping. The 5.5 value in the ping call is the length of time (inseconds) to wait before giving up, if the host doesn’t respond.Lines 17–20 deal with writing the output to file, with the time of the ping and then the ping responseduration printed to three decimal places with printf. Note that this is the time after the ping, not beforeit. In this context, this is unlikely to matter, but it could if you wanted to set a $currenttime value beforethe ping and then use that here. Note that the $duration value is returned in milliseconds, so youmultiply it by 1,000 to get seconds.Run this every minute (or however often you want) with a line in a cron file. This can be your owncron file rather than that of root.* * * * * /home/jkemp/bin/nfsserver_ping.pl227Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!