#!/bin/bash
#
# Copyright 2011-2013, Dell
# Copyright 2013-2014, SUSE LINUX Products GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

[[ $CROWBAR_IP ]] || CROWBAR_IP="127.0.0.1"

stat_line_re='status(OK|WARNING|CRITICAL|UNKNOWN|PENDING)'
echo "Host   OK  WARN  CRIT  UNKNOWN  PENDING"
while read HOST; do
    HOST=${HOST%%.*}
    OK=0 WARN=0 CRIT=0 UNKN=0 PEND=0
    while read -r line; do
	[[ $line =~ $stat_line_re ]] || continue
	case ${BASH_REMATCH[1]} in
	    OK) ((OK++));;
	    WARNING) ((WARN++));;
	    CRITICAL) ((CRIT++));;
	    UNKNOWN) ((UNKN++));;
	    PENDING) ((PEND++));;
	esac
    done < <(wget --user=nagiosadmin --password=password \
	"http://${CROWBAR_IP:=127.0.0.1}/cgi-bin/nagios3/status.cgi?host=$HOST" -O- -q)
    echo "$HOST  $OK  $WARN  $CRIT  $UNKN  $PEND"
done < <(knife node list | grep "\"" | awk -F\" '{ print $2 }')
