Commit 73a85f92 authored by Micha Mueller's avatar Micha Mueller
Browse files

Adapt all plugins for std::shared_ptrs

parent 294e1751
......@@ -23,7 +23,7 @@ void BACnetSensorGroup::init(boost::asio::io_service& io) {
#ifdef DEBUG
LOG(debug) << " DeviceInstance: " << _deviceInstance;
LOG(debug) << " " << _sensors.size() << " Sensors:";
for(auto s : _sensors) {
for(const auto& s : _sensors) {
LOG(debug) << " " << s->getName();
LOG(debug) << " ObjectInstance: " << s->getObjectInstance();
LOG(debug) << " ObjectType: " << s->getObjectType();
......@@ -64,7 +64,7 @@ void BACnetSensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
for(auto s : _sensors) {
for(const auto& s : _sensors) {
try {
reading.value = _bacClient->readProperty(getDeviceInstance(), s->getObjectInstance(), s->getObjectType(), s->getPropertyId()) * s->getFactor();
#ifdef DEBUG
......
......@@ -19,8 +19,8 @@ public:
IPMIHost();
virtual ~IPMIHost();
/* Translate recordId to SDR record */
bool getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record);
/* Translate recordId to SDR record */
bool getSdrRecord(uint16_t recordId, std::vector<uint8_t>& record);
/* Send raw command to BMC. Returns sensor reading as responded by BMC. */
uint64_t sendRawCmd(const std::vector<uint8_t>& rawCmd, uint16_t start, uint16_t stop);
/* Read the sensor specified by its record id. Returns sensor reading. */
......
......@@ -63,7 +63,7 @@ void IPMISensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
for(auto s : _sensors) {
for(const auto& s : _sensors) {
try {
if (s->getRecordId() != 0) { /* recordId was set */
std::vector<uint8_t> sdrRecord = s->getSdrRecord();
......
......@@ -72,7 +72,7 @@ void OpaSensorGroup::read() {
return;
}
for(auto s : _sensors) {
for(const auto& s : _sensors) {
switch(s->getCounterData()) {
case(portXmitData) :
reading.value = portCounters.portXmitData;
......
......@@ -75,7 +75,7 @@ void PDUSensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
for(auto s : _sensors) {
for(const auto& s : _sensors) {
try {
std::string readStr;
const xmlPathVector_t& xmlPath = s->getXMLPath();
......
......@@ -45,4 +45,6 @@ protected:
unsigned int _config;
};
using PerfSBPtr = std::shared_ptr<PerfSensorBase>;
#endif /* PERFEVENT_PERFSENSORBASE_H_ */
......@@ -90,7 +90,7 @@ void PerfSensorGroup::start() {
//open perf-counters
struct perf_event_attr pe;
PerfSensorBase* pc = _sensors[0];
PerfSBPtr pc = _sensors[0];
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.size = sizeof(struct perf_event_attr);
......@@ -222,7 +222,7 @@ void PerfSensorGroup::read() {
void PerfSensorGroup::readAsync() {
read();
if(_sensorGroupLeader) {
for(auto g : _fellowSensorGroups) {
for(const auto& g : _fellowSensorGroups) {
g->read();
}
}
......
......@@ -11,6 +11,10 @@
#include "../../includes/SensorGroupTemplate.h"
#include "PerfSensorBase.h"
class PerfSensorGroup;
using PerfSGPtr = std::shared_ptr<PerfSensorGroup>;
class PerfSensorGroup : public SensorGroupTemplate<PerfSensorBase> {
public:
PerfSensorGroup(const std::string& name);
......@@ -26,7 +30,7 @@ public:
void setSensorGroupLeader(bool sensorGroupLeader) { _sensorGroupLeader = sensorGroupLeader; }
void setMaxCorrection(double maxCorrection){ _maxCorrection = maxCorrection; }
void pushBackGroup(PerfSensorGroup* perfGroup) { _fellowSensorGroups.push_back(perfGroup); }
void pushBackGroup(PerfSGPtr perfGroup) { _fellowSensorGroups.push_back(perfGroup); }
private:
void read() override;
......@@ -44,7 +48,7 @@ private:
std::vector<uint64_t> _ids;
//the sensorGroupLeader stores attached groups in here
std::vector<PerfSensorGroup*> _fellowSensorGroups;
std::vector<PerfSGPtr> _fellowSensorGroups;
uint64_t _latest_time_enabled;
uint64_t _latest_time_running;
......
......@@ -169,7 +169,7 @@ bool PerfeventConfigurator::readConfig(std::string cfgPath) {
if(!cpuSet.empty()) {
//first create groupLeader
std::set<int>::iterator it = cpuSet.begin();
PerfSensorGroup* leaderSG = new PerfSensorGroup(group);
PerfSGPtr leaderSG = std::make_shared<PerfSensorGroup>(group);
leaderSG->setSensorGroupLeader(true);
std::stringstream mqttPart;
mqttPart << std::setfill ('0') << std::setw(group.getMqttPart().size()) << std::hex << *it << "/";
......@@ -182,7 +182,7 @@ bool PerfeventConfigurator::readConfig(std::string cfgPath) {
//create fellow groups
for (; it != cpuSet.end(); ++it) {
PerfSensorGroup* perfSG = new PerfSensorGroup(group);
PerfSGPtr perfSG = std::make_shared<PerfSensorGroup>(group);
std::stringstream mqttPart;
mqttPart << std::setfill ('0') << std::setw(group.getMqttPart().size()) << std::hex << *it << "/";
......@@ -200,8 +200,8 @@ bool PerfeventConfigurator::readConfig(std::string cfgPath) {
}
}
//read of config finished. Now we build the mqtt-topic for every sensor
for(auto g : _sensorGroups) {
for(auto s : g->getSensors()) {
for(const auto& g : _sensorGroups) {
for(const auto& s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
}
}
......
......@@ -98,7 +98,7 @@ bool ProcfsConfigurator::readConfig(std::string cfgPath) {
} else if (boost::iequals(val.first, _groupName)) {
LOG(debug) << _groupName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
ProcfsSensorGroup* group = new ProcfsSensorGroup(val.second.data());
ProcfsSGPtr group = std::make_shared<ProcfsSensorGroup>(val.second.data());
if (readSensorGroup(*group, val.second)) {
storeSensorGroup(group);
} else {
......@@ -141,7 +141,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
std::string filePath="", fileType="", mqttStart="00";
// Set of cpu ids read during configuration
std::set<int> cpuSet;
std::vector<ProcfsSensorBase*> derivedSensors;
std::vector<ProcfsSBPtr> derivedSensors;
ProcfsParser *parser;
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
......@@ -200,7 +200,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
// Adding the sensors corresponding to availableMetrics
for(int i=0;i < numMetrics; i++) {
ProcfsSensorBase *sensor = (ProcfsSensorBase*)sGroup.getSensors().at(i);
const ProcfsSBPtr& sensor = sGroup.getDerivedSensors().at(i);
if ( autoMQTT ) sensor->setMqtt(this->increaseMqtt(mqttStart, metricsCounter->at(sensor->getCPUId() + 1)++ ));
// If the metric does not refer to a specific CPU core, the topic is prefix + default mqttPart + suffix
// The suffix is increased automatically through the metricsCounter vector
......
......@@ -46,15 +46,15 @@ ProcfsParser::~ProcfsParser() { this->close(); }
* metrics for all available CPUs in the system will be extracted.
* @return true if initialization was successful, false otherwise
*/
bool ProcfsParser::init(std::vector<ProcfsSensorBase*> *sensorVec, std::set<int> *cpuSet) {
bool ProcfsParser::init(std::vector<ProcfsSBPtr> *sensorVec, std::set<int> *cpuSet) {
if( this->_initialized )
return true;
if ((this->_metricsFile = fopen(this->_path.c_str(), "r")) == NULL)
return false;
// Building the auxiliary sensor map
std::map<std::string, ProcfsSensorBase*> *sensorMap = sensorVec != NULL ? new std::map<std::string, ProcfsSensorBase*>() : NULL;
std::map<std::string, ProcfsSBPtr> *sensorMap = sensorVec != NULL ? new std::map<std::string, ProcfsSBPtr>() : NULL;
if(sensorVec != NULL )
for( auto s : *sensorVec )
for( auto& s : *sensorVec )
sensorMap->insert(std::make_pair(s->getMetric(), s));
// For the parser to be initialized, both the metrics' names and initial readings must be successfully read
......@@ -84,7 +84,6 @@ void ProcfsParser::close() {
delete this->_readings;
}
if( this->_sensors != NULL ) {
for( auto s: *this->_sensors) delete s;
this->_sensors->clear();
delete this->_sensors;
}
......@@ -104,7 +103,7 @@ void ProcfsParser::close() {
*
* @return Pointer to a vector of ProcfsSensorBase objects if the parser is initialized and no errors are encountered, NULL otherwise
*/
std::vector<ProcfsSensorBase*> *ProcfsParser::getSensors() {
std::vector<ProcfsSBPtr> *ProcfsParser::getSensors() {
if( !this->_initialized )
return NULL;
return this->_sensors;
......@@ -138,12 +137,12 @@ std::vector<reading_t> *ProcfsParser::readSensors() {
* metrics for all available CPUs in the system will be extracted.
* @return true if successful, false otherwise
*/
bool MeminfoParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensorMap, std::set<int> *cpuSet) {
bool MeminfoParser::_readNames(std::map<std::string, ProcfsSBPtr> *sensorMap, std::set<int> *cpuSet) {
if ( this->_sensors == NULL ) {
bool memUsedEnabled = sensorMap != NULL && !sensorMap->empty() && sensorMap->find(this->_memUsedToken)!=sensorMap->end();
this->_memFreeLine = -1;
this->_memTotalLine = -1;
this->_sensors = new std::vector<ProcfsSensorBase*>();
this->_sensors = new std::vector<ProcfsSBPtr>();
this->_skipLine.clear();
char *lineToken, *savePtr;
fseek(this->_metricsFile, 0, SEEK_SET);
......@@ -161,9 +160,9 @@ bool MeminfoParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensorM
// We check if the token matches one entry in sensorMap, meaning that it must be extracted
else if ( sensorMap == NULL || sensorMap->empty() || sensorMap->find(lineToken) != sensorMap->end() ) {
if ( sensorMap == NULL || sensorMap->empty() )
this->_sensors->push_back(new ProcfsSensorBase(std::string(lineToken), std::string(lineToken)));
this->_sensors->push_back(std::make_shared<ProcfsSensorBase>(std::string(lineToken), std::string(lineToken)));
else
this->_sensors->push_back(new ProcfsSensorBase(*sensorMap->find(lineToken)->second));
this->_sensors->push_back(std::make_shared<ProcfsSensorBase>(*sensorMap->find(lineToken)->second));
this->_skipLine.push_back(false);
}
// If the token does not match any entry in sensorMap, its line is flagged as ignored
......@@ -179,7 +178,7 @@ bool MeminfoParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensorM
// If MemUsed was requested and both MemTotal and MemFree were found, we create a new sensor
if(this->_memTotalLine != -1 && this->_memFreeLine != -1)
this->_sensors->push_back(new ProcfsSensorBase(*sensorMap->find(this->_memUsedToken)->second));
this->_sensors->push_back(std::make_shared<ProcfsSensorBase>(*sensorMap->find(this->_memUsedToken)->second));
this->_numMetrics = this->_sensors->size();
}
......@@ -262,14 +261,14 @@ bool MeminfoParser::_readMetrics() {
* metrics for all available CPUs in the system will be extracted.
* @return true if successful, false otherwise
*/
bool ProcstatParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensorMap, std::set<int> *cpuSet) {
bool ProcstatParser::_readNames(std::map<std::string, ProcfsSBPtr> *sensorMap, std::set<int> *cpuSet) {
if ( this->_sensors == NULL ) {
this->_sensors = new std::vector<ProcfsSensorBase*>();
this->_sensors = new std::vector<ProcfsSBPtr>();
this->_skipLine.clear();
this->_skipColumn.clear();
char *lineToken, *savePtr;
short currCPU = -1, colCtr = 0, parsedCols = 0;
ProcfsSensorBase *s = NULL;
ProcfsSBPtr s = NULL;
// We detect which columns in the procstat file, corresponding to CPU-specific metrics must be skipped or
// considered depending on the sensors in the sensormap
......@@ -318,11 +317,11 @@ bool ProcstatParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensor
while ((lineToken = strtok_r(this->_stringBuffer, this->LINE_SEP, &savePtr)) != NULL && colCtr < this->DEFAULTMETRICS) {
if( this->_skipColumn[colCtr] == 1 || ( this->_skipColumn[colCtr] == 2 && currCPU == -1 ) ) {
if( sensorMap != NULL && !sensorMap->empty() ) {
s = new ProcfsSensorBase(*sensorMap->find(this->DEFAULT_NAMES[colCtr])->second);
s = std::make_shared<ProcfsSensorBase>(*sensorMap->find(this->DEFAULT_NAMES[colCtr])->second);
s->setName(currCPU == -1 ? s->getName() : cpuName + "_" + s->getName());
}
else
s = new ProcfsSensorBase(currCPU == -1 ? this->DEFAULT_NAMES[colCtr] : cpuName + "_" + this->DEFAULT_NAMES[colCtr]);
s = std::make_shared<ProcfsSensorBase>(currCPU == -1 ? this->DEFAULT_NAMES[colCtr] : cpuName + "_" + this->DEFAULT_NAMES[colCtr]);
s->setMetric( currCPU == -1 ? this->DEFAULT_NAMES[colCtr] : cpuName + "_" + this->DEFAULT_NAMES[colCtr] );
s->setCPUId(currCPU);
......@@ -345,9 +344,9 @@ bool ProcstatParser::_readNames(std::map<std::string, ProcfsSensorBase*> *sensor
this->_skipLine.push_back(true);
else {
if(sensorMap != NULL && !sensorMap->empty())
s = new ProcfsSensorBase(*sensorMap->find(lineToken)->second);
s = std::make_shared<ProcfsSensorBase>(*sensorMap->find(lineToken)->second);
else
s = new ProcfsSensorBase(std::string(lineToken));
s = std::make_shared<ProcfsSensorBase>(std::string(lineToken));
s->setCPUId(-1);
s->setMetric(std::string(lineToken));
this->_sensors->push_back(s);
......
......@@ -30,7 +30,7 @@ public:
virtual ~ProcfsParser();
// Init/close methods
bool init(std::vector<ProcfsSensorBase*> *sensorVec = NULL, std::set<int> *cpuSet = NULL);
bool init(std::vector<ProcfsSBPtr> *sensorVec = NULL, std::set<int> *cpuSet = NULL);
virtual void close();
// Setters and getters
......@@ -39,18 +39,18 @@ public:
unsigned int getNumMetrics() { return this->_numMetrics; }
unsigned int getNumCPUs() { return this->_numCPUs; }
std::vector<ProcfsSensorBase*> *getSensors();
std::vector<ProcfsSBPtr> *getSensors();
std::vector<reading_t> *readSensors();
protected:
// Private parsing methods that implement all necessary logic to parse metrics' names (and associated CPU cores, if any)
// and values. MUST be overridden by children classes
virtual bool _readNames(std::map<std::string, ProcfsSensorBase*> *sensorMap, std::set<int> *cpuSet) { return false; };
virtual bool _readNames(std::map<std::string, ProcfsSBPtr> *sensorMap, std::set<int> *cpuSet) { return false; };
virtual bool _readMetrics() { return false; };
// Vector containing the names of parsed metrics, associated CPU cores (if any), and values.
// After initialization, _sensors is always non-NULL.
std::vector<ProcfsSensorBase*> *_sensors;
std::vector<ProcfsSBPtr> *_sensors;
std::vector<reading_t> *_readings;
// Keeps track of which lines and columns in the parsed file must be skipped
......@@ -81,7 +81,7 @@ public:
MeminfoParser(const std::string path = "") : ProcfsParser(path) { this->LINE_SEP = " :\t"; if(path == "") this->_path = "/proc/meminfo"; else this->_path = path; }
protected:
bool _readNames(std::map<std::string, ProcfsSensorBase*> *sensorMap, std::set<int> *cpuSet) override;
bool _readNames(std::map<std::string, ProcfsSBPtr> *sensorMap, std::set<int> *cpuSet) override;
bool _readMetrics() override;
unsigned int _memTotalLine;
......@@ -119,7 +119,7 @@ public:
ProcstatParser(const std::string path = "") : ProcfsParser(path) { this->LINE_SEP = " \t"; if(path == "") this->_path = "/proc/stat"; else this->_path = path; }
protected:
bool _readNames(std::map<std::string, ProcfsSensorBase*> *sensorMap, std::set<int> *cpuSet) override;
bool _readNames(std::map<std::string, ProcfsSBPtr> *sensorMap, std::set<int> *cpuSet) override;
bool _readMetrics() override;
// Internal variables
......
......@@ -57,4 +57,6 @@ protected:
};
using ProcfsSBPtr = std::shared_ptr<ProcfsSensorBase>;
#endif /* PROCFSSENSOR_H_ */
......@@ -27,20 +27,18 @@ ProcfsSensorGroup::~ProcfsSensorGroup() {
* @param newSensors: Vector of ProcfsSensorBase objects to replace the old set
*
*/
void ProcfsSensorGroup::replaceSensors(std::vector<ProcfsSensorBase*> *newSensors) {
void ProcfsSensorGroup::replaceSensors(std::vector<ProcfsSBPtr> *newSensors) {
std::set<std::string> sensorSet = std::set<std::string>();
for( auto s_new : *newSensors ) sensorSet.insert( s_new->getMetric() );
for( auto s : this->_sensors ) {
for( const auto& s_new : *newSensors ) sensorSet.insert( s_new->getMetric() );
for( const auto& s : this->_sensors ) {
if( sensorSet.find(s->getMetric()) == sensorSet.end() )
LOG(warning) << _groupName << "::Sensor " << s->getName() << " could not be matched to any metric!";
// We clean up the original sensor
delete s;
}
sensorSet.clear();
this->_sensors.clear();
this->_baseSensors.clear();
for( auto s : *newSensors ) this->pushBackSensor(new ProcfsSensorBase(*s));
for( auto s : *newSensors ) this->pushBackSensor(std::make_shared<ProcfsSensorBase>(*s));
}
/**
......
......@@ -35,8 +35,8 @@ public:
std::string getType() { return this->_type; }
// Method to get the internal sensor vector used by the sensor group (different from getSensors)
std::vector<ProcfsSensorBase*> getDerivedSensors() { return this->_sensors; }
void replaceSensors(std::vector<ProcfsSensorBase*> *newSensors);
std::vector<ProcfsSBPtr> getDerivedSensors() { return this->_sensors; } //TODO return reference?
void replaceSensors(std::vector<ProcfsSBPtr> *newSensors);
private:
// Methods inherited from SensorGroupTemplate
......@@ -53,4 +53,6 @@ private:
};
using ProcfsSGPtr = std::shared_ptr<ProcfsSensorGroup>;
#endif /* PROCFSSENSORGROUP_H_ */
......@@ -50,7 +50,7 @@ void SNMPSensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
for(auto s : _sensors) {
for(const auto& s : _sensors) {
try {
reading.value = _connection->issueGet(s->getOID(), s->getOIDLen());
#ifdef DEBUG
......
......@@ -61,7 +61,7 @@ void SysfsSensorGroup::read() {
if (nelem) {
buf[nelem-1] = 0;
for(auto s : _sensors) {
for(const auto& s : _sensors) {
reading.value = 0;
try {
//filter the payload if necessary
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment