summaryrefslogtreecommitdiffstats
path: root/plugins/server_utils.py
diff options
context:
space:
mode:
authorShubhendu Tripathi <shtripat@redhat.com>2014-05-06 14:39:42 +0530
committerSahina Bose <sabose@redhat.com>2014-05-20 00:20:50 -0700
commit0c6651331d3a8fc867a1799b18baed4c0789ba69 (patch)
treec09a84fd2f806b1a5da4e10ac2884f39cd3ba917 /plugins/server_utils.py
parent25a9105f9583cf165526db0c24c67bd12c4336fe (diff)
nagios-server-addons: NRPE command with timeout
Introduced a utility method to return a NRPE base command with timeout set externally. Currently if a plugin internally executes NRPE to get the details from the node, there is no mechanism that timeout could be set to more than 10 sec. This method provides the NRPE command with timeout (if passed). This is required for some of the NRPE calls where gluster commands get executed and they might take more time. All the plugins, which execute a NRPE within, can provide optional command line argument for timeout, and same can be used for forming the proper NRPE call with timeout value. Change-Id: Id97624df743664a320a585acc4a85cfcf64d0a07 Signed-off-by: Shubhendu Tripathi <shtripat@redhat.com> Reviewed-on: http://review.gluster.org/7682 Reviewed-by: Sahina Bose <sabose@redhat.com> Tested-by: Sahina Bose <sabose@redhat.com>
Diffstat (limited to 'plugins/server_utils.py')
-rwxr-xr-xplugins/server_utils.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/plugins/server_utils.py b/plugins/server_utils.py
index b3767bd..46a8cf4 100755
--- a/plugins/server_utils.py
+++ b/plugins/server_utils.py
@@ -1,5 +1,5 @@
#!/usr/bin/python
-# discovery.py Nagios plugin to discover Gluster entities using NRPE
+# server_utils.py Utility methods used by nagios-server-addons module
# Copyright (C) 2014 Red Hat Inc
#
# This program is free software; you can redistribute it and/or
@@ -16,14 +16,18 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
+import sys
+import json
import datetime
from pynag import Model
from glusternagios import utils
import submit_external_command
+from constants import NRPE_PATH
serviceCmdPath = utils.CommandPath("service", "/sbin/service", )
+nrpeCmdPath = utils.CommandPath("check_nrpe", NRPE_PATH, )
def restartNagios():
@@ -76,3 +80,40 @@ def getHostGroup(name):
return hostgroup[0]
else:
return None
+
+
+def getNRPEBaseCommand(host, timeout=None):
+ command = NRPE_PATH + " -H " + host
+ if timeout is not None:
+ command += " -t %s" % timeout
+ command += " -c "
+ return command
+
+
+def execNRPECommand(host,
+ command,
+ arguments=None,
+ timeout=None,
+ json_output=True):
+ nrpeCmd = getNRPEBaseCommand(host, timeout).split()
+ nrpeCmd.append(command)
+ if arguments:
+ nrpeCmd.append('-a')
+ nrpeCmd.extend(arguments)
+ (returncode, outputStr, err) = utils.execCmd(nrpeCmd, raw=True)
+ if returncode == 0:
+ if json_output:
+ try:
+ resultDict = json.loads(outputStr)
+ except Exception as e:
+ e.args += (outputStr,)
+ raise
+ return resultDict
+ else:
+ return outputStr
+ else:
+ print "Failed to execute NRPE command '%s' in host '%s' " \
+ "\nError : %s" \
+ "Make sure NPRE server in host '%s' is configured to accept " \
+ "requests from Nagios server" % (command, host, outputStr, host)
+ sys.exit(utils.PluginStatusCode.CRITICAL)