Operating System - HP-UX
1830340 Members
2329 Online
110001 Solutions
New Discussion

script output to email arrives unreadable

 
SOLVED
Go to solution
BrianDoyle
Trusted Contributor

script output to email arrives unreadable

Script in cron checks FileSystems and if over a certain threshold it sends an email alert. The output arrives mainly as gibberish like this:

?&f1k16d-1L ?&f2k16d-1L ?&f3k16d-1L
?&f4k16d-1L ?&f5k16d-1L ?&f6k16d-1L
?&f7k16d-1L ?&f8k7d1L Cancel??&s1A?&jB?&jR?&f1k16d-1L
?&f2k16d-1L ?&f3k16d-1L ?&f4k16d-1L
?&f5k16d-1L ?&f6k16d-1L ?&f7k16d-1L
?&f8k7d1L Cancel??&s1A?&jB?&jR?&f1k14d1L Display Msg
?&f2k14d1L Mail Msgm?&f3k15d1L Reply to Msgr?&f4k14d1L Save
Msgs?&f5k14d1L Delete Msgd?&f6k14d1LUndelete Msgu?&f7k14d1L Print
Msgp?&f8k14d1L Quit
ELMq?&s1A?&jB?&jR?&s1A?&jB?&jR?&s0A?&j@?&f1k16d-1L
?&f2k16d-1L ?&f3k16d-1L ?&f4k16d-1L
?&f5k16d-1L ?&f6k16d-1L ?&f7k16d-1L
?&f8k7d1L Cancel??&s1A?&jB?&jR?&f1k16d-1L ?&f2k16d-1L
?&f3k16d-1L ?&f4k16d-1L ?&f5k16d-1L
?&f6k16d-1L ?&f7k16d-1L ?&f8k7d1L
Cancel??&s1A?&jB?&jR?&f1k14d1L Display Msg
?&f2k14d1L Mail Msgm?&f3k15d1L Reply to Msgr?&f4k14d1L Save
Msgs?&f5k14d1L Delete Msgd?&f6k14d1LUndelete Msgu?&f7k14d1L Print
Msgp?&f8k14d1L Quit ELMq?&s1A?&jB?&jR?&s1A?&jB?&jR?&s0A?&j@

*************************************************
Cron: The previous message is the standard output
and standard error of one of your crontab commands:

sh /scripts/disk_chk.sh 2>&1 >/dev/null

SCRIPT:


#!/bin/sh

for a in `bdf -l | grep -v "Mounted" | grep -v "used" | grep -v "deviceFile"| sed -e "s/%//" | awk '{print $6, $5, $4}' | awk
'{print $1}'`
do
fs=`echo $a | awk '{print $1}'`
for b in `bdf -l | grep -v "Mounted" | grep -v "used" | grep -v "deviceFile" | sed -e "s/%//" | awk '{print $6, $5, $4}' | gre
p "$a " |awk '{print $2}'`
do
fsval=`echo $b`
done
warn=95
crit=95
if [ $fsval -gt $warn ]
then elm -s "WARNING: $fs is at $fsval" email@xyz.com fi
if [ $fsval -gt $crit ]
then elm -s "CRITICAL: $fs is at $fsval" email@xyz.com fi
done


On other systems it is not arriving in this format. Any ideas?
HP-UX 11.31
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: script output to email arrives unreadable

>email arrives unreadable

These are fancy escape sequences for drawing the screen.

You should NOT be using elm to mail in a script. Simply use mailx:
mailx -s "CRITICAL: $fs is at $fsval" email@xyz.com < /dev/null
James R. Ferguson
Acclaimed Contributor
Solution

Re: script output to email arrives unreadable

Hi Briano:

You could greatly reduce the number of processes this script uses as well as its complexity. Consider something like this (where 'awk' skips the first header line and eliminates the "%" character:

# cat bdfalert
#/usr/bin/sh
typeset WARN=50
typeset CRIT=75
bdf -l | awk '{if (NR>1) {sub("%","",$5);print $6,$5}}' | \
while read FS PCT X
do
if [ "${PCT}" -gt "${CRIT}" ]; then
mailx -s "CRITICAL: ${FS} is at ${PCT}" email@xyz.com < /dev/null
elif [ "${PCT}" -gt "${WARN}" ]; then
mailx "WARNING : ${FS} is at ${PCT}" email@xyz.com < dev/null
fi
done
exit 0

...Note that I let the shell's 'read' pluck out the variables from each line filtered by 'awk' thus presenting them to the shell logic. It is always good practice to enclose interpolated variables in curly braces and add double quotes around them in tests.

I reordered your 'if' statements so that you don't get both a "warning" and a "critical" message when a critical leval has been reached.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: script output to email arrives unreadable

Shalom,

Try this email attachment sender script in conjunction with JRF's recommended changes.

http://www.hpux.ws/?p=7

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com