I'm a scripting noob in need of some help
I am creating a script that checks the filesystems and will alert based upon if the percent full is greater than the threshold set.
The problem that I am having is that when I set the 'filesystem' variable, all of the output is treated as 1 single variable instead of 4 variables that will run through.
so: /dev/sda2 /dev/sda1 /dev/sda3 /dev/sda4 will be set as 1 variable instead of being broken out and spilt into 4 different variables...
I think this needs to be done with an array, but i'm not sure how to accomplish this.
Any suggestions?
Thanks:)
#!/bin/bash
THRESHOLD="30%"
filesystems=$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $1}')
for i in $filesystems
do
UTILIZED=$(/bin/df -k $filesystems | /usr/bin/tail -1 | /bin/awk '{print $5}')
# Compare the utilized value against the threshold:
if [[ "$UTILIZED" > "$THRESHOLD" || "$UTILIZED" = "100%" ]]; then
echo -e "OS|Filesystem|PctUsed\t$filesystems\t2\t$filesystems is at $UTILIZED. Please have a sysadmin check the environment for issues."
else
echo -e "OS|Filesytem|PctUsed\t$filesystems\t0"
fi
done
exit 0
$ df -i .
/home (/home ) : 1834176 total i-nodes
116539 free i-nodes
1717637 used i-nodes
1473 % i-nodes used
$
HPUX does a strange percentage calc, but inodes are not a problem on vxfs, as it makes more on demand, so some filtering to ensure only static inode count FS are checked is in order!
#!/usr/bin/env ruby
require 'net/smtp'
def send_email(from,to,subject,message,server)
msg =
---------- Post updated at 10:02 PM ---------- Previous update was at 09:58 PM ----------
#!/usr/bin/env ruby
require 'net/smtp'
def send_email(from,to,subject,message,server)
msg = <<-END_OF_MESSAGE
From: <#{from}>
To: <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
Net::SMTP.start(server) do |smtp|
smtp.send_message msg, from, to
end
end
threshold=80
df=`df -H`
msg=[]
df.split("\n").each do|s|
s=s.split
cap=s[4].to_i
filesystem=s[0]
if cap >= threshold
msg<<"#{filesystem}: #{cap}"
end
end
if msg.size>1
send_email("root@localhost","root","Alert","#{msg.join("\n")}","localhost")
end
threshold=33
df -P | { read
while read fs size used avail pcent mount
do
if [ ${pcent%?} -gt ${threshold:?} ]
then
printf "Filesystem %s on %s is %s full\n" "$fs" "$mount" "$pcent"
fi
done
}
We got the "df -H" , but you posted "df -h" not "df -k". When we have a script which is not working we like to see what the output from the key commands in the script looks like.
Also what do you want the final output of the script to look like?
Btw. If you have "df -P" on your system, just use cfajohnson's post .
@DGPickett . This is clearly not HP-UX (because "df -H" is not valid), but we don't know yet what O/S it is.
@cfajohnson
Though not the first time I have seen "df -P", it is very rarely used in day-today scripting because of the units. The percentage used figure is good and all we needed for this exercise.
The syntax is not valid in umpteen versions of unix, though we can deduce that the O/P has a modern one which may well offer a POSIX compliant "df" option. Whether the POSIX version is called "df" or "bdf" is a matter for the standards people.
@DGPickett
You may find that issuing "df" commands as a non-root user gives you a different view of free space.