Register
It is currently Thu Oct 30, 2014 11:38 am

if/else statement not working correctly


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Jun 22, 2014 12:26 pm   

Joined: Sat Jun 21, 2014 11:57 am
Posts: 1
Hello,

I'm working on a custom Nagios script that will monitor cPanel to make sure it is running and give back a status depending on what it gets from an output of service cpanel status. This is what I have:

Code:
##############################################################################
#   Constants

cpanelstate="running..."

ALERT_OK="OK - cPanel is running"
ALERT_CRITICAL="CRITICAL - cPanel is NOT running"
###############################################################################

cpanel=$(service cpanel status | head -1)

if [ "$cpanel" = "$cpanelstate" ]; then

        echo $ALERT_OK
        exit 0
else
        echo $ALERT_CRITICAL
        exit 2

fi

exit $exitstatus


When I run the script, this is the output I get:

Code:
root@shared01 [/home/mvelez]# /usr/local/nagios/libexec/check_cpanel
CRITICAL - cPanel is NOT running


When I run the script, cPanel IS RUNNING but this is the output I get. As a matter of fact, no matter what the status reports for cPanel this is the output that comes out. When I comment out the ELSE, ECHO and EXIT 2 statement:

Code:
#else
#       echo $ALERT_CRITICAL
#       exit 2


It gives back a blank output:

Code:
root@shared01 [/home/mvelez]# /usr/local/nagios/libexec/check_cpanel
root@shared01 [/home/mvelez]#


I'm not sure what I'm not doing correctly as I am very new to bash scripting and trying to learn as I go along. Thank you in advanced for any and all help very very much!


Top
 Profile  
 PostPosted: Mon Jun 23, 2014 4:40 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

may be invisible characters ?
however, you'd better test return code :
Code:
service cpanel status
returnCode=$?
if test $returnCode -eq 0
then
   echo running
else
   echo stopped
fi


Top
 Profile  
 PostPosted: Mon Jun 23, 2014 11:41 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
The service command adds colours to the output, god knows what else it does.
Sadly the service command will still report success (exit 0) if the service is stopped (at least it does on my distro)
I don't know anything about cpanel but you may need to check the service more directly, eg is the port the service runs on open.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 48 guests


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