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 4 ■ TAKING BACKUPS AND MANAGING DATALines 17–28 get the list of machines on your network from the LDAP database (see Chapter 2 for adiscussion of LDAP setup) and sort them by name. Lines 30–44 check each machine for the available diskand the current usage. Line 31 gets the name of the machine from the LDAP entry (retrieved from the listof LDAP entries in line 30). Line 32 checks that you received a value for the name of the machine (thisallows for any garbage returned by the LDAP query); then in line 33, the script connects to the machinewith ssh and runs df to get the disk information. You use open with a pipe for the system call to ssh(rather than backticks, system, or exec), because this means that the information is returned line by line,which makes it very straightforward to loop over it, which is done in the while loop (lines 36–42).Line 37 checks the output of df for /dev/ (which will indicate a local hard disk). Note that this willinclude any currently connected external disks but won’t pick up on unmounted ones. The df line isthen split (on whitespace, which is the default if you don’t give an argument to split). The second entryon each df line is the total size of that disk (line 39 adds this to our running total), and the third entry isthe size of the disk that is used (line 40 adds that to the other running total).After all the machines have been checked, the LDAP connection is shut down, and the final value ofall the local disks on all the checked machines is printed. Note that it’ll be in 1KB blocks, because this isthe default df output.■ Note If you don’t use LDAP, you can use this slightly altered script:01 #!/usr/bin/perl -w02 # Script to tally up total disk space available and used in local network03 # JK 25.03.20090405 use strict;0607 die "Usage: dfscript\n" unless @ARGV == 0;0809 my $total = 0;10 my $used = 0;11 my @machines = qw/ client1 client2 server1 server2 /;1213 foreach my $machine ( @machines ) {14 open my $info, '-|', "ssh $machine 'df'"15 or warn "Can't ssh to $machine";16 while () {17 if ( /^\/dev/ ) {18 my @list = split;19 $total += $list[1];20 $used += $list[2];98Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!