diff options
author | Ramesh Nachimuthu <rnachimu@redhat.com> | 2014-05-05 10:05:29 +0530 |
---|---|---|
committer | Sahina Bose <sabose@redhat.com> | 2014-05-04 23:53:01 -0700 |
commit | 33f927b5ff56f4b00635234de75b8ae57cb56214 (patch) | |
tree | 24d97b30546fb29b66885c3d5073482f8ee99efe | |
parent | 2d1f9f2f5e0e5c0816a4116d280254566d711699 (diff) |
autoconf: handle exceptions in nrpe commands
Handling the exceptions and failure from NRPE commands.
Change-Id: I36cba2d6adf8484ba134ad3f7aec77437ba07857
Signed-off-by: Ramesh Nachimuthu <rnachimu@redhat.com>
Reviewed-on: http://review.gluster.org/7659
Reviewed-by: Sahina Bose <sabose@redhat.com>
-rwxr-xr-x | plugins/discovery.py | 43 | ||||
-rw-r--r-- | tests/test_discovery.py | 6 |
2 files changed, 28 insertions, 21 deletions
diff --git a/plugins/discovery.py b/plugins/discovery.py index 69e1d86..2f1b9f6 100755 --- a/plugins/discovery.py +++ b/plugins/discovery.py @@ -39,22 +39,29 @@ from config_generator import CHANGE_MODE_UPDATE nrpeCmdPath = utils.CommandPath("nrpe", NRPE_PATH, ) -def excecNRPECommand(host, command, arguments=None, jsonOutput=True): +def execNRPECommand(host, command, arguments=None, jsonOutput=True): nrpeCmd = [nrpeCmdPath.cmd, "-H", host, "-c", command] if arguments: nrpeCmd.append('-a') nrpeCmd.extend(arguments) - (returncode, outputStr, err) = utils.execCmd(nrpeCmd) - #convert to dictionary - if not jsonOutput: - return outputStr[0] + (returncode, outputStr, err) = utils.execCmd(nrpeCmd, raw=True) + if returncode == 0: + if jsonOutput: + try: + #convert to dictionary + resultDict = json.loads(outputStr) + except Exception as e: + e.args += (outputStr,) + raise + return resultDict + else: + return outputStr else: - try: - output = json.loads(outputStr[0]) - except Exception as e: - e.args += (outputStr[0]) - raise - return output + 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) #Discovers volumes info one by one. @@ -63,10 +70,10 @@ def excecNRPECommand(host, command, arguments=None, jsonOutput=True): #in NRPE. def discoverVolumes(hostip): resultDict = {'volumes': []} - volumeList = excecNRPECommand(hostip, "discover_volume_list") + volumeList = execNRPECommand(hostip, "discover_volume_list") for volumeName in volumeList.keys(): - volumeDetail = excecNRPECommand(hostip, "discover_volume_info", - [volumeName]) + volumeDetail = execNRPECommand(hostip, "discover_volume_info", + [volumeName]) resultDict['volumes'].append(volumeDetail.get(volumeName)) return resultDict @@ -100,12 +107,12 @@ def discoverCluster(hostip, cluster): #Discover the logical components componentlist = discoverVolumes(hostip) #Discover the peers - hostlist = excecNRPECommand(hostip, "discoverpeers") + hostlist = execNRPECommand(hostip, "discoverpeers") #Add the ip address of the root node given by the user to the peer list hostlist[0]['hostip'] = hostip for host in hostlist: #Get host names - hostDetails = excecNRPECommand(host['hostip'], "discoverhostparams") + hostDetails = execNRPECommand(host['hostip'], "discoverhostparams") host.update(hostDetails) #Get the list of bricks for this host and add to dictionary host['bricks'] = [] @@ -342,7 +349,7 @@ def configureNodes(clusterDelta, nagiosServerAddress, mode): #Only when a new node is added or whole cluster is added freshly. if (clusterDelta.get('changeMode') == CHANGE_MODE_ADD or host.get('changeMode') == CHANGE_MODE_ADD) \ - and (host['use'] != 'gluster_cluster'): + and (host['use'] == 'gluster-host'): if not nagiosServerAddress: #Nagios server address should be specified as arg in auto mode if mode == "manual": @@ -356,7 +363,7 @@ def configureNodes(clusterDelta, nagiosServerAddress, mode): #Configure the nodes. clusterName, Nagios server address and #host_name is passed as an argument to nrpe command #'configure_gluster_node' - excecNRPECommand( + execNRPECommand( host['address'], 'configure_gluster_node', [clusterDelta['hostgroup_name'], nagiosServerAddress, host['host_name']], False) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index a211167..122d8a1 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -21,8 +21,8 @@ from testrunner import PluginsTestCase as TestCaseBase class TestDiscovery(TestCaseBase): - def _mockExcecNRPECommand(self, host, command, arguments=None, - jsonOutput=True): + def _mockExecNRPECommand(self, host, command, arguments=None, + jsonOutput=True): if command == "discover_volume_list": return self._getVolumeNames() elif command == "discover_volume_info": @@ -93,7 +93,7 @@ class TestDiscovery(TestCaseBase): # Method to test the discoverCluster() method def testDiscoverCluster(self): - discovery.excecNRPECommand = self._mockExcecNRPECommand + discovery.execNRPECommand = self._mockExecNRPECommand clusterName = "test-cluster" host = "172.16.53.1" clusterdata = discovery.discoverCluster(host, clusterName) |