summaryrefslogtreecommitdiffstats
path: root/output/ulogd_output_GRAPHITE.c
blob: 25b578e204a9d3d779c25382444f2a72b8b9ce06 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* ulogd_GRAPHITE.c
 *
 * ulogd output target to feed data to a graphite system
 *
 * (C) 2012 by Eric Leblond <eric@regit.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ulogd/ulogd.h>
#include <ulogd/conffile.h>


enum {
	KEY_SUM_NAME,
	KEY_SUM_PKTS,
	KEY_SUM_BYTES,
	KEY_OOB_TIME_SEC,
};


static struct ulogd_key graphite_inp[] = {
	[KEY_SUM_NAME] {
		.type	= ULOGD_RET_STRING,
		.name	= "sum.name",
	},
	[KEY_SUM_PKTS] {
		.type	= ULOGD_RET_UINT64,
		.name	= "sum.pkts",
	},
	[KEY_SUM_BYTES] {
		.type	= ULOGD_RET_UINT64,
		.name	= "sum.bytes",
	},
	[KEY_OOB_TIME_SEC] {
		.type = ULOGD_RET_UINT32,
		.name = "oob.time.sec",
	},
};


static struct config_keyset graphite_kset = {
	.num_ces = 3,
	.ces = {
		{
			.key = "host",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
		},
		{
			.key = "port",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
		},
		{
			.key = "prefix",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
		},
	},
};

#define host_ce(x)	(x->ces[0])
#define port_ce(x)	(x->ces[1])
#define prefix_ce(x)	(x->ces[2])

struct graphite_instance {
	int sck;
};

static int _connect_graphite(struct ulogd_pluginstance *pi)
{
	struct graphite_instance *li = (struct graphite_instance *) &pi->private;
	char *host;
	char * port;
	struct addrinfo hints;
	struct addrinfo *result, *rp;
	int sfd, s;

	ulogd_log(ULOGD_DEBUG, "connecting to graphite\n");

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = 0;
	hints.ai_protocol = 0;

	host = host_ce(pi->config_kset).u.string;
	port = port_ce(pi->config_kset).u.string;
	s = getaddrinfo(host, port, &hints, &result);
	if (s != 0) {
		ulogd_log(ULOGD_ERROR, "getaddrinfo: %s\n", gai_strerror(s));
		return -1;
	}

	for (rp = result; rp != NULL; rp = rp->ai_next) {
		int on = 1;

		sfd = socket(rp->ai_family, rp->ai_socktype,
				rp->ai_protocol);
		if (sfd == -1)
			continue;

		setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
			   (char *) &on, sizeof(on));

		if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
			break;

		close(sfd);
	}

	freeaddrinfo(result);

	if (rp == NULL) {
		ulogd_log(ULOGD_ERROR, "Could not connect\n");
		return -1;
	}

	li->sck = sfd;

	return 0;
}

static int _output_graphite(struct ulogd_pluginstance *upi)
{
	struct graphite_instance *li = (struct graphite_instance *) &upi->private;
	struct ulogd_key *inp = upi->input.keys;
	static char buf[256];
	int ret;

	time_t now;
	int msg_size = 0;

	if (ikey_get_u32(&inp[KEY_OOB_TIME_SEC]))
		now = (time_t) ikey_get_u32(&inp[KEY_OOB_TIME_SEC]);
	else
		now = time(NULL);

	msg_size = snprintf(buf, sizeof(buf), "%s.%s.pkts %" PRIu64
			    " %" PRIu64 "\n%s.%s.bytes %" PRIu64 " %" PRIu64 "\n",
		 prefix_ce(upi->config_kset).u.string,
		 (char *)ikey_get_ptr(&inp[KEY_SUM_NAME]),
		 ikey_get_u64(&inp[KEY_SUM_PKTS]),
		 now,
		 prefix_ce(upi->config_kset).u.string,
		 (char *)ikey_get_ptr(&inp[KEY_SUM_NAME]),
		 ikey_get_u64(&inp[KEY_SUM_BYTES]),
		 now
		 );
	if (msg_size == -1) {
		ulogd_log(ULOGD_ERROR, "Could not create message\n");
		return ULOGD_IRET_ERR;
	}
	ret = send(li->sck, buf, msg_size, MSG_NOSIGNAL);
	if (ret != msg_size) {
		ulogd_log(ULOGD_ERROR, "Failure sending message\n");
		if (ret == -1) {
			return _connect_graphite(upi);
		}
	}

	return ULOGD_IRET_OK;
}

static int start_graphite(struct ulogd_pluginstance *pi)
{
	char *host;
	char *port;

	ulogd_log(ULOGD_DEBUG, "starting graphite\n");

	host = host_ce(pi->config_kset).u.string;
	if (host == NULL)
		return -1;
	port = port_ce(pi->config_kset).u.string;
	if (port == NULL)
		return -1;
	return _connect_graphite(pi);
}

static int fini_graphite(struct ulogd_pluginstance *pi) {
	struct graphite_instance *li = (struct graphite_instance *) &pi->private;

	close(li->sck);
	li->sck = 0;

	return 0;
}

static int configure_graphite(struct ulogd_pluginstance *pi,
			    struct ulogd_pluginstance_stack *stack)
{
	ulogd_log(ULOGD_DEBUG, "parsing config file section %s\n", pi->id);
	return config_parse_file(pi->id, pi->config_kset);
}

static struct ulogd_plugin graphite_plugin = {
	.name = "GRAPHITE",
	.input = {
		.keys = graphite_inp,
		.num_keys = ARRAY_SIZE(graphite_inp),
		.type = ULOGD_DTYPE_SUM,
	},
	.output = {
		.type = ULOGD_DTYPE_SINK,
	},
	.config_kset 	= &graphite_kset,
	.priv_size 	= sizeof(struct graphite_instance),

	.configure	= &configure_graphite,
	.start	 	= &start_graphite,
	.stop	 	= &fini_graphite,

	.interp 	= &_output_graphite,
	.version	= VERSION,
};

void __attribute__ ((constructor)) init(void);

void init(void)
{
	ulogd_register_plugin(&graphite_plugin);
}