Register
It is currently Fri Dec 19, 2014 10:44 pm

Variable treated as an Escape?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri May 22, 2009 8:48 am   

Joined: Fri May 22, 2009 8:39 am
Posts: 1
I'm new to bash scripting, I just got into it while implementing a Nagios system at work. I'm writing a simple script to call some Nagios commands, and I'm passing a variable to the script (name of a process I want to monitor).

I can't get this thing to work with a variable, but the command works fine if you hard code the name in. I've been going through some bash scripting documentation, and I think the issue is that my variable is being called next to a "\", wihch I think is treating it literally and not substituting a value. I'm not sure how I can edit this around to get it to call the variable. The results of the command are 0 (zero), which is the default return value of the Nagios command if something is messed up in how it's called. Like I said, if I hard-code the process name into the script, it works, so I think the issue is with how the variable is being called.

Below is a copy of the script. The $warn and $crit are not yet implemented.

#/bin/bash
#
#Custom script for encapsulating W3WP checks for graphing

#Set names for metrics & etc.
#Each call automatically pulls metric for each of four processes

met=$1
warn=$2
crit=$3

#Get the selected metric for each of four processes

W3WP=`/usr/local/nagios/libexec/check_nt -H rrscweb01.hca.corpad.net -p12489 -v COUNTER -l "\\Process(w3wp)\\$met"`

W3WP1=`/usr/local/nagios/libexec/check_nt -H rrscweb01.hca.corpad.net -p12489 -v COUNTER -l "\\Process(w3wp#1)\\$met"`

W3WP2=`/usr/local/nagios/libexec/check_nt -H rrscweb01.hca.corpad.net -p12489 -v COUNTER -l "\\Process(w3wp#2)\\$met"`

W3WP3=`/usr/local/nagios/libexec/check_nt -H rrscweb01.hca.corpad.net -p12489 -v COUNTER -l "\\Process(w3wp#3)\\$met"`

echo "$met on W3WP = $W3WP $met on W3WP#1 = $W3WP1 $met on W3WP#2 = $W3WP2 $met on W3WP#3 = $W3WP3"


Top
 Profile  
 PostPosted: Tue May 26, 2009 8:02 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Hi,

The easiest way to write a nagios script is something like this (if you want to monitor processes)

Code:
#!/bin/bash
# This is the variable which contains the first argument passed to the bash script.
proc=$1

# Check to see if the process is found in the process list
ps aux |grep -i "$proc" &> /dev/null
# Grep returns 0 if something is found and returns 1 if not.
if [ $? -eq 0 ]; then
   echo "Process $proc found."
   exit 0
else
   echo "Process $proc not found"
   exit 1
fi

Usage: ./script.sh httpd

This will only check if the process is found or not, and it will return whichever return code that follows an "exit". I know that Nagios have some diffrent supports for this.
I believe that return code 1 is "normal" and return code 0 is "ok". So you might have to change the "exit 1" to 2 or 3 (2 is warning, 3 is critical).
(Atleast this is what I had to do last time I wrote nagios scripts, thou that was for OpenVMS using perl)

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP