#!/bin/bash
# vim: expandtab:ts=4:sw=4:syntax=perl

# read rc files if exist
unset PROFILEDOTD
[ -e /etc/thruk/thruk.env  ] && . /etc/thruk/thruk.env
[ -e ~/etc/thruk/thruk.env ] && . ~/etc/thruk/thruk.env
[ -e ~/.thruk              ] && . ~/.thruk
[ -e ~/.profile            ] && . ~/.profile

BASEDIR=$(dirname $0)/..

# git version
if [ -d $BASEDIR/.git -a -e $BASEDIR/lib/Thruk.pm ]; then
  export PERL5LIB="$BASEDIR/lib:$PERL5LIB";
  if [ "$OMD_ROOT" != "" -a "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$BASEDIR/"; fi

# omd
elif [ "$OMD_ROOT" != "" ]; then
  export PERL5LIB=$OMD_ROOT/share/thruk/lib:$PERL5LIB
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi

# pkg installation
else
  export PERL5LIB=$PERL5LIB:@DATADIR@/lib:@THRUKLIBS@;
  if [ "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG='@SYSCONFDIR@'; fi
fi

eval 'exec perl -x $0 ${1+"$@"} ;'
    if 0;
# / this slash makes vscode syntax highlighting work
#! -*- perl -*-
#line 35

##############################################
use strict;
use warnings;
use Thruk::Utils::CLI;

my $states = {
    '0' => 'Ok',
    '1' => 'Warning',
    '2' => 'Critical',
    '3' => 'Unknown',
};

my $cli      = Thruk::Utils::CLI->new;
my $db       = $cli->get_db();
my $services = $db->get_services();

printf("%-20s   %-30s %-10s\n", "Host", "Service", "Status");
print("===============================================================\n");
for my $s (@{$services}) {
    printf("%-20s   %-30s %-10s\n", $s->{'host_name'}, $s->{'description'}, $states->{$s->{'state'}});
}
exit 0;

##############################################

=head1 NAME

dump_services - Command Line Utility which dumps services by using Thruks connection pool

=head1 SYNOPSIS

Usage: dump_services

=head1 DESCRIPTION

This script lists all services

=head1 AUTHOR

2013, Sven Nierlein, <sven.nierlein@consol.de>

=cut
