int main(int argc, char **argv) { riemann_message_t msg = RIEMANN_MSG_INIT; riemann_message_t *resp; riemann_query_t qry = RIEMANN_QUERY_INIT; riemann_client_t cli = RIEMANN_CLIENT_INIT; int i; if (argc != 3) { fprintf(stderr, "%s <IP> <PORT>\n", argv[0]); exit(EXIT_FAILURE); } riemann_query_set_string(&qry, "true"); riemann_message_set_query(&msg, &qry); riemann_client_connect(&cli, TCP, argv[1], atoi(argv[2])); riemann_client_send_message(&cli, &msg, 0, NULL); resp = riemann_client_recv_message(&cli, 0, NULL); if (!resp->ok) { fprintf(stderr, "Message error %s\n", resp->error); exit(EXIT_FAILURE); } else { puts("Sucess"); } for (i = 0; i < resp->n_events; i++) puts(resp->events[i]->service); riemann_message_free(resp); /* responses should be freed */ riemann_client_free(&cli); return 0; }
END_TEST START_TEST (test_riemann_simple_query) { riemann_client_t *client; riemann_message_t *response; ck_assert (riemann_query (NULL, "service = \"test-simple\"") == NULL); ck_assert_errno (-errno, ENOTCONN); client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); riemann_send (client, RIEMANN_EVENT_FIELD_SERVICE, "test-simple", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE); response = riemann_query (client, "service = \"test-simple\""); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); riemann_message_free (response); riemann_client_free (client); }
END_TEST START_TEST (test_riemann_client_free) { errno = 0; riemann_client_free (NULL); ck_assert_errno (-errno, EINVAL); }
/* host->lock must be held when calling this function. */ static int wrr_disconnect(struct riemann_host *host) /* {{{ */ { if (!host->client) return 0; riemann_client_free(host->client); host->client = NULL; return 0; } /* }}} int wrr_disconnect */
END_TEST START_TEST (test_riemann_client_get_fd) { ck_assert_errno (riemann_client_get_fd (NULL), EINVAL); if (network_tests_enabled ()) { riemann_client_t *client; client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); ck_assert (riemann_client_get_fd (client) != 0); riemann_client_free (client); } }
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; }
/* host->lock must be held when calling this function. */ static int wrr_connect(struct riemann_host *host) /* {{{ */ { char const *node; int port; if (host->client) return 0; node = (host->node != NULL) ? host->node : RIEMANN_HOST; port = (host->port) ? host->port : RIEMANN_PORT; host->client = NULL; host->client = riemann_client_create( host->client_type, node, port, RIEMANN_CLIENT_OPTION_TLS_CA_FILE, host->tls_ca_file, RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, host->tls_cert_file, RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, host->tls_key_file, RIEMANN_CLIENT_OPTION_NONE); if (host->client == NULL) { c_complain(LOG_ERR, &host->init_complaint, "write_riemann plugin: Unable to connect to Riemann at %s:%d", node, port); return -1; } #if RCC_VERSION_NUMBER >= 0x010800 if (host->timeout.tv_sec != 0) { if (riemann_client_set_timeout(host->client, &host->timeout) != 0) { riemann_client_free(host->client); host->client = NULL; c_complain(LOG_ERR, &host->init_complaint, "write_riemann plugin: Unable to connect to Riemann at %s:%d", node, port); return -1; } } #endif set_sock_opts(riemann_client_get_fd(host->client)); c_release(LOG_INFO, &host->init_complaint, "write_riemann plugin: Successfully connected to %s:%d", node, port); return 0; } /* }}} int wrr_connect */
int main(int argc, char **argv) { riemann_message_t msg = RIEMANN_MSG_INIT; riemann_message_t *resp; riemann_query_t qry = RIEMANN_QUERY_INIT; riemann_client_t cli; int i; int error; if (argc != 4) { fprintf(stderr, "%s <IP> <PORT> <FMT>\n", argv[0]); exit(EXIT_FAILURE); } riemann_query_set_string(&qry, "true"); riemann_message_set_query(&msg, &qry); riemann_client_init(&cli); riemann_client_connect(&cli, RIEMANN_TCP, argv[1], atoi(argv[2])); riemann_client_send_message(&cli, &msg, 0, NULL); resp = riemann_client_recv_message(&cli, 0, NULL); if (!resp->ok) { fprintf(stderr, "Message error %s\n", resp->error); exit(EXIT_FAILURE); } else { puts("Sucess"); } for (i = 0; i < resp->n_events; i++) { error = riemann_event_strfevent(buffer, BUFSIZ, argv[3], resp->events[i]); if (error) { fprintf(stderr, "riemann_event_strfevent() error\n"); exit(EXIT_FAILURE); } puts(buffer); } riemann_message_free(resp); /* responses should be freed */ riemann_client_free(&cli); return 0; }
END_TEST START_TEST (test_riemann_simple_communicate_event) { riemann_client_t *client; riemann_message_t *response; client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); response = riemann_communicate_event (client, RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate_event", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); ck_assert_int_eq (response->n_events, 0); riemann_message_free (response); response = riemann_communicate_event (client, 256, RIEMANN_EVENT_FIELD_NONE); ck_assert (response == NULL); ck_assert_errno (-errno, EPROTO); riemann_send (client, RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate_event", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE); response = riemann_communicate_event (client, RIEMANN_EVENT_FIELD_NONE); ck_assert (response != NULL); ck_assert (response->has_ok == 1); ck_assert (response->ok == 1); riemann_message_free (response); riemann_client_free (client); }
END_TEST START_TEST (test_riemann_simple_communicate_query) { riemann_client_t *client; riemann_message_t *response; client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); response = riemann_communicate_query (client, "true"); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); ck_assert (response->n_events > 0); riemann_message_free (response); riemann_client_disconnect (client); client = riemann_client_create (RIEMANN_CLIENT_UDP, "127.0.0.1", 5555); response = riemann_communicate_query (client, "true"); ck_assert (response == NULL); ck_assert_errno (-errno, ENOTSUP); riemann_client_disconnect (client); riemann_client_free (client); }
riemann_client_t * SYMVER(riemann_client_create) (riemann_client_type_t type, const char *hostname, int port, ...) { riemann_client_t *client; int e; va_list ap; client = riemann_client_new (); va_start (ap, port); e = riemann_client_connect_va (client, type, hostname, port, ap); if (e != 0) { riemann_client_free (client); va_end (ap); errno = -e; return NULL; } va_end (ap); return client; }
int main(int argc, char ** argv) { int ret; struct MHD_Daemon *daemon; int opt; riemann_client_t riemann_client; struct sockaddr_in localhost_addr; init_from_env(); while ((opt = getopt(argc, argv, "l:r:p:s:h:m:")) != -1) { switch (opt) { case 'l': listen_port = atoi(optarg); break; case 'r': riemann_host = strdup(optarg); break; case 'p': riemann_port = atoi(optarg); break; case 's': riemann_field_service = strdup(optarg); break; case 'h': riemann_field_host = strdup(optarg); break; case 'm': maintainance_page = strdup(optarg); break; default: print_help(); exit(EXIT_FAILURE); } } if (!listen_port || !riemann_host || !riemann_field_host) { if (!listen_port) { fprintf(stderr, "port to listen on is not specified (LISTEN_PORT or -l)\n"); } if (!riemann_host) { fprintf(stderr, "riemann host is not specified (RIEMANN_HOST or -r)\n"); } if (!riemann_field_host) { fprintf(stderr, "host field for riemann payload is not specified (RIEMANN_FIELD_HOST or -h)\n"); } print_help(); exit(EXIT_FAILURE); } if (maintainance_page) { int ret; int fd; struct stat stat; fd = open(maintainance_page, O_RDONLY); if (fd == -1) { fprintf(stderr, "Cannot open maintainance page '%s': strerror(%s)\n", maintainance_page, strerror(errno)); exit(EXIT_FAILURE); } ret = fstat(fd, &stat); if (ret != 0) { fprintf(stderr, "Cannot get size of maintainance page '%s': strerror(%s)\n", maintainance_page, strerror(errno)); close(fd); exit(EXIT_FAILURE); } body_len = stat.st_size; body = calloc(body_len, sizeof(char)); if (body == NULL) { fprintf(stderr, "Cannot allocate enough memory for content of maintainance page '%s'\n", maintainance_page); close(fd); exit(EXIT_FAILURE); } ret = read(fd, body, body_len); if (ret != body_len) { fprintf(stderr, "Error while reading maintainance page content '%s': strerror(%s)\n", maintainance_page, strerror(errno)); close(fd); exit(EXIT_FAILURE); } } ret = riemann_client_init(&riemann_client); if (ret) { fprintf(stderr, "Cannot initialize riemann_client: strerror(%s)\n", strerror(errno)); exit(EXIT_FAILURE); } ret = riemann_client_connect(&riemann_client, RIEMANN_TCP, riemann_host, riemann_port); if (ret) { fprintf(stderr, "Cannot connect: strerror(%s) gai_strerrror(%s)\n", strerror(errno), gai_strerror(ret)); exit(EXIT_FAILURE); } memset(&localhost_addr, 0, sizeof(struct sockaddr_in)); localhost_addr.sin_family = AF_INET; localhost_addr.sin_port = htons(listen_port); ret = inet_pton(AF_INET, "127.0.0.1", &localhost_addr.sin_addr); if (ret != 1) { fprintf(stderr, "Cannot construct address structure: strerror(%s)\n", strerror(errno)); exit(EXIT_FAILURE); } daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL, listen_port, NULL, NULL, &write_to_riemann, &riemann_client, MHD_OPTION_SOCK_ADDR, &localhost_addr, MHD_OPTION_END); if (daemon == NULL) { fprintf(stderr, "Cannot start HTTP daemon\n"); exit(EXIT_FAILURE); } wait(); MHD_stop_daemon(daemon); riemann_client_free(&riemann_client); return 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; }
END_TEST START_TEST (test_riemann_simple_communicate) { riemann_client_t *client, *dummy_client; riemann_message_t *message, *response; client = riemann_client_create (RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); ck_assert (riemann_communicate (NULL, NULL) == NULL); ck_assert_errno (-errno, ENOTCONN); ck_assert (riemann_communicate (client, NULL) == NULL); ck_assert_errno (-errno, EINVAL); ck_assert (riemann_communicate (NULL, message) == NULL); ck_assert_errno (-errno, ENOTCONN); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); dummy_client = riemann_client_new (); ck_assert (riemann_communicate (dummy_client, message) == NULL); ck_assert_errno (-errno, ENOTCONN); riemann_client_free (dummy_client); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); response = riemann_communicate (client, message); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); riemann_message_free (response); response = riemann_communicate (client, riemann_message_create_with_query (riemann_query_new ("true"))); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); ck_assert (response->n_events > 0); riemann_message_free (response); riemann_client_disconnect (client); riemann_client_connect (client, RIEMANN_CLIENT_UDP, "127.0.0.1", 5555); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); response = riemann_communicate (client, message); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); riemann_message_free (response); riemann_client_disconnect (client); riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate #2", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); riemann_message_set_query (message, riemann_query_new ("true")); response = riemann_communicate (client, message); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); ck_assert (response->n_events > 0); riemann_message_free (response); riemann_client_disconnect (client); riemann_client_connect (client, RIEMANN_CLIENT_UDP, "127.0.0.1", 5555); message = riemann_message_create_with_events (riemann_event_create (RIEMANN_EVENT_FIELD_HOST, "localhost", RIEMANN_EVENT_FIELD_SERVICE, "test_riemann_simple_communicate #2", RIEMANN_EVENT_FIELD_STATE, "ok", RIEMANN_EVENT_FIELD_NONE), NULL); riemann_message_set_query (message, riemann_query_new ("true")); response = riemann_communicate (client, message); ck_assert (response != NULL); ck_assert_int_eq (response->ok, 1); ck_assert (response->n_events == 0); riemann_message_free (response); riemann_client_disconnect (client); riemann_client_free (client); }
END_TEST START_TEST (test_riemann_client_connect) { riemann_client_t *client; client = riemann_client_new (); ck_assert_errno (riemann_client_connect (NULL, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555), EINVAL); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_NONE, "127.0.0.1", 5555), EINVAL); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TCP, NULL, 5555), EINVAL); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", -1), ERANGE); if (network_tests_enabled ()) { ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5559), ECONNREFUSED); ck_assert (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555) == 0); ck_assert_errno (riemann_client_disconnect (client), 0); ck_assert (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555, RIEMANN_CLIENT_OPTION_NONE) == 0); ck_assert_errno (riemann_client_disconnect (client), 0); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "non-existent.example.com", 5555), EADDRNOTAVAIL); mock (socket, mock_enosys_int_always_fail); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TCP, "127.0.0.1", 5555), ENOSYS); restore (socket); /** TLS tests **/ #if HAVE_GNUTLS ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TLS, "127.0.0.1", 5554, RIEMANN_CLIENT_OPTION_NONE), EINVAL); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TLS, "127.0.0.1", 5554, 256, RIEMANN_CLIENT_OPTION_NONE), EINVAL); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TLS, "127.0.0.1", 5554, RIEMANN_CLIENT_OPTION_TLS_CA_FILE, "tests/data/cacert.pem", RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, "tests/data/client.crt", RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, "tests/data/client.key", RIEMANN_CLIENT_OPTION_NONE), 0); riemann_client_disconnect (client); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TLS, "127.0.0.1", 5554, RIEMANN_CLIENT_OPTION_TLS_CA_FILE, "tests/data/cacert-invalid.pem", RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, "tests/data/client.crt", RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, "tests/data/client.key", RIEMANN_CLIENT_OPTION_NONE), EPROTO); ck_assert_errno (riemann_client_connect (client, RIEMANN_CLIENT_TLS, "127.0.0.1", 5555, RIEMANN_CLIENT_OPTION_TLS_CA_FILE, "tests/data/cacert.pem", RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, "tests/data/client.crt", RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, "tests/data/client.key", RIEMANN_CLIENT_OPTION_TLS_HANDSHAKE_TIMEOUT, 1000, RIEMANN_CLIENT_OPTION_NONE), EPROTO); #endif } ck_assert (client != NULL); riemann_client_free (client); }