-static int riemann_event_add_attribute(Event *event, /* {{{ */
- char const *key, char const *value)
-{
- Attribute **new_attributes;
- Attribute *a;
-
- new_attributes = realloc (event->attributes,
- sizeof (*event->attributes) * (event->n_attributes + 1));
- if (new_attributes == NULL)
- {
- ERROR ("write_riemann plugin: realloc failed.");
- return (ENOMEM);
- }
- event->attributes = new_attributes;
-
- a = malloc (sizeof (*a));
- if (a == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- return (ENOMEM);
- }
- attribute__init (a);
-
- a->key = strdup (key);
- if (value != NULL)
- a->value = strdup (value);
-
- event->attributes[event->n_attributes] = a;
- event->n_attributes++;
-
- return (0);
-} /* }}} int riemann_event_add_attribute */
-
-static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ */
- notification_t const *n)
-{
- Msg *msg;
- Event *event;
- char service_buffer[6 * DATA_MAX_NAME_LEN];
- char const *severity;
- notification_meta_t *meta;
- int i;
-
- msg = malloc (sizeof (*msg));
- if (msg == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- return (NULL);
- }
- memset (msg, 0, sizeof (*msg));
- msg__init (msg);
-
- msg->events = malloc (sizeof (*msg->events));
- if (msg->events == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- sfree (msg);
- return (NULL);
- }
-
- event = malloc (sizeof (*event));
- if (event == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- sfree (msg->events);
- sfree (msg);
- return (NULL);
- }
- memset (event, 0, sizeof (*event));
- event__init (event);
-
- msg->events[0] = event;
- msg->n_events = 1;
-
- event->host = strdup (n->host);
- event->time = CDTIME_T_TO_TIME_T (n->time);
- event->has_time = 1;
-
- switch (n->severity)
- {
- case NOTIF_OKAY: severity = "ok"; break;
- case NOTIF_WARNING: severity = "warning"; break;
- case NOTIF_FAILURE: severity = "critical"; break;
- default: severity = "unknown";
- }
- event->state = strdup (severity);
-
- riemann_event_add_tag (event, "notification");
- if (n->host[0] != 0)
- riemann_event_add_attribute (event, "host", n->host);
- if (n->plugin[0] != 0)
- riemann_event_add_attribute (event, "plugin", n->plugin);
- if (n->plugin_instance[0] != 0)
- riemann_event_add_attribute (event, "plugin_instance",
- n->plugin_instance);
-
- if (n->type[0] != 0)
- riemann_event_add_attribute (event, "type", n->type);
- if (n->type_instance[0] != 0)
- riemann_event_add_attribute (event, "type_instance",
- n->type_instance);
-
- for (i = 0; i < riemann_attrs_num; i += 2)
- riemann_event_add_attribute(event,
- riemann_attrs[i],
- riemann_attrs[i +1]);
-
- for (i = 0; i < riemann_tags_num; i++)
- riemann_event_add_tag (event, riemann_tags[i]);
-
- format_name (service_buffer, sizeof (service_buffer),
- /* host = */ "", n->plugin, n->plugin_instance,
- n->type, n->type_instance);
- event->service = strdup (&service_buffer[1]);
-
- if (n->message[0] != 0)
- riemann_event_add_attribute (event, "description", n->message);
-
- /* Pull in values from threshold and add extra attributes */
- for (meta = n->meta; meta != NULL; meta = meta->next)
- {
- if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
- {
- event->metric_d = meta->nm_value.nm_double;
- event->has_metric_d = 1;
- continue;
- }
-
- if (meta->type == NM_TYPE_STRING) {
- riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
- continue;
- }
- }
-
- DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
- "host = \"%s\", service = \"%s\", state = \"%s\"",
- event->host, event->service, event->state);
- return (msg);
-} /* }}} Msg *riemann_notification_to_protobuf */
-
-static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ */
- data_set_t const *ds,
- value_list_t const *vl, size_t index,
- gauge_t const *rates,
- int status)
-{
- Event *event;
- char name_buffer[5 * DATA_MAX_NAME_LEN];
- char service_buffer[6 * DATA_MAX_NAME_LEN];
- double ttl;
- int i;
-
- event = malloc (sizeof (*event));
- if (event == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- return (NULL);
- }
- memset (event, 0, sizeof (*event));
- event__init (event);
-
- event->host = strdup (vl->host);
- event->time = CDTIME_T_TO_TIME_T (vl->time);
- event->has_time = 1;
-
- if (host->check_thresholds) {
- switch (status) {
- case STATE_OKAY:
- event->state = strdup("ok");
- break;
- case STATE_ERROR:
- event->state = strdup("critical");
- break;
- case STATE_WARNING:
- event->state = strdup("warning");
- break;
- case STATE_MISSING:
- event->state = strdup("unknown");
- break;
- }
- }
-
- ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
- event->ttl = (float) ttl;
- event->has_ttl = 1;
-
- riemann_event_add_attribute (event, "plugin", vl->plugin);
- if (vl->plugin_instance[0] != 0)
- riemann_event_add_attribute (event, "plugin_instance",
- vl->plugin_instance);
-
- riemann_event_add_attribute (event, "type", vl->type);
- if (vl->type_instance[0] != 0)
- riemann_event_add_attribute (event, "type_instance",
- vl->type_instance);
-
- if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
- {
- char ds_type[DATA_MAX_NAME_LEN];
-
- ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
- DS_TYPE_TO_STRING(ds->ds[index].type));
- riemann_event_add_attribute (event, "ds_type", ds_type);
- }
- else
- {
- riemann_event_add_attribute (event, "ds_type",
- DS_TYPE_TO_STRING(ds->ds[index].type));
- }
- riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
- {
- char ds_index[DATA_MAX_NAME_LEN];
-
- ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
- riemann_event_add_attribute (event, "ds_index", ds_index);
- }
-
- for (i = 0; i < riemann_attrs_num; i += 2)
- riemann_event_add_attribute(event,
- riemann_attrs[i],
- riemann_attrs[i +1]);
-
- for (i = 0; i < riemann_tags_num; i++)
- riemann_event_add_tag (event, riemann_tags[i]);
-
- if (ds->ds[index].type == DS_TYPE_GAUGE)
- {
- event->has_metric_d = 1;
- event->metric_d = (double) vl->values[index].gauge;
- }
- else if (rates != NULL)
- {
- event->has_metric_d = 1;
- event->metric_d = (double) rates[index];
- }
- else
- {
- event->has_metric_sint64 = 1;
- if (ds->ds[index].type == DS_TYPE_DERIVE)
- event->metric_sint64 = (int64_t) vl->values[index].derive;
- else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
- event->metric_sint64 = (int64_t) vl->values[index].absolute;
- else
- event->metric_sint64 = (int64_t) vl->values[index].counter;
- }
-
- format_name (name_buffer, sizeof (name_buffer),
- /* host = */ "", vl->plugin, vl->plugin_instance,
- vl->type, vl->type_instance);
- if (host->always_append_ds || (ds->ds_num > 1))
- {
- if (host->event_service_prefix == NULL)
- ssnprintf (service_buffer, sizeof (service_buffer), "%s/%s",
- &name_buffer[1], ds->ds[index].name);
- else
- ssnprintf (service_buffer, sizeof (service_buffer), "%s%s/%s",
- host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
- }
- else
- {
- if (host->event_service_prefix == NULL)
- sstrncpy (service_buffer, &name_buffer[1], sizeof (service_buffer));
- else
- ssnprintf (service_buffer, sizeof (service_buffer), "%s%s",
- host->event_service_prefix, &name_buffer[1]);
- }
-
- event->service = strdup (service_buffer);
-
- DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
- "host = \"%s\", service = \"%s\"",
- event->host, event->service);
- return (event);
-} /* }}} Event *riemann_value_to_protobuf */
-
-static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
- data_set_t const *ds,
- value_list_t const *vl,
- int *statuses)
-{
- Msg *msg;
- size_t i;
- gauge_t *rates = NULL;
-
- /* Initialize the Msg structure. */
- msg = malloc (sizeof (*msg));
- if (msg == NULL)
- {
- ERROR ("write_riemann plugin: malloc failed.");
- return (NULL);
- }
- memset (msg, 0, sizeof (*msg));
- msg__init (msg);
-
- /* Set up events. First, the list of pointers. */
- msg->n_events = (size_t) vl->values_len;
- msg->events = calloc (msg->n_events, sizeof (*msg->events));
- if (msg->events == NULL)
- {
- ERROR ("write_riemann plugin: calloc failed.");
- riemann_msg_protobuf_free (msg);
- return (NULL);
- }
-
- if (host->store_rates)
- {
- rates = uc_get_rate (ds, vl);
- if (rates == NULL)
- {
- ERROR ("write_riemann plugin: uc_get_rate failed.");
- riemann_msg_protobuf_free (msg);
- return (NULL);
- }
- }
-
- for (i = 0; i < msg->n_events; i++)
- {
- msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
- (int) i, rates, statuses[i]);
- if (msg->events[i] == NULL)
- {
- riemann_msg_protobuf_free (msg);
- sfree (rates);
- return (NULL);
- }
- }
-
- sfree (rates);
- return (msg);
-} /* }}} Msg *riemann_value_list_to_protobuf */