Example #1
0
int main(int argc, char **argv)
{
        riemann_message_t msg = RIEMANN_MSG_INIT;
        riemann_event_t **events;
        size_t n_events = STATIC_ARRAY_SIZE(cpus);
        int i;
        int error;
        riemann_client_t cli = RIEMANN_CLIENT_INIT;

        if (argc != 3) {
                fprintf(stderr, "%s <IP> <PORT>\n", argv[0]);
                exit(EXIT_FAILURE);
        }

        events = riemann_event_alloc_events(n_events);
        assert(events);
        for (i = 0; i < n_events; i++) {
                events[i] = riemann_event_alloc_event();
                assert(events[i]);
                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_message_set_events(&msg, events, n_events);
        
        error = riemann_client_connect(&cli, UDP, argv[1], atoi(argv[2]));
        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);
        }

        riemann_events_free(events, n_events);
        riemann_client_free(&cli);

        return 0;
}
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 = &marker;
        *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;
}
Example #3
0
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;
}