0

Script in Solaris when usage in all filesystem >90% & send a mail i don't know how to send mail from script

#!/bin/bash
# Outputs alert if filesystem is above 90%
{
        for fs in $(df -hk | awk '{print $6}' | sed '1 d'); do
                chk=$(df -hk ${fs} | sed '1 d' | awk '{print $5}' | awk -F\% '{print $1}')
                if [ ${chk} -gt ${threshold} ]; then
                        echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%."
                fi
        done

It's getting "unary operator expected".

  • You would need to have a mail client installed first. Something like ssmtp. It has simple configuration. After you've properly configured ssmtp you can parse output of df with awk and execute sendmail -t with here-doc structure as input text. Unfortunately, I've no solaris box at hand to provide a complete answer on how to install ssmtp in the first place, so that's some research you'll have to do yourself probably, or wait for someone to expand on this comment as a proper answer – Sergiy Kolodyazhnyy Feb 26 '19 at 07:53
  • 1
    As Romeo says, you should put all your variables into quotes.  But also, the braces don’t really do you any good; you can say just "$fs", "$chk" and "$threshold"; see ${variable_name} doesn’t mean what you think it does … – G-Man Says 'Reinstate Monica' Feb 26 '19 at 08:40

1 Answers1

1

In the if command you should add double quotes around the variables. And check if those variables have something assigned to them:

 if [ "${chk}" -gt "${threshold}" ]; then

To send mail you can use your echo command on this way

echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%."|mail user@host

If you want one mail for all you can use something like:

>/tmp/output
for fs in $(df -hk | awk '{print $6}' | sed '1 d'); do
chk=$(df -hk ${fs} | sed '1 d' | awk '{print $5}' | awk -F\% '{print $1}')
if [ "${chk}" -gt "${threshold}" ]; then
  echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%." >>/tmp/output
fi
done
cat /tmp/output|mailx -s "Subject" username@host

EDIT1: And one more point, command df -hk is a bit nonsense, you want on the same time result in human readable format ('h') and to be in kilobytes ('k')

Romeo Ninov
  • 17,484
  • exactly worked fine as per your suggestions but there is an issue in the received mail...in from address it showed the server name but it didnt mention the filesystem name and for every filesystem it sent a seperate mail rather than a cumulative mail – user4485 Feb 26 '19 at 09:49
  • and is there a way to mention the subject in the mail..the mail was received without any subject – user4485 Feb 26 '19 at 09:50
  • @user338853, you can add this information in temporary file which you can send later – Romeo Ninov Feb 26 '19 at 09:50
  • @user338853, add also subject support – Romeo Ninov Feb 26 '19 at 09:53
  • i did those steps but got this error – user4485 Feb 26 '19 at 10:35
  • The flags you gave are used only when sending mail. Usage: mailx -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address -s SUBJECT -f FILE users – user4485 Feb 26 '19 at 10:35
  • @user338853, correct. At the end you want to send mail, right? And there was one space missing (add it) – Romeo Ninov Feb 26 '19 at 10:37
  • i can't thank you enough for the super fast responses...finally can you guide me how to run that in crontab for every 5hours – user4485 Feb 26 '19 at 11:11
  • @user338853, if you are happy with the answer please consider to upvote and/or accept the answer. About run in cron please ask new question – Romeo Ninov Feb 26 '19 at 11:13
  • sure will do...there are 38 filesys in the server and I am getting 38 mails under a single mail like a chained mail....is there a way around to recieve it as single mail? – user4485 Feb 26 '19 at 11:15
  • @user338853, i forgot to add done operator in the script, please check my updated answer (to send only one mail) – Romeo Ninov Feb 26 '19 at 11:17
  • i added done and executed but still the same result..mails are coming incrementally – user4485 Feb 26 '19 at 11:22
  • Edit it again, sorry, some problem with inline editor :) – Romeo Ninov Feb 26 '19 at 11:23
  • #!/bin/bash threshold="1" { >/tmp/output for fs in $(df -hk | awk '{print $6}' | sed '1 d'); do chk=$(df -hk ${fs} | sed '1 d' | awk '{print $5}' | awk -F% '{print $1}') if [ "${chk}" -gt "${threshold}" ]; then echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%." >>/tmp/output fi cat /tmp/output| mailx -s "Critical Space Alert" abc@xyz.com done } – user4485 Feb 26 '19 at 11:25
  • thats the exact one i am using..yet i am getting huge number of mails ;) – user4485 Feb 26 '19 at 11:25
  • No, the donemust be before mail command (after fi) – Romeo Ninov Feb 26 '19 at 11:26
  • 1
    fabulous...kudos....your help is highly is appreciated and i'll upvote :) – user4485 Feb 26 '19 at 11:29