1832128 Members
3093 Online
110038 Solutions
New Discussion

scipt help please ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

scipt help please ..

Hey everyone ..
I have been working with this script that I attached that I didnt write. I have taken stuff out and got what I need but I am missing one part.First here is the output of the script that is goinging into a txt file. And here is the output:
Sat Aug 11 10:32:56 CDT 2001

cnrint is running OK as PID 22996

Three items below should all be close to 10
DHCP Health is 10
DNS Health is 10
TFTP Health is 10

I need to add an if statement that if cnrint is not running it will send out an email.
And if the DHCP DNS or TFTP Health is less then 10 it will send out an email. Relay and everything is set up. I just dont know where to add the commands to make it do this.

Thanks

Richard

6 REPLIES 6
Praveen Bezawada
Respected Contributor
Solution

Re: scipt help please ..

Hi
You can use the attached script. It send a mail when
cnrint is not running or if the values rae less tah 10.

Hope it helps.

..BPK...
Wodisch
Honored Contributor

Re: scipt help please ..

Hello Richard,

you already have the needed "if" in there in
"cnr_status"...

CNRINTPID=`cnr_pid`
if [ $CNRINTPID -ne 0 ]; then
echo `date`
echo ""
echo "cnrint is running OK as PID $CNRINTPID"
else
echo "cnrint is NOT RUNNING!"
-> echo "cnrint is NOT RUNNING!" |
-> mailx -s"cnrint NOT RUNNING" root
fi
echo
echo "Three items below should all be close to 10"
echo "DHCP Health is "`item_health dhcp`
echo " DNS Health is "`item_health dns`
echo "TFTP Health is "`item_health tftp`
-> if [ "$(ietm_healt dhcp)" -lt 10 ]
-> then echo "DHCP Health is "`item_health dhcp` |
-> mailx -s"DHCP" root
-> fi

...and similiar for the other "items".

HTH,
Wodisch
James R. Ferguson
Acclaimed Contributor

Re: scipt help please ..

Hi Richard:

To add an email alert if cnrint is not running, after the statement (or as a substitute to the statement:

echo "cnrint is NOT RUNNING!"

...add:

mailx -s "Alert: cnrint is NOT running!" root < /dev/null > /dev/null

...this will mail 'root' the message shown. Since there is no message body, I substituted /dev/null. Since 'mailx' will helpfully offer the response that there is no message body, I suppress that by redirecting stdout to /dev/null.

In a similar fashion, you can constuct email messages for your other conditons.

If you want to generate one alert, you could echo all of your messages into one file and use that file as the message body to 'mailx', like:

mailx -s "Alert!!!" root < /tmp/alert.$$

Notice, again, that here I didn't redirect stdout to /dev/null. That's because 'mailx' is "happy" it found a non-null message body in the form of the /tmp/alert.$$ file.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: scipt help please ..

There is a common problem using ps -ef | grep

grep does not care what it matches. To grep for cnrint means that multiple hits can occur and this will confuse the script. For instance, programs like cnrint3 and 9cnrint will match, and even a user login called cnrint will match--not good as it may work today and break next week.

Instead, use the little known -C option of ps which will return exact matches for process names. To use this feature, ps must run in UNIX95 mode. For example:

UNIX95= ps -C sh

which will show only sh and NOT ksh (grep will fail in this task by matching csh and ksh and even bash as well as usernames containing sh).

And just to keep the script healthy, count the number of occurances from ps -C. There is always the possibility of multiple copies and this may be an error that should be reported.


Bill Hassell, sysadmin
someone_4
Honored Contributor

Re: scipt help please ..

Hey everyone.
I can post now what a great thing!!
Anyway I applied all of your ideas. And they all give me the final result. Now does it matter what I use? My goal is to write smart scripts. Not just shell scripts but smart scripts.

Mr Bill
your info on the ps -C option is great.So I should use
UNIX95= ps -C cnrint |awk '{print $1}'|grep -v PID
to grep for the PID in the script or should I use another syantax?

Thanks

Richard
James R. Ferguson
Acclaimed Contributor

Re: scipt help please ..

Hi Richard:

With regard to your last question. I?d change the syntax Bill suggested slightly.

The syntax he recommends returns a list of a zero or more processes. The first issue is that a descriptive header line is returned (?PID TTY TIME CMD?). We don?t want this at all, and really, all we want is the first column which is the ?pid? we?re interested in. We can do this in one stroke as:

PIDS= PIDS=`UNIX95= ps -C cnrint|awk 'NR>1 {print $1}'`

Then, as Bill suggested, you might want to count the number of matches you found. This too is easy.

N=`echo $PIDS|wc -w`
case $N in
0 ) echo "no matches"
;;
1 ) echo "one pid returned"
;;
* ) echo "more than one pid returned!"
;;
esac

Regards!

...JRF...