1st instance of a for loop exits if I am looking for a specific exit status

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to get some disk monitoring from a storage device .
I can query the device and get out put .



e.g. -- vol_list



dr_prdoracle_bkup 83%
dr_test 6%
infra_backup 3%
logs 28%
oem_shared 2%
prd_backup 51%
rhev_export 24%
ss_backup 2%
ss_data 23%


Here I am trying to alarm on percentage used . So here I am trying to create a warning @50% and Critical @70%



The thresholds are parsed on command line



WarnSpace=$2
CritSpace=$2


ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

for line in $(cat "vol_list"); do
SpaceUsed=`echo $line|awk 'print $2'|sed 's/%//'`
volume=`echo $line|awk 'print $1'`

if [ -n "$WarnSpace" -a -n "$CritSpace" ]
then
if [ "$SpaceUsed" -ge "$WarnSpace" -a "$SpaceUsed" -le "$CritSpace" ]
then
#echo "WARNING - $output | $perfdata"
echo "WARNING; $volume: total $SpaceUsed"%""
exit $ST_WR
elif [ "$SpaceUsed" -ge "$CritSpace" ]
then
#echo "CRITICAL - $output | $perfdata"
echo "CRITICAL; $volume: total $SpaceUsed"%""

exit $ST_CR


fi
done


When I use the exit status it breaks out of the loop
e.g.



./purefs_check.sh -w 50 -c 70

WARNING; dev_client: total 67%


If I remove the exit code I get what I expect



WARNING; dev_client: total 67%
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
WARNING; dr_client: total 54%
CRITICAL; dr_prdoracle_bkup: total 78%
WARNING; prd_backup: total 51%


WHat I need i to finish the loop and give me an exit status of 1 for Warning and 2 for Critical .



Preferably only 1 exit code >1 entries .



So here I would like to see



./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?
WARNING; dev_client: total 67%
WARNING; dr_client: total 54%
WARNING; prd_backup: total 51%
1
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
CRITICAL; dr_prdoracle_bkup: total 78%
2


Any help would be appreciated ...







share|improve this question





















  • I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
    – Jeff Schaller
    May 29 at 13:03










  • Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
    – Cromwer
    May 29 at 13:14














up vote
1
down vote

favorite












I am trying to get some disk monitoring from a storage device .
I can query the device and get out put .



e.g. -- vol_list



dr_prdoracle_bkup 83%
dr_test 6%
infra_backup 3%
logs 28%
oem_shared 2%
prd_backup 51%
rhev_export 24%
ss_backup 2%
ss_data 23%


Here I am trying to alarm on percentage used . So here I am trying to create a warning @50% and Critical @70%



The thresholds are parsed on command line



WarnSpace=$2
CritSpace=$2


ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

for line in $(cat "vol_list"); do
SpaceUsed=`echo $line|awk 'print $2'|sed 's/%//'`
volume=`echo $line|awk 'print $1'`

if [ -n "$WarnSpace" -a -n "$CritSpace" ]
then
if [ "$SpaceUsed" -ge "$WarnSpace" -a "$SpaceUsed" -le "$CritSpace" ]
then
#echo "WARNING - $output | $perfdata"
echo "WARNING; $volume: total $SpaceUsed"%""
exit $ST_WR
elif [ "$SpaceUsed" -ge "$CritSpace" ]
then
#echo "CRITICAL - $output | $perfdata"
echo "CRITICAL; $volume: total $SpaceUsed"%""

exit $ST_CR


fi
done


When I use the exit status it breaks out of the loop
e.g.



./purefs_check.sh -w 50 -c 70

WARNING; dev_client: total 67%


If I remove the exit code I get what I expect



WARNING; dev_client: total 67%
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
WARNING; dr_client: total 54%
CRITICAL; dr_prdoracle_bkup: total 78%
WARNING; prd_backup: total 51%


WHat I need i to finish the loop and give me an exit status of 1 for Warning and 2 for Critical .



Preferably only 1 exit code >1 entries .



So here I would like to see



./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?
WARNING; dev_client: total 67%
WARNING; dr_client: total 54%
WARNING; prd_backup: total 51%
1
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
CRITICAL; dr_prdoracle_bkup: total 78%
2


Any help would be appreciated ...







share|improve this question





















  • I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
    – Jeff Schaller
    May 29 at 13:03










  • Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
    – Cromwer
    May 29 at 13:14












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to get some disk monitoring from a storage device .
I can query the device and get out put .



e.g. -- vol_list



dr_prdoracle_bkup 83%
dr_test 6%
infra_backup 3%
logs 28%
oem_shared 2%
prd_backup 51%
rhev_export 24%
ss_backup 2%
ss_data 23%


Here I am trying to alarm on percentage used . So here I am trying to create a warning @50% and Critical @70%



The thresholds are parsed on command line



WarnSpace=$2
CritSpace=$2


ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

for line in $(cat "vol_list"); do
SpaceUsed=`echo $line|awk 'print $2'|sed 's/%//'`
volume=`echo $line|awk 'print $1'`

if [ -n "$WarnSpace" -a -n "$CritSpace" ]
then
if [ "$SpaceUsed" -ge "$WarnSpace" -a "$SpaceUsed" -le "$CritSpace" ]
then
#echo "WARNING - $output | $perfdata"
echo "WARNING; $volume: total $SpaceUsed"%""
exit $ST_WR
elif [ "$SpaceUsed" -ge "$CritSpace" ]
then
#echo "CRITICAL - $output | $perfdata"
echo "CRITICAL; $volume: total $SpaceUsed"%""

exit $ST_CR


fi
done


When I use the exit status it breaks out of the loop
e.g.



./purefs_check.sh -w 50 -c 70

WARNING; dev_client: total 67%


If I remove the exit code I get what I expect



WARNING; dev_client: total 67%
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
WARNING; dr_client: total 54%
CRITICAL; dr_prdoracle_bkup: total 78%
WARNING; prd_backup: total 51%


WHat I need i to finish the loop and give me an exit status of 1 for Warning and 2 for Critical .



Preferably only 1 exit code >1 entries .



So here I would like to see



./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?
WARNING; dev_client: total 67%
WARNING; dr_client: total 54%
WARNING; prd_backup: total 51%
1
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
CRITICAL; dr_prdoracle_bkup: total 78%
2


Any help would be appreciated ...







share|improve this question













I am trying to get some disk monitoring from a storage device .
I can query the device and get out put .



e.g. -- vol_list



dr_prdoracle_bkup 83%
dr_test 6%
infra_backup 3%
logs 28%
oem_shared 2%
prd_backup 51%
rhev_export 24%
ss_backup 2%
ss_data 23%


Here I am trying to alarm on percentage used . So here I am trying to create a warning @50% and Critical @70%



The thresholds are parsed on command line



WarnSpace=$2
CritSpace=$2


ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

for line in $(cat "vol_list"); do
SpaceUsed=`echo $line|awk 'print $2'|sed 's/%//'`
volume=`echo $line|awk 'print $1'`

if [ -n "$WarnSpace" -a -n "$CritSpace" ]
then
if [ "$SpaceUsed" -ge "$WarnSpace" -a "$SpaceUsed" -le "$CritSpace" ]
then
#echo "WARNING - $output | $perfdata"
echo "WARNING; $volume: total $SpaceUsed"%""
exit $ST_WR
elif [ "$SpaceUsed" -ge "$CritSpace" ]
then
#echo "CRITICAL - $output | $perfdata"
echo "CRITICAL; $volume: total $SpaceUsed"%""

exit $ST_CR


fi
done


When I use the exit status it breaks out of the loop
e.g.



./purefs_check.sh -w 50 -c 70

WARNING; dev_client: total 67%


If I remove the exit code I get what I expect



WARNING; dev_client: total 67%
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
WARNING; dr_client: total 54%
CRITICAL; dr_prdoracle_bkup: total 78%
WARNING; prd_backup: total 51%


WHat I need i to finish the loop and give me an exit status of 1 for Warning and 2 for Critical .



Preferably only 1 exit code >1 entries .



So here I would like to see



./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?
WARNING; dev_client: total 67%
WARNING; dr_client: total 54%
WARNING; prd_backup: total 51%
1
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
CRITICAL; dr_prdoracle_bkup: total 78%
2


Any help would be appreciated ...









share|improve this question












share|improve this question




share|improve this question








edited May 29 at 13:13









ilkkachu

47.8k668131




47.8k668131









asked May 29 at 12:49









Cromwer

62




62











  • I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
    – Jeff Schaller
    May 29 at 13:03










  • Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
    – Cromwer
    May 29 at 13:14
















  • I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
    – Jeff Schaller
    May 29 at 13:03










  • Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
    – Cromwer
    May 29 at 13:14















I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
– Jeff Schaller
May 29 at 13:03




I might be out-of-date here, but if that’s a script for nagios, I thought nagios expected only one line of warning or critical output. You’d like the multiple filesystem reports into one line.
– Jeff Schaller
May 29 at 13:03












Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
– Cromwer
May 29 at 13:14




Hi Jeff . There maybe an occurrence where there is more than 1 volume breaching thresholds . That's why I would like clarity on the output .
– Cromwer
May 29 at 13:14










2 Answers
2






active

oldest

votes

















up vote
2
down vote













exit will exit the shell, immediately. If you want to set an exit code to be used later, you'll need to set a flag or save the exit code manually.



#!/bin/bash
warns=""
crits=""
for x in ...; do
if warning_condition; then
warns=1 # or keep a count with warns=$((warns + 1))
elif critical_condition; then
crits=1
fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1


Or even



#!/bin/bash
exit_code=0
set_exit_code()
# save the greatest given exit code
[ "$1" -gt "$exit_code" ] && exit_code=$1

for x in ...; do
if warning_condition; then
set_exit_code 1
elif critical_condition; then
set_exit_code 2
fi
done
exit "$exit_code"



As for this...



$ ./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?


it won't work as $? will hold the exit code of the grep. That could be used to find out if there were any output lines that contained WAR, but I don't think that's what you wanted.



If you want to sort the output warnings first, you'll need to collect the warnings and critical warnings to arrays, and print them at the end. Or just go through the data twice and look for warnings first, and criticals only after that.






share|improve this answer























  • Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
    – Cromwer
    May 29 at 13:11

















up vote
0
down vote













You have:



WarnSpace=$2
CritSpace=$2


and you invoke the script like



./purefs_check.sh -w 50 -c 70 


So the CritSpace value is 50.



You're not processing the options at all. Do this instead:



#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
case $opt in
w) WarnSpace=$OPTARG ;;
c) CritSpace=$OPTARG ;;
*) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
echo "Error: specify a warning threshold with -w" >&2
exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
echo "Error: specify a critical threshold with -c" >&2
exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
SpaceUsed=$SpaceUsed//[^[:digit:]]/ # delete all non-digits

if (( SpaceUsed >= CritSpace )); then
echo "CRITICAL; $volume: total $SpaceUsed"
((crit++))
elif (( SpaceUsed >= WarnSpace )); then
echo "WARNING; $volume: total $SpaceUsed"
((warn++))
fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK





share|improve this answer























  • Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
    – Cromwer
    May 30 at 8:18










  • When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
    – glenn jackman
    May 30 at 14:17











  • "Space used should the position 4 of the file" -- what does that mean? what file?
    – glenn jackman
    May 30 at 14:18










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446687%2f1st-instance-of-a-for-loop-exits-if-i-am-looking-for-a-specific-exit-status%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













exit will exit the shell, immediately. If you want to set an exit code to be used later, you'll need to set a flag or save the exit code manually.



#!/bin/bash
warns=""
crits=""
for x in ...; do
if warning_condition; then
warns=1 # or keep a count with warns=$((warns + 1))
elif critical_condition; then
crits=1
fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1


Or even



#!/bin/bash
exit_code=0
set_exit_code()
# save the greatest given exit code
[ "$1" -gt "$exit_code" ] && exit_code=$1

for x in ...; do
if warning_condition; then
set_exit_code 1
elif critical_condition; then
set_exit_code 2
fi
done
exit "$exit_code"



As for this...



$ ./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?


it won't work as $? will hold the exit code of the grep. That could be used to find out if there were any output lines that contained WAR, but I don't think that's what you wanted.



If you want to sort the output warnings first, you'll need to collect the warnings and critical warnings to arrays, and print them at the end. Or just go through the data twice and look for warnings first, and criticals only after that.






share|improve this answer























  • Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
    – Cromwer
    May 29 at 13:11














up vote
2
down vote













exit will exit the shell, immediately. If you want to set an exit code to be used later, you'll need to set a flag or save the exit code manually.



#!/bin/bash
warns=""
crits=""
for x in ...; do
if warning_condition; then
warns=1 # or keep a count with warns=$((warns + 1))
elif critical_condition; then
crits=1
fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1


Or even



#!/bin/bash
exit_code=0
set_exit_code()
# save the greatest given exit code
[ "$1" -gt "$exit_code" ] && exit_code=$1

for x in ...; do
if warning_condition; then
set_exit_code 1
elif critical_condition; then
set_exit_code 2
fi
done
exit "$exit_code"



As for this...



$ ./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?


it won't work as $? will hold the exit code of the grep. That could be used to find out if there were any output lines that contained WAR, but I don't think that's what you wanted.



If you want to sort the output warnings first, you'll need to collect the warnings and critical warnings to arrays, and print them at the end. Or just go through the data twice and look for warnings first, and criticals only after that.






share|improve this answer























  • Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
    – Cromwer
    May 29 at 13:11












up vote
2
down vote










up vote
2
down vote









exit will exit the shell, immediately. If you want to set an exit code to be used later, you'll need to set a flag or save the exit code manually.



#!/bin/bash
warns=""
crits=""
for x in ...; do
if warning_condition; then
warns=1 # or keep a count with warns=$((warns + 1))
elif critical_condition; then
crits=1
fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1


Or even



#!/bin/bash
exit_code=0
set_exit_code()
# save the greatest given exit code
[ "$1" -gt "$exit_code" ] && exit_code=$1

for x in ...; do
if warning_condition; then
set_exit_code 1
elif critical_condition; then
set_exit_code 2
fi
done
exit "$exit_code"



As for this...



$ ./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?


it won't work as $? will hold the exit code of the grep. That could be used to find out if there were any output lines that contained WAR, but I don't think that's what you wanted.



If you want to sort the output warnings first, you'll need to collect the warnings and critical warnings to arrays, and print them at the end. Or just go through the data twice and look for warnings first, and criticals only after that.






share|improve this answer















exit will exit the shell, immediately. If you want to set an exit code to be used later, you'll need to set a flag or save the exit code manually.



#!/bin/bash
warns=""
crits=""
for x in ...; do
if warning_condition; then
warns=1 # or keep a count with warns=$((warns + 1))
elif critical_condition; then
crits=1
fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1


Or even



#!/bin/bash
exit_code=0
set_exit_code()
# save the greatest given exit code
[ "$1" -gt "$exit_code" ] && exit_code=$1

for x in ...; do
if warning_condition; then
set_exit_code 1
elif critical_condition; then
set_exit_code 2
fi
done
exit "$exit_code"



As for this...



$ ./purefs_check.sh -w 50 -c 70 |grep WAR ;echo $?


it won't work as $? will hold the exit code of the grep. That could be used to find out if there were any output lines that contained WAR, but I don't think that's what you wanted.



If you want to sort the output warnings first, you'll need to collect the warnings and critical warnings to arrays, and print them at the end. Or just go through the data twice and look for warnings first, and criticals only after that.







share|improve this answer















share|improve this answer



share|improve this answer








edited May 29 at 13:17


























answered May 29 at 13:05









ilkkachu

47.8k668131




47.8k668131











  • Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
    – Cromwer
    May 29 at 13:11
















  • Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
    – Cromwer
    May 29 at 13:11















Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
– Cromwer
May 29 at 13:11




Much appreciated . I can have the for loop as a function and call it for Warning and Critical ...
– Cromwer
May 29 at 13:11












up vote
0
down vote













You have:



WarnSpace=$2
CritSpace=$2


and you invoke the script like



./purefs_check.sh -w 50 -c 70 


So the CritSpace value is 50.



You're not processing the options at all. Do this instead:



#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
case $opt in
w) WarnSpace=$OPTARG ;;
c) CritSpace=$OPTARG ;;
*) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
echo "Error: specify a warning threshold with -w" >&2
exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
echo "Error: specify a critical threshold with -c" >&2
exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
SpaceUsed=$SpaceUsed//[^[:digit:]]/ # delete all non-digits

if (( SpaceUsed >= CritSpace )); then
echo "CRITICAL; $volume: total $SpaceUsed"
((crit++))
elif (( SpaceUsed >= WarnSpace )); then
echo "WARNING; $volume: total $SpaceUsed"
((warn++))
fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK





share|improve this answer























  • Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
    – Cromwer
    May 30 at 8:18










  • When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
    – glenn jackman
    May 30 at 14:17











  • "Space used should the position 4 of the file" -- what does that mean? what file?
    – glenn jackman
    May 30 at 14:18














up vote
0
down vote













You have:



WarnSpace=$2
CritSpace=$2


and you invoke the script like



./purefs_check.sh -w 50 -c 70 


So the CritSpace value is 50.



You're not processing the options at all. Do this instead:



#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
case $opt in
w) WarnSpace=$OPTARG ;;
c) CritSpace=$OPTARG ;;
*) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
echo "Error: specify a warning threshold with -w" >&2
exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
echo "Error: specify a critical threshold with -c" >&2
exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
SpaceUsed=$SpaceUsed//[^[:digit:]]/ # delete all non-digits

if (( SpaceUsed >= CritSpace )); then
echo "CRITICAL; $volume: total $SpaceUsed"
((crit++))
elif (( SpaceUsed >= WarnSpace )); then
echo "WARNING; $volume: total $SpaceUsed"
((warn++))
fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK





share|improve this answer























  • Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
    – Cromwer
    May 30 at 8:18










  • When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
    – glenn jackman
    May 30 at 14:17











  • "Space used should the position 4 of the file" -- what does that mean? what file?
    – glenn jackman
    May 30 at 14:18












up vote
0
down vote










up vote
0
down vote









You have:



WarnSpace=$2
CritSpace=$2


and you invoke the script like



./purefs_check.sh -w 50 -c 70 


So the CritSpace value is 50.



You're not processing the options at all. Do this instead:



#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
case $opt in
w) WarnSpace=$OPTARG ;;
c) CritSpace=$OPTARG ;;
*) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
echo "Error: specify a warning threshold with -w" >&2
exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
echo "Error: specify a critical threshold with -c" >&2
exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
SpaceUsed=$SpaceUsed//[^[:digit:]]/ # delete all non-digits

if (( SpaceUsed >= CritSpace )); then
echo "CRITICAL; $volume: total $SpaceUsed"
((crit++))
elif (( SpaceUsed >= WarnSpace )); then
echo "WARNING; $volume: total $SpaceUsed"
((warn++))
fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK





share|improve this answer















You have:



WarnSpace=$2
CritSpace=$2


and you invoke the script like



./purefs_check.sh -w 50 -c 70 


So the CritSpace value is 50.



You're not processing the options at all. Do this instead:



#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
case $opt in
w) WarnSpace=$OPTARG ;;
c) CritSpace=$OPTARG ;;
*) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
echo "Error: specify a warning threshold with -w" >&2
exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
echo "Error: specify a critical threshold with -c" >&2
exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
SpaceUsed=$SpaceUsed//[^[:digit:]]/ # delete all non-digits

if (( SpaceUsed >= CritSpace )); then
echo "CRITICAL; $volume: total $SpaceUsed"
((crit++))
elif (( SpaceUsed >= WarnSpace )); then
echo "WARNING; $volume: total $SpaceUsed"
((warn++))
fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK






share|improve this answer















share|improve this answer



share|improve this answer








edited May 29 at 14:26


























answered May 29 at 14:19









glenn jackman

45.7k265100




45.7k265100











  • Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
    – Cromwer
    May 30 at 8:18










  • When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
    – glenn jackman
    May 30 at 14:17











  • "Space used should the position 4 of the file" -- what does that mean? what file?
    – glenn jackman
    May 30 at 14:18
















  • Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
    – Cromwer
    May 30 at 8:18










  • When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
    – glenn jackman
    May 30 at 14:17











  • "Space used should the position 4 of the file" -- what does that mean? what file?
    – glenn jackman
    May 30 at 14:18















Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
– Cromwer
May 30 at 8:18




Thanks . Glenn . Space used should the position 4 of the file . Calculating the percentage . SpaceUsed=$SpaceUsed//[^[:digit:]]/ this is causing issues and giving all volumes as Critical . CRITICAL; dev_backup: total 1051200020170307095454172210016 CRITICAL; dev_client: total 2000020161025144206172230016
– Cromwer
May 30 at 8:18












When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
– glenn jackman
May 30 at 14:17





When I run this code with the vol_list file you provide, I get CRITICAL; dr_prdoracle_bkup: total 83 and WARNING; prd_backup: total 51 and the exit status is 2
– glenn jackman
May 30 at 14:17













"Space used should the position 4 of the file" -- what does that mean? what file?
– glenn jackman
May 30 at 14:18




"Space used should the position 4 of the file" -- what does that mean? what file?
– glenn jackman
May 30 at 14:18












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446687%2f1st-instance-of-a-for-loop-exits-if-i-am-looking-for-a-specific-exit-status%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay