I saw your command and understood the issue. Now I am posting my version of the solution.
so as I understand that you would like to check the disk space on the remote host or multiple hosts, and if the disk space is above 70%, GENERATE alert. Correct me if my understanding is not correct.
Before we see the script, lets see the output:
%_Host@User:/home/Gaurava/study> ./fscheck.sh
==========[192.168.246.132 STARTS]==========
[ **ALERT** (192.168.246.132) has FS above THRESHOLD ---vmhgfs-fuse 224G 182G 42G 82% /mnt/hgfs--- ]
[ **ALERT** (192.168.246.132) has FS above THRESHOLD ---/dev/sda1 497M 376M 122M 76% /boot--- ]
==========[192.168.246.132 ENDS]==========
==========[192.168.246.137 STARTS]==========
[ **ALERT** (192.168.246.137) has FS above THRESHOLD ---.host:/ 224G 182G 42G 82% /mnt/hgfs--- ]
==========[192.168.246.137 ENDS]==========
%_Host@User:/home/Gaurava/study>
In above output, the script logs to 2 remote hosts, one by one and then executes command to check disk space and it found 3 file systems above the specified limit. So it generate ALERT.
The script:
#!/bin/bash
# Define your command, host/s and user/s.
command='df -Ph'
host1=192.168.246.132
host2=192.168.246.137
user=gaurav
# Main loop STARTS
for h in $host1 $host2
do
# This line can be removed.
echo "==========[$h STARTS]=========="
# Here you can replace this with your sshpass command.
# I am feeding the command output to a while loop to read
# the output line by line, for each of the host/s.
ssh $user@$h "$command" | while read line
do
# Now we check, if the disk space output contains anything
# which matches value greater than 70%, thats it! and
# generate alert.
if [[ $line =~ .*7[1-9]%.* || $line =~ .*[89][0-9]%.* ]]
then
# If above 'if' statement is TRUE, It generates ALERT
# in the below format. '$line' is the variable holding
# info about the file system breaching the threshold.
echo "[ **ALERT** ($h) has FS above THRESHOLD ---$line--- ]"
elif [[ $line =~ .*100% ]]
then
echo "[ **ALERT** ($h) FS reached 100% ---$line--- ]"
fi
done
# This line can be removed.
echo "==========[$h ENDS]==========" ; echo
done
# Main loop ENDS.
My script is not using the exact same logic which you were trying to use but it is works on a similar logic, and utilizes a couple of loops, which simplify our problem and provide greater control
I hope this helps to resolve your problem. Please let me know if it was any good!
Edit: Added an else part to the if loop. Although when I checked, it is ignoring any FS if it has already reached 100%, but still, it poses no harm to add a warning/notification. It is a good idea indeed.
Edit2: updated the if loop with one more condition and added echo for 100%. I realized later that earlier loop wasn't matching numbers 71,81,91.