0

I have this script i'm trying to write.

#!/bin/bash

libexec="/usr/local/nagios/libexec"
#hostname=$1
hostname="hostname1 hostname2 hostname3 hostname4 hostname5 hostname6"
nagios_ok=0
nagios_warning=1
nagios_critical=2
nagios_unknown=3

#if [[ $hostname == "" ]]; then
#  echo "please enter a host to check"
#  exit $nagios_unknown
#fi

for host in $hostname;
do
output=$(curl -s -i -H 'accept: application/json' -H 'x-xxx-webservice-client-id: apimonitoring' "http://$host/v4/catalog/category/5545" -H 'Host:api-local.dal.xxx.com' | grep "HTTP/1.1" | awk '{print $2}')

if [[ $output == "200" ]]; then
echo "STATUS: OK HTTP Response $output for $host"
#    exit $nagios_ok (with the appropriate exit $nagios_ok)
else
echo  "STATUS: NOT OK HTTP Response $output for $host"
#    exit $nagios_critical (with appropriate exit $nagios_critical)
fi
done

the output is not something i really like

STATUS: OK HTTP Response 200 for 10.xx.xx.xx
STATUS: OK HTTP Response 200 for 10.xx.xx.xx
STATUS: OK HTTP Response 200 for 10.xx.xx.xx
STATUS: NOT OK HTTP Response for 10.xx.xx.xx
STATUS: OK HTTP Response 200 for 10.xx.xx.xx
STATUS: NOT OK HTTP Response for 10.xx.xx.xx

I want something like this

STATUS: OK HTTP Response 200 for 10.xx.xx.xx, 10.xx.xx.xx, 10.xx.xx.xx, etc ..
STATUS: NOT OK HTTP Response 404 for 10.xx.xx.xx, 10.xx.xx.xx, 10.xx.xx.xx, etc..

Thank you very much for the help

3

Using arrays in bash can make your code much easier to read and to maintain.

#!/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

for also setting the exit status, you can:

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
2
  • Hey glenn, thank you for this code, it work wonderful, just a follow up, is there a way to add the exit statement in for this? – Hai Le Oct 26 '17 at 17:04
  • @HaiLe, don't edit answers when you should be adding a comment. – glenn jackman Oct 26 '17 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.