diff --git a/confluent_client/bin/nodehealth b/confluent_client/bin/nodehealth index 46240fc3..83e4a9d8 100755 --- a/confluent_client/bin/nodehealth +++ b/confluent_client/bin/nodehealth @@ -34,46 +34,56 @@ except IndexError: sys.stderr.write('Usage: {0} \n'.format(sys.argv[0])) sys.exit(1) -session = client.Command() -exitcode = 0 -healthbynode = {} -healthexplanations = {} -for health in session.read('/noderange/{0}/health/hardware'.format(noderange)): - if 'error' in health: - sys.stderr.write(health['error'] + '\n') - if 'errorcode' in health: - exitcode |= health['errorcode'] - else: - exitcode |= 1 - continue - if 'databynode' not in health: - continue - health = health['databynode'] - for node in health: - if 'error' in health[node]: - sys.stderr.write('{0}: Error: {1}\n'.format( - node, health[node]['error'])) - exitcode |= 1 - if 'health' in health[node]: - healthbynode[node] = health[node]['health']['value'] - if 'sensors' in health[node]: - healthexplanations[node] = [] - for sensor in health[node]['sensors']: - explanation = sensor['name'] + ':' - if sensor['value'] is not None: - explanation += str(sensor['value']) - if sensor['units'] is not None: - explanation += sensor['units'] - if sensor['states']: - explanation += ',' - if sensor['states']: - explanation += ','.join(sensor['states']) - healthexplanations[node].append(explanation) - if node in healthbynode and node in healthexplanations: - if healthexplanations[node]: - print('{0}: {1} ({2})'.format( - node, healthbynode[node], - ','.join(healthexplanations[node]))) - else: - print('{0}: {1}'.format(node, healthbynode[node])) + +def main(): + global session, exitcode, healthbynode, healthexplanations, health, node, sensor, explanation + session = client.Command() + exitcode = 0 + healthbynode = {} + healthexplanations = {} + for health in session.read( + '/noderange/{0}/health/hardware'.format(noderange)): + if 'error' in health: + sys.stderr.write(health['error'] + '\n') + if 'errorcode' in health: + exitcode |= health['errorcode'] + else: + exitcode |= 1 + continue + if 'databynode' not in health: + continue + health = health['databynode'] + for node in health: + if 'error' in health[node]: + sys.stderr.write('{0}: Error: {1}\n'.format( + node, health[node]['error'])) + exitcode |= 1 + if 'health' in health[node]: + healthbynode[node] = health[node]['health']['value'] + if 'sensors' in health[node]: + healthexplanations[node] = [] + for sensor in health[node]['sensors']: + explanation = sensor['name'] + ':' + if sensor['value'] is not None: + explanation += str(sensor['value']) + if sensor['units'] is not None: + explanation += sensor['units'] + if sensor['states']: + explanation += ',' + if sensor['states']: + explanation += ','.join(sensor['states']) + healthexplanations[node].append(explanation) + if node in healthbynode and node in healthexplanations: + if healthexplanations[node]: + print('{0}: {1} ({2})'.format( + node, healthbynode[node], + ','.join(healthexplanations[node]))) + else: + print('{0}: {1}'.format(node, healthbynode[node])) + +try: + main() +except KeyboardInterrupt: + print('') + sys.exit(0) diff --git a/confluent_client/bin/nodesensors b/confluent_client/bin/nodesensors index 590321c6..254b5ebe 100755 --- a/confluent_client/bin/nodesensors +++ b/confluent_client/bin/nodesensors @@ -208,4 +208,8 @@ def main(): sensorpass(True) -main() +try: + main() +except KeyboardInterrupt: + print('') + sys.exit(0) diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index 82f1512a..cb4058ee 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -98,23 +98,27 @@ class Command(object): def simple_noderange_command(self, noderange, resource, input=None, key=None): - rc = 0 - if resource[0] == '/': - resource = resource[1:] - # The implicit key is the resource basename - if key is None: - ikey = resource.rpartition('/')[-1] - else: - ikey = key - if input is None: - for res in self.read('/noderange/{0}/{1}'.format( - noderange, resource)): - rc = self.handle_results(ikey, rc, res) - else: - for res in self.update('/noderange/{0}/{1}'.format( - noderange, resource), {ikey: input}): - rc = self.handle_results(ikey, rc, res) - return rc + try: + rc = 0 + if resource[0] == '/': + resource = resource[1:] + # The implicit key is the resource basename + if key is None: + ikey = resource.rpartition('/')[-1] + else: + ikey = key + if input is None: + for res in self.read('/noderange/{0}/{1}'.format( + noderange, resource)): + rc = self.handle_results(ikey, rc, res) + else: + for res in self.update('/noderange/{0}/{1}'.format( + noderange, resource), {ikey: input}): + rc = self.handle_results(ikey, rc, res) + return rc + except KeyboardInterrupt: + print('') + return 0