3d230d6fd83d6988d51346644b391c7a1e1c0348
[clearscm.git] / clients / GD / FSMon / Filesystem.pm
1 =pod
2
3 =head2 NAME $RCSfile: FileSystem.pm,v $
4
5 Object oriented interface to filesystems
6
7 =head2 VERSION
8
9 =over
10
11 =item Author:
12
13 Andrew DeFaria <Andrew@ClearSCM.com>
14
15 =item Revision:
16
17 $Revision: $
18
19 =item Created:
20
21 Thu Dec 11 10:39:12 MST 2008
22
23 =item Modified:
24
25 $Date:$
26
27 =back
28
29 =head2 SYNOPSIS
30
31 This module implements a FileSystem object.
32
33   $fs = new FileSystem ("hosta");
34
35   while ($fs->filesystem) {
36     display "Filesystem: $_";
37     display "\tSize:\t$fs{$_}->size";
38     display "\tUsed:$fs{$_}->used";
39     display "\tFree:$fs{$_}->free";
40     display "\tUsed %:$fs{$_}->usedPct";
41     display "\tMounted on:$fs{$_}->mount";
42   } # while
43
44 =head2 DESCRIPTION
45
46 Filesystem creates a filesystem object that encapsulates information
47 about the file system as a whole.
48
49 =head2 ROUTINES
50
51 The following routines are exported:
52
53 =over
54
55 =cut
56
57 use strict;
58 use warnings;
59
60 package Filesystem;
61
62 use base "Exporter";
63
64 use OSDep;
65 use Display;
66 use Utils;
67 use Rexec;
68
69 =pod
70
71 =head3 new (<parms>)
72
73 Construct a new Filesystem object. The following OO style arguments are
74 supported:
75
76 Parameters:
77
78 =for html <blockquote>
79
80 =over
81
82 =item none
83
84 Returns:
85
86 =for html <blockquote>
87
88 =over
89
90 =item Filesystem object
91
92 =back
93
94 =for html </blockquote>
95
96 =cut
97
98 sub new ($;$$$$) {
99   my ($class, $system, $ostype, $username, $password, $prompt, $shellstyle) = @_;
100
101   # Set prompt if not passed in
102   $prompt ||= $Rexec::default_prompt;
103
104   # Connect to remote machine
105   my $remote = new Rexec (
106     host        => $system,
107     username    => $username,
108     password    => $password,
109     prompt      => $prompt,
110     shellstyle  => $shellstyle,
111   );
112
113   unless ($remote) {
114     error "Unable to connect to $system";
115
116     return undef;
117   } # if
118
119   my (@fs, %fs);
120
121   # Sun is so braindead!
122   if ($ostype eq "Unix") {
123     foreach ("ufs", "vxfs") {
124       my $cmd = "/usr/bin/df -k -F $_";
125
126       my @unixfs = $remote->exec ($cmd);
127
128       if ($remote->status != 0) {
129         error ("Unable to determine fsinfo on $system ($cmd)\n" . join ("\n", @fs));;
130         return undef;
131       } # if
132
133       # Skip heading
134       shift @unixfs;
135
136       for (my $i = 0; $i < scalar @unixfs; $i++) {
137         my (%fsinfo, $firstField);
138         
139         # Trim leading and trailing spaces
140         $unixfs[$i] =~ s/^\s+//;
141         $unixfs[$i] =~ s/\s+$//;
142
143         my @fields = split /\s+/, $unixfs[$i];
144
145         if (scalar @fields == 1) {
146           $fsinfo{fs}   = $fields[0];
147           $firstField   = 0;
148           $i++;
149
150           # Trim leading and trailing spaces
151           $unixfs[$i] =~ s/^\s+//;
152           $unixfs[$i] =~ s/\s+$//;
153
154           @fields       = split /\s+/, $unixfs[$i];;
155         } else {
156           $fsinfo{fs}   = $fields[0];
157           $firstField   = 1;
158         } #if
159
160         $fsinfo{size}           = $fields[$firstField]     * 1024;
161         $fsinfo{used}           = $fields[$firstField + 1] * 1024;
162         $fsinfo{free}           = $fields[$firstField + 2] * 1024;
163         $fsinfo{reserve}        = $fsinfo{size} - $fsinfo{used} - $fsinfo{free};
164
165         $fs{$fields[$firstField + 4]} = \%fsinfo;
166       } # for
167     } # foreach
168   } elsif ($ostype eq "Linux") {
169     foreach ("ext3") {
170       my $cmd = "/bin/df --block-size=1 -t $_";
171
172       my @linuxfs = $remote->exec ($cmd);
173
174       if ($remote->status != 0) {
175         error ("Unable to determine fsinfo on $system ($cmd)\n" . join ("\n", @fs));;
176         return undef;
177       } # if
178
179       # Skip heading
180       shift @linuxfs;
181
182       foreach (@linuxfs) {
183         my %fsinfo;
184         my @fields = split;
185         
186         $fsinfo{fs}             = $fields[0];
187         $fsinfo{size}           = $fields[1];
188         $fsinfo{used}           = $fields[2];
189         $fsinfo{free}           = $fields[3];
190         $fsinfo{reserve}        = $fsinfo{size} - $fsinfo{used} - $fsinfo{free};
191
192         $fs{$fields[5]} = \%fsinfo;
193       } # foreach
194     } # foreach
195   } else {
196     error "Can't handle $ostype", 1;
197   } # if
198
199   bless \%fs, $class;
200 } # new
201
202 =pod
203
204 =head3 mounts ()
205
206 Returns an array of mount points
207
208 Parameters:
209
210 =for html <blockquote>
211
212 =over
213
214 =item none
215
216 None
217
218 =back
219
220 =for html </blockquote>
221
222 Returns:
223
224 =for html <blockquote>
225
226 =over
227
228 =item Array of mount points
229
230 =back
231
232 =for html </blockquote>
233
234 =cut
235
236 sub mounts () {
237   my ($self) = shift;
238
239   return keys %{$self}
240 } # mounts
241
242 =pod
243
244 =head3 getFSInfo ($mount)
245
246 Returns a hash of filesystem info for a mount point
247
248 Parameters:
249
250 =for html <blockquote>
251
252 =over
253
254 =item $mount: Mount point
255
256 None
257
258 =back
259
260 =for html </blockquote>
261
262 Returns:
263
264 =for html <blockquote>
265
266 =over
267
268 =item Hash of filesystem info
269
270 =back
271
272 =for html </blockquote>
273
274 =cut
275
276 sub getFSInfo ($) {
277   my ($self, $mount) = @_;
278
279   return %{$self->{$mount}};
280 } # getFSInfo
281
282 1;
283
284 =back
285
286 =head2 CONFIGURATION AND ENVIRONMENT
287
288 None
289
290 =head2 DEPENDENCIES
291
292   Display
293   OSDep
294   Utils
295
296 =head2 INCOMPATABILITIES
297
298 None yet...
299
300 =head2 BUGS AND LIMITATIONS
301
302 There are no known bugs in this module.
303
304 Please report problems to Andrew DeFaria (Andrew@ClearSCM.com).
305
306 =head2 LICENSE AND COPYRIGHT
307
308 This Perl Module is freely available; you can redistribute it and/or
309 modify it under the terms of the GNU General Public License as
310 published by the Free Software Foundation; either version 2 of the
311 License, or (at your option) any later version.
312
313 This Perl Module is distributed in the hope that it will be useful,
314 but WITHOUT ANY WARRANTY; without even the implied warranty of
315 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
316 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
317 details.
318
319 You should have received a copy of the GNU General Public License
320 along with this Perl Module; if not, write to the Free Software Foundation,
321 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
322 reserved.
323
324 =cut