LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-11-2020, 06:04 AM   #1
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Rep: Reputation: Disabled
Regarding Swap memory usahe monitot via shell script


Hi Team,

trying to monitor swap memory usage from below script.

#! /bin/bash

swap=$(free | awk '/Swap/{printf ("%.2f%"), $3/$2*100}')

if [ $swap -lt 20% ]
then
echo "OK - Swap Memory at $swap %"
exit 0
elif [[ $swap -ge 20% && $swap -lt 40% ]]
then
echo "Warning - Swap Memory at $swap %"
exit 1
elif [ $swap -ge 40% ]
then
echo "CRITICAL - Swap Memory at $swap %"
exit 2
else
echo "UNKNOWN - unable to retrieve Swap Memory usage"
exit 3
fi

================================================================

But getting error as below. Not sure where am doing wrong. Can someone please assist.

./swap.sh: line 5: [: 7.40%: integer expression expected
./swap.sh: line 9: [[: 7.40%: syntax error: invalid arithmetic operator (error token is ".40%")
./swap.sh: line 13: [: 7.40%: integer expression expected
UNKNOWN - unable to retrieve Swap Memory usage

Thanks
 
Old 06-11-2020, 06:48 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
First off, you should inspect those errors.

Has it not occurred to you that they are at every line where you've placed a test against a percentage?

Therefore you could search for information about how to compare numbers in bash.

Percentages are not valid, at least not with these types of comparisons, instead you'd compare strings. But if you don't need to, then instead remove the percent sign and just compare the integers.

This all depends on what is in the variable swap.

This also appears to not be the code where you've seen the errors, there are not 3 repetitions of the string "40%" in lines which are 4 apart from each other.

Anyways, that's a fundamental problem with your script, your compares. Suggest you print out the variable $swap to see what it contains as a debug before you code your comparisons.
 
3 members found this post helpful.
Old 06-11-2020, 06:50 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
My additional point being, if you just take one piece of information where I say remove the percent sign in your compares, that is not necessarily the solution, in fact may be very unlikely. It's not helpful if you do just that and then reply "it didn't work", so that point is to debug the script so you know what comparison works correctly.

There are a few things you can change for your awk statement to make your life easier.

Last edited by rtmistler; 06-11-2020 at 06:56 AM.
 
2 members found this post helpful.
Old 06-11-2020, 06:58 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would suggest you yo use www.shellcheck.net to check your script. (it will also help you to solve the problems found).
 
5 members found this post helpful.
Old 06-12-2020, 04:37 AM   #5
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Original Poster
Rep: Reputation: Disabled
[QUOTE=pan64;6133065]I would suggest you yo use www.shellcheck.net to check your script. (it will also help you to solve the problems found).[/QUOTE

Hi thanks for posting the useful tool to check script which i was not aware of.
It did fix the issue.
Thanks for it.
 
1 members found this post helpful.
Old 06-16-2020, 03:24 AM   #6
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi Team,

As script was testing at "OK" threshold before. but now one of the host crossed ok and reached "warning" level. but still getting ok status as below.
[test ~]$ ./swap_memory_health_check.sh
OK - Swap Memory at 25.40%

Code used is below. seems good with if loop but not sure where am doing wrong.

#! /bin/bash

swap=$(free | awk '/Swap/{printf ("%.2f%"), $3/$2*100}')

if [ "$swap -lt 20%" ]
then
echo "OK - Swap Memory at $swap "
exit 0
elif [ "$swap -ge 20% && $swap -lt 40%" ]
then
echo "Warning - Swap Memory at $swap "
exit 1
elif [ "$swap -ge 40%" ]
then
echo "CRITICAL - Swap Memory at $swap "
exit 2
else
echo "UNKNOWN - unable to retrieve Swap Memory usage"
exit 3
fi

Please assist.
 
Old 06-16-2020, 03:45 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
see post #5 and try that useful tool. And also please use code tags.
 
1 members found this post helpful.
Old 06-16-2020, 03:50 AM   #8
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Don't have VM right now for Linux but what is the output of:

swap=$(free | awk '/Swap/{printf ("%.2f%"), $3/$2*100}')

Is the output of $swap equals to 20%?

If yes, then you should compare as string. -lt then is for comparing numbers. 20% -is considered a string because of percent sign

try like these instead:

if [ "$swap" -eq "20%" ]

Good luck!

Last edited by JJJCR; 06-16-2020 at 03:50 AM. Reason: edit
 
1 members found this post helpful.
Old 06-16-2020, 06:39 AM   #9
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi JJJCR

I have corrected as you have mentioned and it worked and reporting warning level as per output.
Thanks for it.

Hope i should do the same for other threshold levels conditions too
 
Old 06-16-2020, 09:58 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,632

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ncnaveen_arasu View Post
Hi JJJCR
I have corrected as you have mentioned and it worked and reporting warning level as per output. Thanks for it.

Hope i should do the same for other threshold levels conditions too
Haven't we been here before??
https://www.linuxquestions.org/quest...ed-4175673188/

Almost the same problem, just with a different script. And again, we aren't on your 'team'...we are not your co-workers, but volunteers who try to help others. We're happy to do so, but you have to show that you're applying what you're getting told and taking the advice offered. Otherwise, we're just doing all your debugging work for you.
 
2 members found this post helpful.
Old 06-16-2020, 04:39 PM   #11
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Anyway, why not try running a program such as (for example) glances in a terminal screen.
It will monitor your swap and a host of other things...
 
1 members found this post helpful.
Old 06-16-2020, 09:13 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Putting alert thresholds on swap usage does not make much sense. Better monitor available RAM + swap.
 
1 members found this post helpful.
Old 06-17-2020, 01:02 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Regarding the reported problem, comparing strings can go wrong.
But you can compare pure numbers (without a percent sign)
Code:
swap=$(free | awk '/Swap/{printf "%.0f", 100*$3/$2}')

if [ $swap -lt 20 ]
...
 
1 members found this post helpful.
Old 06-17-2020, 01:55 AM   #14
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I would suggest you yo use www.shellcheck.net to check your script. (it will also help you to solve the problems found).
Quote:
Originally Posted by JeremyBoden View Post
Anyway, why not try running a program such as (for example) glances in a terminal screen.
It will monitor your swap and a host of other things...
Hi

thanks for the suggestion. we are in restricted environment where third party application/software cant be installed.
or else would have been tried.
thanks for advice,will be try using it in other environment.
 
Old 06-17-2020, 01:58 AM   #15
ncnaveen_arasu
LQ Newbie
 
Registered: Oct 2019
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Haven't we been here before??
https://www.linuxquestions.org/quest...ed-4175673188/

Almost the same problem, just with a different script. And again, we aren't on your 'team'...we are not your co-workers, but volunteers who try to help others. We're happy to do so, but you have to show that you're applying what you're getting told and taking the advice offered. Otherwise, we're just doing all your debugging work for you.
Hi New to forum and Linux too..and was not knowing that i can show gratitude by clicking on green button. So was marking it as helpful.
Thanks for the suggestion.
 
  


Reply

Tags
scripting, shell script



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
Live DVD runs out of memory - using swap - free memory doesn't help at upgrade .... LiNuXkOlOnIe Linux - Distributions 3 06-09-2013 09:35 PM
memory metric to monitor memory usage or swap? karlochacon Linux - Newbie 5 08-13-2011 03:49 PM
process, memory, swap, virtual memory wakatana Linux - Hardware 1 08-31-2009 07:55 AM
Swap Memory / Virtual Memory in Fedora Core 6 Jojo_CFT Linux - Newbie 2 10-15-2007 04:23 AM
Difference between Swap Virtrual memory and Swap Parition Nappa Slackware 4 11-27-2003 07:58 PM

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

All times are GMT -5. The time now is 03:39 AM.

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