Big update of Clearadm
[clearscm.git] / clearadm / updatefs.pl
1 #!/usr/local/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: updatefs.pl,v $
6
7 Update Filesystem
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.29 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2011/06/16 15:12:50 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage updatefs.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34                     [-host [<host>|all]] [-fs [<fs>|all]]
35
36  Where:
37    -u|sage:     Displays usage
38  
39    -ve|rbose:   Be verbose
40    -deb|ug:     Output debug messages
41    
42    -host [<host>|all]: Update host or all hosts (Default: all)
43    -fs   [<fs>|all]:   Update filesystem or all (Default: all)   
44
45 =head1 DESCRIPTION
46
47 This script will record the state of a filesystem.
48
49 =cut
50
51 use strict;
52 use warnings;
53
54 use Net::Domain qw(hostname);
55 use FindBin;
56 use Getopt::Long;
57
58 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
59
60 use Clearadm;
61 use Clearexec;
62 use DateUtils;
63 use Display;
64 use Utils;
65
66 my $VERSION  = '$Revision: 1.29 $';
67   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
68
69 my $clearadm  = Clearadm->new;
70 my $clearexec = Clearexec->new; 
71
72 my ($host, $fs);
73
74 # Given a host and a filesystem, formulate a fs record
75 sub snapshotFS ($$) {
76   my ($systemRef, $filesystem) = @_;
77
78   my %system = %{$systemRef};
79
80   my %filesystem = $clearadm->GetFilesystem ($system{name}, $filesystem);
81   
82   unless (%filesystem) {
83         error "Filesystem $host:$filesystem not in clearadm database - try adding it";
84         
85         return;
86   } # unless
87   
88   my %fs = (
89     system     => $system{name},
90     filesystem => $filesystem,
91     timestamp  => Today2SQLDatetime,
92   );
93
94   # Sun is so braindead!
95   # TODO: Verify this works under Solaris
96   if ($system{type} eq 'Unix') {
97     my $cmd = "df -v $filesystem{mount}";
98
99     my ($status, @unixfs) = $clearexec->execute ($cmd);
100
101     if ($status != 0) {
102       error ('Unable to determine fsinfo for '
103            . "$system{name}:$filesystem{mount} ($cmd)\n" .
104              join "\n", @unixfs);
105    
106       return;
107     } # if
108
109     # Skip heading
110     shift @unixfs;
111
112     for (my $i = 0; $i < scalar @unixfs; $i++) {
113       my @fields = split ' ', $unixfs[$i];
114
115       $fs{mount}   = $fields[0];
116       $fs{size}    = $fields[2] * 1024;
117       $fs{used}    = $fields[3] * 1024;
118       $fs{free}    = $fields[4] * 1024;
119       $fs{reserve} = $fs{size} - $fs{used} - $fs{free};
120     } # for
121   } elsif ($system{type} eq 'Linux' or $system{type} eq 'Windows') {
122     my $cmd = "/bin/df --block-size=1 -P $filesystem{mount}";
123
124     my ($status, @linuxfs) = $clearexec->execute ($cmd);
125
126     if ($status != 0) {
127       error ("Unable to determine fsinfo for $system{name}:$filesystem{mount}\n"
128           . join "\n", @linuxfs
129       );
130                
131       return;
132     } # if
133
134     # Skip heading
135     shift @linuxfs;
136     
137     $_ = shift @linuxfs;
138     my @fields = split;
139     
140     $fs{size}    = $fields[1];
141     $fs{used}    = $fields[2];
142     $fs{free}    = $fields[3];
143     $fs{mount}   = $fields[5];
144     $fs{reserve} = $fs{size} - $fs{used} - $fs{free};
145   } # if
146
147   return %fs;  
148 } # snapshotFS
149
150 # Main
151 GetOptions (
152   'usage'   => sub { Usage },
153   'verbose' => sub { set_verbose },
154   'debug'   => sub { set_debug },
155   'host=s'  => \$host,
156   'fs=s'    => \$fs,
157 ) or Usage "Invalid parameter";
158
159 Usage 'Extraneous options: ' . join ' ', @ARGV
160   if @ARGV;
161
162 # Announce ourselves
163 verbose "$FindBin::Script V$VERSION";
164
165 my $exit = 0;
166
167 for my $system ($clearadm->FindSystem ($host)) {
168   next if $$system{active} eq 'false';
169   
170   my $status = $clearexec->connectToServer (
171     $$system{name}, 
172     $$system{port}
173   );
174   
175   unless ($status) {
176     verbose "Unable to connect to system $$system{name}:$$system{port}";
177     next;
178   } # unless
179
180   for my $filesystem ($clearadm->FindFilesystem ($$system{name}, $fs)) {
181     verbose "Snapshotting $$system{name}:$$filesystem{filesystem}";
182   
183     my %fs = snapshotFS ($system, $$filesystem{filesystem});
184     
185     if (%fs) {
186       my ($err, $msg) = $clearadm->AddFS (%fs);
187   
188       error $msg, $err if $err;
189     } # if
190     
191     # Check if over threshold
192     my %notification = $clearadm->GetNotification ('Filesystem');
193
194     next
195       unless %notification;
196   
197     my $usedPct = '0%';
198
199     $usedPct = sprintf ('%.2f', (($fs{used} + $fs{reserve}) / $fs{size}) * 100) if $fs{size} != 0;
200     
201     if ($usedPct >= $$filesystem{threshold}) {
202       $exit = 2;
203       display YMDHMS
204             . " System: $$filesystem{system} "
205             . "Filesystem: $$filesystem{filesystem} Used: $usedPct% " 
206             . "Threshold: $$filesystem{threshold}";    
207     } else {
208       $clearadm->ClearNotifications ($$system{name}, $$filesystem{filesystem});    
209     } # if
210   } # for
211   
212   $clearexec->disconnectFromServer;
213 } # for
214
215 exit $exit;
216
217 =pod
218
219 =head1 CONFIGURATION AND ENVIRONMENT
220
221 DEBUG: If set then $debug is set to this level.
222
223 VERBOSE: If set then $verbose is set to this level.
224
225 TRACE: If set then $trace is set to this level.
226
227 =head1 DEPENDENCIES
228
229 =head2 Perl Modules
230
231 L<FindBin>
232
233 L<Getopt::Long|Getopt::Long>
234
235 L<Net::Domain|Net::Domain>
236
237 =head2 ClearSCM Perl Modules
238
239 =begin man 
240
241  Clearadm
242  Clearexec
243  DateUtils
244  Display
245  Utils
246
247 =end man
248
249 =begin html
250
251 <blockquote>
252 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
253 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
254 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
255 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
256 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
257 </blockquote>
258
259 =end html
260
261 =head1 BUGS AND LIMITATIONS
262
263 There are no known bugs in this script
264
265 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
266
267 =head1 LICENSE AND COPYRIGHT
268
269 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
270
271 =cut