2015-03-24 14:42:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
# Copyright 2015-2017 Lenovo
|
2015-03-24 14:42:04 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import codecs
|
2017-01-06 18:28:28 +00:00
|
|
|
import optparse
|
2015-03-24 14:42:04 +00:00
|
|
|
import os
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2015-03-24 14:42:04 +00:00
|
|
|
import sys
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-03-24 14:42:04 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
|
|
|
|
|
|
|
import confluent.client as client
|
|
|
|
|
|
|
|
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser = optparse.OptionParser(usage="Usage: %prog <noderange>")
|
|
|
|
(options, args) = argparser.parse_args()
|
2015-03-25 21:19:58 +00:00
|
|
|
try:
|
2017-01-06 18:28:28 +00:00
|
|
|
noderange = args[0]
|
2015-03-25 21:19:58 +00:00
|
|
|
except IndexError:
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser.print_help()
|
2015-03-25 21:19:58 +00:00
|
|
|
sys.exit(1)
|
2015-03-24 14:42:04 +00:00
|
|
|
|
2015-03-26 14:38:44 +00:00
|
|
|
|
|
|
|
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']
|
2015-03-24 14:42:04 +00:00
|
|
|
else:
|
2015-03-26 14:38:44 +00:00
|
|
|
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]:
|
2015-07-14 18:13:29 +00:00
|
|
|
print(u'{0}: {1} ({2})'.format(
|
2015-03-26 14:38:44 +00:00
|
|
|
node, healthbynode[node],
|
|
|
|
','.join(healthexplanations[node])))
|
|
|
|
else:
|
|
|
|
print('{0}: {1}'.format(node, healthbynode[node]))
|
|
|
|
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('')
|
|
|
|
sys.exit(0)
|
2015-03-24 14:42:04 +00:00
|
|
|
|