/
lib_statsd.c
134 lines (113 loc) · 3.03 KB
/
lib_statsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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>
#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;
}