diff --git a/modules/statsd/lib_statsd.c b/modules/statsd/lib_statsd.c new file mode 100644 index 00000000000..9928f1c5854 --- /dev/null +++ b/modules/statsd/lib_statsd.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../sr_module.h" +#include "lib_statsd.h" + +static StatsdSocket statsd_socket = { + "/var/run/statsd/statsd.sock", + -1, + 500, // timeout 500ms if no answer + 0, + "" +}; + +static StatsConnection statsd_connection = { + "127.0.0.1", + "8125" +}; + +int statsd_connect(void){ + + struct addrinfo *serverAddr; + int rc, error; + + if (statsd_socket.sock > 0){ + return True; + } + + error = getaddrinfo( + statsd_connection.ip, statsd_connection.port, + NULL, &serverAddr); + if (error != 0) + { + LM_ERR("could not initiate server information (%s)\n",gai_strerror(error)); + return False; + } + + statsd_socket.sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (statsd_socket.sock == 0 ){ + LM_ERR("could not initiate a connect to statsd\n"); + return False; + } + + rc = connect( + statsd_socket.sock, serverAddr->ai_addr, serverAddr->ai_addrlen); + if (rc <0){ + LM_ERR("could not initiate a connect to statsd\n"); + return False; + } + return True; +} + +int send_command(char *command){ + int send_result; + + if (!statsd_connect()){ + LM_ERR("Connection lost to statsd"); + return False; + } + + send_result = send(statsd_socket.sock, command, strlen(command), 0); + if ( send_result < 0){ + LM_ERR("could not send the correct info to statsd (%i| %s)", + send_result, strerror(errno)); + return True; + } + LM_DBG("Sent to statsd (%s)", command); + return True; +} + +int statsd_set(char *key, char *value){ + char* end = 0; + char command[254]; + int val; + val = strtol(value, &end, 0); + if (*end){ + LM_ERR("statsd_count could not use the provide value(%s)", value); + return False; + } + snprintf(command, sizeof command, "%s:%i|s\n", key, val); + return send_command(command); +} + + +int statsd_gauge(char *key, char *value){ + char command[254]; + snprintf(command, sizeof command, "%s:%s|g\n", key, value); + return send_command(command); +} + +int statsd_count(char *key, char *value){ + char* end = 0; + char command[254]; + int val; + + val = strtol(value, &end, 0); + if (*end){ + LM_ERR("statsd_count could not use the provide value(%s)", value); + return False; + } + snprintf(command, sizeof command, "%s:%i|c\n", key, val); + return send_command(command); +} + +int statsd_timing(char *key, int value){ + char command[254]; + snprintf(command, sizeof command, "%s:%i|ms\n", key, value); + return send_command(command); +} + +int statsd_init(char *ip, char *port){ + + if (ip != NULL){ + statsd_connection.ip = ip; + } + if (port != NULL ){ + statsd_connection.port = port; + } + LM_ERR("Statsd_init ip %s", statsd_connection.ip); + LM_ERR("Statsd_init port %s", statsd_connection.port); + return statsd_connect(); +} + +int statsd_destroy(void){ + statsd_socket.sock = 0; + return True; +} diff --git a/modules/statsd/lib_statsd.h b/modules/statsd/lib_statsd.h index c52de31970c..b7d178e9c25 100644 --- a/modules/statsd/lib_statsd.h +++ b/modules/statsd/lib_statsd.h @@ -1,14 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../sr_module.h" #define BUFFER_SIZE 8192 typedef int Bool; @@ -20,7 +9,6 @@ typedef struct StatsConnection{ char *port; } StatsConnection; - typedef struct StatsdSocket { char *name; // name int sock; // socket @@ -29,124 +17,11 @@ typedef struct StatsdSocket { char data[BUFFER_SIZE]; // buffer for the answer data } StatsdSocket; -static StatsdSocket statsd_socket = { - "/var/run/statsd/statsd.sock", - -1, - 500, // timeout 500ms if no answer - 0, - "" -}; - -static StatsConnection statsd_connection = { - "127.0.0.1", - "8125" -}; - -static Bool statsd_connect(void){ - - struct addrinfo *serverAddr; - int rc, error; - - if (statsd_socket.sock > 0){ - return True; - } - - error = getaddrinfo( - statsd_connection.ip, statsd_connection.port, - NULL, &serverAddr); - if (error != 0) - { - LM_ERR("could not initiate server information (%s)\n",gai_strerror(error)); - return False; - } - - statsd_socket.sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (statsd_socket.sock == 0 ){ - LM_ERR("could not initiate a connect to statsd\n"); - return False; - } - - rc = connect( - statsd_socket.sock, serverAddr->ai_addr, serverAddr->ai_addrlen); - if (rc <0){ - LM_ERR("could not initiate a connect to statsd\n"); - return False; - } - return True; -} - -static int send_command(char *command){ - int send_result; - - if (!statsd_connect()){ - LM_ERR("Connection lost to statsd"); - return False; - } - - send_result = send(statsd_socket.sock, command, strlen(command), 0); - if ( send_result < 0){ - LM_ERR("could not send the correct info to statsd (%i| %s)", - send_result, strerror(errno)); - return True; - } - LM_DBG("Sent to statsd (%s)", command); - return True; -} - -static int statsd_set(char *key, char *value){ - char* end = 0; - char command[254]; - int val; - val = strtol(value, &end, 0); - if (*end){ - LM_ERR("statsd_count could not use the provide value(%s)", value); - return False; - } - snprintf(command, sizeof command, "%s:%i|s\n", key, val); - return send_command(command); -} - - -static int statsd_gauge(char *key, char *value){ - char command[254]; - snprintf(command, sizeof command, "%s:%s|g\n", key, value); - return send_command(command); -} - -static int statsd_count(char *key, char *value){ - char* end = 0; - char command[254]; - int val; - - val = strtol(value, &end, 0); - if (*end){ - LM_ERR("statsd_count could not use the provide value(%s)", value); - return False; - } - snprintf(command, sizeof command, "%s:%i|c\n", key, val); - return send_command(command); -} - -static int statsd_timing(char *key, int value){ - char command[254]; - snprintf(command, sizeof command, "%s:%i|ms\n", key, value); - return send_command(command); -} - -static int statsd_init(char *ip, char *port){ - - if (ip != NULL){ - statsd_connection.ip = ip; - } - if (port != NULL ){ - statsd_connection.port = port; - } - LM_ERR("Statsd_init ip %s", statsd_connection.ip); - LM_ERR("Statsd_init port %s", statsd_connection.port); - return statsd_connect(); -} - -static int statsd_destroy(void){ - statsd_socket.sock = 0; - return True; -} +int statsd_connect(void); +int send_command(char *command); +int statsd_set(char *key, char *value); +int statsd_gauge(char *key, char *value); +int statsd_count(char *key, char *value); +int statsd_timing(char *key, int value); +int statsd_init(char *ip, char *port); +int statsd_destroy(void);