Operating System - HP-UX
1834752 Members
3014 Online
110070 Solutions
New Discussion

Re: what wrong with my script

 
kholikt
Super Advisor

what wrong with my script

Hi,

I am writing some script to monitor the CPU usage of a remote machine. If the CPU is busy then I will get an email. Apparently I notice that the IDLE value is always empty. If I run the script it seem to be okay

IDLE=`ssh user@hostname sar -u 1 5 | grep Average | awk '{print $5}'`
function func_send_email
{
(
echo "To:myemail@mydomain.com"
echo "Subject:CPU Alert"
) | /usr/sbin/sendmail -t
}
echo $IDLE >> /tmp/cpu.log
if [ $IDLE -lt "10" ]
then
echo "Warning High CPU more than 90%"
func_send_email
fi
abc
10 REPLIES 10
Geoff Wild
Honored Contributor

Re: what wrong with my script

Seems to work for me - maybe try /usr/bin/awk ?

Did you set a shell at the start?

#!/bin/sh

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
kholikt
Super Advisor

Re: what wrong with my script

I have tried also I think my script work if I just run it manually. It won't work if I put it in cron. The IDLE value seems to be empty
abc
RAC_1
Honored Contributor

Re: what wrong with my script

Required varibales. Do export IDLE="whatevr goes here"
There is no substitute to HARDWORK
Senthil Kumar .A_1
Honored Contributor

Re: what wrong with my script

Hi,

Please be aware that when cron runs your script, it is not in the shell with profile/.profile executed. So you have 2 choices. One either mention Obsolute path names to all your commands are define a PATH variable explicitly.

Along with RAC's suggestion of exporting the IDLE variable, Please also define a PATH variable.. You can see it defined in /etc/profile, ~/.profile or /etc/PATH file..

In my example I would include the following lines in my script.

PATH=/usr/bin:/usr/contrib/bin:/opt/local/bin:/opt/perf/bin:/opt/OV/bin/OpC:/opt/hparray/bin:/opt/graphics/common/bin:/usr/sbin/diag/contrib:/opt/h
pnpl//bin:/opt/ansic/bin:/opt/langtools/bin:/opt/imake/bin:/opt/OV/bin:/opt/ignite/bin:/opt/resmon/bin:.

export PATH

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
kholikt
Super Advisor

Re: what wrong with my script

I have checked this is something to do with this

ssh user@hostname sar -u 1 5 | grep Average | awk '{print $5}

the remote execution of command won't work in my script if it is running in background.

Any more input
abc
Ralph Grothe
Honored Contributor

Re: what wrong with my script

First you could avoid the redundant pipe to grep if you put
e.g.

IDLE=`ssh user@hostname sar -u 1 5|awk '$1~/verage/{print$5}'`

But that's only cosmetic.
However, there's a subtle difference if you quote the pipe or don't, as you have done.
e.g.
... sar -u 1 5 \| awk ...
In the first case (i.e. quoted pipe) the parsing is done on the remote host,
while in the latter awk is called on the local host.
Apropos quotes,
you also may have a quoting issue, especially with awk's execution block.
Sometimes some weird quoting is necessary.
In such cases I usually play at the shell to see how tokens get expanded until I fixed any quoting issues.

What return code is reported in /var/adm/cron/log for your job?
Does the cronjob's user receive a mail of stdout and stderr on the local host?

How does your local user (i.e. the uid the cronjob runs under) authenticate with the remote host?
(e.g. Rhosts vs. RSAPubKey)
Maybe extra reference to the right keys is necessary (viz. ssh -i ...)?

Maybe split the job into single tasks for debugging.
E.g. first run the ssh sar command as cron or at job to see if the remote command gets executed and its output is caught.

Madness, thy name is system administration
Geoff Wild
Honored Contributor

Re: what wrong with my script

If it works command line but not in cron - then it is a PATH/env issue.

You need to run commands explicitly - IE give the full path...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Carlos Roberto Schimidt
Regular Advisor

Re: what wrong with my script

Hi,

Why dont use net-snmp? Is possible use command snmpget for get information from another host.

Download "HP-UX Internet Express for HP-UX 11i version 1"

See description:

"HP-UX Internet Express version A.06.00 for HP-UX 11i version 1 contains two sets of components. The first set is a collection of HP-UX Internet Express Open Source Web, Internet, and security components, which have been tested and qualified on HP-UX. The second set of components is a selection of software products available on HP-UX 11i version 1 Operating Environments (OEs)."


Schimidt
Carlos Roberto Schimidt
Regular Advisor

Re: what wrong with my script

Remember, if your host who send email dont have much idle cpu, is possible the email not send immediatilly.

See /etc/mail/sendmail.cf

# In order to limit load on a very busy system, sendmail can be #
# configured to queue up low priority messages rather than attempt #
# delivery immediately if the five-minute load average is greater #
# than some integer value, by default 8. This value is defined on #
# the line beginning O QueueLA=.
Volker Borowski
Honored Contributor

Re: what wrong with my script

Hi,
assuming you use key-authentication in that ssh connection with the default environment with HOME being not set in cron, ssh (which should be fully quallified as already said) needs to know where the key-files is.

-i file Identity for public key authentication (default: ~/.ssh/identity)

with "~" beinig a substititute for $HOME.

Specify the key file with the "-i" option or set
export HOME=....
prior calling ssh

Good luck !
Volker