24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

IPMISensorBase.h 6.37 KB
Newer Older
1 2 3
//================================================================================
// Name        : IPMISensorBase.h
// Author      : Michael Ott, Micha Mueller
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Copyright   : Leibniz Supercomputing Centre
// Description : Sensor base class for IPMI plugin.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2017-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
27

28 29 30 31 32 33 34
/**
 * @defgroup ipmi IPMI plugin
 * @ingroup  pusherplugins
 *
 * @brief Collect data from devices via the IPMI protocol.
 */

35 36 37
#ifndef SRC_SENSORS_IPMI_IPMISENSORBASE_H_
#define SRC_SENSORS_IPMI_IPMISENSORBASE_H_

38
#include "sensorbase.h"
39 40 41

#include "IPMIHost.h"

42
#include <boost/algorithm/string.hpp>
43 44
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
Micha Müller's avatar
Micha Müller committed
45
#include <sstream>
46
#include <vector>
47

48 49 50 51 52
/**
 * @brief SensorBase specialization for this plugin.
 *
 * @ingroup ipmi
 */
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
class IPMISensorBase : public SensorBase {
      public:
	IPMISensorBase(const std::string &name)
	    : SensorBase(name),
	      _type(undefined),
	      _recordId(0),
	      _factor(1),
	      _lsb(0),
	      _msb(0) {}

	IPMISensorBase(const IPMISensorBase &other)
	    : SensorBase(other),
	      _type(other._type),
	      _recordId(other._recordId),
	      _sdrRecord(other._sdrRecord),
	      _factor(other._factor),
	      _rawCmd(other._rawCmd),
	      _lsb(other._lsb),
	      _msb(other._msb) {}
72 73 74

	virtual ~IPMISensorBase() {}

75 76 77 78 79 80 81 82 83 84 85
	IPMISensorBase &operator=(const IPMISensorBase &other) {
		SensorBase::operator=(other);
		_type = other._type;
		_recordId = other._recordId;
		_sdrRecord = other._sdrRecord;
		_factor = other._factor;
		_rawCmd = other._rawCmd;
		_lsb = other._lsb;
		_msb = other._msb;

		return *this;
86
	}
87

88 89 90
	enum sensorType {
		undefined = 0,
		raw,
91
		sdr,
Michael Ott's avatar
Michael Ott committed
92
		xccDatastorePower,
93
		xccSingleEnergy,
94 95
		xccBulkPower,
		xccBulkEnergy
96
	};
97

98 99 100 101 102 103 104
	void initSensor(unsigned interval) override {
		if (_type == xccDatastorePower) {
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(3000));
		}
		SensorBase::initSensor(interval);
	}
	
105 106 107 108 109 110 111 112 113 114 115
	uint16_t                    getRecordId() const { return _recordId; }
	const std::vector<uint8_t> &getSdrRecord() const { return _sdrRecord; }
	double                      getFactor() const { return _factor; }
	const std::vector<uint8_t> &getRawCmd() const { return _rawCmd; }
	std::string                 getRawCmdString() const {
                std::stringstream ss;
                ss << "0x";
                for (auto i : _rawCmd) {
                        ss << std::setfill('0') << std::setw(2) << std::hex << (int)i << " ";
                }
                return ss.str().substr(0, _rawCmd.size() * 3 + 1);
Micha Müller's avatar
Micha Müller committed
116
	}
117 118 119
	uint8_t     getLsb() const { return _lsb; }
	uint8_t     getMsb() const { return _msb; }
	sensorType  getType() const { return _type; }
120
	std::string getTypeString() const {
121 122 123 124 125
		switch (_type) {
			case raw:
				return std::string("raw");
			case sdr:
				return std::string("sdr");
126 127
			case xccDatastorePower:
				return std::string("xccDatastorePower");
Michael Ott's avatar
Michael Ott committed
128 129
			case xccSingleEnergy:
				return std::string("xccSingleEnergy");
130 131
			case xccBulkPower:
				return std::string("xccBulkPower");
132 133
			case xccBulkEnergy:
				return std::string("xccBulkEnergy");
134 135
			default:
				return std::string("undefined");
136 137
		}
	}
138

139
	void setRecordId(const std::string &recordId) {
140 141 142
		_recordId = stoul(recordId);
		_type = sdr;
	}
143 144 145 146 147
	void setSdrRecord(const std::vector<uint8_t> &sdrRecord) { _sdrRecord = sdrRecord; }
	void setFactor(const std::string &factor) { _factor = stod(factor); }
	void setRawCmd(std::string &rawCmd) {
		boost::regex                                       expr("(?:0x)?([0-9a-fA-F]+)");
		boost::regex_token_iterator<std::string::iterator> it{rawCmd.begin(), rawCmd.end(), expr, 1};
148 149 150 151
		boost::regex_token_iterator<std::string::iterator> end;
		while (it != end) {
			_rawCmd.push_back(stoi(*it++, NULL, 16));
		}
152
		_type = raw;
153
	}
154 155 156 157 158
	void setLsb(const std::string &lsb) { _lsb = stoi(lsb); }
	void setLsb(uint8_t lsb) { _lsb = lsb; }
	void setMsb(const std::string &msb) { _msb = stoi(msb); }
	void setMsb(uint8_t msb) { _msb = msb; }
	void setType(const std::string &type) {
159 160 161 162
		if (boost::iequals(type, "raw")) {
			_type = raw;
		} else if (boost::iequals(type, "sdr")) {
			_type = sdr;
163 164
		} else if (boost::iequals(type, "xccDatastorePower")) {
			_type = xccDatastorePower;
Michael Ott's avatar
Michael Ott committed
165 166
		} else if (boost::iequals(type, "xccSingleEnergy")) {
			_type = xccSingleEnergy;
167 168
		} else if (boost::iequals(type, "xccBulkPower")) {
			_type = xccBulkPower;
169 170
		} else if (boost::iequals(type, "xccBulkEnergy")) {
			_type = xccBulkEnergy;
171 172 173 174
		} else {
			_type = undefined;
		}
	}
175

Alessio Netti's avatar
Alessio Netti committed
176
	void printConfig(LOG_LEVEL ll, LOGGER &lg, unsigned leadingSpaces = 16) override {
177
		std::string leading(leadingSpaces, ' ');
178 179
		LOG_VAR(ll) << leading << "    Type:              " << getTypeString();
		switch (_type) {
180
			case raw:
181
				LOG_VAR(ll) << leading << "    Raw Cmd:           " << getRawCmdString();
182 183
				LOG_VAR(ll) << leading << "    lsb:               " << (int)_lsb;
				LOG_VAR(ll) << leading << "    msb:               " << (int)_msb;
184 185
				break;
			case sdr:
186
				LOG_VAR(ll) << leading << "    Record Id:         " << _recordId;
187
				break;
188 189
			default:
				break;
190
		}
191
		LOG_VAR(ll) << leading << "    Factor:            " << _factor;
192
	}
Micha Müller's avatar
Micha Müller committed
193

194 195 196
      protected:
	sensorType           _type;
	uint16_t             _recordId;
197
	std::vector<uint8_t> _sdrRecord;
198
	double               _factor;
199 200

	std::vector<uint8_t> _rawCmd;
201 202
	uint8_t              _lsb;
	uint8_t              _msb;
203 204 205
};

#endif /* SRC_SENSORS_IPMI_IPMISENSORBASE_H_ */