├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile.am ├── README.md ├── bootstrap ├── configure.ac ├── src ├── Makefile.am ├── client.c ├── client.h ├── cluster_admin.c ├── cluster_admin.h ├── database.c ├── database.h ├── database_user.c ├── database_user.h ├── influxdb.h ├── query.c ├── query.h ├── series.c ├── series.h ├── server.c ├── server.h ├── shard.c ├── shard.h ├── utils.c └── utils.h └── tests ├── Makefile.am ├── cluster_admin.c ├── database.c ├── database_user.c ├── server.c ├── tests.c └── tests.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Temporary files 2 | *~ 3 | .*.swp 4 | *.log 5 | 6 | # Autotools 7 | aclocal.m4 8 | autom4te.cache/ 9 | build-aux/ 10 | configure 11 | config.status 12 | libtool 13 | Makefile 14 | Makefile.in 15 | m4/ 16 | src/config.h 17 | src/config.h.in 18 | .deps/ 19 | .libs/ 20 | stamp-h1 21 | 22 | # Doxygen 23 | docs/ 24 | doxygen.config 25 | 26 | # Object files 27 | *.o 28 | *.lo 29 | *.ko 30 | *.obj 31 | *.elf 32 | 33 | # Libraries 34 | *.lib 35 | *.la 36 | *.a 37 | 38 | # Shared objects (inc. Windows DLLs) 39 | *.dll 40 | *.so 41 | *.so.* 42 | *.dylib 43 | 44 | # Executables 45 | *.exe 46 | *.out 47 | *.app 48 | *.i*86 49 | *.x86_64 50 | *.hex 51 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | compiler: 3 | - gcc 4 | - clang 5 | before_install: 6 | - autoconf --version 7 | - automake --version 8 | - sudo apt-get update 9 | - sudo apt-get install libcurl4-openssl-dev libjson0-dev 10 | - wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb 11 | - sudo useradd influxdb 12 | - sudo dpkg -i influxdb_latest_amd64.deb 13 | - travis_retry sudo service influxdb restart 14 | - sudo service influxdb status 15 | install: 16 | - ./bootstrap 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2014 InfluxDB 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I m4 2 | 3 | SUBDIRS = src tests 4 | 5 | MAINTAINERCLEANFILES = aclocal.m4 Makefile.in configure depcomp install-sh missing 6 | 7 | CLEANFILES = *~ \#* *.swp 8 | 9 | test: check 10 | 11 | doxygen.config: Makefile.in 12 | echo "PROJECT_NAME = libinfluxdb" > doxygen.config 13 | echo "INPUT = src/" >> doxygen.config 14 | echo "FILE_PATTERNS = *.h *.c" >> doxygen.config 15 | echo "OUTPUT_DIRECTORY = docs/" >> doxygen.config 16 | echo "OPTIMIZE_OUTPUT_FOR_C = YES" >> doxygen.config 17 | echo "EXTRACT_ALL = YES" >> doxygen.config 18 | 19 | docs: doxygen.config 20 | doxygen doxygen.config 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | influxdb-c 2 | ========== 3 | 4 | # This library is not updated for InfluxDB 0.9.0. There are breaking changes to the API, use at your own risk. 5 | 6 | We encourage you to submit a pull request if you have a contribution. If you make a PR please explicitly call @beckettsean to get eyes on your PR. 7 | 8 | ---------- 9 | 10 | A C89 library to play with the REST API of an InfluxDB database. 11 | 12 | This implementation is meant as a C rewrite of the influxdb-go package. 13 | 14 | ## Typical usages 15 | 16 | ### Create a new database 17 | 18 | ```c 19 | #include 20 | 21 | int main() { 22 | int status; 23 | s_influxdb_client *client = influxdb_client_new("localhost:8086", "root", "root", "", 0); 24 | 25 | status = influxdb_create_database(client, "toto"); 26 | 27 | influxdb_client_free(client); 28 | 29 | return status != 201; 30 | } 31 | ``` 32 | 33 | ```sh 34 | gcc -I/usr/local/include/ -L/usr/local/lib -linfluxdb -o test1 test1.c 35 | ./test1 36 | ``` 37 | 38 | ### List users of toto database 39 | 40 | ```c 41 | #include 42 | #include 43 | 44 | int main() { 45 | s_influxdb_client *client = influxdb_client_new("localhost:8086", "root", "root", "", 0); 46 | char **users_list; 47 | size_t nb = influxdb_get_database_user_list(client, "toto", &users_list); 48 | size_t i; 49 | 50 | printf("There are %lu users:\n", nb); 51 | for (i = 0; i < nb; i++) { 52 | printf(" - %s\n", users_list[i]); 53 | free(users_list[i]); 54 | } 55 | 56 | free(users_list); 57 | 58 | return 0; 59 | } 60 | ``` 61 | 62 | ```sh 63 | gcc -I/usr/local/include/ -L/usr/local/lib -linfluxdb -o test2 test2.c 64 | ./test2 65 | ``` 66 | 67 | ### Send data 68 | 69 | ```c 70 | #include 71 | #include 72 | 73 | int main() { 74 | s_influxdb_client *client = influxdb_client_new("localhost:8086", "root", "root", "toto", 0); 75 | int status; 76 | s_influxdb_series *series = influxdb_series_create("my_metrics", NULL); 77 | char **tab1 = malloc(sizeof (char *) * 4); 78 | char **tab2 = malloc(sizeof (char *) * 4); 79 | 80 | influxdb_series_add_colums(series, "col1"); 81 | influxdb_series_add_colums(series, "col2"); 82 | influxdb_series_add_colums(series, "col3"); 83 | influxdb_series_add_colums(series, "col4"); 84 | 85 | tab1[0] = strdup("4"); tab1[1] = strdup("1"); tab1[2] = strdup("3"); tab1[3] = strdup("2"); 86 | influxdb_series_add_points(series, tab1); 87 | 88 | tab2[0] = strdup("0"); tab2[1] = strdup("2"); tab2[2] = strdup("1"); tab2[3] = strdup("3"); 89 | influxdb_series_add_points(series, tab2); 90 | 91 | status = influxdb_write_serie(client, series); 92 | 93 | influxdb_series_free(series, NULL); 94 | 95 | return status != 200; 96 | } 97 | ``` 98 | 99 | ```sh 100 | gcc -I/usr/local/include/ -L/usr/local/lib -linfluxdb -o test3 test3.c 101 | ./test3 102 | ``` 103 | 104 | ## Build 105 | 106 | Requirements: 107 | * A compiler supporting C89 (tested with GCC 4.8.2 and Clang 3.3); 108 | * Autotools & libtool; 109 | * [libcurl](http://libcurl.org/); 110 | * [libjson](https://github.com/json-c/json-c); 111 | 112 | Steps to build and install the library: 113 | ```sh 114 | ./bootstrap 115 | ./configure 116 | make 117 | sudo make install 118 | ``` 119 | 120 | That will install the `libinfluxdb.so` into `/usr/local/lib/` and headers into `/usr/local/include/influxdb/`. 121 | 122 | ## Documentation 123 | 124 | Generate documentation with: 125 | 126 | ```sh 127 | make docs 128 | ``` 129 | 130 | Then, you will find HTML and LaTeX in `docs` directory. 131 | -------------------------------------------------------------------------------- /bootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd `dirname $0` 4 | 5 | mkdir -p m4/ 6 | 7 | cd src 8 | autoheader 9 | cd .. 10 | 11 | autoreconf -fvi 12 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.67]) 5 | AC_INIT([influxdb-c], [1.0], [influxdb@googlegroups.com]) 6 | 7 | AC_CONFIG_AUX_DIR([build-aux]) 8 | AC_CONFIG_MACRO_DIR([m4]) 9 | AC_CONFIG_SRCDIR([src/]) 10 | AC_CONFIG_HEADERS([src/config.h]) 11 | 12 | # Automake. 13 | AM_INIT_AUTOMAKE([1.11.1 dist-bzip2 no-dist-gzip foreign 14 | color-tests parallel-tests 15 | nostdinc silent-rules -Wall]) 16 | AM_SILENT_RULES([yes]) 17 | 18 | # Checks for programs. 19 | AC_PROG_CC 20 | 21 | # Checks for libraries. 22 | PKG_CHECK_MODULES([curl], [libcurl],, 23 | [AC_MSG_ERROR([No pkg-config for libcurl])]) 24 | AC_SUBST(CURL_CFLAGS) 25 | AC_SUBST(CURL_LIBS) 26 | 27 | AC_CHECK_LIB([curl], [curl_easy_init],,[AC_MSG_ERROR([libcurl required!])]) 28 | AC_CHECK_LIB([curl], [curl_easy_escape],,[AC_MSG_ERROR([libcurl required!])]) 29 | AC_CHECK_LIB([curl], [curl_easy_setopt],,[AC_MSG_ERROR([libcurl required!])]) 30 | AC_CHECK_LIB([curl], [curl_easy_cleanup],,[AC_MSG_ERROR([libcurl required!])]) 31 | AC_CHECK_LIB([curl], [curl_free],,[AC_MSG_ERROR([libcurl required!])]) 32 | 33 | AC_SUBST(CURL_CFLAGS) 34 | AC_SUBST(CURL_LIBS) 35 | 36 | 37 | JSON_LIBS= 38 | PKG_CHECK_MODULES(JSON, json-c,, [AC_MSG_WARN("json-c not found")]) 39 | 40 | if test "$JSON_LIBS" == ""; then 41 | PKG_CHECK_MODULES(JSON, json, 42 | [AC_DEFINE([HAVE_JSON_0],[1],[Use JSON0])], 43 | [AC_MSG_ERROR("json0 missing")]) 44 | fi 45 | 46 | AC_SUBST(JSON_CFLAGS) 47 | AC_SUBST(JSON_LIBS) 48 | 49 | # Checks for header files. 50 | AC_CHECK_HEADERS([stddef.h stdint.h stdio.h stdlib.h string.h]) 51 | 52 | # Checks for typedefs, structures, and compiler characteristics. 53 | AC_TYPE_SIZE_T 54 | AC_TYPE_INT64_T 55 | AC_TYPE_UINT32_T 56 | 57 | # Checks for library functions. 58 | AC_FUNC_MALLOC 59 | AC_FUNC_REALLOC 60 | AC_CHECK_FUNCS([calloc strchr strcpy strncat sprintf]) 61 | 62 | AM_PROG_AR 63 | LT_INIT 64 | 65 | AC_OUTPUT( Makefile src/Makefile tests/Makefile ) 66 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = libinfluxdb.la 2 | 3 | include_HEADERS = client.h cluster_admin.h config.h database.h database_user.h influxdb.h query.h series.h server.h shard.h utils.h 4 | 5 | MAINTAINERCLEANFILES = Makefile.in 6 | 7 | libinfluxdb_la_SOURCES = client.c cluster_admin.c database.c database_user.c query.c series.c server.c shard.c utils.c 8 | 9 | LIBS = $(CURL_LIBS) $(JSON_LIBS) 10 | AM_CPPFLAGS = $(CURL_CFLAGS) $(JSON_CFLAGS) 11 | 12 | includedir = $(prefix)/include/influxdb 13 | -------------------------------------------------------------------------------- /src/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "client.h" 14 | #include "utils.h" 15 | 16 | s_influxdb_client 17 | *influxdb_client_new(char *host, 18 | char *username, 19 | char *password, 20 | char *database, 21 | char ssl) 22 | { 23 | s_influxdb_client *client = malloc(sizeof (s_influxdb_client)); 24 | 25 | client->schema = ssl ? "https" : "http"; 26 | client->host = influxdb_strdup(host); 27 | client->username = influxdb_strdup(username); 28 | client->password = influxdb_strdup(password); 29 | client->database = curl_easy_escape(NULL, database, 0); 30 | client->ssl = ssl; 31 | 32 | return client; 33 | } 34 | 35 | /** 36 | * Forge real URL to the API using given client config and parameters 37 | */ 38 | int 39 | influxdb_client_get_url_with_credential(s_influxdb_client *client, 40 | char (*buffer)[], 41 | size_t size, 42 | char *path, 43 | char *username, 44 | char *password) 45 | { 46 | char *username_enc = curl_easy_escape(NULL, username, 0); 47 | char *password_enc = curl_easy_escape(NULL, password, 0); 48 | 49 | (*buffer)[0] = '\0'; 50 | strncat(*buffer, client->schema, size); 51 | strncat(*buffer, "://", size); 52 | strncat(*buffer, client->host, size); 53 | strncat(*buffer, path, size); 54 | 55 | if (strchr(path, '?')) 56 | strncat(*buffer, "&", size); 57 | else 58 | strncat(*buffer, "?", size); 59 | 60 | strncat(*buffer, "u=", size); 61 | strncat(*buffer, username_enc, size); 62 | strncat(*buffer, "&p=", size); 63 | strncat(*buffer, password_enc, size); 64 | 65 | free(username_enc); 66 | free(password_enc); 67 | 68 | return strlen(*buffer); 69 | } 70 | 71 | /** 72 | * Forge real URL to the API using only given client config 73 | */ 74 | int 75 | influxdb_client_get_url(s_influxdb_client *client, 76 | char (*buffer)[], 77 | size_t size, 78 | char *path) 79 | { 80 | return influxdb_client_get_url_with_credential(client, buffer, size, path, 81 | client->username, 82 | client->password); 83 | } 84 | 85 | /** 86 | * CURL Callback reading data to userdata buffer 87 | */ 88 | size_t 89 | influxdb_client_write_data(char *buf, 90 | size_t size, 91 | size_t nmemb, 92 | void *userdata) 93 | { 94 | size_t realsize = size * nmemb; 95 | if (userdata != NULL) 96 | { 97 | char **buffer = userdata; 98 | 99 | *buffer = realloc(*buffer, strlen(*buffer) + realsize + 1); 100 | 101 | strncat(*buffer, buf, realsize); 102 | } 103 | return realsize; 104 | } 105 | 106 | /** 107 | * Low level function performing real HTTP request 108 | */ 109 | int 110 | influxdb_client_curl(char *url, 111 | char *reqtype, 112 | json_object *body, 113 | char **response) 114 | { 115 | CURLcode c; 116 | CURL *handle = curl_easy_init(); 117 | 118 | if (reqtype != NULL) 119 | curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, reqtype); 120 | curl_easy_setopt(handle, CURLOPT_URL, url); 121 | curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, 122 | influxdb_client_write_data); 123 | curl_easy_setopt(handle, CURLOPT_WRITEDATA, response); 124 | if (body != NULL) 125 | curl_easy_setopt(handle, CURLOPT_POSTFIELDS, 126 | json_object_to_json_string(body)); 127 | 128 | c = curl_easy_perform(handle); 129 | 130 | if (c == CURLE_OK) 131 | { 132 | long status_code = 0; 133 | if (curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, 134 | &status_code) == CURLE_OK) 135 | c = status_code; 136 | } 137 | 138 | curl_easy_cleanup(handle); 139 | 140 | return c; 141 | } 142 | 143 | int 144 | influxdb_client_delete(s_influxdb_client *client, 145 | char *path, 146 | json_object *body) 147 | { 148 | char url[INFLUXDB_URL_MAX_SIZE]; 149 | 150 | influxdb_client_get_url(client, &url, INFLUXDB_URL_MAX_SIZE, path); 151 | 152 | return influxdb_client_curl(url, "DELETE", body, NULL); 153 | } 154 | 155 | int 156 | influxdb_client_get(s_influxdb_client *client, char *path, json_object **res) 157 | { 158 | int status; 159 | char url[INFLUXDB_URL_MAX_SIZE]; 160 | char *buffer = calloc(1, sizeof (char)); 161 | 162 | influxdb_client_get_url(client, &url, INFLUXDB_URL_MAX_SIZE, path); 163 | 164 | status = influxdb_client_curl(url, NULL, NULL, &buffer); 165 | 166 | if (status >= 200 && status < 300 && res != NULL) 167 | *res = json_tokener_parse(buffer); 168 | 169 | free(buffer); 170 | 171 | return status; 172 | } 173 | 174 | int 175 | influxdb_client_post(s_influxdb_client *client, 176 | char *path, 177 | json_object *body, 178 | json_object **res) 179 | { 180 | int status; 181 | char url[INFLUXDB_URL_MAX_SIZE]; 182 | char *buffer = calloc(1, sizeof (char)); 183 | 184 | influxdb_client_get_url(client, &url, INFLUXDB_URL_MAX_SIZE, path); 185 | 186 | status = influxdb_client_curl(url, NULL, body, &buffer); 187 | 188 | if (status >= 200 && status < 300 && res != NULL) 189 | *res = json_tokener_parse(buffer); 190 | 191 | free(buffer); 192 | 193 | return status; 194 | } 195 | 196 | size_t 197 | influxdb_client_list_something(s_influxdb_client *client, 198 | char *path, 199 | void ***list, 200 | influxdb_client_object_extract extractor) 201 | { 202 | json_object *jo = NULL; 203 | size_t len, i; 204 | 205 | if (influxdb_client_get(client, path, &jo) != 200) 206 | return 0; 207 | 208 | len = json_object_array_length(jo); 209 | 210 | if (list != NULL) 211 | { 212 | *list = malloc(sizeof (char *) * (len + 1)); 213 | 214 | for (i = 0; i < len; i++) { 215 | (*list)[i] = extractor(json_object_array_get_idx(jo, i)); 216 | } 217 | } 218 | 219 | json_object_put(jo); 220 | 221 | return len; 222 | } 223 | 224 | void 225 | influxdb_client_free(s_influxdb_client *client) 226 | { 227 | if (client) 228 | { 229 | curl_free(client->username); 230 | curl_free(client->password); 231 | curl_free(client->database); 232 | free(client->host); 233 | free(client); 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /src/client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef CLIENT_H_ 11 | # define CLIENT_H_ 12 | 13 | # include 14 | 15 | # include "config.h" 16 | 17 | # ifdef HAVE_JSON_0 18 | # include 19 | # else 20 | # include 21 | # endif 22 | 23 | # define INFLUXDB_URL_MAX_SIZE 1024 24 | 25 | typedef struct influxdb_client { 26 | char *schema; 27 | char *host; 28 | char *username; 29 | char *password; 30 | char *database; 31 | char ssl; 32 | } s_influxdb_client; 33 | 34 | /** 35 | * Initialize a client struct with given parameters 36 | * 37 | * \return An initialized client struct 38 | */ 39 | s_influxdb_client *influxdb_client_new(char *host, 40 | char *username, 41 | char *password, 42 | char *database, 43 | char ssl); 44 | 45 | /** 46 | * Clean memory used by a client 47 | */ 48 | void influxdb_client_free(s_influxdb_client *client); 49 | 50 | /** 51 | * Perform a DELETE request to API 52 | * 53 | * \param client A initialized client 54 | * \param path Relative path from API root 55 | * \param body JSON sent as request content, can be NULL 56 | * \return HTTP status code or CURLcode (if < 100) 57 | */ 58 | int influxdb_client_delete(s_influxdb_client *client, 59 | char *path, 60 | json_object *body); 61 | 62 | /** 63 | * Perform a GET request to API 64 | * 65 | * \param client A initialized client 66 | * \param path Relative path from API root 67 | * \param res Parsed JSON response, can be NULL 68 | * \return HTTP status code or CURLcode (if < 100) 69 | */ 70 | int influxdb_client_get(s_influxdb_client *client, 71 | char *path, 72 | json_object **res); 73 | 74 | /** 75 | * Perform a POST request to API 76 | * 77 | * \param client A initialized client 78 | * \param path Relative path from API root 79 | * \param body JSON sent as request content, can be NULL 80 | * \param res Parsed JSON response, can be NULL 81 | * \return HTTP status code or CURLcode (if < 100) 82 | */ 83 | int influxdb_client_post(s_influxdb_client *client, 84 | char *path, 85 | json_object *body, 86 | json_object **res); 87 | 88 | typedef void *(*influxdb_client_object_extract)(json_object *obj); 89 | 90 | /** 91 | * Return a list of elements 92 | * 93 | * \param client A initialized client 94 | * \param path Relative path from API root 95 | * \param db_list The non-malloced list where store response, can be NULL (just count) 96 | * \return The list size 97 | */ 98 | size_t influxdb_client_list_something(s_influxdb_client *client, 99 | char *path, 100 | void ***list, 101 | influxdb_client_object_extract extractor); 102 | 103 | #endif /* !CLIENT_H_ */ 104 | -------------------------------------------------------------------------------- /src/cluster_admin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "cluster_admin.h" 13 | #include "utils.h" 14 | 15 | int influxdb_create_cluster_admin(s_influxdb_client *client, 16 | char *name, 17 | char *password) 18 | { 19 | int c; 20 | json_object *jo = json_object_new_object(); 21 | json_object_object_add(jo, "name", json_object_new_string(name)); 22 | json_object_object_add(jo, "password", json_object_new_string(password)); 23 | 24 | c = influxdb_client_post(client, "/cluster_admins", jo, NULL); 25 | 26 | json_object_put(jo); 27 | 28 | return c; 29 | } 30 | 31 | int influxdb_update_cluster_admin(s_influxdb_client *client, 32 | char *name, char *password) 33 | { 34 | int c; 35 | char path[INFLUXDB_URL_MAX_SIZE]; 36 | json_object *jo = json_object_new_object(); 37 | json_object_object_add(jo, "password", json_object_new_string(password)); 38 | 39 | path[0] = '\0'; 40 | strncat(path, "/cluster_admins/", INFLUXDB_URL_MAX_SIZE); 41 | strncat(path, name, INFLUXDB_URL_MAX_SIZE); 42 | 43 | c = influxdb_client_post(client, path, jo, NULL); 44 | 45 | json_object_put(jo); 46 | 47 | return c; 48 | } 49 | 50 | int 51 | influxdb_change_cluster_admin_password(s_influxdb_client *client, 52 | char *name, 53 | char *newPassword) 54 | { 55 | return influxdb_update_cluster_admin(client, name, newPassword); 56 | } 57 | 58 | int influxdb_delete_cluster_admin(s_influxdb_client *client, 59 | char *name) 60 | { 61 | int c; 62 | char path[INFLUXDB_URL_MAX_SIZE]; 63 | json_object *jo = json_object_new_object(); 64 | json_object_object_add(jo, "name", json_object_new_string(name)); 65 | 66 | path[0] = '\0'; 67 | strncat(path, "/cluster_admins/", INFLUXDB_URL_MAX_SIZE); 68 | strncat(path, name, INFLUXDB_URL_MAX_SIZE); 69 | 70 | c = influxdb_client_delete(client, path, NULL); 71 | 72 | json_object_put(jo); 73 | 74 | return c; 75 | } 76 | 77 | void 78 | *influxdb_cluster_admin_extractor(json_object *obj) 79 | { 80 | return influxdb_strdup( 81 | json_object_get_string( 82 | json_object_object_get(obj, "name") 83 | )); 84 | } 85 | 86 | size_t influxdb_get_cluster_admin_list(s_influxdb_client *client, 87 | char ***cluster_admin_list) 88 | { 89 | return influxdb_client_list_something(client, "/cluster_admins", 90 | (void ***) cluster_admin_list, 91 | &influxdb_cluster_admin_extractor); 92 | } 93 | -------------------------------------------------------------------------------- /src/cluster_admin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef CLUSTER_ADMIN_H_ 11 | # define CLUSTER_ADMIN_H_ 12 | 13 | # include "client.h" 14 | 15 | /** 16 | * Create a new cluster admin 17 | * 18 | * \param client A initialized client 19 | * \param name The admin name 20 | * \param password The admin password 21 | * \return HTTP status code or CURLcode (if < 100) 22 | */ 23 | int influxdb_create_cluster_admin(s_influxdb_client *client, 24 | char *name, 25 | char *password); 26 | 27 | /** 28 | * Update a cluster admin password 29 | * 30 | * \param client A initialized client 31 | * \param name The admin name 32 | * \param password The new admin password to use 33 | * \return HTTP status code or CURLcode (if < 100) 34 | */ 35 | int influxdb_update_cluster_admin(s_influxdb_client *client, 36 | char *name, 37 | char *newPassword); 38 | int influxdb_change_cluster_admin_password(s_influxdb_client *client, 39 | char *name, 40 | char *newPassword); 41 | 42 | /** 43 | * Remove a cluster admin 44 | * 45 | * \param client A initialized client 46 | * \param name The admin name 47 | * \return HTTP status code or CURLcode (if < 100) 48 | */ 49 | int influxdb_delete_cluster_admin(s_influxdb_client *client, 50 | char *name); 51 | 52 | /** 53 | * List existing cluster admins 54 | * 55 | * \param client A initialized client 56 | * \param cluster_admin_list The list to fill with cluster admin names 57 | * \return The number of cluster admins 58 | */ 59 | size_t influxdb_get_cluster_admin_list(s_influxdb_client *client, 60 | char ***cluster_admin_list); 61 | 62 | #endif /* !CLUSTER_ADMIN_H_ */ 63 | -------------------------------------------------------------------------------- /src/database.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "database.h" 13 | #include "utils.h" 14 | 15 | int 16 | influxdb_create_database(s_influxdb_client *client, char *database_name) 17 | { 18 | int c; 19 | json_object *jo = json_object_new_object(); 20 | json_object_object_add(jo, "name", json_object_new_string(database_name)); 21 | 22 | c = influxdb_client_post(client, "/db", jo, NULL); 23 | 24 | json_object_put(jo); 25 | 26 | return c; 27 | } 28 | 29 | int 30 | influxdb_delete_database(s_influxdb_client *client, char *database_name) 31 | { 32 | int c; 33 | char path[INFLUXDB_URL_MAX_SIZE]; 34 | json_object *jo = json_object_new_object(); 35 | json_object_object_add(jo, "name", json_object_new_string(database_name)); 36 | 37 | path[0] = '\0'; 38 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 39 | strncat(path, database_name, INFLUXDB_URL_MAX_SIZE); 40 | 41 | c = influxdb_client_delete(client, path, NULL); 42 | 43 | json_object_put(jo); 44 | 45 | return c; 46 | } 47 | 48 | void 49 | *influxdb_database_extractor(json_object *obj) 50 | { 51 | return influxdb_strdup( 52 | json_object_get_string( 53 | json_object_object_get(obj, "name") 54 | )); 55 | } 56 | 57 | size_t 58 | influxdb_get_database_list(s_influxdb_client *client, char ***databases_list) 59 | { 60 | return influxdb_client_list_something(client, "/db", 61 | (void ***) databases_list, 62 | &influxdb_database_extractor); 63 | } 64 | -------------------------------------------------------------------------------- /src/database.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef DATABASE_H_ 11 | # define DATABASE_H_ 12 | 13 | # include "client.h" 14 | 15 | /** 16 | * Create a new database. 17 | * 18 | * \param client A initialized client 19 | * \param database_name The database name to create 20 | * \return HTTP status code or CURLcode (if < 100) 21 | */ 22 | int influxdb_create_database(s_influxdb_client *client, 23 | char *database_name); 24 | 25 | /** 26 | * Delete an existing database 27 | * 28 | * \param client A initialized client 29 | * \param database_name The database name to delete 30 | * \return HTTP status code or CURLcode (if < 100) 31 | */ 32 | int influxdb_delete_database(s_influxdb_client *client, 33 | char *database_name); 34 | 35 | /** 36 | * List existing databases 37 | * 38 | * \param client A initialized client 39 | * \param database_list The list to fill with database names 40 | * \return The number of databases 41 | */ 42 | size_t influxdb_get_database_list(s_influxdb_client *client, 43 | char ***databases_list); 44 | 45 | #endif /* !DATABASE_H_ */ 46 | -------------------------------------------------------------------------------- /src/database_user.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "database_user.h" 13 | #include "utils.h" 14 | 15 | int 16 | influxdb_create_database_user(s_influxdb_client *client, char *database, 17 | char *name, char *password, 18 | char *r_perm, char *w_perm) 19 | { 20 | int c; 21 | char path[INFLUXDB_URL_MAX_SIZE]; 22 | json_object *jo = json_object_new_object(); 23 | json_object_object_add(jo, "name", json_object_new_string(name)); 24 | json_object_object_add(jo, "password", json_object_new_string(password)); 25 | json_object_object_add(jo, "readFrom", json_object_new_string(r_perm?r_perm:".*")); 26 | json_object_object_add(jo, "writeTo", json_object_new_string(w_perm?w_perm:".*")); 27 | 28 | path[0] = '\0'; 29 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 30 | strncat(path, database, INFLUXDB_URL_MAX_SIZE); 31 | strncat(path, "/users", INFLUXDB_URL_MAX_SIZE); 32 | 33 | c = influxdb_client_post(client, path, jo, NULL); 34 | 35 | json_object_put(jo); 36 | 37 | return c; 38 | } 39 | 40 | int 41 | influxdb_change_database_user_common(s_influxdb_client *client, char *database, 42 | char *name, char *password, char isAdmin, 43 | char *r_perm, char *w_perm) 44 | { 45 | int c; 46 | char path[INFLUXDB_URL_MAX_SIZE]; 47 | json_object *jo = json_object_new_object(); 48 | if (password) 49 | json_object_object_add(jo, "password", json_object_new_string(password)); 50 | if (isAdmin >= 0) 51 | json_object_object_add(jo, "admin", json_object_new_boolean(isAdmin)); 52 | if (r_perm) 53 | json_object_object_add(jo, "readFrom", json_object_new_string(r_perm)); 54 | if (w_perm) 55 | json_object_object_add(jo, "writeTo", json_object_new_string(w_perm)); 56 | 57 | path[0] = '\0'; 58 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 59 | strncat(path, database, INFLUXDB_URL_MAX_SIZE); 60 | strncat(path, "/users/", INFLUXDB_URL_MAX_SIZE); 61 | strncat(path, name, INFLUXDB_URL_MAX_SIZE); 62 | 63 | c = influxdb_client_post(client, path, jo, NULL); 64 | 65 | json_object_put(jo); 66 | 67 | return c; 68 | } 69 | 70 | int 71 | influxdb_change_database_user(s_influxdb_client *client, char *database, 72 | char *name, char *newPassword, char isAdmin, 73 | char *new_r_perm, char *new_w_perm) 74 | { 75 | return influxdb_change_database_user_common(client, database, name, 76 | newPassword, isAdmin, 77 | new_r_perm, new_w_perm); 78 | } 79 | 80 | 81 | int 82 | influxdb_update_database_user(s_influxdb_client *client, char *database, 83 | char *name, char *password) 84 | { 85 | return influxdb_change_database_user_common(client, database, name, 86 | password, -1, NULL, NULL); 87 | } 88 | 89 | int influxdb_update_database_user_permissions(s_influxdb_client *client, 90 | char *database, char *name, 91 | char *r_perm, char *w_perm) 92 | { 93 | return influxdb_change_database_user_common(client, database, name, 94 | NULL, -1, r_perm, w_perm); 95 | } 96 | 97 | int influxdb_alter_database_privilege(s_influxdb_client *client, char *database, 98 | char *name, char isAdmin, 99 | char *r_perm, char *w_perm) 100 | { 101 | return influxdb_change_database_user_common(client, database, name, 102 | NULL, isAdmin, r_perm, w_perm); 103 | } 104 | 105 | int 106 | influxdb_delete_database_user(s_influxdb_client *client, char *database, 107 | char *name) 108 | { 109 | int c; 110 | char path[INFLUXDB_URL_MAX_SIZE]; 111 | json_object *jo = json_object_new_object(); 112 | json_object_object_add(jo, "name", json_object_new_string(database)); 113 | 114 | 115 | path[0] = '\0'; 116 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 117 | strncat(path, database, INFLUXDB_URL_MAX_SIZE); 118 | strncat(path, "/users/", INFLUXDB_URL_MAX_SIZE); 119 | strncat(path, name, INFLUXDB_URL_MAX_SIZE); 120 | 121 | c = influxdb_client_delete(client, path, NULL); 122 | 123 | json_object_put(jo); 124 | 125 | return c; 126 | } 127 | 128 | void 129 | *influxdb_dbusers_extractor(json_object *obj) 130 | { 131 | return influxdb_strdup( 132 | json_object_get_string( 133 | json_object_object_get(obj, "name") 134 | )); 135 | } 136 | 137 | size_t 138 | influxdb_get_database_user_list(s_influxdb_client *client, char *database, 139 | char ***users_list) 140 | { 141 | char path[INFLUXDB_URL_MAX_SIZE]; 142 | 143 | path[0] = '\0'; 144 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 145 | strncat(path, database, INFLUXDB_URL_MAX_SIZE); 146 | strncat(path, "/users", INFLUXDB_URL_MAX_SIZE); 147 | 148 | return influxdb_client_list_something(client, path, 149 | (void ***) users_list, 150 | &influxdb_dbusers_extractor); 151 | } 152 | -------------------------------------------------------------------------------- /src/database_user.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef DATABASE_USER_H_ 11 | # define DATABASE_USER_H_ 12 | 13 | # include "client.h" 14 | 15 | /** 16 | * Create a new database user. 17 | * 18 | * \param client A initialized client 19 | * \param database The database into which add the user 20 | * \param name The user name to create 21 | * \param password The user password 22 | * \param r_perm Optional read permissions 23 | * \param w_perm Optional write permissions 24 | * \return HTTP status code or CURLcode (if < 100) 25 | */ 26 | int influxdb_create_database_user(s_influxdb_client *client, 27 | char *database, 28 | char *name, 29 | char *password, 30 | char *r_perm, 31 | char *w_perm); 32 | 33 | /** 34 | * Change the user password, adming flag and optionally permissions 35 | * 36 | * \param client A initialized client 37 | * \param name The user name to update 38 | * Check InfluxDB documentation for other parameters 39 | * \return HTTP status code or CURLcode (if < 100) 40 | */ 41 | int influxdb_change_database_user(s_influxdb_client *client, 42 | char *database, 43 | char *name, 44 | char *newPassword, 45 | char isAdmin, 46 | char *new_r_perm, 47 | char *new_w_perm); 48 | 49 | /** 50 | * Update some user properties 51 | * 52 | * \param client A initialized client 53 | * \param name The user name to update 54 | * Check InfluxDB documentation for other parameters 55 | * \return HTTP status code or CURLcode (if < 100) 56 | */ 57 | int influxdb_update_database_user(s_influxdb_client *client, 58 | char *database, 59 | char *name, 60 | char *newPassword); 61 | 62 | /** 63 | * Update user permissions on database 64 | * 65 | * \param client A initialized client 66 | * \param database The database name on which permission will apply 67 | * \param name The user name to update 68 | * \param r_perm Optional read permissions 69 | * \param w_perm Optional write permissions 70 | * \return HTTP status code or CURLcode (if < 100) 71 | */ 72 | int influxdb_update_database_user_permissions(s_influxdb_client *client, 73 | char *database, 74 | char *name, 75 | char *new_r_perm, 76 | char *new_w_perm); 77 | 78 | /** 79 | * Remove an user 80 | * 81 | * \param client A initialized client 82 | * \param name The user name to remove 83 | * \return HTTP status code or CURLcode (if < 100) 84 | */ 85 | int influxdb_delete_database_user(s_influxdb_client *client, 86 | char *database, 87 | char *name); 88 | 89 | /** 90 | * List existing users on database 91 | * 92 | * \param client A initialized client 93 | * \param database The database name on which extract users 94 | * \param users_list The list to fill with database users 95 | * \return The number of users 96 | */ 97 | size_t influxdb_get_database_user_list(s_influxdb_client *client, 98 | char *database, 99 | char ***users_list); 100 | 101 | /** 102 | * Update user permissions on database 103 | * 104 | * \param client A initialized client 105 | * \param database The database name on which permission will apply 106 | * \param name The user name to update 107 | * \param is_admin Is the user admin of the database? 108 | * \param new_r_perm Optional read permissions 109 | * \param new_w_perm Optional write permissions 110 | * \return HTTP status code or CURLcode (if < 100) 111 | */ 112 | int influxdb_alter_database_privilege(s_influxdb_client *client, 113 | char *database, 114 | char *name, 115 | char isAdmin, 116 | char *new_r_perm, 117 | char *new_w_perm); 118 | 119 | #endif /* !DATABASE_USER_H_ */ 120 | -------------------------------------------------------------------------------- /src/influxdb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef INFLUXDB_H_ 11 | # define INFLUXDB_H_ 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | # include "client.h" 18 | # include "series.h" 19 | 20 | # include "database.h" 21 | # include "cluster_admin.h" 22 | # include "server.h" 23 | # include "database_user.h" 24 | # include "query.h" 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif /* !INFLUXDB_H_ */ 31 | -------------------------------------------------------------------------------- /src/query.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "query.h" 14 | #include "utils.h" 15 | 16 | int 17 | influxdb_write_serie(s_influxdb_client *client, 18 | s_influxdb_series *serie) 19 | { 20 | return influxdb_write_serie_with_time_precision(client, serie, 21 | INFLUXDB_TP_DEFAULT); 22 | } 23 | 24 | int 25 | influxdb_write_serie_with_time_precision(s_influxdb_client *client, 26 | s_influxdb_series *serie, 27 | e_influxdb_time_precision time_precision) 28 | { 29 | s_influxdb_series *series[1]; 30 | series[0] = serie; 31 | 32 | return influxdb_write_series_with_time_precision(client, series, 1, 33 | time_precision); 34 | } 35 | 36 | int 37 | influxdb_write_series(s_influxdb_client *client, 38 | s_influxdb_series *series[], 39 | size_t series_length) 40 | { 41 | return influxdb_write_series_with_time_precision(client, series, 42 | series_length, 43 | INFLUXDB_TP_DEFAULT); 44 | } 45 | 46 | int 47 | influxdb_write_series_with_time_precision(s_influxdb_client *client, 48 | s_influxdb_series *series[], 49 | size_t series_length, 50 | e_influxdb_time_precision time_precision) 51 | { 52 | int c; 53 | char path[INFLUXDB_URL_MAX_SIZE]; 54 | json_object *jo = influxdb_series_to_json(series, series_length); 55 | 56 | path[0] = '\0'; 57 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 58 | strncat(path, client->database, INFLUXDB_URL_MAX_SIZE); 59 | strncat(path, "/series", INFLUXDB_URL_MAX_SIZE); 60 | 61 | if (time_precision != INFLUXDB_TP_DEFAULT) 62 | { 63 | strncat(path, "?time_precision=", INFLUXDB_URL_MAX_SIZE); 64 | switch (time_precision) 65 | { 66 | case INFLUXDB_TP_MILLISEC: 67 | strncat(path, "m", INFLUXDB_URL_MAX_SIZE); 68 | break; 69 | case INFLUXDB_TP_MICROSEC: 70 | strncat(path, "u", INFLUXDB_URL_MAX_SIZE); 71 | break; 72 | default: 73 | strncat(path, "s", INFLUXDB_URL_MAX_SIZE); 74 | } 75 | } 76 | 77 | c = influxdb_client_post(client, path, jo, NULL); 78 | 79 | json_object_put(jo); 80 | 81 | return c; 82 | } 83 | 84 | int 85 | influxdb_query(s_influxdb_client *client, 86 | char *query, 87 | e_influxdb_time_precision time_precision, 88 | s_influxdb_series ***response, 89 | size_t *response_size) 90 | { 91 | int c; 92 | char path[INFLUXDB_URL_MAX_SIZE * 2]; 93 | char *escaped_query; 94 | json_object *jo; 95 | 96 | path[0] = '\0'; 97 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE * 2); 98 | strncat(path, client->database, INFLUXDB_URL_MAX_SIZE * 2); 99 | strncat(path, "/series?q=", INFLUXDB_URL_MAX_SIZE * 2); 100 | 101 | escaped_query = curl_easy_escape(NULL, query, 0); 102 | strncat(path, escaped_query, INFLUXDB_URL_MAX_SIZE * 2); 103 | free(escaped_query); 104 | 105 | if (time_precision != INFLUXDB_TP_DEFAULT) 106 | { 107 | strncat(path, "&time_precision=", INFLUXDB_URL_MAX_SIZE * 2); 108 | switch (time_precision) 109 | { 110 | case INFLUXDB_TP_MILLISEC: 111 | strncat(path, "m", INFLUXDB_URL_MAX_SIZE * 2); 112 | break; 113 | case INFLUXDB_TP_MICROSEC: 114 | strncat(path, "u", INFLUXDB_URL_MAX_SIZE * 2); 115 | break; 116 | default: 117 | strncat(path, "s", INFLUXDB_URL_MAX_SIZE * 2); 118 | } 119 | } 120 | 121 | c = influxdb_client_get(client, path, &jo); 122 | 123 | *response_size = influxdb_series_from_json(jo, response); 124 | 125 | json_object_put(jo); 126 | 127 | return c; 128 | } 129 | 130 | void 131 | *influxdb_continuous_queries_extractor(json_object *obj) 132 | { 133 | s_influxdb_continuous_query *cq = 134 | malloc(sizeof (s_influxdb_continuous_query)); 135 | 136 | cq->id = json_object_get_int( 137 | json_object_object_get(obj, "id")); 138 | cq->query = influxdb_strdup(json_object_get_string( 139 | json_object_object_get(obj, "query"))); 140 | 141 | return cq; 142 | } 143 | 144 | size_t 145 | influxdb_get_continuous_queries(s_influxdb_client *client, 146 | s_influxdb_continuous_query ***response) 147 | { 148 | char path[INFLUXDB_URL_MAX_SIZE]; 149 | 150 | path[0] = '\0'; 151 | strncat(path, "/db/", INFLUXDB_URL_MAX_SIZE); 152 | strncat(path, client->database, INFLUXDB_URL_MAX_SIZE); 153 | strncat(path, "/continuous_queries", INFLUXDB_URL_MAX_SIZE); 154 | 155 | return influxdb_client_list_something(client, path, (void ***) response, 156 | &influxdb_continuous_queries_extractor); 157 | } 158 | 159 | int 160 | influxdb_delete_continuous_query(s_influxdb_client *client, 161 | int id) 162 | { 163 | char path[INFLUXDB_URL_MAX_SIZE]; 164 | 165 | sprintf(path, "/db/%s/continuous_queries/%d", client->database, id); 166 | 167 | return influxdb_client_delete(client, path, NULL); 168 | } 169 | 170 | 171 | void 172 | influxdb_continuous_query_free(s_influxdb_continuous_query *cq) 173 | { 174 | if (cq != NULL) 175 | { 176 | free(cq->query); 177 | free(cq); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/query.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef QUERY_H_ 11 | # define QUERY_H_ 12 | 13 | # include "client.h" 14 | # include "series.h" 15 | 16 | typedef enum influxdb_time_precision { 17 | INFLUXDB_TP_DEFAULT = 0, 18 | INFLUXDB_TP_SEC, 19 | INFLUXDB_TP_MILLISEC, 20 | INFLUXDB_TP_MICROSEC 21 | } e_influxdb_time_precision; 22 | 23 | /** 24 | * Send a serie to the DB 25 | * 26 | * \param client A initialized client 27 | * \param serie The serie to write 28 | * \return HTTP status code or CURLcode (if < 100) 29 | */ 30 | int influxdb_write_serie(s_influxdb_client *client, 31 | s_influxdb_series *serie); 32 | 33 | /** 34 | * Send a serie to the DB 35 | * 36 | * \param client A initialized client 37 | * \param serie The serie to write 38 | * \param time_precision The time precision 39 | * \return HTTP status code or CURLcode (if < 100) 40 | */ 41 | int influxdb_write_serie_with_time_precision(s_influxdb_client *client, 42 | s_influxdb_series *serie, 43 | e_influxdb_time_precision time_precision); 44 | 45 | /** 46 | * Send some series to the DB 47 | * 48 | * \param client A initialized client 49 | * \param serie The serie to write 50 | * \param series_length The number of series in the precedent argument 51 | * \return HTTP status code or CURLcode (if < 100) 52 | */ 53 | int influxdb_write_series(s_influxdb_client *client, 54 | s_influxdb_series *series[], 55 | size_t series_length); 56 | 57 | /** 58 | * Send some series to the DB 59 | * 60 | * \param client A initialized client 61 | * \param serie The serie to write 62 | * \param series_length The number of series in the precedent argument 63 | * \param time_precision The time precision 64 | * \return HTTP status code or CURLcode (if < 100) 65 | */ 66 | int influxdb_write_series_with_time_precision(s_influxdb_client *client, 67 | s_influxdb_series *series[], 68 | size_t series_length, 69 | e_influxdb_time_precision time_precision); 70 | 71 | /** 72 | * Retrive some series from database 73 | * 74 | * \param client A initialized client 75 | * \param query The query to execute 76 | * \param time_precision The time precision 77 | * \param response The series array to fill 78 | * \param response_size The number of series retrived 79 | * \return HTTP status code or CURLcode (if < 100) 80 | */ 81 | int influxdb_query(s_influxdb_client *client, 82 | char *query, 83 | e_influxdb_time_precision time_precision, 84 | s_influxdb_series ***response, 85 | size_t *response_size); 86 | 87 | typedef struct influxdb_continuous_queries { 88 | int id; 89 | char *query; 90 | } s_influxdb_continuous_query; 91 | 92 | /** 93 | * Retrive list of continuous queries 94 | * 95 | * \param client An initialized client 96 | * \param response The continuous queries array to fill 97 | * \return The size of the filled array 98 | */ 99 | size_t influxdb_get_continuous_queries(s_influxdb_client *client, 100 | s_influxdb_continuous_query ***response); 101 | 102 | /** 103 | * Send a delete request for a given module 104 | * 105 | * \param client A initialized client 106 | * \param id The database to remove 107 | * return HTTP status code or CURLcode (if < 100) 108 | */ 109 | int influxdb_delete_continuous_query(s_influxdb_client *client, 110 | int id); 111 | 112 | /** 113 | * Clean memory used by a continous query 114 | */ 115 | void influxdb_continuous_query_free(s_influxdb_continuous_query *cq); 116 | 117 | #endif /* !QUERY_H_ */ 118 | -------------------------------------------------------------------------------- /src/series.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include "series.h" 11 | #include "utils.h" 12 | 13 | char 14 | *influxdb_series_get_name(s_influxdb_series *series) 15 | { 16 | return series->name; 17 | } 18 | 19 | size_t 20 | influxdb_series_get_columns(s_influxdb_series *series, char ***list) 21 | { 22 | *list = series->columns; 23 | return series->columns_length; 24 | } 25 | 26 | size_t influxdb_series_get_points(s_influxdb_series *series, char ****list) 27 | { 28 | *list = series->points; 29 | return series->points_length; 30 | } 31 | 32 | char 33 | influxdb_series_set_name(s_influxdb_series *series, char *name) 34 | { 35 | free(series->name); 36 | series->name = influxdb_strdup(name); 37 | return 1; 38 | } 39 | 40 | char 41 | **influxdb_series_set_columns(s_influxdb_series *series, 42 | char **columns, 43 | size_t length) 44 | { 45 | char **old = series->columns; 46 | series->columns = columns; 47 | series->columns_length = length; 48 | return old; 49 | } 50 | 51 | char 52 | influxdb_series_add_colums(s_influxdb_series *series, char *name) 53 | { 54 | if (series->columns_length == 0) { 55 | series->columns = malloc(sizeof (char *) * INFLUXDB_SERIES_STEP); 56 | } else if (series->columns_length % INFLUXDB_SERIES_STEP == 0) { 57 | series->columns = realloc(series->columns, 58 | sizeof (char *) * (series->columns_length + INFLUXDB_SERIES_STEP)); 59 | } 60 | 61 | if (series->columns == NULL) 62 | return 0; 63 | 64 | series->columns[series->columns_length++] = influxdb_strdup(name); 65 | 66 | return 1; 67 | } 68 | 69 | char 70 | ***influxdb_series_set_points(s_influxdb_series *series, 71 | char ***matrix, 72 | size_t length) 73 | { 74 | char ***old = series->points; 75 | series->points = matrix; 76 | series->points_length = length; 77 | return old; 78 | } 79 | 80 | char 81 | influxdb_series_add_points(s_influxdb_series *series, char **row) 82 | { 83 | if (series->points_length == 0) { 84 | series->points = malloc(sizeof (char *) * INFLUXDB_SERIES_STEP); 85 | } else if (series->points_length % INFLUXDB_SERIES_STEP == 0) { 86 | series->points = realloc(series->points, 87 | sizeof (char *) * (series->points_length + INFLUXDB_SERIES_STEP)); 88 | } 89 | 90 | if (series->points == NULL) 91 | return 0; 92 | 93 | series->points[series->points_length++] = row; 94 | 95 | return 1; 96 | } 97 | 98 | s_influxdb_series 99 | *influxdb_series_create(char *name, influxdb_series_free_callback free_cb) 100 | { 101 | s_influxdb_series *series = malloc(sizeof (s_influxdb_series)); 102 | 103 | if (series != NULL) 104 | { 105 | series->name = influxdb_strdup(name); 106 | series->columns = NULL; 107 | series->points = NULL; 108 | series->columns_length = 0; 109 | series->points_length = 0; 110 | series->free_cb = free_cb; 111 | } 112 | 113 | return series; 114 | } 115 | 116 | void 117 | influxdb_series_free(s_influxdb_series *series, 118 | influxdb_series_free_callback free_cb) 119 | { 120 | if (series) 121 | { 122 | size_t i, j; 123 | 124 | free(series->name); 125 | 126 | for (i = 0; i < series->points_length; i++) 127 | { 128 | if (free_cb != NULL) 129 | free_cb(series->points[i]); 130 | else if (series->free_cb != NULL) 131 | series->free_cb(series->points[i]); 132 | else 133 | { 134 | for (j = 0; j < series->columns_length; j++) 135 | free(series->points[i][j]); 136 | free(series->points[i]); 137 | } 138 | } 139 | free(series->points); 140 | series->points_length = 0; 141 | 142 | for (i = 0; i < series->columns_length; i++) 143 | free(series->columns[i]); 144 | free(series->columns); 145 | series->columns_length = 0; 146 | 147 | free(series); 148 | } 149 | } 150 | 151 | json_object 152 | *influxdb_serie_to_json(s_influxdb_series *series) 153 | { 154 | json_object *jo = json_object_new_object(); 155 | 156 | /* Name */ 157 | json_object_object_add(jo, "name", json_object_new_string(series->name)); 158 | 159 | /* Columns */ 160 | { 161 | size_t i; 162 | json_object *cols = json_object_new_array(); 163 | 164 | for (i = 0; i < series->columns_length; i++) 165 | json_object_array_add(cols, 166 | json_object_new_string(series->columns[i])); 167 | 168 | json_object_object_add(jo, "columns", cols); 169 | } 170 | 171 | /* Points */ 172 | { 173 | size_t i, j; 174 | json_object *cols = json_object_new_array(); 175 | 176 | for (i = 0; i < series->points_length; i++) 177 | { 178 | json_object *row = json_object_new_array(); 179 | 180 | for (j = 0; j < series->columns_length; j++) 181 | json_object_array_add(row, 182 | json_object_new_string(series->points[i][j])); 183 | 184 | json_object_array_add(cols, row); 185 | } 186 | 187 | json_object_object_add(jo, "points", cols); 188 | } 189 | 190 | return jo; 191 | } 192 | 193 | s_influxdb_series 194 | *influxdb_serie_from_json(json_object *jo) 195 | { 196 | s_influxdb_series *series = malloc(sizeof (s_influxdb_series)); 197 | 198 | series->free_cb = NULL; 199 | 200 | /* Name */ 201 | series->name = influxdb_strdup( 202 | json_object_get_string( 203 | json_object_object_get(jo, "name") 204 | ) 205 | ); 206 | 207 | /* Columns */ 208 | { 209 | json_object *cols = json_object_object_get(jo, "columns"); 210 | 211 | series->columns_length = json_object_array_length(cols); 212 | series->columns = malloc(sizeof (char *) * series->columns_length); 213 | 214 | { 215 | size_t i; 216 | for (i = 0; i < series->columns_length; i++) 217 | series->columns[i] = influxdb_strdup( 218 | json_object_get_string( 219 | json_object_array_get_idx(cols, i) 220 | ) 221 | ); 222 | } 223 | } 224 | 225 | /* Points */ 226 | { 227 | json_object *points = json_object_object_get(jo, "points"); 228 | 229 | series->points_length = json_object_array_length(points); 230 | series->points = malloc(sizeof (char **) * series->points_length); 231 | 232 | { 233 | size_t i, j; 234 | for (i = 0; i < series->points_length; i++) 235 | { 236 | json_object *cols = json_object_array_get_idx(points, i); 237 | 238 | series->points[i] = malloc( 239 | sizeof (char *) * series->columns_length); 240 | 241 | for (j = 0; j < series->columns_length; j++) 242 | series->points[i][j] = influxdb_strdup( 243 | json_object_get_string( 244 | json_object_array_get_idx(cols, j) 245 | ) 246 | ); 247 | } 248 | } 249 | } 250 | 251 | return series; 252 | } 253 | 254 | json_object 255 | *influxdb_series_to_json(s_influxdb_series *series[], size_t series_length) 256 | { 257 | size_t i; 258 | json_object *sa = json_object_new_array(); 259 | 260 | for (i = 0; i < series_length; i++) 261 | { 262 | json_object_array_add(sa, 263 | influxdb_serie_to_json(series[i]) 264 | ); 265 | } 266 | 267 | return sa; 268 | } 269 | 270 | size_t 271 | influxdb_series_from_json(json_object *jo, s_influxdb_series ***series) 272 | { 273 | size_t i, len = json_object_array_length(jo); 274 | 275 | *series = malloc(sizeof (s_influxdb_series *) * len); 276 | 277 | for (i = 0; i < len; i++) 278 | { 279 | (*series)[i] = influxdb_serie_from_json( 280 | json_object_array_get_idx(jo, i) 281 | ); 282 | } 283 | 284 | return len; 285 | } 286 | -------------------------------------------------------------------------------- /src/series.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef SERIES_H_ 11 | # define SERIES_H_ 12 | 13 | # include 14 | # include "client.h" 15 | 16 | # define INFLUXDB_SERIES_STEP 64 17 | 18 | typedef void (*influxdb_series_free_callback)(char **row); 19 | 20 | typedef struct influxdb_series { 21 | char *name; 22 | char **columns; 23 | char ***points; 24 | size_t columns_length; 25 | size_t points_length; 26 | influxdb_series_free_callback free_cb; 27 | } s_influxdb_series; 28 | 29 | /** 30 | * Retrive the serie name 31 | */ 32 | char *influxdb_series_get_name(s_influxdb_series *series); 33 | 34 | /** 35 | * Retrive the columns list 36 | */ 37 | size_t influxdb_series_get_columns(s_influxdb_series *series, char ***list); 38 | 39 | /** 40 | * Retrive the points matrix 41 | */ 42 | size_t influxdb_series_get_points(s_influxdb_series *series, char ****list); 43 | 44 | 45 | /** 46 | * Define the serie name 47 | */ 48 | char influxdb_series_set_name(s_influxdb_series *series, char *name); 49 | 50 | /** 51 | * Define the columns list 52 | */ 53 | char **influxdb_series_set_columns(s_influxdb_series *series, 54 | char **columns, 55 | size_t length); 56 | 57 | /** 58 | * Add a new column 59 | */ 60 | char influxdb_series_add_colums(s_influxdb_series *series, char *name); 61 | 62 | /** 63 | * Define the points matrix 64 | */ 65 | char ***influxdb_series_set_points(s_influxdb_series *series, 66 | char ***matrix, 67 | size_t length); 68 | 69 | /** 70 | * Add a new points row to matrix 71 | */ 72 | char influxdb_series_add_points(s_influxdb_series *series, char **row); 73 | 74 | 75 | /** 76 | * Initialize a new series 77 | */ 78 | s_influxdb_series *influxdb_series_create(char *name, 79 | influxdb_series_free_callback free_cb); 80 | 81 | /** 82 | * Clean memory used by a series 83 | */ 84 | void influxdb_series_free(s_influxdb_series *series, 85 | influxdb_series_free_callback free_cb); 86 | 87 | /** 88 | * Convert series into a JSON object 89 | */ 90 | json_object *influxdb_series_to_json(s_influxdb_series *series[], size_t series_length); 91 | 92 | /** 93 | * Convert a JSON object to series struct array 94 | */ 95 | size_t influxdb_series_from_json(json_object *jo, s_influxdb_series ***series); 96 | 97 | #endif /* !SERIES_H_ */ 98 | -------------------------------------------------------------------------------- /src/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "server.h" 14 | #include "utils.h" 15 | 16 | void 17 | *influxdb_server_extractor(json_object *obj) 18 | { 19 | s_influxdb_server *server = malloc(sizeof (s_influxdb_server)); 20 | 21 | server->id = json_object_get_int( 22 | json_object_object_get(obj, "id")); 23 | server->protobufConnectString = influxdb_strdup(json_object_get_string( 24 | json_object_object_get(obj, "protobufConnectString"))); 25 | 26 | return server; 27 | } 28 | 29 | size_t 30 | influxdb_servers_get(s_influxdb_client *client, 31 | s_influxdb_server ***servers_list) 32 | { 33 | return influxdb_client_list_something(client, "/cluster/servers", 34 | (void ***) servers_list, 35 | &influxdb_server_extractor); 36 | } 37 | 38 | int 39 | influxdb_server_remove(s_influxdb_client *client, unsigned int id) 40 | { 41 | char path[INFLUXDB_URL_MAX_SIZE]; 42 | 43 | sprintf(path, "/cluster/servers/%u", id); 44 | 45 | return influxdb_client_delete(client, path, NULL); 46 | } 47 | 48 | void 49 | influxdb_server_free(s_influxdb_server *server) 50 | { 51 | if (server) 52 | { 53 | free(server->protobufConnectString); 54 | free(server); 55 | } 56 | } 57 | 58 | int 59 | influxdb_ping(s_influxdb_client *client, char **status) 60 | { 61 | json_object *jo; 62 | int res; 63 | 64 | res = influxdb_client_get(client, "/ping", &jo); 65 | 66 | if (status != NULL) 67 | *status = influxdb_strdup(json_object_get_string( 68 | json_object_object_get(jo, "status"))); 69 | 70 | json_object_put(jo); 71 | 72 | return res; 73 | } 74 | -------------------------------------------------------------------------------- /src/server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef SERVERS_H_ 11 | # define SERVERS_H_ 12 | 13 | # include "client.h" 14 | 15 | typedef struct influxdb_server { 16 | unsigned int id; 17 | char *protobufConnectString; 18 | } s_influxdb_server; 19 | 20 | 21 | /** 22 | * List servers in the cluster 23 | * 24 | * \param client A initialized client 25 | * \param servers_list The list to fill with server struct 26 | * \return The number of servers in the cluster 27 | */ 28 | size_t influxdb_servers_get(s_influxdb_client *client, 29 | s_influxdb_server ***servers_list); 30 | 31 | /** 32 | * Remove from cluster an existing server 33 | * 34 | * \param client A initialized client 35 | * \param id The server identifier to remove 36 | * \return HTTP status code or CURLcode (if < 100) 37 | */ 38 | int influxdb_server_remove(s_influxdb_client *client, 39 | unsigned int id); 40 | 41 | /** 42 | * Clean memory used by a server 43 | */ 44 | void influxdb_server_free(s_influxdb_server *server); 45 | 46 | /** 47 | * Ping the API 48 | */ 49 | int influxdb_ping(s_influxdb_client *client, char **status); 50 | 51 | #endif /* !SERVERS_H_ */ 52 | -------------------------------------------------------------------------------- /src/shard.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "shard.h" 14 | 15 | void 16 | *influxdb_shard_extractor(json_object *row) 17 | { 18 | s_influxdb_shard *shard = malloc(sizeof (s_influxdb_shard)); 19 | 20 | shard->id = json_object_get_int(json_object_object_get(row, "id")); 21 | shard->endtime = json_object_get_int( 22 | json_object_object_get(row, "endTime")); 23 | shard->starttime = json_object_get_int( 24 | json_object_object_get(row, "startTime")); 25 | 26 | { 27 | json_object *servers = json_object_object_get(row, "serverIds"); 28 | size_t j; 29 | 30 | shard->server_ids_length = json_object_array_length(servers); 31 | shard->server_ids = malloc(sizeof (uint32_t) * 32 | shard->server_ids_length); 33 | for (j = 0; j < shard->server_ids_length; j++) 34 | { 35 | shard->server_ids[j] = json_object_get_int( 36 | json_object_array_get_idx(servers, j)); 37 | } 38 | } 39 | 40 | return shard; 41 | } 42 | 43 | size_t 44 | influxdb_shards_from_json(json_object *jo, s_influxdb_shard ***shards) 45 | { 46 | size_t i; 47 | size_t len = json_object_array_length(jo); 48 | *shards = malloc(sizeof (s_influxdb_shard *) * len); 49 | 50 | for (i = 0; i < len; i++) 51 | (*shards)[i] = influxdb_shard_extractor( 52 | json_object_array_get_idx(jo, i)); 53 | 54 | return len; 55 | } 56 | 57 | int 58 | influxdb_shards_get(s_influxdb_client *client, 59 | s_influxdb_long_term_short_term_shards *response) 60 | { 61 | json_object *jo; 62 | int status; 63 | 64 | status = influxdb_client_get(client, "/cluster/shards", 65 | &jo); 66 | 67 | if (status == 200) 68 | { 69 | response->longterm_length = influxdb_shards_from_json( 70 | json_object_object_get(jo, "longTerm"), &response->longterm); 71 | response->shortterm_length = influxdb_shards_from_json( 72 | json_object_object_get(jo, "shortTerm"), &response->shortterm); 73 | } 74 | 75 | return status; 76 | } 77 | 78 | int 79 | influxdb_shard_drop(s_influxdb_client *client, uint32_t id, 80 | uint32_t server_ids[], size_t server_ids_length) 81 | { 82 | char path[INFLUXDB_URL_MAX_SIZE]; 83 | int status; 84 | json_object *jo = json_object_new_object(); 85 | json_object *jo_array = json_object_new_array(); 86 | size_t i; 87 | 88 | for (i = 0; i < server_ids_length; i++) 89 | json_object_array_add(jo_array, 90 | json_object_new_int(server_ids[i])); 91 | 92 | json_object_object_add(jo, "serverIds", jo_array); 93 | 94 | sprintf(path, "/cluster/shards/%u", id); 95 | 96 | status = influxdb_client_delete(client, path, jo); 97 | 98 | json_object_put(jo); 99 | 100 | return status; 101 | } 102 | 103 | void 104 | influxdb_shard_free(s_influxdb_shard *shard) 105 | { 106 | if (shard) 107 | { 108 | free(shard->server_ids); 109 | free(shard); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/shard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef SHARD_H_ 11 | # define SHARD_H_ 12 | 13 | # include 14 | # include "client.h" 15 | 16 | typedef struct influxdb_shard { 17 | uint32_t id; 18 | int64_t endtime; 19 | int64_t starttime; 20 | uint32_t *server_ids; 21 | size_t server_ids_length; 22 | } s_influxdb_shard; 23 | 24 | typedef struct influxdb_long_term_short_term_shards { 25 | s_influxdb_shard **longterm; 26 | size_t longterm_length; 27 | s_influxdb_shard **shortterm; 28 | size_t shortterm_length; 29 | } s_influxdb_long_term_short_term_shards; 30 | 31 | /** 32 | * \param client A initialized client 33 | * \param response The list to fill with 34 | * \return HTTP status code or CURLcode (if < 100) 35 | */ 36 | int influxdb_shards_get(s_influxdb_client *client, 37 | s_influxdb_long_term_short_term_shards *response); 38 | 39 | /** 40 | * \param client A initialized client 41 | * \param id The server identifier to remove 42 | * \return HTTP status code or CURLcode (if < 100) 43 | */ 44 | int influxdb_shard_drop(s_influxdb_client *client, 45 | uint32_t id, 46 | uint32_t server_ids[], 47 | size_t server_ids_length); 48 | 49 | /** 50 | * Clean memory used by a shard 51 | */ 52 | void influxdb_shard_free(s_influxdb_shard *shard); 53 | 54 | #endif /* !SHARD_H_ */ 55 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "utils.h" 14 | 15 | char 16 | *influxdb_strdup(const char *s) 17 | { 18 | char *d; 19 | 20 | if (s == NULL) 21 | return NULL; 22 | 23 | d = malloc(sizeof (char) * (strlen(s) + 1)); 24 | if (d == NULL) 25 | return NULL; 26 | 27 | strcpy(d, s); 28 | return d; 29 | } 30 | -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef UTILS_H_ 11 | # define UTILS_H_ 12 | 13 | /** 14 | * Returns a pointer to a new string which is a duplicate of the string s 15 | */ 16 | char *influxdb_strdup(const char *s); 17 | 18 | #endif /* !UTILS_H_ */ 19 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | LDADD = $(top_builddir)/src/libinfluxdb.la 2 | 3 | TESTS = server database cluster_admin database_user 4 | 5 | check_PROGRAMS = server database cluster_admin database_user 6 | 7 | server_SOURCES = server.c tests.c 8 | database_SOURCES = database.c tests.c 9 | cluster_admin_SOURCES = cluster_admin.c tests.c 10 | database_user_SOURCES = database_user.c tests.c 11 | -------------------------------------------------------------------------------- /tests/cluster_admin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "../src/cluster_admin.h" 13 | #include "tests.h" 14 | 15 | int 16 | find(s_influxdb_client *client) 17 | { 18 | int find = 0; 19 | char **cluster_admin_list; 20 | size_t nb = influxdb_get_cluster_admin_list(client, &cluster_admin_list); 21 | size_t i; 22 | 23 | for (i = 0; i < nb; i++) { 24 | if (!strcmp(cluster_admin_list[i], INFLUXDB_CLUSTERADMIN_TEST)) 25 | find = 1; 26 | free(cluster_admin_list[i]); 27 | } 28 | 29 | free(cluster_admin_list); 30 | 31 | return !find; 32 | } 33 | 34 | int 35 | create(s_influxdb_client *client) 36 | { 37 | char *status_txt; 38 | int status_code; 39 | 40 | status_code = influxdb_create_cluster_admin(client, 41 | INFLUXDB_CLUSTERADMIN_TEST, 42 | INFLUXDB_DATABASE_TEST); 43 | 44 | return status_code != 200 || find(client); 45 | } 46 | 47 | 48 | int 49 | delete(s_influxdb_client *client) 50 | { 51 | char *status_txt; 52 | int status_code; 53 | 54 | status_code = influxdb_delete_cluster_admin(client, INFLUXDB_CLUSTERADMIN_TEST); 55 | 56 | return status_code != 200 || !find(client); 57 | } 58 | 59 | int 60 | chpass(s_influxdb_client *client) 61 | { 62 | char *status_txt; 63 | int status_code; 64 | 65 | status_code = influxdb_change_cluster_admin_password(client, INFLUXDB_CLUSTERADMIN_TEST, "1234"); 66 | 67 | return status_code != 200 || find(client); 68 | } 69 | 70 | int 71 | get(s_influxdb_client *client) 72 | { 73 | char **cluster_admin_list; 74 | size_t nb = influxdb_get_cluster_admin_list(client, &cluster_admin_list); 75 | size_t i; 76 | 77 | for (i = 0; i < nb; i++) { 78 | free(cluster_admin_list[i]); 79 | } 80 | 81 | free(cluster_admin_list); 82 | 83 | return nb <= 0; 84 | } 85 | 86 | influxdb_test tests[] = { 87 | &create, 88 | &get, 89 | &chpass, 90 | &delete, 91 | NULL 92 | }; 93 | -------------------------------------------------------------------------------- /tests/database.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "../src/database.h" 13 | #include "tests.h" 14 | 15 | int 16 | find(s_influxdb_client *client) 17 | { 18 | char **database_list; 19 | size_t nb = influxdb_get_database_list(client, &database_list); 20 | int find = 0; 21 | size_t i; 22 | 23 | for (i = 0; i < nb; i++) { 24 | if (!strcmp(database_list[i], INFLUXDB_DATABASE_TEST)) 25 | find = 1; 26 | free(database_list[i]); 27 | } 28 | 29 | free(database_list); 30 | 31 | return !find; 32 | } 33 | 34 | int 35 | create(s_influxdb_client *client) 36 | { 37 | char *status_txt; 38 | int status_code; 39 | 40 | status_code = influxdb_create_database(client, INFLUXDB_DATABASE_TEST); 41 | 42 | return status_code != 201 || find(client); 43 | } 44 | 45 | int 46 | delete(s_influxdb_client *client) 47 | { 48 | char *status_txt; 49 | int status_code; 50 | 51 | status_code = influxdb_delete_database(client, INFLUXDB_DATABASE_TEST); 52 | 53 | return status_code != 204 || !find(client); 54 | } 55 | 56 | int 57 | get(s_influxdb_client *client) 58 | { 59 | char **database_list; 60 | size_t nb = influxdb_get_database_list(client, &database_list); 61 | size_t i; 62 | 63 | for (i = 0; i < nb; i++) 64 | free(database_list[i]); 65 | 66 | free(database_list); 67 | 68 | return nb <= 0; 69 | } 70 | 71 | influxdb_test tests[] = { 72 | &create, 73 | &get, 74 | &delete, 75 | NULL 76 | }; 77 | -------------------------------------------------------------------------------- /tests/database_user.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "../src/database.h" 13 | #include "../src/database_user.h" 14 | #include "tests.h" 15 | 16 | int 17 | find(s_influxdb_client *client) 18 | { 19 | int find = 0; 20 | char **users_list; 21 | size_t nb = influxdb_get_database_user_list(client, INFLUXDB_DATABASE_TEST, &users_list); 22 | size_t i; 23 | 24 | for (i = 0; i < nb; i++) { 25 | if (!strcmp(users_list[i], INFLUXDB_CLUSTERADMIN_TEST)) 26 | find = 1; 27 | free(users_list[i]); 28 | } 29 | 30 | free(users_list); 31 | 32 | return !find; 33 | } 34 | 35 | int 36 | create_db(s_influxdb_client *client) 37 | { 38 | char *status_txt; 39 | int status_code; 40 | 41 | status_code = influxdb_create_database(client, INFLUXDB_DATABASE_TEST); 42 | 43 | return status_code != 201; 44 | } 45 | 46 | int 47 | delete_db(s_influxdb_client *client) 48 | { 49 | char *status_txt; 50 | int status_code; 51 | 52 | status_code = influxdb_delete_database(client, INFLUXDB_DATABASE_TEST); 53 | 54 | return status_code != 204; 55 | } 56 | 57 | int 58 | create(s_influxdb_client *client) 59 | { 60 | char *status_txt; 61 | int status_code; 62 | 63 | status_code = influxdb_create_database_user(client, 64 | INFLUXDB_DATABASE_TEST, 65 | INFLUXDB_CLUSTERADMIN_TEST, 66 | INFLUXDB_DATABASE_TEST, NULL, NULL); 67 | 68 | return status_code != 200 || find(client); 69 | } 70 | 71 | int 72 | delete(s_influxdb_client *client) 73 | { 74 | char *status_txt; 75 | int status_code; 76 | 77 | status_code = influxdb_delete_database_user(client, 78 | INFLUXDB_DATABASE_TEST, 79 | INFLUXDB_CLUSTERADMIN_TEST); 80 | 81 | return status_code != 200 || !find(client); 82 | } 83 | 84 | int 85 | chpass(s_influxdb_client *client) 86 | { 87 | char *status_txt; 88 | int status_code; 89 | 90 | status_code = influxdb_update_database_user(client, 91 | INFLUXDB_DATABASE_TEST, 92 | INFLUXDB_CLUSTERADMIN_TEST, 93 | "1234"); 94 | 95 | return status_code != 200 || find(client); 96 | } 97 | 98 | int 99 | get_users(s_influxdb_client *client) 100 | { 101 | char **users_list; 102 | size_t nb = influxdb_get_database_user_list(client, INFLUXDB_DATABASE_TEST, &users_list); 103 | size_t i; 104 | 105 | for (i = 0; i < nb; i++) 106 | free(users_list[i]); 107 | 108 | free(users_list); 109 | 110 | return nb; 111 | } 112 | 113 | int 114 | nb_user_start(s_influxdb_client *client) 115 | { 116 | return get_users(client) != 0; 117 | } 118 | int 119 | nb_user_middle(s_influxdb_client *client) 120 | { 121 | return get_users(client) != 1; 122 | } 123 | 124 | influxdb_test tests[] = { 125 | &nb_user_start, 126 | &create_db, 127 | &create, 128 | &nb_user_middle, 129 | &chpass, 130 | &delete, 131 | &nb_user_start, 132 | &delete_db, 133 | NULL 134 | }; 135 | -------------------------------------------------------------------------------- /tests/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "../src/server.h" 13 | #include "tests.h" 14 | 15 | int 16 | ping(s_influxdb_client *client) 17 | { 18 | char *status_txt; 19 | int status_code; 20 | 21 | status_code = influxdb_ping(client, (char **) &status_txt); 22 | 23 | if (strcmp(status_txt, "ok")) 24 | return 1; 25 | 26 | free(status_txt); 27 | 28 | return status_code != 200; 29 | } 30 | 31 | int 32 | get(s_influxdb_client *client) 33 | { 34 | s_influxdb_server **server_list; 35 | size_t nb = influxdb_servers_get(client, &server_list); 36 | size_t i; 37 | 38 | for (i = 0; i < nb; i++) 39 | influxdb_server_free(server_list[i]); 40 | 41 | free(server_list); 42 | 43 | return nb <= 0; 44 | } 45 | 46 | influxdb_test tests[] = { 47 | &ping, 48 | &get, 49 | NULL 50 | }; 51 | -------------------------------------------------------------------------------- /tests/tests.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #include 11 | 12 | #include "tests.h" 13 | 14 | int 15 | main() 16 | { 17 | size_t i, fails = 0; 18 | 19 | s_influxdb_client *client = influxdb_client_new("localhost:8086", 20 | "root", 21 | "root", 22 | INFLUXDB_DATABASE_TEST, 23 | 0); 24 | 25 | if (client == NULL) 26 | { 27 | printf("Unable to initialize client\n"); 28 | return 127; 29 | } 30 | 31 | for (i = 0; tests[i]; i++) 32 | { 33 | if (tests[i](client)) 34 | { 35 | printf("Test %lu fails.\n", i + 1); 36 | fails++; 37 | } 38 | } 39 | 40 | influxdb_client_free(client); 41 | 42 | printf("SUMARRY: %lu tests executed, %lu passed, %lu failed", 43 | i, i - fails, fails); 44 | 45 | return fails > 126 ? 126 : fails; 46 | } 47 | -------------------------------------------------------------------------------- /tests/tests.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014 InfluxDB 3 | * Pierre-Olivier Mercier 4 | * 5 | * This library is free software; you can redistribute it and/or modify 6 | * it under the terms of the MIT license. See COPYING for details. 7 | * 8 | */ 9 | 10 | #ifndef TESTS_H_ 11 | # define TESTS_H_ 12 | 13 | # include 14 | 15 | # include "../src/client.h" 16 | 17 | # define INFLUXDB_DATABASE_TEST "tests" 18 | # define INFLUXDB_CLUSTERADMIN_TEST "toto" 19 | 20 | typedef int (*influxdb_test)(s_influxdb_client *); 21 | 22 | extern influxdb_test tests[]; 23 | 24 | #endif /* !TESTS_H_ */ 25 | --------------------------------------------------------------------------------