From 0eacd9c30c2312faa3f436634201691211eaa857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Rossigneux?= Date: Thu, 31 Jan 2013 17:37:51 +0100 Subject: [PATCH] Add IPMI driver. Change-Id: Ic3b9aac6aa0184949b4bf85644d8e6c07ef3df1f --- kwapi/drivers/ipmi.py | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 kwapi/drivers/ipmi.py diff --git a/kwapi/drivers/ipmi.py b/kwapi/drivers/ipmi.py new file mode 100644 index 0000000..4adbfe7 --- /dev/null +++ b/kwapi/drivers/ipmi.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# +# Author: François Rossigneux +# +# 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 subprocess +import time + +from driver import Driver + + +class Ipmi(Driver): + """Driver for IPMI cards.""" + + def __init__(self, probe_ids, **kwargs): + """Initializes the IPMI driver. + + Keyword arguments: + probe_ids -- list containing the probes IDs + (a wattmeter monitor sometimes several probes) + kwargs -- keyword (device) defining the device to read (/dev/ttyUSB0) + + """ + Driver.__init__(self, probe_ids, kwargs) + + def run(self): + """Starts the driver thread.""" + while not self.stop_request_pending(): + measurements = {} + measurements['w'] = self.get_watts() + self.send_measurements(self.probe_ids[0], measurements) + time.sleep(1) + + def get_watts(self): + """Returns the power consumption.""" + command = 'ipmitool ' + command += '-S ' + kwargs.get('cache_file') + ' ' + command += '-I ' + kwargs.get('interface') + ' ' + command += '-H ' + kwargs.get('host') + ' ' + command += '-U ' + kwargs.get('username', 'root') + ' ' + command += '-P ' + kwargs.get('password') + ' ' + command += 'sensor reading "System Level" | cut -f2 -d"|"' + output, error = subprocess.Popen(command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ).communicate() + return int(output)