-1

I have a script which utilises SaltStack's command-line as well as BASH commands. The script is used to gather data from multiple Linux servers (hence SaltStack), one of the checks which I would like to gather is disk space.

I have done this by using the following command:

salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i

$i = hostname and the use of the ugly '"'"' is so that my command can run via SaltStack as Salt's remote execution functionality requires single quotes around the command, if I left them in my command wouldn't run inside my BASH script.

Example syntax:

salt $hostname cmd.run 'command here'

After many questions on here and with colleagues I have this section of the script sorted. However I now the problem of stripping the output of my above command to remove the 'G' so that my script can compare the output with a threshold I have defined and turn the HTML which this script is piping to red.

Threshold:

diskspace_threshold=5

Command:

while read i ; do
diskspace=`salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i`

Validation check:

if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then
    ckbgc="red"
fi

The method I have used for stripping the G works on the command line but not within my script so it must be something to do with the syntax or just the fact that it is now within a script. Any ideas/thoughts would be helpful.

Cheers!

EDIT: Here is the error message I receive when running my script: serverdetails.sh: line 36: p : 2.8: syntax error: invalid arithmetic operator (error token is ".8")

jto
  • 175
  • 6
  • 21
  • why don't you strip `G` in the long command itself? `sed 's/G//' <<< '10G'` – Avinash Raj Aug 09 '17 at 12:33
  • Hi @AvinashRaj I was thinking about doing this and this is what I will probably have to do. However, I much prefer the webpage to view '10G' than just '10'. Yet the ability to change to red if the threshold isn't met is more important. – jto Aug 09 '17 at 12:37
  • `disknum=$(sed 's/G//'<<<$diskspace);echo $disknum` – Avinash Raj Aug 09 '17 at 12:45
  • Does your script use `#!/bin/sh`? Change it to `#!/bin/bash` -- I believe that `${var//pattern}` is a bashism – glenn jackman Aug 09 '17 at 13:08
  • 1
    Remove *what* "G"? You don't actually show the input string. If it's a *trailing* "G", then do `"${diskspace%G}"`. If you want to remove from the *first* "G" to the end of the string then do `"${diskspace%%G*}"` -- those will work in plain sh – glenn jackman Aug 09 '17 at 13:10
  • Hi @glennjackman yes I am using #!/bin/bash - I'll give it a try, thanks for the comment – jto Aug 09 '17 at 13:17
  • @glennjackman I tried "${diskspace%G}" to no avail. It still returned the same error. – jto Aug 09 '17 at 13:22
  • @AvinashRaj Hi Avinash, thanks for the comment. Just to be clear, where should I add this line? Is it a new variable? A change in my if statement or my command? Sorry, I don't understand! – jto Aug 09 '17 at 13:25
  • after `diskspace=...` , `disknum=$(sed 's/G//'<<<$diskspace)`, `if [[ "$disknum" -lt.. ` – Avinash Raj Aug 09 '17 at 13:39
  • Thanks for the insight @AvinashRaj I updated my script with this and I still get the same error! – jto Aug 09 '17 at 14:28

1 Answers1

2

I assume the error is coming from here (is this line 36?)

if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then

Note the error message:

serverdetails.sh: line 36: p : 2.8: syntax error: invalid arithmetic operator (error token is ".8")

bash does not do floating point arithmetic

$ [[ 2.8 -lt 3 ]] && echo OK
bash: [[: 2.8: syntax error: invalid arithmetic operator (error token is ".8")

You'll need to do something like this:

result=$( bc <<< "${diskspace%G} < $diskspace_threshold" )
if [[ $result == 1 ]]; then
  echo OK
else
  echo Boo
fi
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thanks for the answer Glen - However this script will be ran daily and the available disk space will therefore change, will this method support this? – jto Aug 09 '17 at 16:01
  • I do not understand your comment at all. Which method are you referring to? My hardcoded "2.8" was just to demonstrate the error. – glenn jackman Aug 09 '17 at 16:11
  • @jto , yeah it should work.. What he does is, getting the disk space and then he compared it with the threshold (lesser than or not). if it's lesser then 1 should be stored to result variable else 0 would be stored. Error you got is mainly because of the decimal symbol in the diskspace value... – Avinash Raj Aug 09 '17 at 16:42
  • Thanks guys - I misinterpreted Glen's answer. So, I should change the $result == 1 to 5 so that it is my existing threshold value? – jto Aug 09 '17 at 17:15
  • In `bc`, the result of the `a < b` operation is either "0" (a is not less than b) or "1" (a is actually less than b) – glenn jackman Aug 09 '17 at 17:19
  • Thank you for the answer @glennjackman and thank you Avinash also. I just re-read my comments and I was clearly over worked yesterday, I've implemented your answer in my script and works fine! – jto Aug 10 '17 at 06:50