Hi ive been tasked to create a warning email which will be sent when a process exceeds a given percentage of CPU,
Ive created the script where it runs:
I would then run a mail program to email it to me, This all works fine however I would like to format how it is displayed when it comes through on email,
Given it ago but it doesnt seem to be working this is the script as it stands:
#!/bin/bash
#Send process which has exceeded 5%
#
#
echo 'The below is a list of processes which have exceeded the 5% threshold' >
/export/home/tjmoore/file2
#
echo ' ' >> /export/home/tjmoore/file2
#
echo 'PRI PID USER NICE %CPU COMMAND' >> /export/home/tjmoore/fi
le2
#
if ps -eo pri,pid,user,nice,pcpu,comm | awk 'OFS="\t" {if($5 >= 2); $1=$1; print
$0}' >> /export/home/tjmoore/file2 2>/dev/null
#
then /usr/local/bds/mailsend.s mailx "Process has surpassed 25% threshold" <email address> <email address> /export/home/tjmoore/file2 2&1>/dev/
null
fi
#
---------- Post updated at 09:40 AM ---------- Previous update was at 09:17 AM ----------
sorry I am a novice at scripting, $1=$1 means argument 1 = argument 1?
What do you mean by:
Don't forget to address some field (e.g. $1=$1) so $0 is recreated...
Sorry, I'm not sure I understand what you really want. The original question was on formatting; is that settled by now?
The script in your second post doesn't seem to work as expected - which might be phrased as "send a mail if that output files contains >=1 lines". If that's the case, we need awk to issue an exit code according to its success. As I'm not sure what intrinsic codes it sends, try this in your if statement:
And yes, $1=$1 means field 1 = field 1, which is a dummy assignment that causes $0 to be modified and thus recreated. If $0 is not recreated, the <TAB> OFS will not be inserted.
I dont seem to be getting any email through anymore so I can't see if the formatting has worked or not
#!/bin/bash
#Send process which has exceeded 25%
#
#
echo 'The below is a list of processes which have exceeded the 25% threshold' >
/export/home/tjmoore/file2
#
echo ' ' >> /export/home/tjmoore/file2
#
echo 'PRI PID USER NICE %CPU COMMAND' >> /export/home/tjmoore/fi
le2
#
if ps -eo pri,pid,user,nice,pcpu,comm | awk '{if($1>=2) {$1=$1; found=1; print}}
END {exit !found}' OFS="\t" >> /export/home/tjmoore/file2 2>/dev/null
#
then /usr/local/bds/mailsend.s mailx "Process has surpassed 25% threshold" <email address> <email address> /export/home/tjmoore/file2 2&1>/dev/
null
fi
#
---------- Post updated at 04:35 AM ---------- Previous update was at 04:32 AM ----------
Ive altered the percentage CPU the process has to use to be picked up just so I can be sure I will be getting some results, I will alter back when the script works
OK, so we have to analyze stepwise. Any error messages?
The if statement needs a ; before the then part.
To check the logics of the awk stmt,please repeatedly run
one - try exit found (=leave out "!") so we find out if it doesn't like the negation
two - the man page should give you an indication on how to output the version
ive looked thorugh the man pages and cant find anything on how to tell which version of awk im using however I did see a mention of POSIX locale if that heps at all.
Im now getting the following so it seems to be working just need to get it to put a tab space and not the backslash between fields
As stated before, you need to append a ; to the if statement, i.e. right before the then statement; I'm surprised there was no error msg.
Does the OFS=$'\t' do what it's meant to do, assign the <TAB> char you need in the output?
Just for the current, don't suppress errors, 2>/dev/null , let it show its errors!
Im away from the system now Ill try this tomorrow, the tab output is working great now yeah
---------- Post updated 03-08-12 at 04:31 AM ---------- Previous update was 02-08-12 at 10:03 AM ----------
Looks like it is all working perfectly now!
cheers RudiC, I appreciate all the help you have provided
Finishing Script:
#!/bin/bash
#Send process which has exceeded 5%
#
#
echo 'The below is a list of processes which have exceeded the 5% threshold' > /
export/home/tjmoore/file2
#
echo ' ' >> /export/home/tjmoore/file2
#
echo 'PRI PID USER NICE %CPU TIME COMMAND' >> /ex
port/home/tjmoore/file2
#
if ps -eo pri,pid,user,nice,pcpu,time,comm |awk 'BEGIN {found=1} {if($5>=5) {$1=
$1; found=0; print}} END {exit found}' OFS=$'\t' >> /export/home/tjmoore/file2;
then /usr/local/bds/mailsend.s mailx "Process has surpassed 5% threshold" <email address> <email address> /export/home/tjmoore/file2 > 2&1>/dev
/null
fi
#
In your post #11 you said you found exit [ expr ] in your awk's man page. That's exactly what we are using here: exit found conveys awk's success or failure as its "exit status" to the shell for evaluation in the if construct, 0 representing success and 1 representing failure. BEGIN {found=1} initialises the status to failure, but any line with percentage ($5) matching or exceeding 5 will set {found=0} , maybe repeatedly, so the conveying works.