summaryrefslogtreecommitdiffabout
path: root/src/betab.c
blob: 83d77e376d6da5f7c29265121a3a897ac5f8e0f4 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* This file is part of varnish-mib
   Copyright (C) 2014, 2018 Sergey Poznyakoff

   Varnish-mib is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Varnish-mib 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 varnish-mib.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "varnish_mib.h"
#include <varnish/vas.h>
#include <varnish/vqueue.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "backend.h"

enum {
	vbeHappyProbes,
	vbeRequestHeaderBytes,
	vbeRequestBodyBytes,
	vbeResponseHeaderBytes,
	vbeResponseBodyBytes,
	vbePipeHeaderBytes,
	vbePipeIn,
	vbePipeOut,
	VBE_MAX
};

struct betab_trans {
	char const *param;
	size_t off;
};

/*
	{ "vcls",
	  offsetof(struct backendTable_entry, vbeVcls) },
*/

static struct betab_trans betab_trans[] = {
	{ "happy",
	  offsetof(struct backendTable_entry, vbeHappyProbes) },               
	{ "bereq_hdrbytes",
	  offsetof(struct backendTable_entry, vbeRequestHeaderBytes) },    
	{ "bereq_bodybytes",
	  offsetof(struct backendTable_entry, vbeRequestBodyBytes) },      
	{ "beresp_hdrbytes",
	  offsetof(struct backendTable_entry, vbeResponseHeaderBytes) },      
	{ "beresp_bodybytes",
	  offsetof(struct backendTable_entry, vbeResponseBodyBytes) },     
	{ "pipe_hdrbytes",
	  offsetof(struct backendTable_entry, vbePipeHeaderBytes) },
	{ "pipe_in",
	  offsetof(struct backendTable_entry, vbePipeIn) },
	{ "pipe_out",
	  offsetof(struct backendTable_entry, vbePipeOut) },
	{ NULL }
};

struct backend_dfn {
	long vbeIndex;

	char *vbeIdent;
	size_t vbeIdent_len;

	char vbeIPv4[4];
	size_t vbeIPv4_len;
	char vbeIPv6[16];
	size_t vbeIPv6_len;
	u_long vbePort;
	
	const volatile uint64_t *vpt[VBE_MAX];
	
	VTAILQ_ENTRY(backend_dfn) list;
};	

static VTAILQ_HEAD(, backend_dfn) backends
	= VTAILQ_HEAD_INITIALIZER(backends);

void
backend_clear(void)
{
	while (!VTAILQ_EMPTY(&backends)) {
		struct backend_dfn *dfn = VTAILQ_FIRST(&backends);
		VTAILQ_REMOVE(&backends, dfn, list);
		free(dfn->vbeIdent);
		SNMP_FREE(dfn);
	}
}

void
backend_register(char const *name, size_t len, char const *param,
		 const struct VSC_point *vpt)
{
	int i = 0;
	struct backend_dfn *dfn;
	
	VTAILQ_FOREACH(dfn, &backends, list) {
		if (dfn->vbeIdent_len == len
		    && memcmp(dfn->vbeIdent, name, len) == 0)
			break;
		i++;
	}
	if (!dfn) {
		dfn = SNMP_MALLOC_TYPEDEF(struct backend_dfn);
		AN(dfn);
		dfn->vbeIndex = i;
		dfn->vbeIdent = malloc(len + 1);
		AN(dfn->vbeIdent);
		memcpy(dfn->vbeIdent, name, len);
		dfn->vbeIdent[len] = 0;
		dfn->vbeIdent_len = len;
		VTAILQ_INSERT_TAIL(&backends, dfn, list);
	}
	for (i = 0; betab_trans[i].param; i++) {
		if (strcmp(betab_trans[i].param, param) == 0) {
			dfn->vpt[i] = vpt->ptr;
			break;
		}
	}
}

int
backendTable_load(netsnmp_cache *cache, void *vmagic)
{
	netsnmp_tdata *table_data = (netsnmp_tdata *) vmagic;
	struct backend_dfn *dfn;

	DEBUGMSGTL(("varnish_mib:backend", "loading backend table\n"));

	VTAILQ_FOREACH(dfn, &backends, list) {
                int i;

		netsnmp_tdata_row *row;
		struct backendTable_entry *ent;
		
		ent = SNMP_MALLOC_TYPEDEF(struct backendTable_entry);
		ent->vbeIndex = dfn->vbeIndex;
		ent->vbeIdent = malloc(dfn->vbeIdent_len);
		memcpy(ent->vbeIdent, dfn->vbeIdent, dfn->vbeIdent_len);
		ent->vbeIdent_len = dfn->vbeIdent_len;
		memset(ent->vbeIPv4, 0, sizeof ent->vbeIPv4);

		memcpy(ent->vbeIPv4, dfn->vbeIPv4, dfn->vbeIPv4_len);
		ent->vbeIPv4_len = dfn->vbeIPv4_len;
		memcpy(ent->vbeIPv6, dfn->vbeIPv6, ent->vbeIPv6_len);
		ent->vbeIPv6_len = dfn->vbeIPv6_len;
		ent->vbePort = dfn->vbePort;
		
                for (i = 0; i < VBE_MAX; i++) {
                        U64 *u = (U64*)((char*)ent + betab_trans[i].off);
                        uint64_t n = *dfn->vpt[i];
                        u->high = n >> 32;
                        u->low = n & 0xffffffff;
		}
		
		row = netsnmp_tdata_create_row();
		if (!row)
			break;
		row->data = ent;
		netsnmp_tdata_row_add_index(row, ASN_INTEGER,
					    &ent->vbeIndex,
					    sizeof(ent->vbeIndex));
		netsnmp_tdata_add_row(table_data, row);
	}
	return 0;
}
		
void
backendTable_free(netsnmp_cache *cache, void *vmagic)
{
	netsnmp_tdata  *table = (netsnmp_tdata *) vmagic;
	netsnmp_tdata_row *row;

	DEBUGMSGTL(("varnish_mib:backend", "freeing backend table\n"));
	while ((row = netsnmp_tdata_row_first(table))) {
                struct backendTable_entry *entry = row->data;
		free(entry->vbeIdent);
		SNMP_FREE(entry);
		netsnmp_tdata_remove_and_delete_row(table, row);
	}
}

static struct backend_dfn *
dfn_lookup(be_string_t *vclname, be_string_t *label)
{
	size_t namelen;
	struct backend_dfn *dfn;

	namelen = vclname->len + label->len + 1;
	VTAILQ_FOREACH(dfn, &backends, list) {
		if (dfn->vbeIdent_len == namelen
		    && memcmp(dfn->vbeIdent, vclname->start, vclname->len) == 0
		    && dfn->vbeIdent[vclname->len] == '.'
		    && memcmp(dfn->vbeIdent + vclname->len + 1,
			      label->start, label->len) == 0)
			return dfn;
	}
	return NULL;
}

static u_long
to_port(be_string_t *str)
{
	int i;
	u_long v = 0;
	for (i = 0; i < str->len; i++) {
		static char dig[] = "0123456789";
		char *p = strchr(dig, str->start[i]);
		if (!p)
			return 0;
		v = v * 10 + p - dig;
	}
	return v;
}

static void
uint32_to_bytes (unsigned char *bytes, uint32_t u)
{
	int i;

	for (i = 0; i < 4; i++)	{
		bytes[i] = u & 0xff;
		u >>= 8;
	}
}

static void
update_dfn(be_string_t *label, be_string_t *host, be_string_t *port,
	   void *data)
{
	struct backend_dfn *dfn;
	
	dfn = dfn_lookup(data, label);
	if (!dfn)
		return;
	dfn->vbePort = to_port(port);
	if (host->len) {
		struct addrinfo *res, *ap;
		int rc;
		char *node;

		node = malloc(host->len+1);
		AN(node);
		memcpy(node, host->start, host->len);
		node[host->len] = 0;
		rc = getaddrinfo(node, NULL, NULL, &res);
		free(node);
		if (rc)
			return;
		for (ap = res; ap; ap = ap->ai_next) {
			if (ap->ai_addr->sa_family == AF_INET) {
				if (dfn->vbeIPv4_len == 0) {
					struct sockaddr_in *s =
						(struct sockaddr_in *)ap->ai_addr;
					uint32_to_bytes((unsigned char*)dfn->vbeIPv4,
							s->sin_addr.s_addr);
					dfn->vbeIPv4_len = 4;
				}
			} else if (ap->ai_addr->sa_family == AF_INET6) {
				if (dfn->vbeIPv4_len == 0) {
					struct sockaddr_in6 *s =
						(struct sockaddr_in6 *)ap->ai_addr;
					dfn->vbeIPv6_len = 16;
					memcpy(dfn->vbeIPv6, &s->sin6_addr,
					       dfn->vbeIPv6_len);
				}
			}
		}
		freeaddrinfo(res);
	}
}

static int
backend_parse(struct vcli_conn *conn, be_string_t *name)
{
	if (vcli_asprintf(conn, "vcl.show %*.*s\n",
			  (int)name->len, (int)name->len,
			  name->start)
	    || vcli_write(conn))
		return SNMP_ERR_GENERR;

	if (vcli_read_response(conn))
		return SNMP_ERR_GENERR;
	
	if (conn->resp != CLIS_OK) {
		snmp_log(LOG_ERR, "vcl.show command rejected: %u %s\n",
			 conn->resp, conn->base);
		return SNMP_ERR_GENERR;
	}
	backend_parser(conn->base, conn->bufsize, update_dfn, name);
	return 0;
}

struct backend_name {
	be_string_t str;
	VTAILQ_ENTRY(backend_name) list;
};
VTAILQ_HEAD(backend_name_list, backend_name);

static void
backend_name_add(struct backend_name_list *namelist,
		 char const *str, size_t len)
{
	struct backend_name *name;
	VTAILQ_FOREACH(name, namelist, list) {
		if (name->str.len == len
		    && memcmp(name->str.start, str, len) == 0)
			return;
	}
	name = SNMP_MALLOC_TYPEDEF(struct backend_name);
	AN(name);
	name->str.start = str;
	name->str.len = len;
	VTAILQ_INSERT_TAIL(namelist, name, list);
}

int
backend_collect_addr(struct vsm *vsm)
{
	struct backend_dfn *dfn;
	struct vcli_conn conn;
	int rc;
	struct backend_name_list namelist
		= VTAILQ_HEAD_INITIALIZER(namelist);
	
	DEBUGMSGTL(("varnish_mib:backend", "getting backend info\n"));
	
	rc = vcli_connect(vsm, &conn);
	if (rc != SNMP_ERR_NOERROR)
		return rc;

	/* Select backend names */
	VTAILQ_FOREACH(dfn, &backends, list) {
		int i;
		for (i = 0; i < dfn->vbeIdent_len; i++)
			if (dfn->vbeIdent[i] == '.')
				break;
		if (i == dfn->vbeIdent_len)
			continue;
		backend_name_add(&namelist, dfn->vbeIdent, i);
	}

	while (!VTAILQ_EMPTY(&namelist)) {
		struct backend_name *name = VTAILQ_FIRST(&namelist);
		VTAILQ_REMOVE(&namelist, name, list);
		backend_parse(&conn, &name->str);
	}
	
	vcli_disconnect(&conn);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.