Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
[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
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.