Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / diskspace
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         diskspace
5 # Description:  Checks diskspace
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Jul 16 11:05:06 PDT 1999
8 # Modified:
9 # Language:     Korn Shell
10 #
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
12 #
13 ################################################################################
14 # Set me to command name
15 me=$(basename $0)
16
17 # Set adm_base
18 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
19
20 # Set adm_fpath
21 adm_fpath=${adm_fpath:-$adm_base/functions}
22
23 # Source functions
24 . $adm_fpath/common
25 tmpprefix=/tmp/$me.$$
26 . $adm_fpath/tmpfiles
27
28 function usage {
29   display "$me [-v|verbose] [-d|debug] [-u|sage] [ -n|etwork | -l|ocal ]"
30   display "        -v|erbose:      Turns on verbose mode"
31   display "        -d|ebug:        Turns on debug mode"
32   display "        -u|sage:        Print this usage message"
33   display "        -n|etwork:      Produce report for network wide storages areas"
34   display "        -l|ocal:        Produce report for local file systems only"
35
36   error "$1" 1
37 } # usage
38
39 function check_filesystem_space {
40   debug "ENTER check_filesystem_space ($1, $2)"
41   kind=$1
42   fs=$2
43
44   # Make sure that file system is mounted
45   ls $fs > /dev/null 2>&1
46
47   if [ $? -ne 0 ]; then
48     warning "Cannot mount $kind filesystem $fs" 1
49     debug "RETURN check_filesystem_space"
50     return
51   fi
52
53   # Isolate the disk usage line
54   diskusage=$(df -k $fs | tail -1 | tr -s ' ')
55
56   # Get hostname from the now mounted filesystem
57   # Separate field of interest
58   host=$(print $diskusage | cut -f1 -d:)
59   available=$(print $diskusage | cut -f4 -d' ' | awk '{printf "%.3f", $1/1024}')
60   integer capacity=$(print $diskusage | cut -f5 -d' ' | cut -f1 -d'%')
61
62   # Check if over threshold and report
63   if [ $capacity -le $threshold ]; then
64     if [ ! -z "$verbose" ]; then
65       display "$kind filesystem $host:$fs is $capacity% full and has $available Megs left"
66     fi
67   else
68     display "$kind filesystem $host:$fs has exceeded the threshold of $threshold%."
69     display "It is $capacity% full and has $available Megs left"
70   fi
71
72   debug "EXIT check_filesystem_space"
73 } # check_filesystem_space
74
75 function check_vob_space {
76   debug "ENTER check_vob_space"
77   vob_filesystems="/vbs/bog /vbs/bof /vbs/boh /vbs/bol"
78
79   for vbs in $vob_filesystems; do
80     check_filesystem_space VOB $vbs
81   done
82   debug "EXIT check_vob_space"
83 } # check_vob_space
84
85 function check_view_space {
86   debug "ENTER check_view_space"
87   view_filesystems="/vws/bpj /vws/bpq /vws/bpr"
88
89   for vws in $view_filesystems; do
90     check_filesystem_space View $vws
91   done
92   debug "EXIT check_view_space"
93 } # check_view_space
94
95 function check_local_filesystems {
96   debug "ENTER check_local_filesystems"
97   local_filesystems=$(df -k -F ufs | grep /dev | awk '{print $NF}')
98   host=$(uname -n)
99   kind=Local
100
101   for lfs in $local_filesystems; do
102     # Isolate the disk usage line
103     diskusage=$(df -k $lfs | tail -1 | tr -s ' ')
104
105     # Separate field of interest
106     available=$(print $diskusage | cut -f4 -d' ' | awk '{printf "%.3f", $1/1024}')
107     integer capacity=$(print $diskusage | cut -f5 -d' ' | cut -f1 -d'%')
108
109     # Check if over threshold and report
110     if [ $capacity -le $threshold ]; then
111       if [ ! -z "$verbose" ]; then
112         display "$kind filesystem $host:$lfs is $capacity% full and has $available Megs left"
113       fi
114     else
115       display "$kind filesystem $host:$lfs has exceeded the threshold of $threshold%."
116       display "It is $capacity% full and has $available Megs left"
117     fi
118   done
119
120   debug "EXIT check_local_filesystems"
121 } # check_local_filesystems
122
123 # Get parameters
124 threshold=90      # default threshold
125 filesystems=local # default to local filesystems
126 while [ $# -ge 1 ]; do
127   case "$1" in
128     -usage)
129       usage
130     ;;
131
132     -v|-verbose)
133       verbose=yes
134     ;;
135
136     -d|-debug)
137       debug=yes
138     ;;
139
140     -t|-threshold)
141       shift
142       if [ $# -lt 1 ]; then
143         error "Threshold not specified" 0
144         usage
145       fi
146
147       integer threshold=$1
148     ;;
149
150     -n|-network)
151       filesystems=network
152     ;;
153
154     -l|-local)
155       filesystems=local
156     ;;
157
158     *)
159       usage
160     ;;
161   esac
162   shift
163 done
164
165 if [ "$filesystems" = "local" ]; then
166   check_local_filesystems
167 else
168   check_vob_space
169   check_view_space
170 fi