Fixed bug where history did not have newline
[clearscm.git] / bin / diskspace
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: diskspace,v $
5 # Revision:     $Revision: 1.2 $
6 # Description:  Check filesystems to see if they are becoming too full
7 # Author:       Andrew@DeFaria.com
8 # Created:      Fri Mar 12 10:17:44 PST 2004
9 # Modified:     $Date: 2010/06/08 15:03:27 $
10 # Language:     Perl
11 #
12 # (c) Copyright 2005, ClearSCM, Inc., all rights reserved
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17 use File::Spec;
18
19 use FindBin;
20 use lib "$FindBin::Bin/../lib";
21
22 use Display;
23
24 my $threshold = 90;
25
26 sub Usage {
27   my $msg = shift;
28
29   display "ERROR: $msg\n" if defined $msg;
30
31   display "diskspace\t[-v] [-d] [-u] [ -t <threshold> ]";
32   display "\t-v\tTurn on verbose mode";
33   display "\t-d\tTurn on debug mode";
34   display "\t-u\tThis usage message";
35   display "\t-t\tThreshold (0-100)";
36
37   exit 1;
38 } # Usage
39
40 sub CheckLocalFilesystems {
41   my @local_filesystems = `df -lP`;
42
43   @local_filesystems = grep {/^\/dev/} @local_filesystems;
44
45   foreach (@local_filesystems) {
46     my ($fs, $blocks, $used, $available, $used_percent, $mounted_on) = split;
47
48     if ($used_percent =~ /(\d+)%/) {
49       $used_percent = $1;
50     } # if
51
52     $available = sprintf ("%.3f", $available / 1024);
53
54     # Check if over threshold and report
55     if ($used_percent <= $threshold ) {
56       verbose "$mounted_on is $used_percent% full - $available Megs left";
57     } else {
58       warning "$mounted_on is $used_percent% full - $available Megs left";
59     } # if
60   } # foreach
61 } # CheckLocalFilesystems
62
63 # Get parameters
64 while ($ARGV [0]) {
65   if ($ARGV [0] eq "-v") {
66     set_verbose;
67   } elsif ($ARGV [0] eq "-d") {
68     set_debug;
69   } elsif ($ARGV [0] eq "-t") {
70     shift (@ARGV);
71     if (!$ARGV [0]) {
72       Usage "Must specify threshold after -t";
73     } else {
74       $threshold = $ARGV [0];
75     } # if
76   } elsif ($ARGV [0] eq "-u") {
77     Usage;
78   } else {
79     Usage "Unknown argument found: " . $ARGV [0];
80   } # if
81
82   shift (@ARGV);
83 } # while
84
85 verbose "Theshold: $threshold\%";
86 CheckLocalFilesystems;