From: Florian Forster Date: Fri, 6 Oct 2017 11:14:01 +0000 (+0200) Subject: Merge remote-tracking branch 'github/pr/2091' X-Git-Tag: collectd-5.8.0~56 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=1581f3b307554cbf8b5784729754c5d73a1192a5 Merge remote-tracking branch 'github/pr/2091' --- 1581f3b307554cbf8b5784729754c5d73a1192a5 diff --cc src/ipmi.c index c8c80429,ac475e44..87f8b441 --- a/src/ipmi.c +++ b/src/ipmi.c @@@ -200,6 -206,55 +206,54 @@@ static void sensor_read_handler(ipmi_se plugin_dispatch_values(&vl); } /* void sensor_read_handler */ + static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) { + char temp[DATA_MAX_NAME_LEN] = {0}; + ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor); + const char *entity_id_string = ipmi_entity_get_entity_id_string(ent); + char sensor_name[DATA_MAX_NAME_LEN] = ""; + char *sensor_name_ptr; + + if ((buffer == NULL) || (buf_len == 0)) + return; + + ipmi_sensor_get_name(sensor, temp, sizeof(temp)); + temp[sizeof(temp) - 1] = 0; + + if (entity_id_string != NULL && strlen(temp)) - ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, - entity_id_string); ++ snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string); + else if (entity_id_string != NULL) + sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name)); + else + sstrncpy(sensor_name, temp, sizeof(sensor_name)); + + if (strlen(temp)) { + sstrncpy(temp, sensor_name, sizeof(temp)); + sensor_name_ptr = strstr(temp, ")."); + if (sensor_name_ptr != NULL) { + /* If name is something like "foo (123).bar", + * change that to "bar (123)". + * Both, sensor_name_ptr and sensor_id_ptr point to memory within the + * `temp' array, which holds a copy of the current `sensor_name'. */ + char *sensor_id_ptr; + + /* `sensor_name_ptr' points to ").bar". */ + sensor_name_ptr[1] = 0; + /* `temp' holds "foo (123)\0bar\0". */ + sensor_name_ptr += 2; + /* `sensor_name_ptr' now points to "bar". */ + + sensor_id_ptr = strstr(temp, "("); + if (sensor_id_ptr != NULL) { + /* `sensor_id_ptr' now points to "(123)". */ - ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr, - sensor_id_ptr); ++ snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr, ++ sensor_id_ptr); + } + /* else: don't touch sensor_name. */ + } + } + sstrncpy(buffer, sensor_name, buf_len); + } + static int sensor_list_add(ipmi_sensor_t *sensor) { ipmi_sensor_id_t sensor_id; c_ipmi_sensor_list_t *list_item; @@@ -417,9 -434,170 +433,163 @@@ static int sensor_list_remove_all(void list_item = list_next; } /* while (list_item) */ - return (0); + return 0; } /* int sensor_list_remove_all */ + static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) { - int _severity = NOTIF_OKAY; - + switch (severity) { + case IPMI_LOWER_NON_CRITICAL: + case IPMI_UPPER_NON_CRITICAL: - _severity = NOTIF_OKAY; - break; ++ return NOTIF_OKAY; + case IPMI_LOWER_CRITICAL: + case IPMI_UPPER_CRITICAL: - _severity = NOTIF_WARNING; - break; ++ return NOTIF_WARNING; + case IPMI_LOWER_NON_RECOVERABLE: + case IPMI_UPPER_NON_RECOVERABLE: - _severity = NOTIF_FAILURE; - break; ++ return NOTIF_FAILURE; + default: - break; ++ return NOTIF_OKAY; + } /* switch (severity) */ - - return (_severity); + } /* int sensor_convert_threshold_severity */ + + static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor, + enum ipmi_event_dir_e dir, + ipmi_event_t *event) { + ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor); + + plugin_notification_meta_add_string(n, "entity_name", + ipmi_entity_get_entity_id_string(ent)); + plugin_notification_meta_add_signed_int(n, "entity_id", + ipmi_entity_get_entity_id(ent)); + plugin_notification_meta_add_signed_int(n, "entity_instance", + ipmi_entity_get_entity_instance(ent)); + plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION); + + if (event) + plugin_notification_meta_add_signed_int(n, "event_type", + ipmi_event_get_type(event)); + } /* void add_event_sensor_meta_data */ + + static int sensor_threshold_event_handler( + ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir, + enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low, + enum ipmi_value_present_e value_present, unsigned int raw_value, + double value, void *cb_data, ipmi_event_t *event) { + + /* From the IPMI specification Chapter 2: Events. + * If a callback handles the event, then all future callbacks called due to + * the event will receive a NULL for the event. So be ready to handle a NULL + * event in all your event handlers. A NULL may also be passed to an event + * handler if the callback was not due to an event. */ + if (event == NULL) - return (IPMI_EVENT_NOT_HANDLED); ++ return IPMI_EVENT_NOT_HANDLED; + + /* offset is a table index and it's represented as enum of strings that are + organized in the way - high and low for each threshold severity level */ + notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL}; + unsigned int offset = (2 * threshold) + high_low; + unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor); + unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor); + const char *event_state = + ipmi_get_reading_name(event_type, sensor_type, offset); + sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance)); + if (value_present != IPMI_NO_VALUES_PRESENT) - ssnprintf(n.message, sizeof(n.message), - "sensor %s received event: %s, value is %f", n.type_instance, - event_state, value); ++ snprintf(n.message, sizeof(n.message), ++ "sensor %s received event: %s, value is %f", n.type_instance, ++ event_state, value); + else - ssnprintf(n.message, sizeof(n.message), - "sensor %s received event: %s, value not provided", - n.type_instance, event_state); ++ snprintf(n.message, sizeof(n.message), ++ "sensor %s received event: %s, value not provided", ++ n.type_instance, event_state); + + DEBUG("Threshold event received for sensor %s", n.type_instance); + + sstrncpy(n.host, hostname_g, sizeof(n.host)); + sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type)); + n.severity = sensor_convert_threshold_severity(threshold); + n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event)); + + plugin_notification_meta_add_string(&n, "severity", + ipmi_get_threshold_string(threshold)); + plugin_notification_meta_add_string(&n, "direction", + ipmi_get_value_dir_string(high_low)); + + switch (value_present) { + case IPMI_BOTH_VALUES_PRESENT: + plugin_notification_meta_add_double(&n, "val", value); - /* both values present, so fall-through to add raw value too */ ++ /* both values present, so fall-through to add raw value too */ + case IPMI_RAW_VALUE_PRESENT: { + char buf[DATA_MAX_NAME_LEN] = {0}; + snprintf(buf, sizeof(buf), "0x%2.2x", raw_value); + plugin_notification_meta_add_string(&n, "raw", buf); + } break; + default: + break; + } /* switch (value_present) */ + + add_event_common_data(&n, sensor, dir, event); + + plugin_dispatch_notification(&n); + plugin_notification_meta_free(n.meta); + + /* Delete handled ipmi event from the list */ + if (c_ipmi_sel_clear_event) { + ipmi_event_delete(event, NULL, NULL); - return (IPMI_EVENT_HANDLED); ++ return IPMI_EVENT_HANDLED; + } + - return (IPMI_EVENT_NOT_HANDLED); ++ return IPMI_EVENT_NOT_HANDLED; + } /* int sensor_threshold_event_handler */ + + static int sensor_discrete_event_handler(ipmi_sensor_t *sensor, + enum ipmi_event_dir_e dir, int offset, + int severity, int prev_severity, + void *cb_data, ipmi_event_t *event) { + /* From the IPMI specification Chapter 2: Events. + * If a callback handles the event, then all future callbacks called due to + * the event will receive a NULL for the event. So be ready to handle a NULL + * event in all your event handlers. A NULL may also be passed to an event + * handler if the callback was not due to an event. */ + if (event == NULL) - return (IPMI_EVENT_NOT_HANDLED); ++ return IPMI_EVENT_NOT_HANDLED; + + notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL}; + unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor); + unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor); + const char *event_state = + ipmi_get_reading_name(event_type, sensor_type, offset); + sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance)); - ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s", - n.type_instance, event_state); ++ snprintf(n.message, sizeof(n.message), "sensor %s received event: %s", ++ n.type_instance, event_state); + + DEBUG("Discrete event received for sensor %s", n.type_instance); + + sstrncpy(n.host, hostname_g, sizeof(n.host)); + sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type)); + n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event)); + + plugin_notification_meta_add_signed_int(&n, "offset", offset); + + if (severity != -1) + plugin_notification_meta_add_signed_int(&n, "severity", severity); + + if (prev_severity != -1) + plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity); + + add_event_common_data(&n, sensor, dir, event); + + plugin_dispatch_notification(&n); + plugin_notification_meta_free(n.meta); + + /* Delete handled ipmi event from the list */ + if (c_ipmi_sel_clear_event) { + ipmi_event_delete(event, NULL, NULL); - return (IPMI_EVENT_HANDLED); ++ return IPMI_EVENT_HANDLED; + } + - return (IPMI_EVENT_NOT_HANDLED); ++ return IPMI_EVENT_NOT_HANDLED; + } /* int sensor_discrete_event_handler */ + /* * Entity handlers */ @@@ -459,6 -668,23 +660,23 @@@ static void domain_entity_update_handle } } /* void domain_entity_update_handler */ -static void smi_event_handler(ipmi_con_t __attribute__((unused)) *ipmi, - const ipmi_addr_t __attribute__((unused)) *addr, ++static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi, ++ const ipmi_addr_t __attribute__((unused)) * addr, + unsigned int __attribute__((unused)) addr_len, + ipmi_event_t *event, void *cb_data) { + unsigned int type = ipmi_event_get_type(event); + ipmi_domain_t *domain = cb_data; + + DEBUG("%s: Event received: type %u", __FUNCTION__, type); + + if (type != 0x02) + /* It's not a standard IPMI event. */ + return; + + /* force domain to reread SELs */ + ipmi_domain_reread_sels(domain, NULL, NULL); + } + static void domain_connection_change_handler(ipmi_domain_t *domain, int err, unsigned int conn_num, unsigned int port_num, @@@ -545,24 -779,22 +771,22 @@@ static int c_ipmi_config(const char *ke if (strcasecmp("Sensor", key) == 0) { ignorelist_add(ignorelist, value); } else if (strcasecmp("IgnoreSelected", key) == 0) { - int invert = 1; - if (IS_TRUE(value)) - invert = 0; - ignorelist_set_invert(ignorelist, invert); + ignorelist_set_invert(ignorelist, !IS_TRUE(value)); } else if (strcasecmp("NotifySensorAdd", key) == 0) { - if (IS_TRUE(value)) - c_ipmi_nofiy_add = 1; + c_ipmi_notify_add = IS_TRUE(value); } else if (strcasecmp("NotifySensorRemove", key) == 0) { - if (IS_TRUE(value)) - c_ipmi_nofiy_remove = 1; + c_ipmi_notify_remove = IS_TRUE(value); } else if (strcasecmp("NotifySensorNotPresent", key) == 0) { - if (IS_TRUE(value)) - c_ipmi_nofiy_notpresent = 1; + c_ipmi_notify_notpresent = IS_TRUE(value); + } else if (strcasecmp("SELEnabled", key) == 0) { + c_ipmi_sel_enabled = IS_TRUE(value); + } else if (strcasecmp("SELClearEvent", key) == 0) { + c_ipmi_sel_clear_event = IS_TRUE(value); } else { - return (-1); + return -1; } - return (0); + return 0; } /* int c_ipmi_config */ static int c_ipmi_init(void) {