add service

This commit is contained in:
uvos 2023-05-03 10:52:48 +02:00
parent a6e6b758e4
commit b1c92cd561
5 changed files with 68 additions and 41 deletions

View File

@ -4,11 +4,15 @@ project(ipmifan LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
find_package(Doxygen)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "..." FORCE)
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
find_package(PkgConfig REQUIRED)
pkg_search_module(IPMI_MONITORING REQUIRED libipmimonitoring)
pkg_search_module(IPMI REQUIRED libfreeipmi)
pkg_search_module(SYSTEMD systemd)
add_executable(${PROJECT_NAME} main.cpp ipmi.cpp lm.cpp)
target_link_libraries(${PROJECT_NAME} ${IPMI_LINK_LIBRARIES} ${IPMI_MONITORING_LINK_LIBRARIES} sensors)
@ -16,3 +20,11 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${IPMI_INCLUDE_DIRS} ${IPMI_M
target_compile_options(${PROJECT_NAME} PRIVATE "-Wall" "-O2" "-g" "-fno-strict-aliasing" "-Wfatal-errors" "-Wno-reorder")
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
if(SYSTEMD_FOUND)
pkg_get_variable(SYSTEMD_UNIT_DIR_PKG systemd systemd_system_unit_path)
message(STATUS ${SYSTEMD_UNIT_DIR_PKG})
string(REPLACE ":" ";" SYSTEMD_UNIT_DIR_LIST ${SYSTEMD_UNIT_DIR_PKG})
list(GET SYSTEMD_UNIT_DIR_LIST 0 SYSTEMD_UNIT_DIR)
install(FILES ipmifan.service DESTINATION ${SYSTEMD_UNIT_DIR})
endif(SYSTEMD_FOUND)

View File

@ -2,6 +2,8 @@
#include <algorithm>
#include <iostream>
static constexpr size_t IPMI_RAW_MAX_ARGS = 65536*2;
static double ipmi_convert_sensor_reading(void *sensor_reading, int sensor_reading_type)
{
if(sensor_reading_type == IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL)
@ -102,3 +104,40 @@ ipmi_monitoring_ctx_t init_ipmi_monitoring()
return ctx;
}
ipmi_ctx_t ipmi_open_context()
{
ipmi_ctx_t ctx = ipmi_ctx_create();
if(!ctx)
{
std::cerr<<"Could not allocae raw context\n";
return nullptr;
}
ipmi_driver_type_t driver = IPMI_DEVICE_OPENIPMI;
int ret = ipmi_ctx_find_inband(ctx, &driver, false, 0, 0, nullptr, 0, 0);
if(ret < 0)
{
std::cerr<<"Could not create raw context "<<ipmi_ctx_errormsg(ctx)<<'\n';
ipmi_ctx_destroy(ctx);
return nullptr;
}
return ctx;
}
bool ipmi_set_fan_group(ipmi_ctx_t raw_ctx, uint8_t group, double speed)
{
char converted_speed = std::max(std::min(static_cast<char>(100), static_cast<char>(speed*100)), static_cast<char>(0));
std::cout<<"setting fan group "<<static_cast<int>(group)<<" to "<<speed*100<<"% ("<<static_cast<int>(converted_speed)<<")\n";
char command[] = {0x70, 0x66, 0x01, static_cast<char>(group), converted_speed};
char bytesrx[IPMI_RAW_MAX_ARGS] = {0};
int rxlen = ipmi_cmd_raw(raw_ctx, 0, 0x30, command, sizeof(command), bytesrx, IPMI_RAW_MAX_ARGS);
if(rxlen < 0)
{
std::cerr<<"Raw write to ipmi failed with: "<<ipmi_ctx_errormsg(raw_ctx);
return false;
}
return true;
}

5
ipmi.h
View File

@ -1,6 +1,7 @@
#include <vector>
#include <string>
#include <ipmi_monitoring.h>
#include <freeipmi/freeipmi.h>
#include "sensor.h"
@ -11,3 +12,7 @@ bool ipmi_fill_sensor_ids(std::vector<Sensor>& sensors, ipmi_monitoring_ctx_t ct
bool ipmi_update_sensors(std::vector<Sensor>& sensors, ipmi_monitoring_ctx_t ctx, struct ipmi_monitoring_ipmi_config* config);
ipmi_monitoring_ctx_t init_ipmi_monitoring();
ipmi_ctx_t ipmi_open_context();
bool ipmi_set_fan_group(ipmi_ctx_t raw_ctx, uint8_t group, double speed);

10
ipmifan.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=Start impi fan control
After=lm_sensors.service systemd-modules-load.service
[Service]
Type=simple
ExecStart=/usr/bin/ipmifan
[Install]
WantedBy=multi-user.target

View File

@ -15,8 +15,6 @@
#include "ipmi.h"
#include "lm.h"
static constexpr size_t IPMI_RAW_MAX_ARGS = 65536*2;
sig_atomic_t running = true;
void sig_handler(int sig)
@ -114,43 +112,6 @@ std::vector<double> get_fan_zones(const std::vector<Sensor>& sensors)
return out;
}
ipmi_ctx_t ipmi_open()
{
ipmi_ctx_t ctx = ipmi_ctx_create();
if(!ctx)
{
std::cerr<<"Could not allocae raw context\n";
return nullptr;
}
ipmi_driver_type_t driver = IPMI_DEVICE_OPENIPMI;
int ret = ipmi_ctx_find_inband(ctx, &driver, false, 0, 0, nullptr, 0, 0);
if(ret < 0)
{
std::cerr<<"Could not create raw context "<<ipmi_ctx_errormsg(ctx)<<'\n';
ipmi_ctx_destroy(ctx);
return nullptr;
}
return ctx;
}
bool ipmi_set_fan_group(ipmi_ctx_t raw_ctx, uint8_t group, double speed)
{
char converted_speed = std::max(std::min(static_cast<char>(100), static_cast<char>(speed*100)), static_cast<char>(0));
std::cout<<"setting fan group "<<static_cast<int>(group)<<" to "<<speed*100<<"% ("<<static_cast<int>(converted_speed)<<")\n";
char command[] = {0x70, 0x66, 0x01, static_cast<char>(group), converted_speed};
char bytesrx[IPMI_RAW_MAX_ARGS] = {0};
int rxlen = ipmi_cmd_raw(raw_ctx, 0, 0x30, command, sizeof(command), bytesrx, IPMI_RAW_MAX_ARGS);
if(rxlen < 0)
{
std::cerr<<"Raw write to ipmi failed with: "<<ipmi_ctx_errormsg(raw_ctx);
return false;
}
return true;
}
int main(int argc, char **argv)
{
signal(SIGABRT, sig_handler);
@ -173,7 +134,7 @@ int main(int argc, char **argv)
if(!monitoring_ctx)
return 1;
ipmi_ctx_t raw_ctx = ipmi_open();
ipmi_ctx_t raw_ctx = ipmi_open_context();
if(!raw_ctx)
return 1;