src/modules/statsd/lib_statsd.c
8a85d8fd
 #include <stdio.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <netdb.h>
 #include <math.h>
 #include <errno.h>
 
cf83221d
 #include "../../core/sr_module.h"
8a85d8fd
 #include "lib_statsd.h"
 
 static StatsConnection statsd_connection = {
     "127.0.0.1",
8ead5ae6
     "8125",
     -1
8a85d8fd
 };
 
5d2d7237
 bool statsd_connect(void){
8a85d8fd
 
f6f6c7f5
     struct addrinfo *serverAddr = NULL;
8ead5ae6
     int rc;
8a85d8fd
 
8ead5ae6
     if (statsd_connection.sock > 0){
5d2d7237
         return true;
8a85d8fd
     }
 
8ead5ae6
     rc = getaddrinfo(
8a85d8fd
         statsd_connection.ip, statsd_connection.port,
         NULL, &serverAddr);
f75eba63
     if (rc != 0 || serverAddr == NULL)
8a85d8fd
     {
f75eba63
         LM_ERR("Statsd: could not initiate server information (%s)\n",
8ead5ae6
             gai_strerror(rc));
f75eba63
         if(serverAddr) freeaddrinfo(serverAddr);
5d2d7237
         return false;
8a85d8fd
     }
 
8ead5ae6
     statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP);
06a9516a
     if (statsd_connection.sock < 0 ){
         LM_ERR("Statsd: could not create a socket for statsd connection\n");
f75eba63
         freeaddrinfo(serverAddr);
5d2d7237
         return false;
8a85d8fd
     }
 
f75eba63
     rc = connect(statsd_connection.sock, serverAddr->ai_addr,
             serverAddr->ai_addrlen);
     freeaddrinfo(serverAddr);
4729d163
     if (rc < 0){
         LM_ERR("Statsd: could not initiate a connect to statsd\n");
5d2d7237
         return false;
8a85d8fd
     }
5d2d7237
     return true;
8a85d8fd
 }
 
5d2d7237
 bool send_command(char *command){
8a85d8fd
     int send_result;
 
     if (!statsd_connect()){
5d2d7237
         return false;
8a85d8fd
     }
 
8ead5ae6
     send_result = send(statsd_connection.sock, command, strlen(command), 0);
8a85d8fd
     if ( send_result < 0){
4729d163
         LM_ERR("could not send the correct info to statsd (%i| %s)\n",
8a85d8fd
             send_result, strerror(errno));
5d2d7237
         return true;
8a85d8fd
     }
     LM_DBG("Sent to statsd (%s)", command);
5d2d7237
     return true;
8a85d8fd
 }
 
5d2d7237
 bool statsd_set(char *key, char *value){
8a85d8fd
    char* end = 0;
    char command[254];
    int val;
    val = strtol(value, &end, 0);
    if (*end){
4729d163
        LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
5d2d7237
        return false;
8a85d8fd
    }
    snprintf(command, sizeof command, "%s:%i|s\n", key, val);
    return send_command(command);
 }
 
 
5d2d7237
 bool statsd_gauge(char *key, char *value){
8a85d8fd
    char command[254];
    snprintf(command, sizeof command, "%s:%s|g\n", key, value);
    return send_command(command);
 }
 
5d2d7237
 bool statsd_count(char *key, char *value){
8a85d8fd
    char* end = 0;
    char command[254];
    int val;
 
    val = strtol(value, &end, 0);
    if (*end){
4729d163
        LM_ERR("statsd_count could not  use the provide value(%s)\n", value);
5d2d7237
        return false;
8a85d8fd
    }
    snprintf(command, sizeof command, "%s:%i|c\n", key, val);
    return send_command(command);
 }
 
5d2d7237
 bool statsd_timing(char *key, int value){
8a85d8fd
    char command[254];
    snprintf(command, sizeof command, "%s:%i|ms\n", key, value);
    return send_command(command);
 }
 
5d2d7237
 bool statsd_init(char *ip, char *port){
8a85d8fd
 
     if (ip != NULL){
         statsd_connection.ip = ip;
     }
     if (port != NULL ){
        statsd_connection.port = port;
     }
     return statsd_connect();
 }
 
5d2d7237
 bool statsd_destroy(void){
8ead5ae6
     statsd_connection.sock = 0;
5d2d7237
     return true;
8a85d8fd
 }