#!/bin/bash

#Author: Tino

#VARIABLES NAGIOS
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

PROGNAME=`basename $0 .sh`
VERSION="Version 1.0"

print_version() {
    echo "$VERSION"
}

print_help() {
    print_version $PROGNAME $VERSION
    echo ""
    echo "$PROGNAME is a Nagios plugin to check a specific service using systemctl."
    echo ""
    echo "$PROGNAME -s <service_name>"
    exit $UNKNOWN
}

if test -z "$1"
then
        print_help
        exit $CRITICAL
fi

while test -n "$1"; do
    case "$1" in
        --help|-h)
            print_help
            exit $UNKNOWN
            ;;
        --version|-v)
            print_version $PROGNAME $VERSION
            exit $UNKNOWN
            ;;
        --service|-s)
            SERVICE=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_help
            exit $UNKNOWN
            ;;
        esac
    shift
done

if systemctl is-active $SERVICE >/dev/null 2>&1
then
    echo -e "OK: Service $SERVICE is running!"
    exit $OK
else
    echo -e "CRITICAL: Service $SERVICE is not running!"
    exit $CRITICAL
fi

__END__

=for stopwords
service Nagios linux remctl 

=head1 NAME

check_service - Nagios plugin that returns the status of a linux service

=head1 SYNOPSIS

B<check_service> -s <Linux Service Name>

=head1 DESCRIPTION

This Nagios plugin calls C<systemctl> to check on the running status of the
requested Linux Service, the status determines the Nagios state on return.

=head1 EXIT STATUS

B<check_service> follows the standard Nagios exit status requirements.
This means that it will exit with status 0 if there specified service
is running or with status 2 if the specified service is not running.
For other errors, such as invalid syntax, B<check_service> will exit 
with status 3.


=head1 AUTHORS

Downloaded from: https://exchange.nagios.org/directory/Plugins/Reporting/Linux-Check-Service/details
by Lonlone Lee <lonlone@stanford.edu>

=cut
