0

Running out of space "/dev/mapper/LVMG1-tmp (87%)" on Hostname as on Mon Feb 3 15:44:03 IST 2020

above is the message I'm getting in the email but I need to use a proper salutation and declaration how can I archive this kidly suggest your ideas?

Code :

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 80 ]; then
    echo  "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "FILE SYSTEM ALERT" name@doamin.com 
  fi
done
Lacek
  • 7,233
  • 24
  • 28
vk_dinesh
  • 5
  • 3

1 Answers1

0

Here are a couple of functions that do what you are seeking. There are a couple of mail related variables you'll have to define elsewhere in your script, but that ought to be easy enough.

I just put the raw output into a variable and add that to the message body variable. You can manipulate that in many ways before sending it to the mailBody function.

Let me know if you have any questions.

function func_mailVariables() { 
    recipientAddress="somebody@somewhere.smth" 
    senderAddress="someoneelse@somewhereelse.smth" 
    mailSubject="Something Important blahblahblah" 
} 

function func_mailSend() { 
    importantNumbers="$( df -H | grep -vE '^Filesystem|tmpfs' | awk '{ print $5 " " $1 }'  )" 
    sendmail -t <<< "${mailBody}" 
} 

function func_mailBody() { 
    read -r -d '' mailBody <<-EOF
    To: ${recipientAddress}
    From: ${senderAddress}
    Subject: ${mailSubject}

    Hello, 

    Here are those numbers that are so important:  

    ${importantNumbers}


    Sincerely, 

    ${senderFullName} 
    EOF
} 

function main() { 
    func_mailVariables 
    func_mailBody 
    func_mailSend 
} 
JamesIsIn
  • 11
  • 3
  • ./compose: line 26: warning: here-document at line 10 delimited by end-of-file (wanted `EOF') ./compose: line 27: syntax error: unexpected end of file – vk_dinesh Feb 17 '20 at 10:15
  • this is my output when I run this through the script – vk_dinesh Feb 17 '20 at 10:16
  • we have postfix mail configuration – vk_dinesh Feb 17 '20 at 12:21
  • Make sure your declaration line for the heredoc includes the hyphen. You can't indent the closing EOF without it. ```<<-EOF``` (EOF is just an assigned keyword and can be substituted by something else if desired, with caveats.) – JamesIsIn Feb 18 '20 at 06:48