Added client work scripts
[clearscm.git] / clients / HP / bin / diskspace
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         diskspace
5 # Description:  Check filesystems to see if they are becoming too full
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Mar 12 10:17:44 PST 2004
8 # Language:     Perl
9 # Modifications:
10 #
11 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved
12 #
13 ################################################################################
14 use strict;
15 use warnings;
16 use File::Spec;
17
18 my ($me, $abs_path, $lib_path, $bin_path, $log_path);
19
20 BEGIN {
21   # Extract relative path and basename from script name.
22   $0 =~ /(.*)[\/\\](.*)/;
23
24   $abs_path     = (!defined $1) ? "." : File::Spec->rel2abs ($1);
25   $me           = (!defined $2) ? $0  : $2;
26
27   # Setup paths
28   $bin_path             = "$abs_path";
29   $lib_path             = "$abs_path/../../lib";
30   $log_path             = "$abs_path/../../log";
31
32   # Add the appropriate path to our modules to @INC array.
33   unshift (@INC, "$lib_path");
34 } # BEGIN
35
36 use Display;
37
38 my $threshold = 90;
39
40 sub Usage {
41   my $msg = shift;
42
43   display "ERROR: $msg\n" if defined $msg;
44
45   display "$me\t[-v] [-d] [-u] [ -n | -l ]";
46   display "\t-v\tTurn on verbose mode";
47   display "\t-d\tTurn on debug mode";
48   display "\t-u\tThis usage message";
49   display "\t-t\tThreshold (0-100)";
50
51   exit 1;
52 } # Usage
53
54 sub CheckFilesystemSpace {
55   my $filesystem = shift;
56
57   # Isolate the disk usage line
58   my @diskusage = `df -k $filesystem`;
59
60   # Get fields
61   $_ = $diskusage [1];
62   my ($fs, $blocks, $used, $available, $used_percent, $mounted_on) = split;
63
64   if ($used_percent =~ /(\d+)%/) {
65     $used_percent = $1;
66   } # if
67
68   $available = sprintf ("%.3f", $available / 1024);
69
70   # Check if over threshold and report
71   if ($used_percent <= $threshold ) {
72     verbose "$mounted_on is $used_percent% full - $available Megs left";
73   } else {
74     warning "$mounted_on is $used_percent% full - $available Megs left";
75   } # if
76 } # CheckFilesystemSpace
77
78 sub CheckLocalFilesystems {
79   my @local_filesystems = `df -k`;
80
81   @local_filesystems = grep {/^\/dev/} @local_filesystems;
82
83   foreach (@local_filesystems) {
84     my ($fs, $blocks, $used, $available, $used_percent, $mounted_on) = split;
85     CheckFilesystemSpace $mounted_on;
86   } # foreach
87 } # CheckLocalFilesystems
88
89 # Get parameters
90 while ($ARGV [0]) {
91   if ($ARGV [0] eq "-v") {
92     set_verbose;
93   } elsif ($ARGV [0] eq "-d") {
94     set_debug;
95   } elsif ($ARGV [0] eq "-t") {
96     shift (@ARGV);
97     if (!$ARGV [0]) {
98       Usage "Must specify threshold after -t";
99     } else {
100       $threshold = $ARGV [0];
101     } # if
102   } elsif ($ARGV [0] eq "-u") {
103     Usage;
104   } else {
105     Usage "Unknown argument found: " . $ARGV [0];
106   } # if
107
108   shift (@ARGV);
109 } # while
110
111 debug "Theshold: $threshold";
112 CheckLocalFilesystems;