#!/bin/bash

PLUGINS_DIR=/usr/lib/nagios/plugins
PLUGIN=$1
HOST=$2
PORTS=$3
VERB=$4

##
EXIT=0
OK=""
WARN=""
CRIT=""

function add_msg {
	CODE=$1
	MSG="$2"
	PORT="$3"
	pre=""
	if [ $CODE -eq 0 ]
	then
		[ "$OK" != "" ] && pre=" | "
		OK="${OK}${pre}${MSG}"
	fi
	if [ $CODE -eq 1 ]
	then
		[ "$WARN" != "" ] && pre=" | "
		WARN="${WARN}${pre}Port : $PORT - ${MSG}"
		[ $EXIT -eq 0 ] && export EXIT=1
	fi
	if [ $CODE -eq 2 ]
	then
		[ "$CRIT" != "" ] && pre=" | "
		CRIT="${CRIT}${pre}Port : $PORT - ${MSG}"
		export EXIT=2
	fi
}

for p in $PORTS
do
	msg="`$PLUGINS_DIR/$PLUGIN -H $HOST -p $p`"
	add_msg $? "$msg" "$p"
	[ "$VERB" == "-v" ] && echo "$PLUGIN - $p - $msg"
done

[ $EXIT -eq 0 ] && echo "$OK" && exit 0
[ $EXIT -eq 1 ] && echo "$WARN" && exit 1
[ $EXIT -eq 2 ] && echo "$CRIT" && exit 2
