Search code examples
pythonpluginsnagios

Remote execution of command in Python


By no means do I write scripts very often, but I am trying to write a Nagios plugin to check the status of a RAID controller on a remote host. The issue is that the command to get the output requires elevated privileges. What would be the correct, and most effective way to pull this off? The goal is to run:

'/opt/MegaRAID/MegaCli/MegaCli64 -ShowSummary -a0'

on a remote host from the monitoring server,

and then follow the basic idea of this logic:

#Nagios Plugin for Testing LSI Raid Status
import os, sys
import argparse
import socket
import subprocess
#nagios exit codes do not change#
OK = 0
WARNING = 1
CRITICAL = 2
DEPENDENT = 3
UNKNOWN = 4
#nagios exit codes do not change#
#patterns to be searched
active = str("Active")
online = str("Online")
k = str("OK")
degrade = str("Degraded")
fail = str("Failed")
parser = argparse.ArgumentParser(description='Py3 script for monitoring RAID status.')
#arguments
parser.add_argument("--user",
       metavar = '-U',
       help = "username for remote connection")
parser.add_argument("--hostname",
        metavar = '-H',
        help = "hostname of the remote host")
args = parser.parse_args()
print(args)
#turning args into variables
hostname = args.hostname
user = args.user
ssh = subprocess.Popen(f"ssh {user}@{hostname} /opt/MegaRAID/MegaCli/MegaCli64 -ShowSummary -a0", shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
check = ssh.stdoutreadlines()
OK_STR = str("RAID is OK!")
WARN_STR = str("Warning! Something is wrong with the RAID!")
CRIT_STR = str("CRITICAL! THE RAID IS BROKEN")
UNK_STR = str("Uh oh! Something ain't right?")
if (degrade) in (check):
    print(WARN_STR) and exit(WARNING)
elif (fail) in (check):
    print (CRIT_STR) and exit(CRITICAL)
elif (active) or (online) or (k) in (check):
    print(OK_STR) and exit(OK)
else:
    print(UNK_STR) and exit(UNKNOWN)

Any thoughts? This is far from my forte (and also an unfinished script) so I apologize for the layman format and any confusion in my phrasing.


Solution

  • I am trying to write a Nagios plugin to check the status of a RAID controller on a remote host. The issue is that the command to get the output requires elevated privileges. What would be the correct, and most effective way to pull this off?

    I would recommend running the script remotely over NRPE on the system in question, and then give the user the NRPE daemon is running as (probably nagios or similar) sudo permissions to run that script with some very exact parameters.

    The nrpe.cfg file mentions this example:

    # Usage scenario:
    # Execute restricted commmands using sudo.  For this to work, you need to add
    # the nagios user to your /etc/sudoers.  An example entry for alllowing
    # execution of the plugins from might be:
    #
    # nagios          ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/
    

    ...but there's no reason to be so forgiving, you can make it a lot safer by only allowing an exact command:

    nagios ALL = NOPASSWD: /usr/sbin/megacli
    

    Note that this allows any parameters with that command, this is even safer as it will not allow any other variants (example):

    nagios ALL = NOPASSWD: /usr/sbin/megacli -a foo -b bar -c5 -w1
    

    Then configure the nrpe command to run the above with sudo before it, and it should work. You can verify by su:ing to the nagios user and trying the sudo command yourself.

    Also, note that there are very likely some available modules you can import for python nagios plugins that makes it easier for you, to get built-in support for things like thresholds and their syntax.