sensor monitoring compleat, starting on fan control
This commit is contained in:
118
main.cpp
118
main.cpp
@ -5,73 +5,71 @@
|
||||
#include <unistd.h>
|
||||
#include <sensors/sensors.h>
|
||||
#include <sensors/error.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "ipmi.h"
|
||||
#include "lm.h"
|
||||
|
||||
std::vector<const sensors_chip_name*> lm_get_chips(const std::string& match)
|
||||
sig_atomic_t running = true;
|
||||
|
||||
void sig_handler(int sig)
|
||||
{
|
||||
std::vector<const sensors_chip_name*> out;
|
||||
(void)sig;
|
||||
running = false;
|
||||
}
|
||||
|
||||
sensors_chip_name name_glob;
|
||||
int ret = sensors_parse_chip_name(match.c_str(), &name_glob);
|
||||
if(ret < 0)
|
||||
std::vector<Sensor> gather_sensors(std::vector<Sensor>& ipmi_sensors, ipmi_monitoring_ctx_t ctx, std::vector<const sensors_chip_name*>& lm_chips)
|
||||
{
|
||||
std::vector<Sensor> out;
|
||||
struct ipmi_monitoring_ipmi_config ipmi_config = {};
|
||||
ipmi_config.driver_type = IPMI_MONITORING_DRIVER_TYPE_OPENIPMI;
|
||||
|
||||
bool grabids = false;
|
||||
for(Sensor& sensor : ipmi_sensors)
|
||||
{
|
||||
std::cerr<<"could not parse chip name\n";
|
||||
return out;
|
||||
if(sensor.id <= 0)
|
||||
{
|
||||
grabids = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const sensors_chip_name* s_name;
|
||||
int nr = 0;
|
||||
do
|
||||
if(grabids)
|
||||
{
|
||||
s_name = sensors_get_detected_chips(&name_glob, &nr);
|
||||
if(s_name)
|
||||
out.push_back(s_name);
|
||||
} while(s_name);
|
||||
sensors_free_chip_name(&name_glob);
|
||||
if(!ipmi_fill_sensor_ids(ipmi_sensors, ctx, &ipmi_config))
|
||||
{
|
||||
std::cout<<"could not get ids for all the required sensors\n";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ipmi_update_sensors(ipmi_sensors, ctx, &ipmi_config);
|
||||
}
|
||||
|
||||
out.insert(out.end(), ipmi_sensors.begin(), ipmi_sensors.end());
|
||||
std::vector<Sensor> lm_sensors = lm_get_temperatures(lm_chips);
|
||||
out.insert(out.end(), lm_sensors.begin(), lm_sensors.end());
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<Sensor> lm_get_sensors(std::vector<const sensors_chip_name*>& chips)
|
||||
std::vector<double> get_fan_zones(const std::vector<Sensor>& sensors)
|
||||
{
|
||||
std::vector<Sensor> out;
|
||||
std::vector<double> out;
|
||||
|
||||
for(const sensors_chip_name* chip : chips)
|
||||
{
|
||||
int nr = 0;
|
||||
const sensors_feature* feature;
|
||||
while((feature = sensors_get_features(chip, &nr)))
|
||||
{
|
||||
if(feature->type != SENSORS_FEATURE_TEMP)
|
||||
continue;
|
||||
const sensors_subfeature* subfeature = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
|
||||
if(!subfeature)
|
||||
{
|
||||
std::cerr<<"could not read subfeature\n";
|
||||
continue;
|
||||
}
|
||||
for(const Sensor& sensor : sensors)
|
||||
std::cout<<sensor.chip<<' '<<sensor.name<<": "<<sensor.reading;
|
||||
|
||||
double val = 0;
|
||||
int ret = sensors_get_value(chip, subfeature->number, &val);
|
||||
if(ret < 0)
|
||||
{
|
||||
std::cerr<<"unable to read temperature for subfeature "
|
||||
<<subfeature->number<<' '<<subfeature->name<<": "<<sensors_strerror(ret)<<'\n';
|
||||
continue;
|
||||
}
|
||||
Sensor sensor;
|
||||
sensor.name.assign(sensors_get_label(chip, feature));
|
||||
sensor.id = subfeature->number;
|
||||
sensor.reading = val;
|
||||
out.push_back(sensor);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
signal(SIGABRT, sig_handler);
|
||||
signal(SIGTERM, sig_handler);
|
||||
signal(SIGHUP, sig_handler);
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
int ret = sensors_init(nullptr);
|
||||
if(ret < 0)
|
||||
@ -80,34 +78,18 @@ int main (int argc, char **argv)
|
||||
}
|
||||
|
||||
std::vector<const sensors_chip_name*> lm_chips = lm_get_chips("amdgpu-*");
|
||||
std::vector<Sensor> lmsensors = lm_get_sensors(lm_chips);
|
||||
|
||||
for(const Sensor& sensor : lmsensors)
|
||||
{
|
||||
std::cout<<sensor.name<<": "<<sensor.reading<<'\n';
|
||||
}
|
||||
|
||||
struct ipmi_monitoring_ipmi_config ipmi_config = {};
|
||||
ipmi_config.driver_type = IPMI_MONITORING_DRIVER_TYPE_OPENIPMI;
|
||||
|
||||
std::vector<Sensor> sensors;
|
||||
sensors.push_back(Sensor("CPU Temp"));
|
||||
sensors.push_back(Sensor("System Temp"));
|
||||
std::vector<Sensor> ipmi_sensors;
|
||||
ipmi_sensors.push_back(Sensor("IPMI", "CPU Temp"));
|
||||
ipmi_sensors.push_back(Sensor("IPMI", "System Temp"));
|
||||
|
||||
ipmi_monitoring_ctx_t ctx = init_ipmi_monitoring();
|
||||
if(!ctx)
|
||||
return 1;
|
||||
|
||||
if(!ipmi_fill_sensor_ids(sensors, ctx, &ipmi_config))
|
||||
while(running)
|
||||
{
|
||||
std::cout<<"could not get ids for all the required sensors\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
while(true)
|
||||
{
|
||||
ipmi_update_sensors(sensors, ctx, &ipmi_config);
|
||||
std::cout<<sensors[0].reading<<std::endl;
|
||||
std::vector<Sensor> sensors = gather_sensors(ipmi_sensors, ctx, lm_chips);
|
||||
get_fan_zones(sensors);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user