Usar arrays no bash pode tornar seu código muito mais fácil de ler e manter.
#!/bin/bash
libexec="/usr/local/nagios/libexec"
# for command line arguments:
hostnames=("$@")
# or to hardcode them:
hostnames=(hostname1 hostname2 hostname3 hostname4 hostname5 hostname6)
nagios_ok=0
nagios_warning=1
nagios_critical=2
nagios_unknown=3
curl_opts=(
--silent
--head
--output /dev/null
--write-out '%{http_code}'
--header 'accept: application/json'
--header 'x-xxx-webservice-client-id: apimonitoring'
--header 'Host:api-local.dal.xxx.com'
)
# an associative array
declare -A host_codes
for host in "${hostnames[@]}"; do
code=$(curl "${curl_opts[@]}" "http://$host/v4/catalog/category/5545")
host_codes["$code"]+="$host, "
done
for code in "${!host_codes[@]}"; do
[[ $code == 2* ]] && ok="OK" || ok="NOT OK"
echo "STATUS: $ok HTTP Response $code for ${host_codes["$code"]%, }"
done
para definir também o status de saída, você pode:
status=$nagios_ok
for code in "${!host_codes[@]}"; do
case $code in
2*) ok="OK" ;;
*) ok="NOT OK"; status=$nagios_critical ;;
esac
echo "STATUS: $ok HTTP Response $code for ${host_codes["$code"]%, }"
done
exit $status