LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2019, 02:36 AM   #1
Janani Priya
LQ Newbie
 
Registered: May 2019
Posts: 7

Rep: Reputation: Disabled
Functions working, but not able to print correct value.


Hi Team,

Please find the code below,

#! /bin/bash


hostname=$(hostname -s)
function name(){
mem_health=$health
disk_health=$health
CPU_health=$health
}
function check(){
echo $1
if [ $1 -ge 25 ];
then
health="CRITICAL"
else
health="NORMAL"

fi
name
echo $health
}
echo $health
memory=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
mem_health=$(free -m | awk 'NR==2{ print int($3*100/$2) }')
check "$mem_health"
#if [ $mem_health -ge 80 ];
#then
# mem_health="CRITICAL"
#else
# mem_health="NORMAL"
#fi
DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}')
disk_health=$(df -h | awk '$NF=="/"{printf int($5)}')
check "$disk_health"
#if [ $disk_health -ge 80 ]
#then
# disk_health="CRITICAL"
#else
# disk_health="NORMAL"
#fi
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%", $(NF-2)}')
CPU_health=$(top -bn1 | grep load | awk '{printf int($(NF-2))}')
check "$CPU_health"
#if [ $CPU_health -ge 80 ]
#then
# CPU_health="CRITICAL"
#else
# CPU_health="NORMAL"
#fi
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-1s\n' "Hostname" "Memory" "MEM_HEALTH" "Disk" "Disk_Health" "CPU" "CPU_Health"
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-1s\n' "$hostname" "$memory" "$mem_health" "$DISK" "$disk_health" "$CPU" "$CPU_health"


OUTPUT :

22
NORMAL
30
CRITICAL
0
NORMAL
Hostname Memory MEM_HEALTH Disk Disk_Health CPU CPU_Health
dallvasbap01xp 22.28% NORMAL 30% NORMAL 0.01% NORMAL

p.S : The value is printing correct, But in the tabular format it is printing wrong,

Dont know here i am making mistake.
 
Old 07-11-2019, 02:54 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,830

Rep: Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033Reputation: 4033
would be nice to use [code] tags to keep formatting. Otherwise hard to see where is the problem
By the way, what do you mean by "it is printing wrong"?

Last edited by pan64; 07-11-2019 at 02:58 AM.
 
1 members found this post helpful.
Old 07-11-2019, 01:46 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,157
Blog Entries: 11

Rep: Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

As noted, "printing wrong" is not useful to others trying to provide help.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
 
Old 07-11-2019, 02:09 PM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 516

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
Your function 'name' sets all 3 variables to the same value.
 
1 members found this post helpful.
Old 07-11-2019, 07:03 PM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 888

Rep: Reputation: 208Reputation: 208Reputation: 208
You mean that the tabular form is printing with decimals. Right?

Those are probably the correct values. This can be checked out by you and I leave you to figure out why the echo is printing without decimals. Also try to figure out from where, the tabular format gets two decimals and not 4 - say.

Next point.

AS OTHERS SAY, USE THE [code] TAGS.

OK

Last edited by AnanthaP; 07-11-2019 at 07:17 PM.
 
Old 07-12-2019, 11:03 AM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 516

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
My previous post should have been sufficient, but perhaps I need to explain.

The OP's difficulty is that the last line of output says that all three health variables are NORMAL, while the middle one, disk_health, was earlier shown to be CRITICAL. This incorrect report occurs because the function 'name' assigns the same value to all three health variables.
Code:
function name(){
mem_health=$health
disk_health=$health
CPU_health=$health
}
This is a logic error, and the OP needs to work out a logically correct method for separately assigning the correct value to each of the three health variables.

Last edited by Beryllos; 07-12-2019 at 11:06 AM.
 
1 members found this post helpful.
  


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
[SOLVED] awk print dos not print text value jozelo Linux - Newbie 2 10-23-2013 04:37 AM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
Not able to print variable value in crontab entry naren_0101bits Programming 6 12-10-2007 12:04 PM
sed not working if value is passed thru a variable containg value suchi_s Programming 7 10-29-2004 07:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration