IPMISensorBase.h 6.17 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 Mueller's avatar
Micha Mueller 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
105
106
107
108
	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 Mueller's avatar
Micha Mueller committed
109
	}
110
111
112
	uint8_t     getLsb() const { return _lsb; }
	uint8_t     getMsb() const { return _msb; }
	sensorType  getType() const { return _type; }
113
	std::string getTypeString() const {
114
115
116
117
118
		switch (_type) {
			case raw:
				return std::string("raw");
			case sdr:
				return std::string("sdr");
119
120
			case xccDatastorePower:
				return std::string("xccDatastorePower");
Michael Ott's avatar
Michael Ott committed
121
122
			case xccSingleEnergy:
				return std::string("xccSingleEnergy");
123
124
			case xccBulkPower:
				return std::string("xccBulkPower");
125
126
			case xccBulkEnergy:
				return std::string("xccBulkEnergy");
127
128
			default:
				return std::string("undefined");
129
130
		}
	}
131

132
	void setRecordId(const std::string &recordId) {
133
134
135
		_recordId = stoul(recordId);
		_type = sdr;
	}
136
137
138
139
140
	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};
141
142
143
144
		boost::regex_token_iterator<std::string::iterator> end;
		while (it != end) {
			_rawCmd.push_back(stoi(*it++, NULL, 16));
		}
145
		_type = raw;
146
	}
147
148
149
150
151
	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) {
152
153
154
155
		if (boost::iequals(type, "raw")) {
			_type = raw;
		} else if (boost::iequals(type, "sdr")) {
			_type = sdr;
156
157
		} else if (boost::iequals(type, "xccDatastorePower")) {
			_type = xccDatastorePower;
Michael Ott's avatar
Michael Ott committed
158
159
		} else if (boost::iequals(type, "xccSingleEnergy")) {
			_type = xccSingleEnergy;
160
161
		} else if (boost::iequals(type, "xccBulkPower")) {
			_type = xccBulkPower;
162
163
		} else if (boost::iequals(type, "xccBulkEnergy")) {
			_type = xccBulkEnergy;
164
165
166
167
		} else {
			_type = undefined;
		}
	}
168

Alessio Netti's avatar
Alessio Netti committed
169
	void printConfig(LOG_LEVEL ll, LOGGER &lg, unsigned leadingSpaces = 16) override {
170
		std::string leading(leadingSpaces, ' ');
171
172
		LOG_VAR(ll) << leading << "    Type:              " << getTypeString();
		switch (_type) {
173
			case raw:
174
				LOG_VAR(ll) << leading << "    Raw Cmd:           " << getRawCmdString();
175
176
				LOG_VAR(ll) << leading << "    lsb:               " << (int)_lsb;
				LOG_VAR(ll) << leading << "    msb:               " << (int)_msb;
177
178
				break;
			case sdr:
179
				LOG_VAR(ll) << leading << "    Record Id:         " << _recordId;
180
				break;
181
182
			default:
				break;
183
		}
184
		LOG_VAR(ll) << leading << "    Factor:            " << _factor;
185
	}
Micha Mueller's avatar
Micha Mueller committed
186

187
188
189
      protected:
	sensorType           _type;
	uint16_t             _recordId;
190
	std::vector<uint8_t> _sdrRecord;
191
	double               _factor;
192
193

	std::vector<uint8_t> _rawCmd;
194
195
	uint8_t              _lsb;
	uint8_t              _msb;
196
197
198
};

#endif /* SRC_SENSORS_IPMI_IPMISENSORBASE_H_ */