#!/usr/bin/perl
# Copyright (C) 2010 Lukas Matecki
# Nagios Check Script to run Checks for the Alcatel Omnitouch Suite Server on RHEL 5.4 trouth SSH
use strict;

sub returnNagios ();
sub printParmFail();

#my @services=`sudo service icsd status`;
my $return="cant run check!";
my @perfData="";
my @serviceToCheck=("jscd", "jsad", "acapid", "app_servd", "alc_lrd", "crsd", "dmcd", "dtad", "ecsd", "e2k7csd", "emsd", "evsd", "flexlmd", "imap4fed", "imsd", "ldapd", "mascd", "msd", "ossd", "otsd", "pmsd", "prsd", "sybased", "tomcatd", "udasd", "umfcd");
my $i=0;
my @checkResult;
my $j=0;


sub printParmFail() {
        print "Wrong Parameter usage: \"./check-alcatel-suite.pl \n no Arguments needed\n";
        return 0;
}

sub returnNagios () {
        #All goes well
        if ($return == 0) {
                printf("OK - All Services are running | @perfData \n");
                exit 0;
        }
        #ohoh
        if ($return == 1) {
                printf("WARNING - Something went wrong! | @perfData \n");
                exit 1;
        }

        #Fuck all ERROR ! PANIC
        if ($return == 2) {
                printf("ERROR - Not all Services running! | @perfData \n");
                exit 2;
        }
}

foreach (@serviceToCheck) {
        @checkResult[$_]=rindex(`service $_ status`, "STARTED");
        if (@checkResult[$_] != -1) {
                @perfData[$j]=$_ . " = OK,";
                $i++;
        } else {
                @perfData[$j]=$_ . " = CRITICAL,";
        }
        $j++;
}
chop( @perfData[$j-1]);

if ($i == 26) {
        $return=0;
        returnNagios();

}

if ($i > 26) {
        $return=1;
        returnNagios();
}


if ($i < 26) {
        $return=2;
        returnNagios();
}

#print "return=" . $return . "      perfData=";
#foreach(@perfData){
#       print " " . $_ . " ";
#}
