Beispiel #1
0
void riemann_event_init(riemann_event_t *evt)
{
        event__init((Event *) evt);
}
Beispiel #2
0
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_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]);

	/* Pull in values from threshold */
	for (meta = n->meta; meta != NULL; meta = meta->next)
	{
		if (strcasecmp ("CurrentValue", meta->name) != 0)
			continue;

		event->metric_d = meta->nm_value.nm_double;
		event->has_metric_d = 1;
		break;
	}

	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 */
Beispiel #3
0
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)
{
	Event *event;
	char name_buffer[5 * DATA_MAX_NAME_LEN];
	char service_buffer[6 * DATA_MAX_NAME_LEN];
	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;
	event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
	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_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))
		ssnprintf (service_buffer, sizeof (service_buffer),
				"%s/%s", &name_buffer[1], ds->ds[index].name);
	else
		sstrncpy (service_buffer, &name_buffer[1],
				sizeof (service_buffer));

	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 */
Event *
create_riemann_event (const char *grid, const char *cluster, const char *host, const char *ip,
                      const char *metric, const char *value, const char *type, const char *units,
                      const char *state, unsigned int localtime, const char *tags_str,
                      const char *location, unsigned int ttl)
{
/*
  debug_msg("[riemann] grid=%s, cluster=%s, host=%s, ip=%s, metric=%s, value=%s %s, type=%s, state=%s, "
            "localtime=%u, tags=%s, location=%s, ttl=%u\n", grid, cluster, host, ip, metric, value,
            units, type, state, localtime, tags_str, location, ttl);
*/
  Event *event = malloc (sizeof (Event));
  event__init (event);

  event->host = strdup (host);
  event->service = strdup (metric);

  if (value) {
    if (!strcmp (type, "int")) {
      event->has_metric_sint64 = 1;
      event->metric_sint64 = strtol (value, (char **) NULL, 10);
    }
    else if (!strcmp (type, "float")) {
      event->has_metric_d = 1;
      event->metric_d = (double) strtod (value, (char **) NULL);
    }
    else {
      event->state = strdup (value);
    }
  }

  event->description = strdup (units);

  if (state)
    event->state = strdup (state);

  if (localtime)
    event->time = localtime;

  char *tags[64] = { NULL };

  event->n_tags = tokenize (tags_str, ",", tags);
  event->tags = malloc (sizeof (char *) * (event->n_tags));
  int j;
  for (j = 0; j< event->n_tags; j++) {
     event->tags[j] = strdup (tags[j]);
     free(tags[j]);
  }

  char attr_str[512];
  sprintf(attr_str, "grid=%s,cluster=%s,ip=%s,location=%s%s%s", grid, cluster, ip, location,
        gmetad_config.riemann_attributes ? "," : "",
        gmetad_config.riemann_attributes ? gmetad_config.riemann_attributes : "");

  char *kv[64] = { NULL };
  event->n_attributes = tokenize (attr_str, ",", kv);

  Attribute **attrs;
  attrs = malloc (sizeof (Attribute *) * (event->n_attributes));

  int i;
  for (i = 0; i < event->n_attributes; i++) {

    char *pair[2] = { NULL };
    tokenize (kv[i], "=", pair);
    free(kv[i]);

    attrs[i] = malloc (sizeof (Attribute));
    attribute__init (attrs[i]);
    attrs[i]->key = strdup(pair[0]);
    attrs[i]->value = strdup(pair[1]);
    free(pair[0]);
    free(pair[1]);
  }
  event->attributes = attrs;

  event->has_ttl = 1;
  event->ttl = ttl;
/*
  debug_msg("[riemann] %zu host=%s, service=%s, state=%s, metric_f=%f, metric_d=%lf, metric_sint64=%" PRId64
            ", description=%s, ttl=%f, tags(%zu), attributes(%zu)", event->time, event->host, event->service,
            event->state, event->metric_f, event->metric_d, event->metric_sint64, event->description,
            event->ttl, event->n_tags, event->n_attributes);
*/
  return event;
}