Commit 82e0f303 authored by Alessio Netti's avatar Alessio Netti
Browse files

Assignment operators for ProcFS and Tester sensor groups

- This allows for the correct operation of templated instantiation,
and all derived attributes on top of those from SensorBase are copied
- Added some minor visual feedback for qosLevel and maxMsgNum
parent 48d7c43a
......@@ -252,6 +252,8 @@ int main(int argc, char** argv) {
} else {
LOG(info) << " Daemonize: Disabled";
}
LOG(info) << " MaxMsgs: " << globalSettings.maxMsgNum;
LOG(info) << " MQTT-QoS: " << globalSettings.qosLevel;
LOG(info) << " MQTT-prefix: " << pluginSettings.mqttPrefix;
LOG(info) << " Write-Dir: " << pluginSettings.tempdir;
LOG(info) << " CacheInterval: " << pluginSettings.cacheInterval / 1000 << " [s]";
......
......@@ -104,27 +104,25 @@ void ProcfsConfigurator::sensorBase(ProcfsSensorBase& s, CFG_VAL config) {
*
*/
void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config) {
// Read additional configuration parameters supported by the Procfs plugin
std::string filePath="", fileType="", mqttStart="00";
// Set of cpu ids read during configuration
std::set<int> cpuSet;
std::vector<ProcfsSBPtr> derivedSensors;
ProcfsParser *parser;
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "type")) {
fileType = val.second.data();
sGroup.setType(val.second.data());
} else if (boost::iequals(val.first, "path")) {
filePath = val.second.data();
sGroup.setPath(val.second.data());
} else if (boost::iequals(val.first, "mqttStart")) {
mqttStart = val.second.data();
sGroup.setMqttStart(val.second.data());
} else if (boost::iequals(val.first, "cpus")) {
cpuSet = parseCpuString(val.second.data());
sGroup.setCpuSet(parseCpuString(val.second.data()));
}
}
// The "type" parameter must refer to either vmstat, procstat or meminfo. If not, the sensor group is not initialized
// The only other case is when an empty string is found, which can happen for template groups
std::string fileType = sGroup.getType();
std::string filePath = sGroup.getPath();
if(fileType == "")
return;
else if(fileType == "vmstat")
......@@ -136,7 +134,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
else if(fileType == "meminfo")
parser = new MeminfoParser(filePath);
else {
LOG(warning) << _groupName << " " << sGroup.getGroupName() << "::" << "Unspecified or invalid type! Available types are vmstat, meminfo, procstat";
LOG(warning) << _groupName << " " << sGroup.getGroupName() << "::" << "Unspecified or invalid type! Available types are vmstat, meminfo, procstat, sar";
return; }
// if any sensor objects specified by users in the configuration file are present,
......@@ -145,8 +143,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
// After getting the vector of available metrics (subset of the sensor map, if any) from the parsed file, we delete
// all sensors not matching any parsed metrics, and arrange their order to respect the one in the file
derivedSensors = sGroup.getDerivedSensors();
parser->init(&derivedSensors, &cpuSet);
sGroup.setType(fileType);
parser->init(&derivedSensors, sGroup.getCpuSet());
// If no metrics were found in the file (or the file is unreadable) the configuration aborts
int numMetrics = parser->getNumMetrics();
......@@ -168,7 +165,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
// Adding the sensors corresponding to availableMetrics
for(int i=0;i < numMetrics; i++) {
ProcfsSBPtr sensor = std::static_pointer_cast<ProcfsSBPtr::element_type>(sGroup.getSensors().at(i));
if ( autoMQTT ) sensor->setMqtt(increaseMqtt(mqttStart, metricsCounter->at(sensor->getCPUId() + 1)++ ));
if ( autoMQTT ) sensor->setMqtt(increaseMqtt(sGroup.getMqttStart(), 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
if( sensor->getCPUId() == -1 )
......@@ -182,6 +179,5 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
// De-allocating memory
delete metricsCounter;
cpuSet.clear();
//parser->close();
}
......@@ -13,6 +13,11 @@ ProcfsSensorGroup& ProcfsSensorGroup::operator=(const ProcfsSensorGroup& other)
_sensors.clear();
_baseSensors.clear();
_cpuSet = other._cpuSet;
_mqttStart = other._mqttStart;
_type = other._type;
_path = other._path;
return *this;
}
......
......@@ -30,13 +30,20 @@ public:
void stop() override;
// Setters and getters
void setParser(ProcfsParser *parser) { this->_parser = parser; }
ProcfsParser *getParser() { return this->_parser; }
void setType(std::string t) { this->_type = t; }
std::string getType() { return this->_type; }
void setParser(ProcfsParser *parser) { this->_parser = parser; }
void setType(std::string t) { this->_type = t; }
void setPath(std::string p) { this->_path = p; }
void setMqttStart(std::string m) { this->_mqttStart = m; }
void setCpuSet(std::set<int> s) { this->_cpuSet = s; }
ProcfsParser *getParser() { return this->_parser; }
std::string getType() { return this->_type; }
std::string getPath() { return this->_path; }
std::string getMqttStart() { return this->_mqttStart; }
std::set<int> *getCpuSet() { return &this->_cpuSet; }
// Method to get the internal sensor vector used by the sensor group (different from getSensors)
std::vector<ProcfsSBPtr> getDerivedSensors() { return this->_sensors; } //TODO return reference?
std::vector<ProcfsSBPtr>& getDerivedSensors() { return this->_sensors; }
void replaceSensors(std::vector<ProcfsSBPtr> *newSensors);
private:
......@@ -48,6 +55,12 @@ private:
ProcfsParser *_parser;
// Type of the ProcfsSensorGroup (i.e. vmstat, meminfo, procstat), currently unused
std::string _type;
// Path to the parsed file
std::string _path;
// Start MQTT part to use when building sensors automatically
std::string _mqttStart;
// Set of cpu ids read during configuration
std::set<int> _cpuSet;
std::vector<reading_t> *_readingVector;
reading_t _readingBuffer;
......
......@@ -18,23 +18,21 @@ TesterConfigurator::~TesterConfigurator() {}
void TesterConfigurator::sensorBase(TesterSensorBase& s, CFG_VAL config) {}
void TesterConfigurator::sensorGroup(TesterSensorGroup& s, CFG_VAL config) {
std::string mqttStart="00";
unsigned int numSensors = 0;
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config)
{
if (boost::iequals(val.first, "numSensors")) {
numSensors = stoull(val.second.data());
s.setNumSensors(stoull(val.second.data()));
} else if (boost::iequals(val.first, "startValue")) {
s.setValue(stoi(val.second.data()));
} else if (boost::iequals(val.first, "mqttStart")) {
mqttStart = val.second.data();
s.setMqttStart(val.second.data());
}
}
for(unsigned int i=0;i<numSensors;i++) {
for(unsigned int i=0;i < s.getNumSensors();i++) {
std::shared_ptr<TesterSensorBase> sensor = std::make_shared<TesterSensorBase>(s.getGroupName() + std::to_string(i));
sensor->setMqtt(increaseMqtt(mqttStart, i));
sensor->setMqtt(increaseMqtt(s.getMqttStart(), i));
s.pushBackSensor(sensor);
}
}
......@@ -14,7 +14,9 @@ using namespace std;
TesterSensorGroup::TesterSensorGroup(const std::string& name) :
SensorGroupTemplate(name),
_value(0) {}
_value(0),
_mqttStart("00"),
_numSensors(0) {}
// Overriding assignment operator so that sensors are not copy-constructed
TesterSensorGroup& TesterSensorGroup::operator=(const TesterSensorGroup& other) {
......@@ -22,6 +24,10 @@ TesterSensorGroup& TesterSensorGroup::operator=(const TesterSensorGroup& other)
_sensors.clear();
_baseSensors.clear();
_numSensors = other._numSensors;
_value = other._value;
_mqttStart = other._mqttStart;
return *this;
}
......
......@@ -21,13 +21,21 @@ public:
void start() override;
void stop() override;
void setValue(long long n) { _value = n; }
void setValue(long long n) { _value = n; }
void setMqttStart(std::string m) { _mqttStart = m; }
void setNumSensors(unsigned int n) { _numSensors = n; }
long long getValue() { return _value; }
std::string getMqttStart() { return _mqttStart; }
unsigned int getNumSensors() { return _numSensors; }
private:
void read() override;
void readAsync() override;
long long _value;
long long _value;
std::string _mqttStart;
unsigned int _numSensors;
};
#endif /* TESTERSENSORGROUP_H_ */
Supports Markdown
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