freeipmi_plugin.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * netdata freeipmi.plugin
  4. * Copyright (C) 2023 Netdata Inc.
  5. * GPL v3+
  6. *
  7. * Based on:
  8. * ipmimonitoring-sensors.c,v 1.51 2016/11/02 23:46:24 chu11 Exp
  9. * ipmimonitoring-sel.c,v 1.51 2016/11/02 23:46:24 chu11 Exp
  10. *
  11. * Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.
  12. * Copyright (C) 2006-2007 The Regents of the University of California.
  13. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  14. * Written by Albert Chu <chu11@llnl.gov>
  15. * UCRL-CODE-222073
  16. */
  17. // ----------------------------------------------------------------------------
  18. // BEGIN NETDATA CODE
  19. // #define NETDATA_TIMING_REPORT 1
  20. #include "libnetdata/libnetdata.h"
  21. #include "libnetdata/required_dummies.h"
  22. #define FREEIPMI_GLOBAL_FUNCTION_SENSORS() do { \
  23. fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"ipmi-sensors\" %d \"%s\"\n", 5, "Displays current sensor state and readings"); \
  24. } while(0)
  25. // component names, based on our patterns
  26. #define NETDATA_SENSOR_COMPONENT_MEMORY_MODULE "Memory Module"
  27. #define NETDATA_SENSOR_COMPONENT_MEMORY "Memory"
  28. #define NETDATA_SENSOR_COMPONENT_PROCESSOR "Processor"
  29. #define NETDATA_SENSOR_COMPONENT_IPU "Image Processor"
  30. #define NETDATA_SENSOR_COMPONENT_STORAGE "Storage"
  31. #define NETDATA_SENSOR_COMPONENT_MOTHERBOARD "Motherboard"
  32. #define NETDATA_SENSOR_COMPONENT_NETWORK "Network"
  33. #define NETDATA_SENSOR_COMPONENT_POWER_SUPPLY "Power Supply"
  34. #define NETDATA_SENSOR_COMPONENT_SYSTEM "System"
  35. #define NETDATA_SENSOR_COMPONENT_PERIPHERAL "Peripheral"
  36. // netdata plugin defaults
  37. #define SENSORS_DICT_KEY_SIZE 2048 // the max size of the key for the dictionary of sensors
  38. #define SPEED_TEST_ITERATIONS 5 // how many times to repeat data collection to decide latency
  39. #define IPMI_SENSORS_DASHBOARD_PRIORITY 90000 // the priority of the sensors charts on the dashboard
  40. #define IPMI_SEL_DASHBOARD_PRIORITY 99000 // the priority of the SEL events chart on the dashboard
  41. #define IPMI_SENSORS_MIN_UPDATE_EVERY 5 // the minimum data collection frequency for sensors
  42. #define IPMI_SEL_MIN_UPDATE_EVERY 30 // the minimum data collection frequency for SEL events
  43. #define IPMI_ENABLE_SEL_BY_DEFAULT true // true/false, to enable/disable SEL by default
  44. #define IPMI_RESTART_EVERY_SECONDS 14400 // restart the plugin every this many seconds
  45. // this is to prevent possible bugs/leaks in ipmi libraries
  46. #define IPMI_RESTART_IF_SENSORS_DONT_ITERATE_EVERY_SECONDS (10 * 60) // stale data collection detection time
  47. // forward definition of functions and structures
  48. struct netdata_ipmi_state;
  49. static void netdata_update_ipmi_sensor_reading(
  50. int record_id
  51. , int sensor_number
  52. , int sensor_type
  53. , int sensor_state
  54. , int sensor_units
  55. , int sensor_reading_type
  56. , char *sensor_name
  57. , void *sensor_reading
  58. , int event_reading_type_code
  59. , int sensor_bitmask_type
  60. , int sensor_bitmask
  61. , char **sensor_bitmask_strings
  62. , struct netdata_ipmi_state *state
  63. );
  64. static void netdata_update_ipmi_sel_events_count(struct netdata_ipmi_state *state, uint32_t events);
  65. // END NETDATA CODE
  66. // ----------------------------------------------------------------------------
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <stdint.h>
  70. #include <string.h>
  71. #include <assert.h>
  72. #include <errno.h>
  73. #include <unistd.h>
  74. #include <sys/time.h>
  75. #include <ipmi_monitoring.h>
  76. #include <ipmi_monitoring_bitmasks.h>
  77. #include <ipmi_monitoring_offsets.h>
  78. /* Communication Configuration - Initialize accordingly */
  79. static netdata_mutex_t stdout_mutex = NETDATA_MUTEX_INITIALIZER;
  80. static bool function_plugin_should_exit = false;
  81. int update_every = IPMI_SENSORS_MIN_UPDATE_EVERY; // this is the minimum update frequency
  82. int update_every_sel = IPMI_SEL_MIN_UPDATE_EVERY; // this is the minimum update frequency for SEL events
  83. /* Hostname, NULL for In-band communication, non-null for a hostname */
  84. char *hostname = NULL;
  85. /* In-band Communication Configuration */
  86. int driver_type = -1; // IPMI_MONITORING_DRIVER_TYPE_KCS, etc. or -1 for default
  87. int disable_auto_probe = 0; /* probe for in-band device */
  88. unsigned int driver_address = 0; /* not used if probing */
  89. unsigned int register_spacing = 0; /* not used if probing */
  90. char *driver_device = NULL; /* not used if probing */
  91. /* Out-of-band Communication Configuration */
  92. int protocol_version = -1; // IPMI_MONITORING_PROTOCOL_VERSION_1_5, etc. or -1 for default
  93. char *username = "";
  94. char *password = "";
  95. unsigned char *k_g = NULL;
  96. unsigned int k_g_len = 0;
  97. int privilege_level = -1; // IPMI_MONITORING_PRIVILEGE_LEVEL_USER, etc. or -1 for default
  98. int authentication_type = -1; // IPMI_MONITORING_AUTHENTICATION_TYPE_MD5, etc. or -1 for default
  99. int cipher_suite_id = -1; /* 0 or -1 for default */
  100. int session_timeout = 0; /* 0 for default */
  101. int retransmission_timeout = 0; /* 0 for default */
  102. /* Workarounds - specify workaround flags if necessary */
  103. unsigned int workaround_flags = 0;
  104. /* Set to an appropriate alternate if desired */
  105. char *sdr_cache_directory = "/tmp";
  106. char *sdr_sensors_cache_format = ".netdata-freeipmi-sensors-%H-on-%L.sdr";
  107. char *sdr_sel_cache_format = ".netdata-freeipmi-sel-%H-on-%L.sdr";
  108. char *sensor_config_file = NULL;
  109. char *sel_config_file = NULL;
  110. // controlled via command line options
  111. unsigned int global_sel_flags = IPMI_MONITORING_SEL_FLAGS_REREAD_SDR_CACHE;
  112. unsigned int global_sensor_reading_flags = IPMI_MONITORING_SENSOR_READING_FLAGS_DISCRETE_READING|IPMI_MONITORING_SENSOR_READING_FLAGS_REREAD_SDR_CACHE;
  113. bool remove_reread_sdr_after_first_use = true;
  114. /* Initialization flags
  115. *
  116. * Most commonly bitwise OR IPMI_MONITORING_FLAGS_DEBUG and/or
  117. * IPMI_MONITORING_FLAGS_DEBUG_IPMI_PACKETS for extra debugging
  118. * information.
  119. */
  120. unsigned int ipmimonitoring_init_flags = 0;
  121. // ----------------------------------------------------------------------------
  122. // functions common to sensors and SEL
  123. static void initialize_ipmi_config (struct ipmi_monitoring_ipmi_config *ipmi_config) {
  124. fatal_assert(ipmi_config);
  125. ipmi_config->driver_type = driver_type;
  126. ipmi_config->disable_auto_probe = disable_auto_probe;
  127. ipmi_config->driver_address = driver_address;
  128. ipmi_config->register_spacing = register_spacing;
  129. ipmi_config->driver_device = driver_device;
  130. ipmi_config->protocol_version = protocol_version;
  131. ipmi_config->username = username;
  132. ipmi_config->password = password;
  133. ipmi_config->k_g = k_g;
  134. ipmi_config->k_g_len = k_g_len;
  135. ipmi_config->privilege_level = privilege_level;
  136. ipmi_config->authentication_type = authentication_type;
  137. ipmi_config->cipher_suite_id = cipher_suite_id;
  138. ipmi_config->session_timeout_len = session_timeout;
  139. ipmi_config->retransmission_timeout_len = retransmission_timeout;
  140. ipmi_config->workaround_flags = workaround_flags;
  141. }
  142. static const char *netdata_ipmi_get_sensor_type_string (int sensor_type, const char **component) {
  143. switch (sensor_type) {
  144. case IPMI_MONITORING_SENSOR_TYPE_RESERVED:
  145. return ("Reserved");
  146. case IPMI_MONITORING_SENSOR_TYPE_TEMPERATURE:
  147. return ("Temperature");
  148. case IPMI_MONITORING_SENSOR_TYPE_VOLTAGE:
  149. return ("Voltage");
  150. case IPMI_MONITORING_SENSOR_TYPE_CURRENT:
  151. return ("Current");
  152. case IPMI_MONITORING_SENSOR_TYPE_FAN:
  153. return ("Fan");
  154. case IPMI_MONITORING_SENSOR_TYPE_PHYSICAL_SECURITY:
  155. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  156. return ("Physical Security");
  157. case IPMI_MONITORING_SENSOR_TYPE_PLATFORM_SECURITY_VIOLATION_ATTEMPT:
  158. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  159. return ("Platform Security Violation Attempt");
  160. case IPMI_MONITORING_SENSOR_TYPE_PROCESSOR:
  161. *component = NETDATA_SENSOR_COMPONENT_PROCESSOR;
  162. return ("Processor");
  163. case IPMI_MONITORING_SENSOR_TYPE_POWER_SUPPLY:
  164. *component = NETDATA_SENSOR_COMPONENT_POWER_SUPPLY;
  165. return ("Power Supply");
  166. case IPMI_MONITORING_SENSOR_TYPE_POWER_UNIT:
  167. *component = NETDATA_SENSOR_COMPONENT_POWER_SUPPLY;
  168. return ("Power Unit");
  169. case IPMI_MONITORING_SENSOR_TYPE_COOLING_DEVICE:
  170. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  171. return ("Cooling Device");
  172. case IPMI_MONITORING_SENSOR_TYPE_OTHER_UNITS_BASED_SENSOR:
  173. return ("Other Units Based Sensor");
  174. case IPMI_MONITORING_SENSOR_TYPE_MEMORY:
  175. *component = NETDATA_SENSOR_COMPONENT_MEMORY;
  176. return ("Memory");
  177. case IPMI_MONITORING_SENSOR_TYPE_DRIVE_SLOT:
  178. *component = NETDATA_SENSOR_COMPONENT_STORAGE;
  179. return ("Drive Slot");
  180. case IPMI_MONITORING_SENSOR_TYPE_POST_MEMORY_RESIZE:
  181. *component = NETDATA_SENSOR_COMPONENT_MEMORY;
  182. return ("POST Memory Resize");
  183. case IPMI_MONITORING_SENSOR_TYPE_SYSTEM_FIRMWARE_PROGRESS:
  184. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  185. return ("System Firmware Progress");
  186. case IPMI_MONITORING_SENSOR_TYPE_EVENT_LOGGING_DISABLED:
  187. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  188. return ("Event Logging Disabled");
  189. case IPMI_MONITORING_SENSOR_TYPE_WATCHDOG1:
  190. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  191. return ("Watchdog 1");
  192. case IPMI_MONITORING_SENSOR_TYPE_SYSTEM_EVENT:
  193. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  194. return ("System Event");
  195. case IPMI_MONITORING_SENSOR_TYPE_CRITICAL_INTERRUPT:
  196. return ("Critical Interrupt");
  197. case IPMI_MONITORING_SENSOR_TYPE_BUTTON_SWITCH:
  198. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  199. return ("Button/Switch");
  200. case IPMI_MONITORING_SENSOR_TYPE_MODULE_BOARD:
  201. return ("Module/Board");
  202. case IPMI_MONITORING_SENSOR_TYPE_MICROCONTROLLER_COPROCESSOR:
  203. *component = NETDATA_SENSOR_COMPONENT_PROCESSOR;
  204. return ("Microcontroller/Coprocessor");
  205. case IPMI_MONITORING_SENSOR_TYPE_ADD_IN_CARD:
  206. return ("Add In Card");
  207. case IPMI_MONITORING_SENSOR_TYPE_CHASSIS:
  208. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  209. return ("Chassis");
  210. case IPMI_MONITORING_SENSOR_TYPE_CHIP_SET:
  211. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  212. return ("Chip Set");
  213. case IPMI_MONITORING_SENSOR_TYPE_OTHER_FRU:
  214. return ("Other Fru");
  215. case IPMI_MONITORING_SENSOR_TYPE_CABLE_INTERCONNECT:
  216. return ("Cable/Interconnect");
  217. case IPMI_MONITORING_SENSOR_TYPE_TERMINATOR:
  218. return ("Terminator");
  219. case IPMI_MONITORING_SENSOR_TYPE_SYSTEM_BOOT_INITIATED:
  220. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  221. return ("System Boot Initiated");
  222. case IPMI_MONITORING_SENSOR_TYPE_BOOT_ERROR:
  223. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  224. return ("Boot Error");
  225. case IPMI_MONITORING_SENSOR_TYPE_OS_BOOT:
  226. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  227. return ("OS Boot");
  228. case IPMI_MONITORING_SENSOR_TYPE_OS_CRITICAL_STOP:
  229. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  230. return ("OS Critical Stop");
  231. case IPMI_MONITORING_SENSOR_TYPE_SLOT_CONNECTOR:
  232. return ("Slot/Connector");
  233. case IPMI_MONITORING_SENSOR_TYPE_SYSTEM_ACPI_POWER_STATE:
  234. return ("System ACPI Power State");
  235. case IPMI_MONITORING_SENSOR_TYPE_WATCHDOG2:
  236. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  237. return ("Watchdog 2");
  238. case IPMI_MONITORING_SENSOR_TYPE_PLATFORM_ALERT:
  239. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  240. return ("Platform Alert");
  241. case IPMI_MONITORING_SENSOR_TYPE_ENTITY_PRESENCE:
  242. return ("Entity Presence");
  243. case IPMI_MONITORING_SENSOR_TYPE_MONITOR_ASIC_IC:
  244. return ("Monitor ASIC/IC");
  245. case IPMI_MONITORING_SENSOR_TYPE_LAN:
  246. *component = NETDATA_SENSOR_COMPONENT_NETWORK;
  247. return ("LAN");
  248. case IPMI_MONITORING_SENSOR_TYPE_MANAGEMENT_SUBSYSTEM_HEALTH:
  249. *component = NETDATA_SENSOR_COMPONENT_SYSTEM;
  250. return ("Management Subsystem Health");
  251. case IPMI_MONITORING_SENSOR_TYPE_BATTERY:
  252. return ("Battery");
  253. case IPMI_MONITORING_SENSOR_TYPE_SESSION_AUDIT:
  254. return ("Session Audit");
  255. case IPMI_MONITORING_SENSOR_TYPE_VERSION_CHANGE:
  256. return ("Version Change");
  257. case IPMI_MONITORING_SENSOR_TYPE_FRU_STATE:
  258. return ("FRU State");
  259. case IPMI_MONITORING_SENSOR_TYPE_UNKNOWN:
  260. return ("Unknown");
  261. default:
  262. if(sensor_type >= IPMI_MONITORING_SENSOR_TYPE_OEM_MIN && sensor_type <= IPMI_MONITORING_SENSOR_TYPE_OEM_MAX)
  263. return ("OEM");
  264. return ("Unrecognized");
  265. }
  266. }
  267. #define netdata_ipmi_get_value_int(var, func, ctx) do { \
  268. (var) = func(ctx); \
  269. if( (var) < 0) { \
  270. collector_error("%s(): call to " #func " failed: %s", \
  271. __FUNCTION__, ipmi_monitoring_ctx_errormsg(ctx)); \
  272. goto cleanup; \
  273. } \
  274. timing_step(TIMING_STEP_FREEIPMI_READ_ ## var); \
  275. } while(0)
  276. #define netdata_ipmi_get_value_ptr(var, func, ctx) do { \
  277. (var) = func(ctx); \
  278. if(!(var)) { \
  279. collector_error("%s(): call to " #func " failed: %s", \
  280. __FUNCTION__, ipmi_monitoring_ctx_errormsg(ctx)); \
  281. goto cleanup; \
  282. } \
  283. timing_step(TIMING_STEP_FREEIPMI_READ_ ## var); \
  284. } while(0)
  285. #define netdata_ipmi_get_value_no_check(var, func, ctx) do { \
  286. (var) = func(ctx); \
  287. timing_step(TIMING_STEP_FREEIPMI_READ_ ## var); \
  288. } while(0)
  289. static int netdata_read_ipmi_sensors(struct ipmi_monitoring_ipmi_config *ipmi_config, struct netdata_ipmi_state *state) {
  290. timing_init();
  291. ipmi_monitoring_ctx_t ctx = NULL;
  292. unsigned int sensor_reading_flags = global_sensor_reading_flags;
  293. int i;
  294. int sensor_count;
  295. int rv = -1;
  296. if (!(ctx = ipmi_monitoring_ctx_create ())) {
  297. collector_error("ipmi_monitoring_ctx_create()");
  298. goto cleanup;
  299. }
  300. timing_step(TIMING_STEP_FREEIPMI_CTX_CREATE);
  301. if (sdr_cache_directory) {
  302. if (ipmi_monitoring_ctx_sdr_cache_directory (ctx, sdr_cache_directory) < 0) {
  303. collector_error("ipmi_monitoring_ctx_sdr_cache_directory(): %s\n", ipmi_monitoring_ctx_errormsg (ctx));
  304. goto cleanup;
  305. }
  306. }
  307. if (sdr_sensors_cache_format) {
  308. if (ipmi_monitoring_ctx_sdr_cache_filenames(ctx, sdr_sensors_cache_format) < 0) {
  309. collector_error("ipmi_monitoring_ctx_sdr_cache_filenames(): %s\n", ipmi_monitoring_ctx_errormsg (ctx));
  310. goto cleanup;
  311. }
  312. }
  313. timing_step(TIMING_STEP_FREEIPMI_DSR_CACHE_DIR);
  314. // Must call otherwise only default interpretations ever used
  315. // sensor_config_file can be NULL
  316. if (ipmi_monitoring_ctx_sensor_config_file (ctx, sensor_config_file) < 0) {
  317. collector_error( "ipmi_monitoring_ctx_sensor_config_file(): %s\n", ipmi_monitoring_ctx_errormsg (ctx));
  318. goto cleanup;
  319. }
  320. timing_step(TIMING_STEP_FREEIPMI_SENSOR_CONFIG_FILE);
  321. if ((sensor_count = ipmi_monitoring_sensor_readings_by_record_id (ctx,
  322. hostname,
  323. ipmi_config,
  324. sensor_reading_flags,
  325. NULL,
  326. 0,
  327. NULL,
  328. NULL)) < 0) {
  329. collector_error( "ipmi_monitoring_sensor_readings_by_record_id(): %s",
  330. ipmi_monitoring_ctx_errormsg (ctx));
  331. goto cleanup;
  332. }
  333. timing_step(TIMING_STEP_FREEIPMI_SENSOR_READINGS_BY_X);
  334. for (i = 0; i < sensor_count; i++, ipmi_monitoring_sensor_iterator_next (ctx)) {
  335. int record_id, sensor_number, sensor_type, sensor_state, sensor_units,
  336. sensor_bitmask_type, sensor_bitmask, event_reading_type_code, sensor_reading_type;
  337. char **sensor_bitmask_strings = NULL;
  338. char *sensor_name = NULL;
  339. void *sensor_reading;
  340. netdata_ipmi_get_value_int(record_id, ipmi_monitoring_sensor_read_record_id, ctx);
  341. netdata_ipmi_get_value_int(sensor_number, ipmi_monitoring_sensor_read_sensor_number, ctx);
  342. netdata_ipmi_get_value_int(sensor_type, ipmi_monitoring_sensor_read_sensor_type, ctx);
  343. netdata_ipmi_get_value_ptr(sensor_name, ipmi_monitoring_sensor_read_sensor_name, ctx);
  344. netdata_ipmi_get_value_int(sensor_state, ipmi_monitoring_sensor_read_sensor_state, ctx);
  345. netdata_ipmi_get_value_int(sensor_units, ipmi_monitoring_sensor_read_sensor_units, ctx);
  346. netdata_ipmi_get_value_int(sensor_bitmask_type, ipmi_monitoring_sensor_read_sensor_bitmask_type, ctx);
  347. netdata_ipmi_get_value_int(sensor_bitmask, ipmi_monitoring_sensor_read_sensor_bitmask, ctx);
  348. // it's ok for this to be NULL, i.e. sensor_bitmask == IPMI_MONITORING_SENSOR_BITMASK_TYPE_UNKNOWN
  349. netdata_ipmi_get_value_no_check(sensor_bitmask_strings, ipmi_monitoring_sensor_read_sensor_bitmask_strings, ctx);
  350. netdata_ipmi_get_value_int(sensor_reading_type, ipmi_monitoring_sensor_read_sensor_reading_type, ctx);
  351. // whatever we read from the sensor, it is ok
  352. netdata_ipmi_get_value_no_check(sensor_reading, ipmi_monitoring_sensor_read_sensor_reading, ctx);
  353. netdata_ipmi_get_value_int(event_reading_type_code, ipmi_monitoring_sensor_read_event_reading_type_code, ctx);
  354. netdata_update_ipmi_sensor_reading(
  355. record_id, sensor_number, sensor_type, sensor_state, sensor_units, sensor_reading_type, sensor_name,
  356. sensor_reading, event_reading_type_code, sensor_bitmask_type, sensor_bitmask, sensor_bitmask_strings,
  357. state
  358. );
  359. #ifdef NETDATA_COMMENTED
  360. /* It is possible you may want to monitor specific event
  361. * conditions that may occur. If that is the case, you may want
  362. * to check out what specific bitmask type and bitmask events
  363. * occurred. See ipmi_monitoring_bitmasks.h for a list of
  364. * bitmasks and types.
  365. */
  366. if (sensor_bitmask_type != IPMI_MONITORING_SENSOR_BITMASK_TYPE_UNKNOWN)
  367. printf (", %Xh", sensor_bitmask);
  368. else
  369. printf (", N/A");
  370. if (sensor_bitmask_type != IPMI_MONITORING_SENSOR_BITMASK_TYPE_UNKNOWN
  371. && sensor_bitmask_strings)
  372. {
  373. unsigned int i = 0;
  374. printf (",");
  375. while (sensor_bitmask_strings[i])
  376. {
  377. printf (" ");
  378. printf ("'%s'",
  379. sensor_bitmask_strings[i]);
  380. i++;
  381. }
  382. }
  383. else
  384. printf (", N/A");
  385. printf ("\n");
  386. #endif // NETDATA_COMMENTED
  387. }
  388. rv = 0;
  389. cleanup:
  390. if (ctx)
  391. ipmi_monitoring_ctx_destroy (ctx);
  392. timing_report();
  393. if(remove_reread_sdr_after_first_use)
  394. global_sensor_reading_flags &= ~(IPMI_MONITORING_SENSOR_READING_FLAGS_REREAD_SDR_CACHE);
  395. return (rv);
  396. }
  397. static int netdata_get_ipmi_sel_events_count(struct ipmi_monitoring_ipmi_config *ipmi_config, struct netdata_ipmi_state *state) {
  398. timing_init();
  399. ipmi_monitoring_ctx_t ctx = NULL;
  400. unsigned int sel_flags = global_sel_flags;
  401. int sel_count;
  402. int rv = -1;
  403. if (!(ctx = ipmi_monitoring_ctx_create ())) {
  404. collector_error("ipmi_monitoring_ctx_create()");
  405. goto cleanup;
  406. }
  407. if (sdr_cache_directory) {
  408. if (ipmi_monitoring_ctx_sdr_cache_directory (ctx, sdr_cache_directory) < 0) {
  409. collector_error( "ipmi_monitoring_ctx_sdr_cache_directory(): %s", ipmi_monitoring_ctx_errormsg (ctx));
  410. goto cleanup;
  411. }
  412. }
  413. if (sdr_sel_cache_format) {
  414. if (ipmi_monitoring_ctx_sdr_cache_filenames(ctx, sdr_sel_cache_format) < 0) {
  415. collector_error("ipmi_monitoring_ctx_sdr_cache_filenames(): %s\n", ipmi_monitoring_ctx_errormsg (ctx));
  416. goto cleanup;
  417. }
  418. }
  419. // Must call otherwise only default interpretations ever used
  420. // sel_config_file can be NULL
  421. if (ipmi_monitoring_ctx_sel_config_file (ctx, sel_config_file) < 0) {
  422. collector_error( "ipmi_monitoring_ctx_sel_config_file(): %s",
  423. ipmi_monitoring_ctx_errormsg (ctx));
  424. goto cleanup;
  425. }
  426. if ((sel_count = ipmi_monitoring_sel_by_record_id (ctx,
  427. hostname,
  428. ipmi_config,
  429. sel_flags,
  430. NULL,
  431. 0,
  432. NULL,
  433. NULL)) < 0) {
  434. collector_error( "ipmi_monitoring_sel_by_record_id(): %s",
  435. ipmi_monitoring_ctx_errormsg (ctx));
  436. goto cleanup;
  437. }
  438. netdata_update_ipmi_sel_events_count(state, sel_count);
  439. rv = 0;
  440. cleanup:
  441. if (ctx)
  442. ipmi_monitoring_ctx_destroy (ctx);
  443. timing_report();
  444. if(remove_reread_sdr_after_first_use)
  445. global_sel_flags &= ~(IPMI_MONITORING_SEL_FLAGS_REREAD_SDR_CACHE);
  446. return (rv);
  447. }
  448. // ----------------------------------------------------------------------------
  449. // copied from freeipmi codebase commit 8dea6dec4012d0899901e595f2c868a05e1cefed
  450. // added netdata_ in-front to not overwrite library functions
  451. // FROM: common/miscutil/network.c
  452. static int netdata_host_is_localhost (const char *host) {
  453. /* Ordered by my assumption of most popular */
  454. if (!strcasecmp (host, "localhost")
  455. || !strcmp (host, "127.0.0.1")
  456. || !strcasecmp (host, "ipv6-localhost")
  457. || !strcmp (host, "::1")
  458. || !strcasecmp (host, "ip6-localhost")
  459. || !strcmp (host, "0:0:0:0:0:0:0:1"))
  460. return (1);
  461. return (0);
  462. }
  463. // FROM: common/parsecommon/parse-common.h
  464. #define IPMI_PARSE_DEVICE_LAN_STR "lan"
  465. #define IPMI_PARSE_DEVICE_LAN_2_0_STR "lan_2_0"
  466. #define IPMI_PARSE_DEVICE_LAN_2_0_STR2 "lan20"
  467. #define IPMI_PARSE_DEVICE_LAN_2_0_STR3 "lan_20"
  468. #define IPMI_PARSE_DEVICE_LAN_2_0_STR4 "lan2_0"
  469. #define IPMI_PARSE_DEVICE_LAN_2_0_STR5 "lanplus"
  470. #define IPMI_PARSE_DEVICE_KCS_STR "kcs"
  471. #define IPMI_PARSE_DEVICE_SSIF_STR "ssif"
  472. #define IPMI_PARSE_DEVICE_OPENIPMI_STR "openipmi"
  473. #define IPMI_PARSE_DEVICE_OPENIPMI_STR2 "open"
  474. #define IPMI_PARSE_DEVICE_SUNBMC_STR "sunbmc"
  475. #define IPMI_PARSE_DEVICE_SUNBMC_STR2 "bmc"
  476. #define IPMI_PARSE_DEVICE_INTELDCMI_STR "inteldcmi"
  477. // FROM: common/parsecommon/parse-common.c
  478. // changed the return values to match ipmi_monitoring.h
  479. static int netdata_parse_outofband_driver_type (const char *str) {
  480. if (strcasecmp (str, IPMI_PARSE_DEVICE_LAN_STR) == 0)
  481. return (IPMI_MONITORING_PROTOCOL_VERSION_1_5);
  482. /* support "lanplus" for those that might be used to ipmitool.
  483. * support typo variants to ease.
  484. */
  485. else if (strcasecmp (str, IPMI_PARSE_DEVICE_LAN_2_0_STR) == 0
  486. || strcasecmp (str, IPMI_PARSE_DEVICE_LAN_2_0_STR2) == 0
  487. || strcasecmp (str, IPMI_PARSE_DEVICE_LAN_2_0_STR3) == 0
  488. || strcasecmp (str, IPMI_PARSE_DEVICE_LAN_2_0_STR4) == 0
  489. || strcasecmp (str, IPMI_PARSE_DEVICE_LAN_2_0_STR5) == 0)
  490. return (IPMI_MONITORING_PROTOCOL_VERSION_2_0);
  491. return (-1);
  492. }
  493. // FROM: common/parsecommon/parse-common.c
  494. // changed the return values to match ipmi_monitoring.h
  495. static int netdata_parse_inband_driver_type (const char *str) {
  496. if (strcasecmp (str, IPMI_PARSE_DEVICE_KCS_STR) == 0)
  497. return (IPMI_MONITORING_DRIVER_TYPE_KCS);
  498. else if (strcasecmp (str, IPMI_PARSE_DEVICE_SSIF_STR) == 0)
  499. return (IPMI_MONITORING_DRIVER_TYPE_SSIF);
  500. /* support "open" for those that might be used to
  501. * ipmitool.
  502. */
  503. else if (strcasecmp (str, IPMI_PARSE_DEVICE_OPENIPMI_STR) == 0
  504. || strcasecmp (str, IPMI_PARSE_DEVICE_OPENIPMI_STR2) == 0)
  505. return (IPMI_MONITORING_DRIVER_TYPE_OPENIPMI);
  506. /* support "bmc" for those that might be used to
  507. * ipmitool.
  508. */
  509. else if (strcasecmp (str, IPMI_PARSE_DEVICE_SUNBMC_STR) == 0
  510. || strcasecmp (str, IPMI_PARSE_DEVICE_SUNBMC_STR2) == 0)
  511. return (IPMI_MONITORING_DRIVER_TYPE_SUNBMC);
  512. #ifdef IPMI_MONITORING_DRIVER_TYPE_INTELDCMI
  513. else if (strcasecmp (str, IPMI_PARSE_DEVICE_INTELDCMI_STR) == 0)
  514. return (IPMI_MONITORING_DRIVER_TYPE_INTELDCMI);
  515. #endif // IPMI_MONITORING_DRIVER_TYPE_INTELDCMI
  516. return (-1);
  517. }
  518. // ----------------------------------------------------------------------------
  519. // BEGIN NETDATA CODE
  520. typedef enum __attribute__((packed)) {
  521. IPMI_COLLECT_TYPE_SENSORS = (1 << 0),
  522. IPMI_COLLECT_TYPE_SEL = (1 << 1),
  523. } IPMI_COLLECTION_TYPE;
  524. struct sensor {
  525. int sensor_type;
  526. int sensor_state;
  527. int sensor_units;
  528. char *sensor_name;
  529. int sensor_reading_type;
  530. union {
  531. uint8_t bool_value;
  532. uint32_t uint32_value;
  533. double double_value;
  534. } sensor_reading;
  535. // netdata provided
  536. const char *context;
  537. const char *title;
  538. const char *units;
  539. const char *family;
  540. const char *chart_type;
  541. const char *dimension;
  542. int priority;
  543. const char *type;
  544. const char *component;
  545. int multiplier;
  546. bool do_metric;
  547. bool do_state;
  548. bool metric_chart_sent;
  549. bool state_chart_sent;
  550. usec_t last_collected_metric_ut;
  551. usec_t last_collected_state_ut;
  552. };
  553. typedef enum __attribute__((packed)) {
  554. ICS_INIT,
  555. ICS_INIT_FAILED,
  556. ICS_RUNNING,
  557. ICS_FAILED,
  558. } IPMI_COLLECTOR_STATUS;
  559. struct netdata_ipmi_state {
  560. bool debug;
  561. struct {
  562. IPMI_COLLECTOR_STATUS status;
  563. usec_t last_iteration_ut;
  564. size_t collected;
  565. usec_t now_ut;
  566. usec_t freq_ut;
  567. int priority;
  568. DICTIONARY *dict;
  569. } sensors;
  570. struct {
  571. IPMI_COLLECTOR_STATUS status;
  572. usec_t last_iteration_ut;
  573. size_t events;
  574. usec_t now_ut;
  575. usec_t freq_ut;
  576. int priority;
  577. } sel;
  578. struct {
  579. usec_t now_ut;
  580. } updates;
  581. };
  582. struct netdata_ipmi_state state = {0};
  583. // ----------------------------------------------------------------------------
  584. // excluded record ids maintenance (both for sensor data and state)
  585. static int *excluded_record_ids = NULL;
  586. size_t excluded_record_ids_length = 0;
  587. static void excluded_record_ids_parse(const char *s, bool debug) {
  588. if(!s) return;
  589. while(*s) {
  590. while(*s && !isdigit(*s)) s++;
  591. if(isdigit(*s)) {
  592. char *e;
  593. unsigned long n = strtoul(s, &e, 10);
  594. s = e;
  595. if(n != 0) {
  596. excluded_record_ids = reallocz(excluded_record_ids, (excluded_record_ids_length + 1) * sizeof(int));
  597. excluded_record_ids[excluded_record_ids_length++] = (int)n;
  598. }
  599. }
  600. }
  601. if(debug) {
  602. fprintf(stderr, "%s: excluded record ids:", program_name);
  603. size_t i;
  604. for(i = 0; i < excluded_record_ids_length; i++) {
  605. fprintf(stderr, " %d", excluded_record_ids[i]);
  606. }
  607. fprintf(stderr, "\n");
  608. }
  609. }
  610. static int *excluded_status_record_ids = NULL;
  611. size_t excluded_status_record_ids_length = 0;
  612. static void excluded_status_record_ids_parse(const char *s, bool debug) {
  613. if(!s) return;
  614. while(*s) {
  615. while(*s && !isdigit(*s)) s++;
  616. if(isdigit(*s)) {
  617. char *e;
  618. unsigned long n = strtoul(s, &e, 10);
  619. s = e;
  620. if(n != 0) {
  621. excluded_status_record_ids = reallocz(excluded_status_record_ids, (excluded_status_record_ids_length + 1) * sizeof(int));
  622. excluded_status_record_ids[excluded_status_record_ids_length++] = (int)n;
  623. }
  624. }
  625. }
  626. if(debug) {
  627. fprintf(stderr, "%s: excluded status record ids:", program_name);
  628. size_t i;
  629. for(i = 0; i < excluded_status_record_ids_length; i++) {
  630. fprintf(stderr, " %d", excluded_status_record_ids[i]);
  631. }
  632. fprintf(stderr, "\n");
  633. }
  634. }
  635. static int excluded_record_ids_check(int record_id) {
  636. size_t i;
  637. for(i = 0; i < excluded_record_ids_length; i++) {
  638. if(excluded_record_ids[i] == record_id)
  639. return 1;
  640. }
  641. return 0;
  642. }
  643. static int excluded_status_record_ids_check(int record_id) {
  644. size_t i;
  645. for(i = 0; i < excluded_status_record_ids_length; i++) {
  646. if(excluded_status_record_ids[i] == record_id)
  647. return 1;
  648. }
  649. return 0;
  650. }
  651. // ----------------------------------------------------------------------------
  652. // data collection functions
  653. struct {
  654. const char *search;
  655. SIMPLE_PATTERN *pattern;
  656. const char *label;
  657. } sensors_component_patterns[] = {
  658. // The order is important!
  659. // They are evaluated top to bottom
  660. // The first the matches is used
  661. {
  662. .search = "*DIMM*|*_DIM*|*VTT*|*VDDQ*|*ECC*|*MEM*CRC*|*MEM*BD*",
  663. .label = NETDATA_SENSOR_COMPONENT_MEMORY_MODULE,
  664. },
  665. {
  666. .search = "*CPU*|SOC_*|*VDDCR*|P*_VDD*|*_DTS|*VCORE*|*PROC*",
  667. .label = NETDATA_SENSOR_COMPONENT_PROCESSOR,
  668. },
  669. {
  670. .search = "IPU*",
  671. .label = NETDATA_SENSOR_COMPONENT_IPU,
  672. },
  673. {
  674. .search = "M2_*|*SSD*|*HSC*|*HDD*|*NVME*",
  675. .label = NETDATA_SENSOR_COMPONENT_STORAGE,
  676. },
  677. {
  678. .search = "MB_*|*PCH*|*VBAT*|*I/O*BD*|*IO*BD*",
  679. .label = NETDATA_SENSOR_COMPONENT_MOTHERBOARD,
  680. },
  681. {
  682. .search = "Watchdog|SEL|SYS_*|*CHASSIS*",
  683. .label = NETDATA_SENSOR_COMPONENT_SYSTEM,
  684. },
  685. {
  686. .search = "PS*|P_*|*PSU*|*PWR*|*TERMV*|*D2D*",
  687. .label = NETDATA_SENSOR_COMPONENT_POWER_SUPPLY,
  688. },
  689. // fallback components
  690. {
  691. .search = "VR_P*|*VRMP*",
  692. .label = NETDATA_SENSOR_COMPONENT_PROCESSOR,
  693. },
  694. {
  695. .search = "*VSB*|*PS*",
  696. .label = NETDATA_SENSOR_COMPONENT_POWER_SUPPLY,
  697. },
  698. {
  699. .search = "*MEM*|*MEM*RAID*",
  700. .label = NETDATA_SENSOR_COMPONENT_MEMORY,
  701. },
  702. {
  703. .search = "*RAID*", // there is also "Memory RAID", so keep this after memory
  704. .label = NETDATA_SENSOR_COMPONENT_STORAGE,
  705. },
  706. {
  707. .search = "*PERIPHERAL*|*USB*",
  708. .label = NETDATA_SENSOR_COMPONENT_PERIPHERAL,
  709. },
  710. {
  711. .search = "*FAN*|*12V*|*VCC*|*PCI*|*CHIPSET*|*AMP*|*BD*",
  712. .label = NETDATA_SENSOR_COMPONENT_SYSTEM,
  713. },
  714. // terminator
  715. {
  716. .search = NULL,
  717. .label = NULL,
  718. }
  719. };
  720. static const char *netdata_sensor_name_to_component(const char *sensor_name) {
  721. for(int i = 0; sensors_component_patterns[i].search ;i++) {
  722. if(!sensors_component_patterns[i].pattern)
  723. sensors_component_patterns[i].pattern = simple_pattern_create(sensors_component_patterns[i].search, "|", SIMPLE_PATTERN_EXACT, false);
  724. if(simple_pattern_matches(sensors_component_patterns[i].pattern, sensor_name))
  725. return sensors_component_patterns[i].label;
  726. }
  727. return "Other";
  728. }
  729. const char *netdata_collect_type_to_string(IPMI_COLLECTION_TYPE type) {
  730. if((type & (IPMI_COLLECT_TYPE_SENSORS|IPMI_COLLECT_TYPE_SEL)) == (IPMI_COLLECT_TYPE_SENSORS|IPMI_COLLECT_TYPE_SEL))
  731. return "sensors,sel";
  732. if(type & IPMI_COLLECT_TYPE_SEL)
  733. return "sel";
  734. if(type & IPMI_COLLECT_TYPE_SENSORS)
  735. return "sensors";
  736. return "unknown";
  737. }
  738. static void netdata_sensor_set_value(struct sensor *sn, void *sensor_reading, struct netdata_ipmi_state *state __maybe_unused) {
  739. switch(sn->sensor_reading_type) {
  740. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL:
  741. sn->sensor_reading.bool_value = *((uint8_t *)sensor_reading);
  742. break;
  743. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER32:
  744. sn->sensor_reading.uint32_value = *((uint32_t *)sensor_reading);
  745. break;
  746. case IPMI_MONITORING_SENSOR_READING_TYPE_DOUBLE:
  747. sn->sensor_reading.double_value = *((double *)sensor_reading);
  748. break;
  749. default:
  750. case IPMI_MONITORING_SENSOR_READING_TYPE_UNKNOWN:
  751. sn->do_metric = false;
  752. break;
  753. }
  754. }
  755. static void netdata_update_ipmi_sensor_reading(
  756. int record_id
  757. , int sensor_number
  758. , int sensor_type
  759. , int sensor_state
  760. , int sensor_units
  761. , int sensor_reading_type
  762. , char *sensor_name
  763. , void *sensor_reading
  764. , int event_reading_type_code __maybe_unused
  765. , int sensor_bitmask_type __maybe_unused
  766. , int sensor_bitmask __maybe_unused
  767. , char **sensor_bitmask_strings __maybe_unused
  768. , struct netdata_ipmi_state *state
  769. ) {
  770. if(unlikely(sensor_state == IPMI_MONITORING_STATE_UNKNOWN &&
  771. sensor_type == IPMI_MONITORING_SENSOR_TYPE_UNKNOWN &&
  772. sensor_units == IPMI_MONITORING_SENSOR_UNITS_UNKNOWN &&
  773. sensor_reading_type == IPMI_MONITORING_SENSOR_READING_TYPE_UNKNOWN &&
  774. (!sensor_name || !*sensor_name)))
  775. // we can't do anything about this sensor - everything is unknown
  776. return;
  777. if(unlikely(!sensor_name || !*sensor_name))
  778. sensor_name = "UNNAMED";
  779. state->sensors.collected++;
  780. char key[SENSORS_DICT_KEY_SIZE + 1];
  781. snprintfz(key, SENSORS_DICT_KEY_SIZE, "i%d_n%d_t%d_u%d_%s",
  782. record_id, sensor_number, sensor_reading_type, sensor_units, sensor_name);
  783. // find the sensor record
  784. const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(state->sensors.dict, key);
  785. if(likely(item)) {
  786. // recurring collection
  787. if(state->debug)
  788. fprintf(stderr, "%s: reusing sensor record for sensor '%s', id %d, number %d, type %d, state %d, units %d, reading_type %d\n",
  789. program_name, sensor_name, record_id, sensor_number, sensor_type, sensor_state, sensor_units, sensor_reading_type);
  790. struct sensor *sn = dictionary_acquired_item_value(item);
  791. if(sensor_reading) {
  792. netdata_sensor_set_value(sn, sensor_reading, state);
  793. sn->last_collected_metric_ut = state->sensors.now_ut;
  794. }
  795. sn->sensor_state = sensor_state;
  796. sn->last_collected_state_ut = state->sensors.now_ut;
  797. dictionary_acquired_item_release(state->sensors.dict, item);
  798. return;
  799. }
  800. if(state->debug)
  801. fprintf(stderr, "Allocating new sensor data record for sensor '%s', id %d, number %d, type %d, state %d, units %d, reading_type %d\n",
  802. sensor_name, record_id, sensor_number, sensor_type, sensor_state, sensor_units, sensor_reading_type);
  803. // check if it is excluded
  804. bool excluded_metric = excluded_record_ids_check(record_id);
  805. bool excluded_state = excluded_status_record_ids_check(record_id);
  806. if(excluded_metric) {
  807. if(state->debug)
  808. fprintf(stderr, "Sensor '%s' is excluded by excluded_record_ids_check()\n", sensor_name);
  809. }
  810. if(excluded_state) {
  811. if(state->debug)
  812. fprintf(stderr, "Sensor '%s' is excluded for status check, by excluded_status_record_ids_check()\n", sensor_name);
  813. }
  814. struct sensor t = {
  815. .sensor_type = sensor_type,
  816. .sensor_state = sensor_state,
  817. .sensor_units = sensor_units,
  818. .sensor_reading_type = sensor_reading_type,
  819. .sensor_name = strdupz(sensor_name),
  820. .component = netdata_sensor_name_to_component(sensor_name),
  821. .do_state = !excluded_state,
  822. .do_metric = !excluded_metric,
  823. };
  824. t.type = netdata_ipmi_get_sensor_type_string(t.sensor_type, &t.component);
  825. switch(t.sensor_units) {
  826. case IPMI_MONITORING_SENSOR_UNITS_CELSIUS:
  827. t.dimension = "temperature";
  828. t.context = "ipmi.sensor_temperature_c";
  829. t.title = "IPMI Sensor Temperature Celsius";
  830. t.units = "Celsius";
  831. t.family = "temperatures";
  832. t.chart_type = "line";
  833. t.priority = state->sensors.priority + 10;
  834. break;
  835. case IPMI_MONITORING_SENSOR_UNITS_FAHRENHEIT:
  836. t.dimension = "temperature";
  837. t.context = "ipmi.sensor_temperature_f";
  838. t.title = "IPMI Sensor Temperature Fahrenheit";
  839. t.units = "Fahrenheit";
  840. t.family = "temperatures";
  841. t.chart_type = "line";
  842. t.priority = state->sensors.priority + 20;
  843. break;
  844. case IPMI_MONITORING_SENSOR_UNITS_VOLTS:
  845. t.dimension = "voltage";
  846. t.context = "ipmi.sensor_voltage";
  847. t.title = "IPMI Sensor Voltage";
  848. t.units = "Volts";
  849. t.family = "voltages";
  850. t.chart_type = "line";
  851. t.priority = state->sensors.priority + 30;
  852. break;
  853. case IPMI_MONITORING_SENSOR_UNITS_AMPS:
  854. t.dimension = "ampere";
  855. t.context = "ipmi.sensor_ampere";
  856. t.title = "IPMI Sensor Current";
  857. t.units = "Amps";
  858. t.family = "current";
  859. t.chart_type = "line";
  860. t.priority = state->sensors.priority + 40;
  861. break;
  862. case IPMI_MONITORING_SENSOR_UNITS_RPM:
  863. t.dimension = "rotations";
  864. t.context = "ipmi.sensor_fan_speed";
  865. t.title = "IPMI Sensor Fans Speed";
  866. t.units = "RPM";
  867. t.family = "fans";
  868. t.chart_type = "line";
  869. t.priority = state->sensors.priority + 50;
  870. break;
  871. case IPMI_MONITORING_SENSOR_UNITS_WATTS:
  872. t.dimension = "power";
  873. t.context = "ipmi.sensor_power";
  874. t.title = "IPMI Sensor Power";
  875. t.units = "Watts";
  876. t.family = "power";
  877. t.chart_type = "line";
  878. t.priority = state->sensors.priority + 60;
  879. break;
  880. case IPMI_MONITORING_SENSOR_UNITS_PERCENT:
  881. t.dimension = "percentage";
  882. t.context = "ipmi.sensor_reading_percent";
  883. t.title = "IPMI Sensor Reading Percentage";
  884. t.units = "%%";
  885. t.family = "other";
  886. t.chart_type = "line";
  887. t.priority = state->sensors.priority + 70;
  888. break;
  889. default:
  890. t.priority = state->sensors.priority + 80;
  891. t.do_metric = false;
  892. break;
  893. }
  894. switch(sensor_reading_type) {
  895. case IPMI_MONITORING_SENSOR_READING_TYPE_DOUBLE:
  896. t.multiplier = 1000;
  897. break;
  898. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL:
  899. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER32:
  900. t.multiplier = 1;
  901. break;
  902. default:
  903. t.do_metric = false;
  904. break;
  905. }
  906. if(sensor_reading) {
  907. netdata_sensor_set_value(&t, sensor_reading, state);
  908. t.last_collected_metric_ut = state->sensors.now_ut;
  909. }
  910. t.last_collected_state_ut = state->sensors.now_ut;
  911. dictionary_set(state->sensors.dict, key, &t, sizeof(t));
  912. }
  913. static void netdata_update_ipmi_sel_events_count(struct netdata_ipmi_state *state, uint32_t events) {
  914. state->sel.events = events;
  915. }
  916. int netdata_ipmi_collect_data(struct ipmi_monitoring_ipmi_config *ipmi_config, IPMI_COLLECTION_TYPE type, struct netdata_ipmi_state *state) {
  917. errno = 0;
  918. if(type & IPMI_COLLECT_TYPE_SENSORS) {
  919. state->sensors.collected = 0;
  920. state->sensors.now_ut = now_monotonic_usec();
  921. if (netdata_read_ipmi_sensors(ipmi_config, state) < 0) return -1;
  922. }
  923. if(type & IPMI_COLLECT_TYPE_SEL) {
  924. state->sel.events = 0;
  925. state->sel.now_ut = now_monotonic_usec();
  926. if(netdata_get_ipmi_sel_events_count(ipmi_config, state) < 0) return -2;
  927. }
  928. return 0;
  929. }
  930. int netdata_ipmi_detect_speed_secs(struct ipmi_monitoring_ipmi_config *ipmi_config, IPMI_COLLECTION_TYPE type, struct netdata_ipmi_state *state) {
  931. int i, checks = SPEED_TEST_ITERATIONS, successful = 0;
  932. usec_t total = 0;
  933. for(i = 0 ; i < checks ; i++) {
  934. if(unlikely(state->debug))
  935. fprintf(stderr, "%s: checking %s data collection speed iteration %d of %d\n",
  936. program_name, netdata_collect_type_to_string(type), i + 1, checks);
  937. // measure the time a data collection needs
  938. usec_t start = now_realtime_usec();
  939. if(netdata_ipmi_collect_data(ipmi_config, type, state) < 0)
  940. continue;
  941. usec_t end = now_realtime_usec();
  942. successful++;
  943. if(unlikely(state->debug))
  944. fprintf(stderr, "%s: %s data collection speed was %"PRIu64" usec\n",
  945. program_name, netdata_collect_type_to_string(type), end - start);
  946. // add it to our total
  947. total += end - start;
  948. // wait the same time
  949. // to avoid flooding the IPMI processor with requests
  950. sleep_usec(end - start);
  951. }
  952. if(!successful)
  953. return 0;
  954. // so, we assume it needed 2x the time
  955. // we find the average in microseconds
  956. // and we round-up to the closest second
  957. return (int)(( total * 2 / successful / USEC_PER_SEC ) + 1);
  958. }
  959. // ----------------------------------------------------------------------------
  960. // data collection threads
  961. struct ipmi_collection_thread {
  962. struct ipmi_monitoring_ipmi_config ipmi_config;
  963. int freq_s;
  964. bool debug;
  965. IPMI_COLLECTION_TYPE type;
  966. SPINLOCK spinlock;
  967. struct netdata_ipmi_state state;
  968. };
  969. void *netdata_ipmi_collection_thread(void *ptr) {
  970. struct ipmi_collection_thread *t = ptr;
  971. if(t->debug) fprintf(stderr, "%s: calling initialize_ipmi_config() for %s\n",
  972. program_name, netdata_collect_type_to_string(t->type));
  973. initialize_ipmi_config(&t->ipmi_config);
  974. if(t->debug) fprintf(stderr, "%s: detecting IPMI minimum update frequency for %s...\n",
  975. program_name, netdata_collect_type_to_string(t->type));
  976. int freq_s = netdata_ipmi_detect_speed_secs(&t->ipmi_config, t->type, &t->state);
  977. if(!freq_s) {
  978. if(t->type & IPMI_COLLECT_TYPE_SENSORS) {
  979. t->state.sensors.status = ICS_INIT_FAILED;
  980. t->state.sensors.last_iteration_ut = 0;
  981. }
  982. if(t->type & IPMI_COLLECT_TYPE_SEL) {
  983. t->state.sel.status = ICS_INIT_FAILED;
  984. t->state.sel.last_iteration_ut = 0;
  985. }
  986. return ptr;
  987. }
  988. else {
  989. if(t->type & IPMI_COLLECT_TYPE_SENSORS) {
  990. t->state.sensors.status = ICS_RUNNING;
  991. }
  992. if(t->type & IPMI_COLLECT_TYPE_SEL) {
  993. t->state.sel.status = ICS_RUNNING;
  994. }
  995. }
  996. t->freq_s = freq_s = MAX(t->freq_s, freq_s);
  997. if(t->debug) {
  998. fprintf(stderr, "%s: IPMI minimum update frequency of %s was calculated to %d seconds.\n",
  999. program_name, netdata_collect_type_to_string(t->type), t->freq_s);
  1000. fprintf(stderr, "%s: starting data collection of %s\n",
  1001. program_name, netdata_collect_type_to_string(t->type));
  1002. }
  1003. size_t iteration = 0, failures = 0;
  1004. usec_t step = t->freq_s * USEC_PER_SEC;
  1005. heartbeat_t hb;
  1006. heartbeat_init(&hb);
  1007. while(++iteration) {
  1008. heartbeat_next(&hb, step);
  1009. if(t->debug)
  1010. fprintf(stderr, "%s: calling netdata_ipmi_collect_data() for %s\n",
  1011. program_name, netdata_collect_type_to_string(t->type));
  1012. struct netdata_ipmi_state tmp_state = t->state;
  1013. if(t->type & IPMI_COLLECT_TYPE_SENSORS) {
  1014. tmp_state.sensors.last_iteration_ut = now_monotonic_usec();
  1015. tmp_state.sensors.freq_ut = t->freq_s * USEC_PER_SEC;
  1016. }
  1017. if(t->type & IPMI_COLLECT_TYPE_SEL) {
  1018. tmp_state.sel.last_iteration_ut = now_monotonic_usec();
  1019. tmp_state.sel.freq_ut = t->freq_s * USEC_PER_SEC;
  1020. }
  1021. if(netdata_ipmi_collect_data(&t->ipmi_config, t->type, &tmp_state) != 0)
  1022. failures++;
  1023. else
  1024. failures = 0;
  1025. if(failures > 10) {
  1026. collector_error("%s() failed to collect %s data for %zu consecutive times, having made %zu iterations.",
  1027. __FUNCTION__, netdata_collect_type_to_string(t->type), failures, iteration);
  1028. if(t->type & IPMI_COLLECT_TYPE_SENSORS) {
  1029. t->state.sensors.status = ICS_FAILED;
  1030. t->state.sensors.last_iteration_ut = 0;
  1031. }
  1032. if(t->type & IPMI_COLLECT_TYPE_SEL) {
  1033. t->state.sel.status = ICS_FAILED;
  1034. t->state.sel.last_iteration_ut = 0;
  1035. }
  1036. break;
  1037. }
  1038. spinlock_lock(&t->spinlock);
  1039. t->state = tmp_state;
  1040. spinlock_unlock(&t->spinlock);
  1041. }
  1042. return ptr;
  1043. }
  1044. // ----------------------------------------------------------------------------
  1045. // sending data to netdata
  1046. static inline bool is_sensor_updated(usec_t last_collected_ut, usec_t now_ut, usec_t freq) {
  1047. return (now_ut - last_collected_ut < freq * 2) ? true : false;
  1048. }
  1049. static size_t send_ipmi_sensor_metrics_to_netdata(struct netdata_ipmi_state *state) {
  1050. if(state->sensors.status != ICS_RUNNING) {
  1051. if(unlikely(state->debug))
  1052. fprintf(stderr, "%s: %s() sensors state is not RUNNING\n",
  1053. program_name, __FUNCTION__ );
  1054. return 0;
  1055. }
  1056. size_t total_sensors_sent = 0;
  1057. int update_every = (int)(state->sensors.freq_ut / USEC_PER_SEC);
  1058. struct sensor *sn;
  1059. netdata_mutex_lock(&stdout_mutex);
  1060. // generate the CHART/DIMENSION lines, if we have to
  1061. dfe_start_reentrant(state->sensors.dict, sn) {
  1062. if(unlikely(!sn->do_metric && !sn->do_state))
  1063. continue;
  1064. bool did_metric = false, did_state = false;
  1065. if(likely(sn->do_metric)) {
  1066. if(unlikely(!is_sensor_updated(sn->last_collected_metric_ut, state->updates.now_ut, state->sensors.freq_ut))) {
  1067. if(unlikely(state->debug))
  1068. fprintf(stderr, "%s: %s() sensor '%s' metric is not UPDATED (last updated %"PRIu64", now %"PRIu64", freq %"PRIu64"\n",
  1069. program_name, __FUNCTION__, sn->sensor_name, sn->last_collected_metric_ut, state->updates.now_ut, state->sensors.freq_ut);
  1070. }
  1071. else {
  1072. if (unlikely(!sn->metric_chart_sent)) {
  1073. sn->metric_chart_sent = true;
  1074. printf("CHART '%s_%s' '' '%s' '%s' '%s' '%s' '%s' %d %d '' '%s' '%s'\n",
  1075. sn->context, sn_dfe.name, sn->title, sn->units, sn->family, sn->context,
  1076. sn->chart_type, sn->priority + 1, update_every, program_name, "sensors");
  1077. printf("CLABEL 'sensor' '%s' 1\n", sn->sensor_name);
  1078. printf("CLABEL 'type' '%s' 1\n", sn->type);
  1079. printf("CLABEL 'component' '%s' 1\n", sn->component);
  1080. printf("CLABEL_COMMIT\n");
  1081. printf("DIMENSION '%s' '' absolute 1 %d\n", sn->dimension, sn->multiplier);
  1082. }
  1083. printf("BEGIN '%s_%s'\n", sn->context, sn_dfe.name);
  1084. switch (sn->sensor_reading_type) {
  1085. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER32:
  1086. printf("SET '%s' = %u\n", sn->dimension, sn->sensor_reading.uint32_value
  1087. );
  1088. break;
  1089. case IPMI_MONITORING_SENSOR_READING_TYPE_DOUBLE:
  1090. printf("SET '%s' = %lld\n", sn->dimension,
  1091. (long long int) (sn->sensor_reading.double_value * sn->multiplier)
  1092. );
  1093. break;
  1094. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL:
  1095. printf("SET '%s' = %u\n", sn->dimension, sn->sensor_reading.bool_value
  1096. );
  1097. break;
  1098. default:
  1099. case IPMI_MONITORING_SENSOR_READING_TYPE_UNKNOWN:
  1100. // this should never happen because we also do the same check at netdata_get_sensor()
  1101. sn->do_metric = false;
  1102. break;
  1103. }
  1104. printf("END\n");
  1105. did_metric = true;
  1106. }
  1107. }
  1108. if(likely(sn->do_state)) {
  1109. if(unlikely(!is_sensor_updated(sn->last_collected_state_ut, state->updates.now_ut, state->sensors.freq_ut))) {
  1110. if (unlikely(state->debug))
  1111. fprintf(stderr, "%s: %s() sensor '%s' state is not UPDATED (last updated %"PRIu64", now %"PRIu64", freq %"PRIu64"\n",
  1112. program_name, __FUNCTION__, sn->sensor_name, sn->last_collected_state_ut, state->updates.now_ut, state->sensors.freq_ut);
  1113. }
  1114. else {
  1115. if (unlikely(!sn->state_chart_sent)) {
  1116. sn->state_chart_sent = true;
  1117. printf("CHART 'ipmi.sensor_state_%s' '' 'IPMI Sensor State' 'state' 'states' 'ipmi.sensor_state' 'line' %d %d '' '%s' '%s'\n",
  1118. sn_dfe.name, sn->priority, update_every, program_name, "sensors");
  1119. printf("CLABEL 'sensor' '%s' 1\n", sn->sensor_name);
  1120. printf("CLABEL 'type' '%s' 1\n", sn->type);
  1121. printf("CLABEL 'component' '%s' 1\n", sn->component);
  1122. printf("CLABEL_COMMIT\n");
  1123. printf("DIMENSION 'nominal' '' absolute 1 1\n");
  1124. printf("DIMENSION 'warning' '' absolute 1 1\n");
  1125. printf("DIMENSION 'critical' '' absolute 1 1\n");
  1126. printf("DIMENSION 'unknown' '' absolute 1 1\n");
  1127. }
  1128. printf("BEGIN 'ipmi.sensor_state_%s'\n", sn_dfe.name);
  1129. printf("SET 'nominal' = %lld\n", sn->sensor_state == IPMI_MONITORING_STATE_NOMINAL ? 1LL : 0LL);
  1130. printf("SET 'warning' = %lld\n", sn->sensor_state == IPMI_MONITORING_STATE_WARNING ? 1LL : 0LL);
  1131. printf("SET 'critical' = %lld\n", sn->sensor_state == IPMI_MONITORING_STATE_CRITICAL ? 1LL : 0LL);
  1132. printf("SET 'unknown' = %lld\n", sn->sensor_state == IPMI_MONITORING_STATE_UNKNOWN ? 1LL : 0LL);
  1133. printf("END\n");
  1134. did_state = true;
  1135. }
  1136. }
  1137. if(likely(did_metric || did_state))
  1138. total_sensors_sent++;
  1139. }
  1140. dfe_done(sn);
  1141. netdata_mutex_unlock(&stdout_mutex);
  1142. return total_sensors_sent;
  1143. }
  1144. static size_t send_ipmi_sel_metrics_to_netdata(struct netdata_ipmi_state *state) {
  1145. static bool sel_chart_generated = false;
  1146. netdata_mutex_lock(&stdout_mutex);
  1147. if(likely(state->sel.status == ICS_RUNNING)) {
  1148. if(unlikely(!sel_chart_generated)) {
  1149. sel_chart_generated = true;
  1150. printf("CHART ipmi.events '' 'IPMI Events' 'events' 'events' ipmi.sel area %d %d '' '%s' '%s'\n"
  1151. , state->sel.priority + 2
  1152. , (int)(state->sel.freq_ut / USEC_PER_SEC)
  1153. , program_name
  1154. , "sel"
  1155. );
  1156. printf("DIMENSION events '' absolute 1 1\n");
  1157. }
  1158. printf(
  1159. "BEGIN ipmi.events\n"
  1160. "SET events = %zu\n"
  1161. "END\n"
  1162. , state->sel.events
  1163. );
  1164. }
  1165. netdata_mutex_unlock(&stdout_mutex);
  1166. return state->sel.events;
  1167. }
  1168. // ----------------------------------------------------------------------------
  1169. static const char *get_sensor_state_string(struct sensor *sn) {
  1170. switch (sn->sensor_state) {
  1171. case IPMI_MONITORING_STATE_NOMINAL:
  1172. return "nominal";
  1173. case IPMI_MONITORING_STATE_WARNING:
  1174. return "warning";
  1175. case IPMI_MONITORING_STATE_CRITICAL:
  1176. return "critical";
  1177. default:
  1178. return "unknown";
  1179. }
  1180. }
  1181. static const char *get_sensor_function_priority(struct sensor *sn) {
  1182. switch (sn->sensor_state) {
  1183. case IPMI_MONITORING_STATE_WARNING:
  1184. return "warning";
  1185. case IPMI_MONITORING_STATE_CRITICAL:
  1186. return "critical";
  1187. default:
  1188. return "normal";
  1189. }
  1190. }
  1191. static void freeimi_function_sensors(const char *transaction, char *function __maybe_unused, int timeout __maybe_unused, bool *cancelled __maybe_unused) {
  1192. time_t expires = now_realtime_sec() + update_every;
  1193. BUFFER *wb = buffer_create(PLUGINSD_LINE_MAX, NULL);
  1194. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS);
  1195. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  1196. buffer_json_member_add_string(wb, "type", "table");
  1197. buffer_json_member_add_time_t(wb, "update_every", update_every);
  1198. buffer_json_member_add_string(wb, "help", "View IPMI sensor readings and its state");
  1199. buffer_json_member_add_array(wb, "data");
  1200. struct sensor *sn;
  1201. dfe_start_reentrant(state.sensors.dict, sn) {
  1202. if (unlikely(!sn->do_metric && !sn->do_state))
  1203. continue;
  1204. double reading = NAN;
  1205. switch (sn->sensor_reading_type) {
  1206. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER32:
  1207. reading = (double)sn->sensor_reading.uint32_value;
  1208. break;
  1209. case IPMI_MONITORING_SENSOR_READING_TYPE_DOUBLE:
  1210. reading = (double)(sn->sensor_reading.double_value);
  1211. break;
  1212. case IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL:
  1213. reading = (double)sn->sensor_reading.bool_value;
  1214. break;
  1215. }
  1216. buffer_json_add_array_item_array(wb);
  1217. buffer_json_add_array_item_string(wb, sn->sensor_name);
  1218. buffer_json_add_array_item_string(wb, sn->type);
  1219. buffer_json_add_array_item_string(wb, sn->component);
  1220. buffer_json_add_array_item_double(wb, reading);
  1221. buffer_json_add_array_item_string(wb, sn->units);
  1222. buffer_json_add_array_item_string(wb, get_sensor_state_string(sn));
  1223. buffer_json_add_array_item_object(wb);
  1224. buffer_json_member_add_string(wb, "severity", get_sensor_function_priority(sn));
  1225. buffer_json_object_close(wb);
  1226. buffer_json_array_close(wb);
  1227. }
  1228. dfe_done(sn);
  1229. buffer_json_array_close(wb); // data
  1230. buffer_json_member_add_object(wb, "columns");
  1231. {
  1232. size_t field_id = 0;
  1233. buffer_rrdf_table_add_field(wb, field_id++, "Sensor", "Sensor Name",
  1234. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1235. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1236. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1237. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY | RRDF_FIELD_OPTS_FULL_WIDTH,
  1238. NULL);
  1239. buffer_rrdf_table_add_field(wb, field_id++, "Type", "Sensor Type",
  1240. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1241. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1242. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1243. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY,
  1244. NULL);
  1245. buffer_rrdf_table_add_field(wb, field_id++, "Component", "Sensor Component",
  1246. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1247. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1248. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1249. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY,
  1250. NULL);
  1251. buffer_rrdf_table_add_field(wb, field_id++, "Reading", "Sensor Current Reading",
  1252. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1253. 2, NULL, 0, RRDF_FIELD_SORT_DESCENDING, NULL,
  1254. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_NONE,
  1255. RRDF_FIELD_OPTS_VISIBLE,
  1256. NULL);
  1257. buffer_rrdf_table_add_field(wb, field_id++, "Units", "Sensor Reading Units",
  1258. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1259. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1260. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1261. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY,
  1262. NULL);
  1263. buffer_rrdf_table_add_field(wb, field_id++, "State", "Sensor State",
  1264. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1265. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1266. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1267. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY,
  1268. NULL);
  1269. buffer_rrdf_table_add_field(
  1270. wb, field_id++,
  1271. "rowOptions", "rowOptions",
  1272. RRDF_FIELD_TYPE_NONE,
  1273. RRDR_FIELD_VISUAL_ROW_OPTIONS,
  1274. RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  1275. RRDF_FIELD_SORT_FIXED,
  1276. NULL,
  1277. RRDF_FIELD_SUMMARY_COUNT,
  1278. RRDF_FIELD_FILTER_NONE,
  1279. RRDF_FIELD_OPTS_DUMMY,
  1280. NULL);
  1281. }
  1282. buffer_json_object_close(wb); // columns
  1283. buffer_json_member_add_string(wb, "default_sort_column", "Type");
  1284. buffer_json_member_add_object(wb, "charts");
  1285. {
  1286. buffer_json_member_add_object(wb, "Sensors");
  1287. {
  1288. buffer_json_member_add_string(wb, "name", "Sensors");
  1289. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1290. buffer_json_member_add_array(wb, "columns");
  1291. {
  1292. buffer_json_add_array_item_string(wb, "Sensor");
  1293. }
  1294. buffer_json_array_close(wb);
  1295. }
  1296. buffer_json_object_close(wb);
  1297. }
  1298. buffer_json_object_close(wb); // charts
  1299. buffer_json_member_add_array(wb, "default_charts");
  1300. {
  1301. buffer_json_add_array_item_array(wb);
  1302. buffer_json_add_array_item_string(wb, "Sensors");
  1303. buffer_json_add_array_item_string(wb, "Component");
  1304. buffer_json_array_close(wb);
  1305. buffer_json_add_array_item_array(wb);
  1306. buffer_json_add_array_item_string(wb, "Sensors");
  1307. buffer_json_add_array_item_string(wb, "State");
  1308. buffer_json_array_close(wb);
  1309. }
  1310. buffer_json_array_close(wb);
  1311. buffer_json_member_add_time_t(wb, "expires", now_realtime_sec() + 1);
  1312. buffer_json_finalize(wb);
  1313. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires, wb);
  1314. buffer_free(wb);
  1315. }
  1316. // ----------------------------------------------------------------------------
  1317. // main, command line arguments parsing
  1318. static void plugin_exit(int code) {
  1319. fflush(stdout);
  1320. function_plugin_should_exit = true;
  1321. exit(code);
  1322. }
  1323. int main (int argc, char **argv) {
  1324. clocks_init();
  1325. nd_log_initialize_for_external_plugins("freeipmi.plugin");
  1326. netdata_threads_init_for_external_plugins(0); // set the default threads stack size here
  1327. bool netdata_do_sel = IPMI_ENABLE_SEL_BY_DEFAULT;
  1328. bool debug = false;
  1329. // ------------------------------------------------------------------------
  1330. // parse command line parameters
  1331. int i, freq_s = 0;
  1332. for(i = 1; i < argc ; i++) {
  1333. if(isdigit(*argv[i]) && !freq_s) {
  1334. int n = str2i(argv[i]);
  1335. if(n > 0 && n < 86400) {
  1336. freq_s = n;
  1337. continue;
  1338. }
  1339. }
  1340. else if(strcmp("version", argv[i]) == 0 || strcmp("-version", argv[i]) == 0 || strcmp("--version", argv[i]) == 0 || strcmp("-v", argv[i]) == 0 || strcmp("-V", argv[i]) == 0) {
  1341. printf("%s %s\n", program_name, VERSION);
  1342. exit(0);
  1343. }
  1344. else if(strcmp("debug", argv[i]) == 0) {
  1345. debug = true;
  1346. continue;
  1347. }
  1348. else if(strcmp("sel", argv[i]) == 0) {
  1349. netdata_do_sel = true;
  1350. continue;
  1351. }
  1352. else if(strcmp("no-sel", argv[i]) == 0) {
  1353. netdata_do_sel = false;
  1354. continue;
  1355. }
  1356. else if(strcmp("reread-sdr-cache", argv[i]) == 0) {
  1357. global_sel_flags |= IPMI_MONITORING_SEL_FLAGS_REREAD_SDR_CACHE;
  1358. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_REREAD_SDR_CACHE;
  1359. remove_reread_sdr_after_first_use = false;
  1360. if (debug) fprintf(stderr, "%s: reread-sdr-cache enabled for both sensors and SEL\n", program_name);
  1361. }
  1362. else if(strcmp("interpret-oem-data", argv[i]) == 0) {
  1363. global_sel_flags |= IPMI_MONITORING_SEL_FLAGS_INTERPRET_OEM_DATA;
  1364. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_INTERPRET_OEM_DATA;
  1365. if (debug) fprintf(stderr, "%s: interpret-oem-data enabled for both sensors and SEL\n", program_name);
  1366. }
  1367. else if(strcmp("assume-system-event-record", argv[i]) == 0) {
  1368. global_sel_flags |= IPMI_MONITORING_SEL_FLAGS_ASSUME_SYSTEM_EVENT_RECORD;
  1369. if (debug) fprintf(stderr, "%s: assume-system-event-record enabled\n", program_name);
  1370. }
  1371. else if(strcmp("ignore-non-interpretable-sensors", argv[i]) == 0) {
  1372. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_IGNORE_NON_INTERPRETABLE_SENSORS;
  1373. if (debug) fprintf(stderr, "%s: ignore-non-interpretable-sensors enabled\n", program_name);
  1374. }
  1375. else if(strcmp("bridge-sensors", argv[i]) == 0) {
  1376. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_BRIDGE_SENSORS;
  1377. if (debug) fprintf(stderr, "%s: bridge-sensors enabled\n", program_name);
  1378. }
  1379. else if(strcmp("shared-sensors", argv[i]) == 0) {
  1380. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_SHARED_SENSORS;
  1381. if (debug) fprintf(stderr, "%s: shared-sensors enabled\n", program_name);
  1382. }
  1383. else if(strcmp("no-discrete-reading", argv[i]) == 0) {
  1384. global_sensor_reading_flags &= ~(IPMI_MONITORING_SENSOR_READING_FLAGS_DISCRETE_READING);
  1385. if (debug) fprintf(stderr, "%s: discrete-reading disabled\n", program_name);
  1386. }
  1387. else if(strcmp("ignore-scanning-disabled", argv[i]) == 0) {
  1388. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_IGNORE_SCANNING_DISABLED;
  1389. if (debug) fprintf(stderr, "%s: ignore-scanning-disabled enabled\n", program_name);
  1390. }
  1391. else if(strcmp("assume-bmc-owner", argv[i]) == 0) {
  1392. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_ASSUME_BMC_OWNER;
  1393. if (debug) fprintf(stderr, "%s: assume-bmc-owner enabled\n", program_name);
  1394. }
  1395. #if defined(IPMI_MONITORING_SEL_FLAGS_ENTITY_SENSOR_NAMES) && defined(IPMI_MONITORING_SENSOR_READING_FLAGS_ENTITY_SENSOR_NAMES)
  1396. else if(strcmp("entity-sensor-names", argv[i]) == 0) {
  1397. global_sel_flags |= IPMI_MONITORING_SEL_FLAGS_ENTITY_SENSOR_NAMES;
  1398. global_sensor_reading_flags |= IPMI_MONITORING_SENSOR_READING_FLAGS_ENTITY_SENSOR_NAMES;
  1399. if (debug) fprintf(stderr, "%s: entity-sensor-names enabled for both sensors and SEL\n", program_name);
  1400. }
  1401. #endif
  1402. else if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
  1403. fprintf(stderr,
  1404. "\n"
  1405. " netdata %s %s\n"
  1406. " Copyright (C) 2023 Netdata Inc.\n"
  1407. " Released under GNU General Public License v3 or later.\n"
  1408. " All rights reserved.\n"
  1409. "\n"
  1410. " This program is a data collector plugin for netdata.\n"
  1411. "\n"
  1412. " Available command line options:\n"
  1413. "\n"
  1414. " SECONDS data collection frequency\n"
  1415. " minimum: %d\n"
  1416. "\n"
  1417. " debug enable verbose output\n"
  1418. " default: disabled\n"
  1419. "\n"
  1420. " sel\n"
  1421. " no-sel enable/disable SEL collection\n"
  1422. " default: %s\n"
  1423. "\n"
  1424. " reread-sdr-cache re-read SDR cache on every iteration\n"
  1425. " default: disabled\n"
  1426. "\n"
  1427. " interpret-oem-data attempt to parse OEM data\n"
  1428. " default: disabled\n"
  1429. "\n"
  1430. " assume-system-event-record \n"
  1431. " tread illegal SEL events records as normal\n"
  1432. " default: disabled\n"
  1433. "\n"
  1434. " ignore-non-interpretable-sensors \n"
  1435. " do not read sensors that cannot be interpreted\n"
  1436. " default: disabled\n"
  1437. "\n"
  1438. " bridge-sensors bridge sensors not owned by the BMC\n"
  1439. " default: disabled\n"
  1440. "\n"
  1441. " shared-sensors enable shared sensors, if found\n"
  1442. " default: disabled\n"
  1443. "\n"
  1444. " no-discrete-reading do not read sensors that their event/reading type code is invalid\n"
  1445. " default: enabled\n"
  1446. "\n"
  1447. " ignore-scanning-disabled \n"
  1448. " Ignore the scanning bit and read sensors no matter what\n"
  1449. " default: disabled\n"
  1450. "\n"
  1451. " assume-bmc-owner assume the BMC is the sensor owner no matter what\n"
  1452. " (usually bridging is required too)\n"
  1453. " default: disabled\n"
  1454. "\n"
  1455. #if defined(IPMI_MONITORING_SEL_FLAGS_ENTITY_SENSOR_NAMES) && defined(IPMI_MONITORING_SENSOR_READING_FLAGS_ENTITY_SENSOR_NAMES)
  1456. " entity-sensor-names sensor names prefixed with entity id and instance\n"
  1457. " default: disabled\n"
  1458. "\n"
  1459. #endif
  1460. " hostname HOST\n"
  1461. " username USER\n"
  1462. " password PASS connect to remote IPMI host\n"
  1463. " default: local IPMI processor\n"
  1464. "\n"
  1465. " no-auth-code-check\n"
  1466. " noauthcodecheck don't check the authentication codes returned\n"
  1467. "\n"
  1468. " driver-type IPMIDRIVER\n"
  1469. " Specify the driver type to use instead of doing an auto selection. \n"
  1470. " The currently available outofband drivers are LAN and LAN_2_0,\n"
  1471. " which perform IPMI 1.5 and IPMI 2.0 respectively. \n"
  1472. " The currently available inband drivers are KCS, SSIF, OPENIPMI and SUNBMC.\n"
  1473. "\n"
  1474. " sdr-cache-dir PATH directory for SDR cache files\n"
  1475. " default: %s\n"
  1476. "\n"
  1477. " sensor-config-file FILE filename to read sensor configuration\n"
  1478. " default: %s\n"
  1479. "\n"
  1480. " sel-config-file FILE filename to read sel configuration\n"
  1481. " default: %s\n"
  1482. "\n"
  1483. " ignore N1,N2,N3,... sensor IDs to ignore\n"
  1484. " default: none\n"
  1485. "\n"
  1486. " ignore-status N1,N2,N3,... sensor IDs to ignore status (nominal/warning/critical)\n"
  1487. " default: none\n"
  1488. "\n"
  1489. " -v\n"
  1490. " -V\n"
  1491. " version print version and exit\n"
  1492. "\n"
  1493. " Linux kernel module for IPMI is CPU hungry.\n"
  1494. " On Linux run this to lower kipmiN CPU utilization:\n"
  1495. " # echo 10 > /sys/module/ipmi_si/parameters/kipmid_max_busy_us\n"
  1496. "\n"
  1497. " or create: /etc/modprobe.d/ipmi.conf with these contents:\n"
  1498. " options ipmi_si kipmid_max_busy_us=10\n"
  1499. "\n"
  1500. " For more information:\n"
  1501. " https://github.com/netdata/netdata/tree/master/collectors/freeipmi.plugin\n"
  1502. "\n"
  1503. , program_name, VERSION
  1504. , update_every
  1505. , netdata_do_sel?"enabled":"disabled"
  1506. , sdr_cache_directory?sdr_cache_directory:"system default"
  1507. , sensor_config_file?sensor_config_file:"system default"
  1508. , sel_config_file?sel_config_file:"system default"
  1509. );
  1510. exit(1);
  1511. }
  1512. else if(i < argc && strcmp("hostname", argv[i]) == 0) {
  1513. hostname = strdupz(argv[++i]);
  1514. char *s = argv[i];
  1515. // mask it be hidden from the process tree
  1516. while(*s) *s++ = 'x';
  1517. if(debug) fprintf(stderr, "%s: hostname set to '%s'\n", program_name, hostname);
  1518. continue;
  1519. }
  1520. else if(i < argc && strcmp("username", argv[i]) == 0) {
  1521. username = strdupz(argv[++i]);
  1522. char *s = argv[i];
  1523. // mask it be hidden from the process tree
  1524. while(*s) *s++ = 'x';
  1525. if(debug) fprintf(stderr, "%s: username set to '%s'\n", program_name, username);
  1526. continue;
  1527. }
  1528. else if(i < argc && strcmp("password", argv[i]) == 0) {
  1529. password = strdupz(argv[++i]);
  1530. char *s = argv[i];
  1531. // mask it be hidden from the process tree
  1532. while(*s) *s++ = 'x';
  1533. if(debug) fprintf(stderr, "%s: password set to '%s'\n", program_name, password);
  1534. continue;
  1535. }
  1536. else if(strcmp("driver-type", argv[i]) == 0) {
  1537. if (hostname) {
  1538. protocol_version = netdata_parse_outofband_driver_type(argv[++i]);
  1539. if(debug) fprintf(stderr, "%s: outband protocol version set to '%d'\n",
  1540. program_name, protocol_version);
  1541. }
  1542. else {
  1543. driver_type = netdata_parse_inband_driver_type(argv[++i]);
  1544. if(debug) fprintf(stderr, "%s: inband driver type set to '%d'\n",
  1545. program_name, driver_type);
  1546. }
  1547. continue;
  1548. } else if (i < argc && (strcmp("noauthcodecheck", argv[i]) == 0 || strcmp("no-auth-code-check", argv[i]) == 0)) {
  1549. if (!hostname || netdata_host_is_localhost(hostname)) {
  1550. if (debug)
  1551. fprintf(stderr, "%s: noauthcodecheck workaround flag is ignored for inband configuration\n",
  1552. program_name);
  1553. }
  1554. else if (protocol_version < 0 || protocol_version == IPMI_MONITORING_PROTOCOL_VERSION_1_5) {
  1555. workaround_flags |= IPMI_MONITORING_WORKAROUND_FLAGS_PROTOCOL_VERSION_1_5_NO_AUTH_CODE_CHECK;
  1556. if (debug)
  1557. fprintf(stderr, "%s: noauthcodecheck workaround flag enabled\n", program_name);
  1558. }
  1559. else {
  1560. if (debug)
  1561. fprintf(stderr, "%s: noauthcodecheck workaround flag is ignored for protocol version 2.0\n",
  1562. program_name);
  1563. }
  1564. continue;
  1565. }
  1566. else if(i < argc && strcmp("sdr-cache-dir", argv[i]) == 0) {
  1567. sdr_cache_directory = argv[++i];
  1568. if(debug)
  1569. fprintf(stderr, "%s: SDR cache directory set to '%s'\n", program_name, sdr_cache_directory);
  1570. continue;
  1571. }
  1572. else if(i < argc && strcmp("sensor-config-file", argv[i]) == 0) {
  1573. sensor_config_file = argv[++i];
  1574. if(debug) fprintf(stderr, "%s: sensor config file set to '%s'\n", program_name, sensor_config_file);
  1575. continue;
  1576. }
  1577. else if(i < argc && strcmp("sel-config-file", argv[i]) == 0) {
  1578. sel_config_file = argv[++i];
  1579. if(debug) fprintf(stderr, "%s: sel config file set to '%s'\n", program_name, sel_config_file);
  1580. continue;
  1581. }
  1582. else if(i < argc && strcmp("ignore", argv[i]) == 0) {
  1583. excluded_record_ids_parse(argv[++i], debug);
  1584. continue;
  1585. }
  1586. else if(i < argc && strcmp("ignore-status", argv[i]) == 0) {
  1587. excluded_status_record_ids_parse(argv[++i], debug);
  1588. continue;
  1589. }
  1590. collector_error("%s(): ignoring parameter '%s'", __FUNCTION__, argv[i]);
  1591. }
  1592. errno = 0;
  1593. if(freq_s && freq_s < update_every)
  1594. collector_info("%s(): update frequency %d seconds is too small for IPMI. Using %d.",
  1595. __FUNCTION__, freq_s, update_every);
  1596. update_every = freq_s = MAX(freq_s, update_every);
  1597. update_every_sel = MAX(update_every, update_every_sel);
  1598. // ------------------------------------------------------------------------
  1599. // initialize IPMI
  1600. if(debug) {
  1601. fprintf(stderr, "%s: calling ipmi_monitoring_init()\n", program_name);
  1602. ipmimonitoring_init_flags |= IPMI_MONITORING_FLAGS_DEBUG|IPMI_MONITORING_FLAGS_DEBUG_IPMI_PACKETS;
  1603. }
  1604. int rc;
  1605. if(ipmi_monitoring_init(ipmimonitoring_init_flags, &rc) < 0)
  1606. fatal("ipmi_monitoring_init: %s", ipmi_monitoring_ctx_strerror(rc));
  1607. // ------------------------------------------------------------------------
  1608. // create the data collection threads
  1609. struct ipmi_collection_thread sensors_data = {
  1610. .type = IPMI_COLLECT_TYPE_SENSORS,
  1611. .freq_s = update_every,
  1612. .spinlock = NETDATA_SPINLOCK_INITIALIZER,
  1613. .debug = debug,
  1614. .state = {
  1615. .debug = debug,
  1616. .sensors = {
  1617. .status = ICS_INIT,
  1618. .last_iteration_ut = now_monotonic_usec(),
  1619. .freq_ut = update_every * USEC_PER_SEC,
  1620. .priority = IPMI_SENSORS_DASHBOARD_PRIORITY,
  1621. .dict = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE|DICT_OPTION_FIXED_SIZE, NULL, sizeof(struct sensor)),
  1622. },
  1623. },
  1624. }, sel_data = {
  1625. .type = IPMI_COLLECT_TYPE_SEL,
  1626. .freq_s = update_every_sel,
  1627. .spinlock = NETDATA_SPINLOCK_INITIALIZER,
  1628. .debug = debug,
  1629. .state = {
  1630. .debug = debug,
  1631. .sel = {
  1632. .status = ICS_INIT,
  1633. .last_iteration_ut = now_monotonic_usec(),
  1634. .freq_ut = update_every_sel * USEC_PER_SEC,
  1635. .priority = IPMI_SEL_DASHBOARD_PRIORITY,
  1636. },
  1637. },
  1638. };
  1639. netdata_thread_t sensors_thread = 0, sel_thread = 0;
  1640. netdata_thread_create(&sensors_thread, "IPMI[sensors]", NETDATA_THREAD_OPTION_DONT_LOG, netdata_ipmi_collection_thread, &sensors_data);
  1641. if(netdata_do_sel)
  1642. netdata_thread_create(&sel_thread, "IPMI[sel]", NETDATA_THREAD_OPTION_DONT_LOG, netdata_ipmi_collection_thread, &sel_data);
  1643. // ------------------------------------------------------------------------
  1644. // the main loop
  1645. if(debug) fprintf(stderr, "%s: starting data collection\n", program_name);
  1646. time_t started_t = now_monotonic_sec();
  1647. size_t iteration = 0;
  1648. usec_t step = 100 * USEC_PER_MS;
  1649. bool global_chart_created = false;
  1650. bool tty = isatty(fileno(stderr)) == 1;
  1651. heartbeat_t hb;
  1652. heartbeat_init(&hb);
  1653. for(iteration = 0; 1 ; iteration++) {
  1654. usec_t dt = heartbeat_next(&hb, step);
  1655. if (!tty) {
  1656. netdata_mutex_lock(&stdout_mutex);
  1657. fprintf(stdout, "\n"); // keepalive to avoid parser read timeout (2 minutes) during ipmi_detect_speed_secs()
  1658. fflush(stdout);
  1659. netdata_mutex_unlock(&stdout_mutex);
  1660. }
  1661. spinlock_lock(&sensors_data.spinlock);
  1662. state.sensors = sensors_data.state.sensors;
  1663. spinlock_unlock(&sensors_data.spinlock);
  1664. spinlock_lock(&sel_data.spinlock);
  1665. state.sel = sel_data.state.sel;
  1666. spinlock_unlock(&sel_data.spinlock);
  1667. switch(state.sensors.status) {
  1668. case ICS_RUNNING:
  1669. step = update_every * USEC_PER_SEC;
  1670. if(state.sensors.last_iteration_ut < now_monotonic_usec() - IPMI_RESTART_IF_SENSORS_DONT_ITERATE_EVERY_SECONDS * USEC_PER_SEC) {
  1671. collector_error("%s(): sensors have not be collected for %zu seconds. Exiting to restart.",
  1672. __FUNCTION__, (size_t)((now_monotonic_usec() - state.sensors.last_iteration_ut) / USEC_PER_SEC));
  1673. fprintf(stdout, "EXIT\n");
  1674. plugin_exit(0);
  1675. }
  1676. break;
  1677. case ICS_INIT:
  1678. continue;
  1679. case ICS_INIT_FAILED:
  1680. collector_error("%s(): sensors failed to initialize. Calling DISABLE.", __FUNCTION__);
  1681. fprintf(stdout, "DISABLE\n");
  1682. plugin_exit(0);
  1683. case ICS_FAILED:
  1684. collector_error("%s(): sensors fails repeatedly to collect metrics. Exiting to restart.", __FUNCTION__);
  1685. fprintf(stdout, "EXIT\n");
  1686. plugin_exit(0);
  1687. }
  1688. if(netdata_do_sel) {
  1689. switch (state.sensors.status) {
  1690. case ICS_RUNNING:
  1691. case ICS_INIT:
  1692. break;
  1693. case ICS_INIT_FAILED:
  1694. case ICS_FAILED:
  1695. collector_error("%s(): SEL fails to collect events. Disabling SEL collection.", __FUNCTION__);
  1696. netdata_do_sel = false;
  1697. break;
  1698. }
  1699. }
  1700. if(unlikely(debug))
  1701. fprintf(stderr, "%s: calling send_ipmi_sensor_metrics_to_netdata()\n", program_name);
  1702. static bool add_func_sensors = true;
  1703. if (add_func_sensors) {
  1704. add_func_sensors = false;
  1705. struct functions_evloop_globals *wg =
  1706. functions_evloop_init(1, "FREEIPMI", &stdout_mutex, &function_plugin_should_exit);
  1707. functions_evloop_add_function(
  1708. wg, "ipmi-sensors", freeimi_function_sensors, PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT);
  1709. FREEIPMI_GLOBAL_FUNCTION_SENSORS();
  1710. }
  1711. state.updates.now_ut = now_monotonic_usec();
  1712. send_ipmi_sensor_metrics_to_netdata(&state);
  1713. if(netdata_do_sel)
  1714. send_ipmi_sel_metrics_to_netdata(&state);
  1715. if(unlikely(debug))
  1716. fprintf(stderr, "%s: iteration %zu, dt %"PRIu64" usec, sensors ever collected %zu, sensors last collected %zu \n"
  1717. , program_name
  1718. , iteration
  1719. , dt
  1720. , dictionary_entries(state.sensors.dict)
  1721. , state.sensors.collected
  1722. );
  1723. netdata_mutex_lock(&stdout_mutex);
  1724. if (!global_chart_created) {
  1725. global_chart_created = true;
  1726. fprintf(stdout,
  1727. "CHART netdata.freeipmi_availability_status '' 'Plugin availability status' 'status' "
  1728. "plugins netdata.plugin_availability_status line 146000 %d '' '%s' '%s'\n"
  1729. "DIMENSION available '' absolute 1 1\n",
  1730. update_every, program_name, "");
  1731. }
  1732. fprintf(stdout,
  1733. "BEGIN netdata.freeipmi_availability_status\n"
  1734. "SET available = 1\n"
  1735. "END\n");
  1736. // restart check (14400 seconds)
  1737. if (now_monotonic_sec() - started_t > IPMI_RESTART_EVERY_SECONDS) {
  1738. collector_info("%s(): reached my lifetime expectancy. Exiting to restart.", __FUNCTION__);
  1739. fprintf(stdout, "EXIT\n");
  1740. plugin_exit(0);
  1741. }
  1742. fflush(stdout);
  1743. netdata_mutex_unlock(&stdout_mutex);
  1744. }
  1745. }