END_TEST START_TEST (test_riemann_event_clone) { riemann_event_t *event, *clone; ck_assert (riemann_event_clone (NULL) == NULL); ck_assert_errno (-errno, EINVAL); event = riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test", RIEMANN_EVENT_FIELD_TIME, (int64_t) 1234, RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_DESCRIPTION, "something", RIEMANN_EVENT_FIELD_TAGS, "tag-1", "tag-2", NULL, RIEMANN_EVENT_FIELD_TTL, (float) 1, RIEMANN_EVENT_FIELD_ATTRIBUTES, riemann_attribute_create ("key-1", "value-1"), riemann_attribute_create ("key-2", "value-2"), NULL, RIEMANN_EVENT_FIELD_METRIC_S64, (int64_t) 12345, RIEMANN_EVENT_FIELD_NONE); clone = riemann_event_clone (event); ck_assert (clone != NULL); ck_assert (clone != event); ck_assert (clone->host != event->host); ck_assert_str_eq (clone->host, event->host); ck_assert (clone->service != event->service); ck_assert_str_eq (clone->service, event->service); ck_assert_int_eq (clone->time, event->time); ck_assert (clone->state != event->state); ck_assert_str_eq (clone->state, event->state); ck_assert (clone->description != event->description); ck_assert_str_eq (clone->description, event->description); ck_assert_int_eq (clone->metric_sint64, event->metric_sint64); ck_assert (clone->tags != NULL); ck_assert_int_eq (clone->n_tags, event->n_tags); ck_assert (clone->tags[0] != event->tags[0]); ck_assert_str_eq (clone->tags[0], event->tags[0]); ck_assert (clone->attributes != NULL); ck_assert_int_eq (clone->n_attributes, event->n_attributes); ck_assert (clone->attributes[0] != event->attributes[0]); ck_assert_str_eq (clone->attributes[0]->key, event->attributes[0]->key); riemann_event_free (event); riemann_event_free (clone); }
END_TEST START_TEST (test_riemann_event_set_one) { riemann_event_t *event; event = riemann_event_new (); ck_assert (riemann_event_set_one (event, TIME, (int64_t) 1234) == 0); ck_assert_int_eq (event->has_time, 1); ck_assert_int_eq (event->time, 1234); ck_assert (riemann_event_set_one (event, HOST, "localhost") == 0); ck_assert_str_eq (event->host, "localhost"); ck_assert (riemann_event_set_one (event, TAGS, "tag-1", "tag-2", NULL) == 0); ck_assert_str_eq (event->tags[0], "tag-1"); ck_assert_str_eq (event->tags[1], "tag-2"); ck_assert (riemann_event_set_one (event, HOST, "localhost2") == 0); ck_assert_str_eq (event->host, "localhost2"); ck_assert (riemann_event_set_one (event, TAGS, "tag-3", NULL) == 0); ck_assert_int_eq (event->n_tags, 1); ck_assert_str_eq (event->tags[0], "tag-3"); riemann_event_free (event); }
END_TEST START_TEST (test_riemann_event_free) { errno = 0; riemann_event_free (NULL); ck_assert_errno (-errno, EINVAL); }
void riemann_events_free(riemann_event_t **evts, size_t n_events) { int i; for (i = 0; i < n_events; i++) { riemann_event_free(evts[i]); xfree(evts[i]); } xfree(evts); }
END_TEST START_TEST (test_riemann_event_create) { riemann_event_t *event; event = riemann_event_create (RIEMANN_EVENT_EMPTY); ck_assert (event != NULL); riemann_event_free (event); event = riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test", RIEMANN_EVENT_FIELD_NONE); ck_assert (event != NULL); ck_assert_str_eq (event->host, "localhost"); ck_assert_str_eq (event->service, "test"); riemann_event_free (event); event = riemann_event_create (255); ck_assert (event == NULL); ck_assert_errno (-errno, EPROTO); }
END_TEST START_TEST (test_riemann_event_tag_add) { riemann_event_t *event; event = riemann_event_new (); ck_assert_errno (riemann_event_tag_add (NULL, NULL), EINVAL); ck_assert_errno (riemann_event_tag_add (event, NULL), EINVAL); ck_assert_errno (riemann_event_tag_add (event, "test-tag"), 0); ck_assert_int_eq (event->n_tags, 1); ck_assert_str_eq (event->tags[0], "test-tag"); riemann_event_free (event); }
END_TEST START_TEST (test_riemann_event_attribute_add) { riemann_event_t *event; event = riemann_event_new (); ck_assert_errno (riemann_event_attribute_add (NULL, NULL), EINVAL); ck_assert_errno (riemann_event_attribute_add (event, NULL), EINVAL); ck_assert_errno (riemann_event_attribute_add (event, riemann_attribute_create ("test-key", "value")), 0); ck_assert_int_eq (event->n_attributes, 1); ck_assert_str_eq (event->attributes[0]->key, "test-key"); riemann_event_free (event); }
static int write_to_riemann(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { int marker, ret; riemann_client_t *riemann_client = cls; if (*ptr != &marker || *upload_data_size != 0) { *ptr = ▮ *upload_data_size = 0; return MHD_YES; } { riemann_message_t msg = RIEMANN_MSG_INIT; riemann_event_t event = RIEMANN_EVENT_INIT; riemann_event_t *events[] = { NULL }; attribute_list_t *head, *cur; int header_count = 2, i; riemann_attribute_pairs_t *attributes; events[0] = &event; riemann_event_set_host(&event, riemann_field_host); riemann_event_set_service(&event, riemann_field_service); head = push_riemann_attribute_pair(NULL, "Method", method); if (head == NULL) { return MHD_NO; } cur = push_riemann_attribute_pair(head, "URL", url); if (cur == NULL) { free(head->attr); free(head); return MHD_NO; } header_count += MHD_get_connection_values(connection, MHD_HEADER_KIND, headers_to_attributes, &cur); attributes = calloc(header_count, sizeof(riemann_attribute_pairs_t)); if (attributes == NULL) { cur = head; while (cur) { free(cur->attr); head = cur; free(cur); cur = head->next; } return MHD_NO; } cur = head; for (i = 0; i < header_count && cur; ++i) { attributes[i].key = cur->attr->key; attributes[i].value = cur->attr->value; free(cur->attr); head = cur->next; free(cur); cur = head; } riemann_event_set_attributes(&event, attributes, header_count); riemann_message_set_events(&msg, events, 1); ret = riemann_client_send_message(riemann_client, &msg, 0, NULL); if (ret) { fprintf(stderr, "Cannot send message: %s\n", strerror(errno)); } fprintf(stderr, "Notfied riemann about '%s'\n", url); riemann_event_free(&event); free(attributes); } { struct MHD_Response *response; response = MHD_create_response_from_buffer(body_len, (void *)body, MHD_RESPMEM_PERSISTENT); if (response) { MHD_add_response_header(response, "Content-Type", "text/html"); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); } } (void)version; (void)upload_data; return ret; }
END_TEST START_TEST (test_riemann_event_set) { riemann_event_t *event; ck_assert_errno (riemann_event_set (NULL), EINVAL); event = riemann_event_new (); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_TIME, (int64_t) 1234, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert_int_eq (event->has_time, 1); ck_assert_int_eq (event->time, 1234); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (event->state != NULL); ck_assert_str_eq (event->state, "ok"); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_SERVICE, "test", RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (event->service != NULL); ck_assert_str_eq (event->service, "test"); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (event->host != NULL); ck_assert_str_eq (event->host, "localhost"); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_DESCRIPTION, "something", RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (event->description != NULL); ck_assert_str_eq (event->description, "something"); ck_assert_errno (riemann_event_set (event, RIEMANN_EVENT_FIELD_TAGS, "tag-1", "tag-2", NULL, RIEMANN_EVENT_FIELD_NONE), 0); ck_assert_int_eq (event->n_tags, 2); ck_assert_str_eq (event->tags[0], "tag-1"); ck_assert_str_eq (event->tags[1], "tag-2"); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_TTL, (float) 1, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert_int_eq (event->has_ttl, 1); ck_assert_float_eq (event->ttl, (float) 1); ck_assert_errno (riemann_event_set (event, RIEMANN_EVENT_FIELD_ATTRIBUTES, riemann_attribute_create ("key-1", "value-1"), riemann_attribute_create ("key-2", "value-2"), NULL, RIEMANN_EVENT_FIELD_NONE), 0); ck_assert_int_eq (event->n_attributes, 2); ck_assert_str_eq (event->attributes[0]->key, "key-1"); ck_assert_str_eq (event->attributes[1]->value, "value-2"); ck_assert_errno (riemann_event_set (event, RIEMANN_EVENT_FIELD_ATTRIBUTES, riemann_attribute_create ("key-3", "value-3"), NULL, RIEMANN_EVENT_FIELD_NONE), 0); ck_assert_int_eq (event->n_attributes, 1); ck_assert_str_eq (event->attributes[0]->key, "key-3"); ck_assert_str_eq (event->attributes[0]->value, "value-3"); ck_assert_errno (riemann_event_set (event, RIEMANN_EVENT_FIELD_STRING_ATTRIBUTES, "key-4", "value-4", "key-5", "value-5", NULL, RIEMANN_EVENT_FIELD_NONE), 0); ck_assert_int_eq (event->n_attributes, 2); ck_assert_str_eq (event->attributes[0]->key, "key-4"); ck_assert_str_eq (event->attributes[1]->value, "value-5"); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_METRIC_S64, (int64_t) 12345, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert_int_eq (event->has_metric_sint64, 1); ck_assert_int_eq (event->metric_sint64, 12345); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_METRIC_D, (double) 1.5, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert_int_eq (event->has_metric_d, 1); ck_assert_float_eq (event->metric_d, 1.5); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_METRIC_F, (float) 1.5, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert_int_eq (event->has_metric_f, 1); ck_assert_float_eq (event->metric_f, 1.5); ck_assert_errno (riemann_event_set (event, RIEMANN_EVENT_FIELD_METRIC_F * 2, 0, RIEMANN_EVENT_FIELD_NONE), EPROTO); ck_assert (riemann_event_set (event, RIEMANN_EVENT_FIELD_SERVICE, NULL, RIEMANN_EVENT_FIELD_NONE) == 0); ck_assert (event->service == NULL); riemann_event_free (event); }
int main(int argc, char **argv) { riemann_message_t msg = RIEMANN_MSG_INIT; riemann_message_t *resp = NULL; size_t n_events = STATIC_ARRAY_SIZE(cpus); riemann_event_t *events[n_events]; /* using stack space */ size_t n_attrs = STATIC_ARRAY_SIZE(attrs); int i; int error; riemann_client_t cli; if (argc != 3) { fprintf(stderr, "%s <IP> <PORT>\n", argv[0]); exit(EXIT_FAILURE); } for (i = 0; i < n_events; i++) { events[i] = alloca(sizeof (riemann_event_t)); riemann_event_init(events[i]); riemann_event_set_host(events[i], "gentoo-x86"); riemann_event_set_service(events[i], cpus[i]); riemann_event_set_metric_f(events[i], 100); /* 100% idle */ riemann_event_set_state(events[i], "ok"); riemann_event_set_tags(events[i], tags, STATIC_ARRAY_SIZE(tags)); riemann_event_set_attributes(events[i], attrs, STATIC_ARRAY_SIZE(attrs)); } riemann_message_set_events(&msg, events, n_events); error = riemann_client_init(&cli); if (error) { fprintf(stderr, "Can't initialize client: strerror(%s)\n", strerror(errno)); exit(EXIT_FAILURE); } error = riemann_client_connect(&cli, TCP, argv[1], atoi(argv[2])); /* functions that returns ints returns 0 on success */ if (error) { fprintf(stderr, "Can't connectd: strerror(%s) gai_strerrror(%s)\n", strerror(errno), gai_strerror(error)); exit(EXIT_FAILURE); } error = riemann_client_send_message(&cli, &msg, 0, NULL); if (error) { fprintf(stderr, "Can't send message: %s\n", strerror(errno)); exit(EXIT_FAILURE); } resp = riemann_client_recv_message(&cli, 0, NULL); /* functions that returns pointers rertuns NULL on failure */ assert(resp); if (!resp->ok) { fprintf(stderr, "Message error %s\n", resp->error); exit(EXIT_FAILURE); } else { puts("Sucess"); } for (i = 0; i < n_events; i++) /* freeing events fields */ riemann_event_free(events[i]); /* Since events are on * stack the may not be * freed. */ riemann_message_free(resp); /* responses should be freed */ riemann_client_free(&cli); return 0; }
static riemann_message_t * wrr_notification_to_message(struct riemann_host *host, /* {{{ */ notification_t const *n) { riemann_message_t *msg; riemann_event_t *event; char service_buffer[6 * DATA_MAX_NAME_LEN]; char const *severity; switch (n->severity) { case NOTIF_OKAY: severity = "ok"; break; case NOTIF_WARNING: severity = "warning"; break; case NOTIF_FAILURE: severity = "critical"; break; default: severity = "unknown"; } format_name(service_buffer, sizeof(service_buffer), /* host = */ "", n->plugin, n->plugin_instance, n->type, n->type_instance); event = riemann_event_create( RIEMANN_EVENT_FIELD_HOST, n->host, RIEMANN_EVENT_FIELD_TIME, (int64_t)CDTIME_T_TO_TIME_T(n->time), RIEMANN_EVENT_FIELD_TAGS, "notification", NULL, RIEMANN_EVENT_FIELD_STATE, severity, RIEMANN_EVENT_FIELD_SERVICE, &service_buffer[1], RIEMANN_EVENT_FIELD_NONE); #if RCC_VERSION_NUMBER >= 0x010A00 riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS, (int64_t)CDTIME_T_TO_US(n->time)); #endif if (n->host[0] != 0) riemann_event_string_attribute_add(event, "host", n->host); if (n->plugin[0] != 0) riemann_event_string_attribute_add(event, "plugin", n->plugin); if (n->plugin_instance[0] != 0) riemann_event_string_attribute_add(event, "plugin_instance", n->plugin_instance); if (n->type[0] != 0) riemann_event_string_attribute_add(event, "type", n->type); if (n->type_instance[0] != 0) riemann_event_string_attribute_add(event, "type_instance", n->type_instance); for (size_t i = 0; i < riemann_attrs_num; i += 2) riemann_event_string_attribute_add(event, riemann_attrs[i], riemann_attrs[i + 1]); for (size_t i = 0; i < riemann_tags_num; i++) riemann_event_tag_add(event, riemann_tags[i]); if (n->message[0] != 0) riemann_event_string_attribute_add(event, "description", n->message); /* Pull in values from threshold and add extra attributes */ for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) { if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) { riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, (double)meta->nm_value.nm_double, RIEMANN_EVENT_FIELD_NONE); continue; } if (meta->type == NM_TYPE_STRING) { riemann_event_string_attribute_add(event, meta->name, meta->nm_value.nm_string); continue; } } msg = riemann_message_create_with_events(event, NULL); if (msg == NULL) { ERROR("write_riemann plugin: riemann_message_create_with_events() failed."); riemann_event_free(event); return NULL; } DEBUG("write_riemann plugin: Successfully created message for notification: " "host = \"%s\", service = \"%s\", state = \"%s\"", event->host, event->service, event->state); return msg; } /* }}} riemann_message_t *wrr_notification_to_message */