LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2014, 03:38 AM   #1
kashif ahmad
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Rep: Reputation: Disabled
Modifying the Bash Script


I am newbie to linux. I am using centos flavor and trying to monitor RAM using nagios.

To monitor RAM I am following this link

http://www.the-tech-tutorial.com/nag...nload-install/

But it does not give me the actual output and whenever I run it on my command panel it says that there is error in Line 93, 94 and 98 of chk_mem.sh file.


Here is the bash script of this file kindly help me out here

Thanks in advance

Here is the bash script code

Code:
#!/bin/bash

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License only.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

PROGNAME=`basename $0`
VERSION="Version 1.0"
AUTHOR="2011, Tyler Allen (http://www.the-tech-tutorial.com/)"

LV_W=100;
LV_C=100;


#Print Version
print_version() {
    echo "$VERSION $AUTHOR"
}

#Print Help
usage(){
        echo $PROGNAME $VERSION
        echo $AUTHOR
        echo
        echo This is a Nagios plugin that will check the curremt memory usage of the system.
        echo
        echo OPTIONS:
        echo  -h Shows this help
        echo  -v Shows the Version
        echo  -w sets the warning level
        echo  -c sets the critical level
}

#Parmature Getter
while getopts "hvw:c:" opt; do
  case $opt in
    h)
      usage
      exit
      ;;
    v)
      print_version
      exit
      ;;
    w)
      LV_W=$OPTARG
      ;;
    c)
      LV_C=$OPTARG
      ;;

    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

#Check the diffrence between the LV values
    if [ ${LV_W} -gt ${LV_C} ]
    then
        echo "Please adjust levels. The critical level must be higher than the warning level!"
        exit 666
    fi

#Values must be between 0 and 100
    if [ "$LV_W" -lt 0 -o "$LV_W" -gt 100 -o "$LV_C" -lt 0 -o "$LV_C" -gt 100 ]
    then
        echo "Warning and critical level values must be between 0 and 100."
        exit 666
    fi

#Get the figures
MEM_TOTAL=`grep "^MemTotal" /proc/meminfo|awk '{print $2}'`
TMP_MEM_FREE=`grep "^MemFree" /proc/meminfo|awk '{print $2}'`
TMP_MEM_USED=`expr $MEM_TOTAL - $TMP_MEM_FREE`
BUFFERS=`grep "^Buffers" /proc/meminfo|awk '{print $2}'`
CACHED=`grep "^Cached" /proc/meminfo|awk '{print $2}'`

P_MEM_FREE=`echo "scale=2; $TMP_MEM_FREE / $MEM_TOTAL * 100" | bc -l | sed 's/.[0-9][0-9]//'`
P_MEM_USED=`echo "scale=0; 100 - $P_MEM_FREE" | bc -l`

if [ ! -z "$LV_W" -a ! -z "$LV_C" ]
    then
        if [ ${P_MEM_USED} -ge ${LV_W} -a ${P_MEM_USED} -lt ${LV_C} ]
        then
            echo "WARNING - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_WR
        elif [ ${P_MEM_USED} -ge ${LV_C} ]
        then
            echo "CRITICAL - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_CR
        else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
        fi
    else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
    fi

Last edited by colucix; 06-19-2014 at 05:07 AM. Reason: Added CODE tags to improve readability.
 
Old 06-19-2014, 06:30 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,460

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
Works just fine on my CentOS 6.5 server.
Code:
# ./check_mem.sh
OK - Used: 88%, Free: 12% | 'mem_used'=88;100;100 'mem_free'=12
 
Old 06-19-2014, 06:58 AM   #3
kashif ahmad
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
My shell output is

[root@HadiTelecom plugins]# ./chk_mem.sh
./chk_mem.sh: line 93: bc: command not found
./chk_mem.sh: line 94: bc: command not found
./chk_mem.sh: line 98: [: too many arguments
./chk_mem.sh: line 102: [: -ge: unary operator expected
OK - Used: %, Free: % | 'mem_used'=;100;100 'mem_free'=


and my nagios web interface output is

Current Status: OK (for 0d 3h 29m 39s)
Status Information: OK - Used: %, Free: %
Performance Data: 'mem_used'=;75%;90% 'mem_free'=
Current Attempt: 1/3 (HARD state)
Last Check Time: 06-19-2014 07:52:30
Check Type: ACTIVE
Check Latency / Duration: 2.484 / 0.020 seconds
Next Scheduled Check: 06-19-2014 08:02:30
Last State Change: 06-19-2014 04:27:23
Last Notification: N/A (notification 0)
Is This Service Flapping? NO (0.00% state change)
In Scheduled Downtime? NO
Last Update: 06-19-2014 07:56:53 ( 0d 0h 0m 9s ago)
Active Checks:
ENABLED
Passive Checks:
ENABLED
Obsessing:
ENABLED
Notifications:
ENABLED
Event Handler:
ENABLED
Flap Detection:
ENABLED

Kindly guide me

Thanks
 
Old 06-19-2014, 07:00 AM   #4
kashif ahmad
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
You can easily see that it is not providing the values in my case
 
Old 06-19-2014, 07:01 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,460

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
Try fixing this error first.
Code:
./chk_mem.sh: line 93: bc: command not found
 
Old 06-19-2014, 07:06 AM   #6
kashif ahmad
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
I really don't know how to fix this. I am new to programming as well as linux.
 
Old 06-19-2014, 07:10 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,460

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
At line 93 of your script, the bc command is not found.
So learn what the bc command is, and then install it on your server.
If you don't know how to install it on your server then learn how to do that too.

If you really can't be bothered learning about linux then let me know, there's a 1 line command that should solve your problem, but telling you would deprive you of the learning opportunity.
 
1 members found this post helpful.
Old 06-19-2014, 07:28 AM   #8
kashif ahmad
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Many Many thanks to you. Yes You were right that you didn't tell me the command. I just used yum command and everything is now alright.
Thanks to you again

Kashif
 
Old 06-19-2014, 10:18 AM   #9
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
Cool

Quote:
Originally Posted by TenTenths View Post
At line 93 of your script, the bc command is not found.
So learn what the bc command is, and then install it on your server.
If you don't know how to install it on your server then learn how to do that too.

If you really can't be bothered learning about linux then let me know, there's a 1 line command that should solve your problem, but telling you would deprive you of the learning opportunity.
single word command with 2 switches... script is overkill

*EDIT* I don't know RHEL / CentOS so maybe the command I'm thinking of isn't there.

Last edited by Tadaen; 06-19-2014 at 10:19 AM.
 
Old 06-19-2014, 10:22 AM   #10
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,460

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
Quote:
Originally Posted by Tadaen View Post
single word command with 2 switches... script is overkill

*EDIT* I don't know RHEL / CentOS so maybe the command I'm thinking of isn't there.
The script the OP is using outputs in a format that's quite handy for Nagios to use and graph. The link the OP quotes as the script source and the fact that the OP mentions nagios in the third post would support this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help modifying file name with bash kike_coello Programming 8 07-12-2009 08:37 PM
Need help modifying users profile to use bash -r at login tljacobs Linux - Security 4 03-01-2009 02:09 PM
Help - Need Help Modifying Script stlouis Programming 4 01-10-2008 10:45 AM
Modifying a script sidra Programming 13 09-28-2007 03:14 PM
bash: modifying a variable in a block eantoranz Programming 4 06-28-2005 11:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration