5 =head1 NAME $RCSfile: updatefs.pl,v $
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Mon Dec 13 09:13:27 EST 2010
27 $Date: 2011/06/16 15:12:50 $
33 Usage updatefs.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34 [-host [<host>|all]] [-fs [<fs>|all]]
37 -u|sage: Displays usage
40 -deb|ug: Output debug messages
42 -host [<host>|all]: Update host or all hosts (Default: all)
43 -fs [<fs>|all]: Update filesystem or all (Default: all)
47 This script will record the state of a filesystem.
54 use Net::Domain qw(hostname);
59 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
67 my $VERSION = '$Revision: 1.29 $';
68 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
70 my $clearadm = Clearadm->new;
71 my $clearexec = Clearexec->new;
75 # Given a host and a filesystem, formulate a fs record
77 my ($systemRef, $filesystem) = @_;
79 my %system = %{$systemRef};
81 my %filesystem = $clearadm->GetFilesystem($system{name}, $filesystem);
83 unless (%filesystem) {
84 error "Filesystem $host:$filesystem not in clearadm database - try adding it";
90 system => $system{name},
91 filesystem => $filesystem,
92 timestamp => Today2SQLDatetime,
95 # Sun is so braindead!
96 # TODO: Verify this works under Solaris
97 if ($system{type} eq 'Unix') {
98 my $cmd = "df -v $filesystem{mount}";
100 my ($status, @unixfs) = $clearexec->execute ($cmd);
103 error ('Unable to determine fsinfo for '
104 . "$system{name}:$filesystem{mount} ($cmd)\n"
105 . join "\n", @unixfs);
113 for (my $i = 0; $i < scalar @unixfs; $i++) {
114 my @fields = split ' ', $unixfs[$i];
116 $fs{mount} = $fields[0];
117 $fs{size} = $fields[2] * 1024;
118 $fs{used} = $fields[3] * 1024;
119 $fs{free} = $fields[4] * 1024;
120 $fs{reserve} = $fs{size} - $fs{used} - $fs{free};
122 } elsif ($system{type} eq 'Linux' or $system{type} eq 'Windows') {
123 my $cmd = "/bin/df --block-size=1 -P $filesystem{mount}";
125 my ($status, @linuxfs) = $clearexec->execute ($cmd);
128 error ("Unable to determine fsinfo for $system{name}:$filesystem{mount}\n"
129 . join "\n", @linuxfs
141 $fs{size} = $fields[1];
142 $fs{used} = $fields[2];
143 $fs{free} = $fields[3];
144 $fs{mount} = $fields[5];
145 $fs{reserve} = $fs{size} - $fs{used} - $fs{free};
153 'usage' => sub { Usage },
154 'verbose' => sub { set_verbose },
155 'debug' => sub { set_debug },
158 ) or Usage "Invalid parameter";
160 Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
163 verbose "$FindBin::Script V$VERSION";
167 for my $system ($clearadm->FindSystem ($host)) {
168 next if $system->{active} eq 'false';
170 my $status = $clearexec->connectToServer(
176 verbose "Unable to connect to system $system->{name}:$system->{port}";
180 for my $filesystem ($clearadm->FindFilesystem ($system->{name}, $fs)) {
181 verbose "Snapshotting $system->{name}:$filesystem->{filesystem}";
183 my %fs = snapshotFS($system, $filesystem->{filesystem});
186 my ($err, $msg) = $clearadm->AddFS(%fs);
188 error $msg, $err if $err;
192 my $cmd = "plotfs.cgi generate=1 system=$system->{name} filesystem=$filesystem->{filesystem} scaling=Day points=7";
194 verbose "Generating fssmall for $system->{name}:$filesystem->{filesystem}";
195 my ($error, @output) = Execute("$cmd tiny=1 2>&1");
197 error 'Unable to generate fssmall' . join("\n", @output), $error if $error;
199 $filesystem->{fssmall} = join '', @output;
201 verbose "Generating fslarge for $system->{name}:$filesystem->{filesystem}";
202 ($error, @output) = Execute("$cmd 2>&1");
204 error 'Unable to generate fslarge' . join("\n", @output), $error if $error;
206 $filesystem->{fslarge} = join '', @output;
208 my ($err, $msg) = $clearadm->UpdateFilesystem($system->{name}, $filesystem->{filesystem}, %$filesystem);
210 error "Unable to update filesystem record $msg", $err if $err;
212 # Check if over threshold
213 my %notification = $clearadm->GetNotification('Filesystem');
215 next unless %notification;
219 $usedPct = sprintf ('%.2f', (($fs{used} + $fs{reserve}) / $fs{size}) * 100) if $fs{size} != 0;
221 if ($usedPct >= $filesystem->{threshold}) {
224 . " System: $filesystem->{system} "
225 . "Filesystem: $filesystem->{filesystem} Used: $usedPct% "
226 . "Threshold: $filesystem->{threshold}";
228 $clearadm->ClearNotifications ($system->{name}, $filesystem->{filesystem});
232 $clearexec->disconnectFromServer;
239 =head1 CONFIGURATION AND ENVIRONMENT
241 DEBUG: If set then $debug is set to this level.
243 VERBOSE: If set then $verbose is set to this level.
245 TRACE: If set then $trace is set to this level.
253 L<Getopt::Long|Getopt::Long>
255 L<Net::Domain|Net::Domain>
257 =head2 ClearSCM Perl Modules
272 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
273 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
274 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
275 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
276 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
281 =head1 BUGS AND LIMITATIONS
283 There are no known bugs in this script
285 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
287 =head1 LICENSE AND COPYRIGHT
289 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.