2015-03-20 21:09:30 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2015 Lenovo
|
|
|
|
#
|
|
|
|
# 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 optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
sensorcollections = {
|
2015-03-23 17:49:59 +00:00
|
|
|
'all': 'sensors/hardware/all/all',
|
|
|
|
'temperature': 'sensors/hardware/temperature/all',
|
|
|
|
'temp': 'sensors/hardware/temperature/all',
|
|
|
|
'power': 'sensors/hardware/power/all',
|
|
|
|
'fans': 'sensors/hardware/fans/all',
|
|
|
|
'fanspeed': 'sensors/hardware/fans/all',
|
2015-03-20 21:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
argparser = optparse.OptionParser(
|
|
|
|
usage="Usage: %prog [options] noderange [sensor(s)")
|
|
|
|
argparser.add_option('-i', '--interval', type='int',
|
|
|
|
help='Interval to do repeated samples over')
|
|
|
|
argparser.add_option('-n', '--numreadings', type='int',
|
|
|
|
help='Number of readings to gather')
|
|
|
|
argparser.add_option('-c', '--csv', action='store_true')
|
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
repeatmode = False
|
|
|
|
if options.interval:
|
|
|
|
repeatmode = True
|
|
|
|
if options.numreadings:
|
|
|
|
repeatmode = options.numreadings
|
|
|
|
if options.interval is None:
|
|
|
|
options.interval = 1
|
|
|
|
|
|
|
|
noderange = args[0]
|
|
|
|
sensors = []
|
2015-03-23 17:49:59 +00:00
|
|
|
for sensorgroup in args[1:]:
|
2015-03-20 21:09:30 +00:00
|
|
|
for sensor in sensorgroup.split(','):
|
2015-03-23 17:49:59 +00:00
|
|
|
sensor = sensor.replace('.', '/').replace(' ', '_').lower()
|
2015-03-20 21:09:30 +00:00
|
|
|
if '/' not in sensor:
|
|
|
|
if sensor in sensorcollections:
|
|
|
|
sensors.append(sensorcollections[sensor])
|
|
|
|
else:
|
2015-03-23 17:49:59 +00:00
|
|
|
sensors.append('sensors/hardware/all/' + sensor)
|
2015-03-20 21:09:30 +00:00
|
|
|
if not sensors:
|
|
|
|
sensors = ['sensors/hardware/all/all']
|
|
|
|
|
|
|
|
session = client.Command()
|
|
|
|
exitcode = 0
|
|
|
|
sensornames = set([])
|
|
|
|
|
|
|
|
def sensorpass(showout=True):
|
2015-03-23 17:49:59 +00:00
|
|
|
global exitcode
|
2015-03-20 21:09:30 +00:00
|
|
|
resultdata = {}
|
|
|
|
for reqsensor in sensors:
|
|
|
|
for reading in session.read('/noderange/' + noderange + '/' + reqsensor):
|
2015-03-23 17:49:59 +00:00
|
|
|
if 'error' in reading:
|
|
|
|
sys.stderr.write('Error: {0}\n'.format(reading['error']))
|
|
|
|
if 'errorcode' in reading:
|
|
|
|
exitcode |= exitcode
|
|
|
|
else:
|
|
|
|
exitcode |= 1
|
|
|
|
if 'databynode' not in reading:
|
2015-03-20 21:09:30 +00:00
|
|
|
continue
|
2015-03-23 17:49:59 +00:00
|
|
|
reading = reading['databynode']
|
2015-03-20 21:09:30 +00:00
|
|
|
for node in reading:
|
|
|
|
if 'error' in reading[node]:
|
|
|
|
sys.stderr.write(
|
2015-03-23 17:49:59 +00:00
|
|
|
'{0}: Error: {1}\n'.format(node,
|
|
|
|
reading[node]['error']))
|
2015-03-20 21:09:30 +00:00
|
|
|
if 'sensors' not in reading[node]:
|
|
|
|
continue
|
|
|
|
for sensedata in reading[node]['sensors']:
|
|
|
|
sensornames.add(sensedata['name'])
|
|
|
|
try:
|
2015-03-23 17:49:59 +00:00
|
|
|
sensedata['states'].remove('Ok')
|
2015-03-20 21:09:30 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
resultdata[sensedata['name']] = sensedata
|
|
|
|
if showout:
|
|
|
|
if sensedata['units'] is not None:
|
2015-03-23 17:49:59 +00:00
|
|
|
showval = u'{0} {1}'.format(
|
2015-03-20 21:09:30 +00:00
|
|
|
sensedata['value'], sensedata['units'])
|
|
|
|
else:
|
|
|
|
showval = sensedata['value']
|
|
|
|
if showval is None:
|
|
|
|
showval = ''
|
|
|
|
datadescription = [sensedata['health']]
|
|
|
|
datadescription.extend(sensedata['states'])
|
|
|
|
showval += ' ({0})'.format(','.join(datadescription))
|
2015-03-23 17:49:59 +00:00
|
|
|
print(u'{0}: {1}: {2}'.format(
|
|
|
|
node, sensedata['name'], showval).encode('utf-8'))
|
2015-03-20 21:09:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sensorpass()
|