Disk Capacity Shell Script


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Disk Capacity Shell Script
# 1  
Old 03-26-2012
Disk Capacity Shell Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
I am pretty close I think, but stuck. I don't know how to send an email to the user specified on the command line, and I receive an error stating -ge expecting a unary value(lines 13 and 17), and ']' missing. The class is not on shell scripting, and I managed all of this from scratch myself so any help would be appreciated. I don't know why the variable and comparison isn't working.

1. The problem statement, all variables and given/known data:
Create a script sends an email message to the user specified on the command line if any of the filesystems at more than 70% of capacity. The script should not process special filesystems as /proc on the ce.uml.edu. It should only process filesystems which are either locally mounted or are mounted via NFS.
An individual email should be sent for each filesystem which is at the warning level. There should be a subject on the email with a message "Warning: Filesystem <put filesystem the>here is at <X>% of capacity" If the filesystem is at greater than 90% of capacity, the "Warning" should be changed to "Critical Warning".
You may use any scripting language including /bin/sh, ksh, bash, awk or perl. Work done in the C-Shell (csh) will not be accepted. This assignment may be done either on ce.uml.edu or on your own Linux/UNIX system.


2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
Code:
#!/bin/sh
# set -x # Uncomment to debug this script
# set -n # Uncomment to check syntax without any execution
# Shell script to monitor or watch the disk space
####Main Script####

df -H | grep -vE '^Filesystem | none' | awk '{ print $1 " " $5 }' | while read
do
 echo $output
 perc=$(echo $output | awk '{ print $1 }' | cut -d'%' –f1)
 part=$(echo $output | awk '{ print $2 }' )

 if [ $perc -ge 90 ]; then
 echo "Critical Warning: Filesystem \"$part\" at “$perc”% of capacity | 
 mail -s "Critical Warning: Filesystem \”$part\” at “$perc”% of capacity”

 elif [ $perc -ge 70 && $perc -lt 90 ]; then
 echo "Warning: Filesystem \"$part\" at “$perc”% of capacity | 
 mail -s "Warning: Filesystem \”$part\” at “$perc”% of capacity”

 fi
done

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Mass, Lowell, Unix/Linux System Admin 90.321-061, Professor Michael Richards, Michael_Richards@uml.edu

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by DukeNuke2; 03-26-2012 at 07:00 PM..
# 2  
Old 03-26-2012
A few things I noticed:

1) you need to read into a variable (I assume output). The syntax would be something like command | while read output. Possibly this is a cut/past error when sharing your code.

2) You are missing ending double quotes in your echo statements that are being used to fill the body of the mail.

3) Your mail commands need to be on the same line as your echo (this might have been a copy/past problem and not something wrong in your code)

4) You are assigning the percentage and partition backwards. The output from the df command is partition ($1) then percentage ($2), but you are assigning percentage from $1 and partition from $2.

5) The reason you are getting 'missing ]' errors is because you cannot use && as a part of a single bracketed expression. The easy fix is to make this two expressions:

Code:
if [ $a = "foo" ] && [ $b = "bar" ]
then

I'm not sure that old Bourne shell (sh) supports this, so you might consider using ksh or bash instead.

Hope these suggestions get you going again.

---------- Post updated at 19:59 ---------- Previous update was at 19:52 ----------

Also thought I'd toss in a quick trick. You could make things a bit more efficient by reading directly into your percentage and partition variables. Consider this small script:

Code:
ls -al | awk '{printf( "%d %s\n", $5, $NF}' | while read size name
do
    echo "name=$name size=$size bytes"
done

Using printf with %d will also remove your trailing percent sign without the need for invoking a cut process.
This User Gave Thanks to agama For This Post:
# 3  
Old 03-27-2012
Reference agama's point 3.

You can use a single backslash at the end of the line to make the two lines be read as one line by the Shell.
Don't forget the email address on the "mail" line. If you are using Linux then "mail" is usually the right command. If you are using unix then "mailx" is the right command.

echo "Critical Warning: Filesystem \"$part\" at “$perc”% of capacity" | \
mail -s "Critical Warning: Filesystem \”$part\” at “$perc”% of capacity” mailaccount@domain.com

Last edited by methyl; 03-27-2012 at 09:59 AM..
This User Gave Thanks to methyl For This Post:
# 4  
Old 03-28-2012
I made the suggested corrections, but am still receiving the following error messages...
Code:
 cut: you must specify a list of bytes, characters, or fields
Try `cut --help' for more information.
diskcapacity.sh: line 13: [: -ge: unary operator expected
diskcapacity.sh: line 17: [: -ge: unary operator expected

I am assuming that something is wrong with the cut syntax, and is causing the errors on 13 and 17. Data isn't getting passed to the variable correctly, but I can't seem to figure it out. The corrected script follows...
Code:
#!/bin/bash
# set -x # Uncomment to debug this script
# set -n # Uncomment to check syntax without any execution
# Shell script to monitor or watch the disk space
####Main Script####

df -H | grep -vE '^Filesystem | none' | awk '{ print $1 " " $5 }' | while read $output
do
 echo $output
 perc=$(echo $output | awk '{ print $2 }' | cut -d'%' –f2 )
 part=$(echo $output | awk '{ print $1 }' )

 if [ $perc -ge 90 ]; then
 echo "Critical Warning: Filesystem \"$part\" at “$perc”% of capacity” | \
 mail -s "Critical Warning: Filesystem \”$part\” at “$perc”% of capacity” clay@clay-ubuntu.com

 elif [ $perc -ge 70 ] && [ $perc -lt 90 ]; then
 echo "Warning: Filesystem \"$part\" at “$perc”% of capacity” | \
 mail -s "Warning: Filesystem \”$part\” at “$perc”% of capacity” clay@clay-ubuntu.com

 fi
done


Last edited by methyl; 03-29-2012 at 06:57 AM.. Reason: please use code tags
# 5  
Old 03-29-2012
A few minor corrections.
1) Lose the $ from $output on the "while read" line. We only have the $ when referring to the value of a variable.
2) Put double quotes round your string variable $output. It contains two values separated with a space character.
3) The percentage field is like 90% . Therefore the number bit is -f1 not -f2 in the cut statement.

Code:
df -H | grep -vE '^Filesystem | none' | awk '{ print $1 " " $5 }' | while read output
do
 echo "$output"
 perc=$(echo "$output" | awk '{ print $2 }' | cut -d'%' –f1 )
 part=$(echo "$output" | awk '{ print $1 }' )


Last edited by methyl; 03-29-2012 at 07:09 AM.. Reason: assorted typos
This User Gave Thanks to methyl For This Post:
# 6  
Old 03-29-2012
Thanks for the help. I made the suggested corrections, but still receive the error message about cut, -ge and an expected unary operator in lines 13 and 17 when I run the script.
# 7  
Old 03-29-2012
Hi, try flipping "-f1" and "-d%" around

Code:
$ cut
usage: cut -b list [-n] [file ...]
       cut -c list [file ...]
       cut -f list [-s] [-d delim] [file ...]

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me with C-shell Script: Disk checking

Hi, i am new in shell script. i have given a task to make a C-shell script. I have list of ip address and device name respectively. For example; cal 1 : 100.21.25.10 cal 2 : 100.21.25.11 cal 3 : 100.21.25.12 cal 4 : 100.21.25.14 and so on... Right now, i have this. #! /bin/csh -f ... (0 Replies)
Discussion started by: lattey
0 Replies

2. UNIX for Dummies Questions & Answers

Tru 64 Disk Capacity Check

i have a query on checking Tru64 disk drives physical capacity. i used hwmgr view devices and saw this disk. 160: /dev/disk/dsk7c COMPAQ BF03688284 bus-3-targ-0-lun-0 checking the model on the internet shows it is a 36.4GB drive. i checked the disk details and saw the... (2 Replies)
Discussion started by: iamnotaguru
2 Replies

3. Shell Programming and Scripting

disk quotas shell script

Hello.. I wrote the following shell script to run disk quotas for assigning limits to users. I need to know the partition which the user has created and mount it. So i copied the partitions (which will be displayed after running fdisk -l) and separted the last line from it and cut the required... (0 Replies)
Discussion started by: kalyanilinux
0 Replies

4. Shell Programming and Scripting

Help with Disk Space script in bash shell

Hi Guys, I'm a newb at shell scripting and successfully attempted a small disk space script (victory!!) but i'm wondering whether it actually takes into consideration KB,MB,GB. Please take a look at the script and advise. ##script to check if file sys has reached threshold. ... (3 Replies)
Discussion started by: Irishboy24
3 Replies

5. Shell Programming and Scripting

Shell script to find filesystem capacity on 50 servers

Hi all, I am new to Unix and I want to write a shell script in a jumpbox for finding the filesystem capacity on 50 unix servers ( by ssh ) and then email the result in HTML format with server name and capacity % to a specific outlook distribution list. any suggestion would be of great help. (17 Replies)
Discussion started by: amitbisht9
17 Replies

6. AIX

how to find out disk capacity

Hi, I would like to know how to find out disk capacity if it is assigned from the storage as a lun. as per below command , I am unable to find out disk capacity. $ bash bash-3.00$ lspv hdisk1 0001579a7fa3c086 None $ lscfg -vl hdisk1 hdisk1 ... (8 Replies)
Discussion started by: manoj.solaris
8 Replies

7. Shell Programming and Scripting

File system capacity meter in shell

HI i need help to show the file system capacity in meter or like the progress bar . OS = Solaris 10 (8 Replies)
Discussion started by: bejo4ever
8 Replies

8. Shell Programming and Scripting

shell script to create disk load

friends , need a shell script to create a disk load. can any one pls guide me with how this can be implemented. Pls provide the concept. from there i will try to design my script. (3 Replies)
Discussion started by: achak01
3 Replies

9. Solaris

Migrate VxVM boot disks to higher capacity disk

Hi, Im getting a downtime of 4 hrs to do porting of bootdisks. Currently, the system is running on Sf4800. 2 internal disk 36G connected to a SE3510 storage. We're getting 72G disks and we want to restore the OS from the current 36G to the 72G disk. System is under veritas volume manager ctrl.... (4 Replies)
Discussion started by: incredible
4 Replies

10. UNIX for Dummies Questions & Answers

Unix disk capacity at 100%

Is there any danger to keeping one of my disks at 100% capacity? (it's a disk just used for read-only files, not system files or anything like that). It's HP-UX if that matters. Thanks! (1 Reply)
Discussion started by: FredSmith
1 Replies
Login or Register to Ask a Question