#!/bin/bash
#

DESCR="This Nagios script checks a Linux Modden Wallet to make sure Staking and Master Nodes are Enabled
Script was created by @Crypt0xPaladin at https://DigitalAssets.Tools"


# Paths to commands used in this script.
# These may have to be modified to match your system setup.

MODDEN_CLI="/usr/local/bin/modden-cli"
MODDEN_CONF="/var/lib/nagios/.modden/modden.conf"

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="0.1"

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4


print_revision() {
    echo "$1 v$2"
	echo ""
    printf '%b' "This plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute\ncopies of the plugins under the terms of the GNU General Public License.\n"
}


print_documentation() {

	print_revision $PROGNAME $REVISION

	echo -e "

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

# Setup Nagios Server NRPE checks
# Edit your server config file and add new service(s)
nano /etc/nagios/servers/server.cfg

# Modden Staking Service	
define service {
	use                        generic-service
	host_name                  YourServerHostname
	service_description        Check Modden Staking
	check_command              check_nrpe!check_modden_staking
}

# Modden Master Node Service
define service {
	use                        generic-service
	host_name                  YourServerHostname
	service_description        Check Modden Master Node
	check_command              check_nrpe!check_modden_masternode
}

# Restart Nagios
systemctl restart nagios

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

# Setup Nagios Client NRPE checks

# The check_modden script needs to be able to execute the modden-cli and read a modden.conf file in the nagios home directory.
# So you need to copy the modden-cli to /usr/local/bin/modden-cli. You can also update the variables at the top of this file.
cp modden-cli /usr/local/bin/
mkdir /var/lib/nagios/.modden
cp ~/.modden/modden.conf /var/lib/nagios/.modden/modden.conf
chown -R nagios.nagios /var/lib/nagios/.modden
 
nano /etc/nagios/nrpe.cfg

# Modden Staking Service
command[check_modden_staking]=/usr/lib/nagios/plugins/check_modden -s

# Modden Master Node Service
command[check_modden_masternode]=/usr/lib/nagios/plugins/check_modden -m

# Restart Nagios NRPE
systemctl restart nagios-nrpe-server
"
}


print_help() {
	echo ""
	echo "$DESCR"
	echo ""
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
}


print_usage() {
    echo "usage: check_modden [-d|-h|-m|-s|-v]"
    echo ""
    echo "optional arguments:"
	echo "  -d		Show Nagios Documentation"
    echo "  -h		Show this help message"
    echo "  -m		Check Modden Master Node Status"
    echo "  -s		Check Modden Staking Status"
    echo "  -v		Show check_modden version"
    echo ""

}


get_mn_status() { 
	GET_MN_STATUS=`$MODDEN_CLI -conf=$MODDEN_CONF getmasternodestatus | grep -c "Masternode successfully started"`
	if [[ "$GET_MN_STATUS" == "1" ]]; then
	        echo "OK: Modden Masternode Running"
	        exitstatus=$STATE_OK
		exit $exitstatus
	else
	        echo "Critical: Modden Masternode stopped or could not get status"
	        exitstatus=$STATE_CRITICAL
		exit $exitstatus
	fi
}


get_stake_status() {         
    GET_STAKE_STATUS=`$MODDEN_CLI -conf=$MODDEN_CONF getstakingstatus | grep staking_status | grep -c true`
    GET_STAKE_STATUS2=`modden-cli`
    if [[ "$GET_STAKE_STATUS" == "1" ]]; then

		GET_WALLET_BAL=`$MODDEN_CLI getwalletinfo | grep '"balance":' | awk -F" " '{print $2}' | awk -F"." '{print $1}'`
		if [[ "$?" != ""0 ]]; then echo "Could not open Wallet"; exit $STATE_CRITICAL; fi

		GET_WALLET_UN=`$MODDEN_CLI getwalletinfo | grep '"unconfirmed_balance":' | awk -F" " '{print $2}' | awk -F"." '{print $1}'`
		if [[ "$?" != ""0 ]]; then echo "Could not open Wallet"; exit $STATE_CRITICAL; fi

		GET_WALLET_IM=`$MODDEN_CLI getwalletinfo | grep '"immature_balance":' | awk -F" " '{print $2}' | awk -F"." '{print $1}'`
		if [[ "$?" != ""0 ]]; then echo "Could not open Wallet"; exit $STATE_CRITICAL; fi

        echo "OK: Modden Staking Enabled - Wallet Bal: $GET_WALLET_BAL, Un: $GET_WALLET_UN, Im: $GET_WALLET_IM"
        exitstatus=$STATE_OK
        exit $exitstatus
    else
        echo "Critical: $GET_STAKE_STATUS2 Modden Staking disabled or could not get status"
        exitstatus=$STATE_CRITICAL
		exit $exitstatus
	fi
}


# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_help
    exit $STATE_UNKNOWN
fi

# Grab the command line arguments

exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
		-d)
			print_documentation
			;;
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        -m)
	    get_mn_status;
            shift
            ;;
        -s)
	    get_stake_status;
            shift
            ;;
        -v)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --exitstatus)
            exitstatus=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done


