git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/nagios-plugins-contrib-24.20190301~bpo9+1/check_varnish/varnish-nagios/check_varnish.c
blob: 809657c58caadc8e245b75d9dac24e181c7103ed (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*-
 * Copyright (c) 2007-2011 Varnish Software AS
 * All rights reserved.
 *
 * Author: Cecilie Fritzvold 
 * Author: Tollef Fog Heen 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id$
 *
 * Nagios plugin for Varnish
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if defined(HAVE_VARNISHAPI_4) || defined(HAVE_VARNISHAPI_4_1)
#include 
#include 
#elif defined(HAVE_VARNISHAPI_3)
#include "vsc.h"
#include "varnishapi.h"
#endif

static int verbose = 0;

struct range {
	intmax_t	lo;
	intmax_t	hi;
	int		inverted:1;
	int		defined:1;
};

static struct range critical;
static struct range warning;

enum {
	NAGIOS_OK = 0,
	NAGIOS_WARNING = 1,
	NAGIOS_CRITICAL = 2,
	NAGIOS_UNKNOWN = 3,
};

static const char *status_text[] = {
	[NAGIOS_OK] = "OK",
	[NAGIOS_WARNING] = "WARNING",
	[NAGIOS_CRITICAL] = "CRITICAL",
	[NAGIOS_UNKNOWN] = "UNKNOWN",
};

/*
 * Parse a range specification
 */
static int
parse_range(const char *spec, struct range *range)
{
	const char *delim;
	char *end;

	/*  _æ_  means invert the range */
	if (*spec == ' _æ_ ') {
		++spec;
		range->inverted = 1;
	} else {
		range->inverted = 0;
	}

	/* empty spec... */
	if (*spec == '\0')
		return (-1);

	if ((delim = strchr(spec, ':')) != NULL) {
		/*
		 * The Nagios plugin documentation says nothing about how
		 * to interpret ":N", so we disallow it.  Allowed forms
		 * are "~:N", "~:", "M:" and "M:N".
		 */
		if (delim - spec == 1 && *spec == '~') {
			range->lo = INTMAX_MIN;
		} else {
			range->lo = strtoimax(spec, &end, 10);
			if (end != delim)
				return (-1);
		}
		if (*(delim + 1) != '\0') {
			range->hi = strtoimax(delim + 1, &end, 10);
			if (*end != '\0')
				return (-1);
		} else {
			range->hi = INTMAX_MAX;
		}
	} else {
		/*
		 * Allowed forms are N
		 */
		range->lo = 0;
		range->hi = strtol(spec, &end, 10);
		if (*end != '\0')
			return (-1);
	}

	/*
	 * Sanity
	 */
	if (range->lo > range->hi)
		return (-1);

	range->defined = 1;
	return (0);
}

/*
 * Check if a given value is within a given range.
 */
static int
inside_range(intmax_t value, const struct range *range)
{

	if (range->inverted)
		return (value < range->lo || value > range->hi);
	return (value >= range->lo && value <= range->hi);
}

/*
 * Check if the thresholds against the value and return the appropriate
 * status code.
 */
static int
check_thresholds(intmax_t value)
{

	if (!warning.defined && !critical.defined)
		return (NAGIOS_UNKNOWN);
	if (critical.defined && !inside_range(value, &critical))
		return (NAGIOS_CRITICAL);
	if (warning.defined && !inside_range(value, &warning))
		return (NAGIOS_WARNING);
	return (NAGIOS_OK);
}

struct stat_priv {
	char *param;
	const char *info;
	intmax_t value;
	int found;
	intmax_t cache_hit;
	intmax_t cache_miss;
};

static int
check_stats_cb(void *priv, const struct VSC_point * const pt)
{
	struct stat_priv *p;
	char tmp[1024];

	if (pt == NULL)
		return(0);

#if defined(HAVE_VARNISHAPI_4_1) || defined(HAVE_VARNISHAPI_4)
	assert(sizeof(tmp) > (strlen(pt->section->fantom->type) + 1 +
			      strlen(pt->section->fantom->ident) + 1 +
			      strlen(pt->desc->name) + 1));
	snprintf(tmp, sizeof(tmp), "%s%s%s%s%s",
		(pt->section->fantom->type[0] == 0 ? "" : pt->section->fantom->type),
		(pt->section->fantom->type[0] == 0 ? "" : "."),
		(pt->section->fantom->ident[0] == 0 ? "" : pt->section->fantom->ident),
		(pt->section->fantom->ident[0] == 0 ? "" : "."),
		 pt->desc->name);
	p = priv;
#endif
#if defined(HAVE_VARNISHAPI_4_1)
	assert(!strcmp(pt->desc->ctype, "uint64_t"));
#elif defined(HAVE_VARNISHAPI_4)
	assert(!strcmp(pt->desc->fmt, "uint64_t"));
#elif defined(HAVE_VARNISHAPI_3)
	assert(sizeof(tmp) > (strlen(pt->class) + 1 +
			      strlen(pt->ident) + 1 +
			      strlen(pt->name) + 1));
	snprintf(tmp, sizeof(tmp), "%s%s%s%s%s",
		(pt->class[0] == 0 ? "" : pt->class),
		(pt->class[0] == 0 ? "" : "."),
		(pt->ident[0] == 0 ? "" : pt->ident),
		(pt->ident[0] == 0 ? "" : "."),
		 pt->name);
	p = priv;
	assert(!strcmp(pt->fmt, "uint64_t"));
#endif
	if (strcmp(tmp, p->param) == 0) {
		p->found = 1;
#if defined(HAVE_VARNISHAPI_4) || defined(HAVE_VARNISHAPI_4_1)
		p->info = pt->desc->sdesc;
#elif defined(HAVE_VARNISHAPI_3)
		p->info = pt->desc;
#endif
		p->value = *(const volatile uint64_t*)pt->ptr;
	} else if (strcmp(p->param, "ratio") == 0) {
		if (strcmp(tmp, "cache_hit") == 0 || strcmp(tmp, "MAIN.cache_hit") == 0) {
			p->found = 1;
			p->cache_hit = *(const volatile uint64_t*)pt->ptr;
		} else if (strcmp(tmp, "cache_miss") == 0 || strcmp(tmp, "MAIN.cache_miss") == 0) {
			p->cache_miss = *(const volatile uint64_t*)pt->ptr;
		}
	}
	return (0);
}

/*
 * Check the statistics for the requested parameter.
 */
static void
check_stats(struct VSM_data *vd, char *param)
{
	int status;
	struct stat_priv priv;

	priv.found = 0;
	priv.param = param;

#if defined(HAVE_VARNISHAPI_4) || defined(HAVE_VARNISHAPI_4_1)
	(void)VSC_Iter(vd, NULL, check_stats_cb, &priv);
#elif defined(HAVE_VARNISHAPI_3)
	(void)VSC_Iter(vd, check_stats_cb, &priv);
#endif
	if (strcmp(param, "ratio") == 0) {
		intmax_t total = priv.cache_hit + priv.cache_miss;
		priv.value = total ? (100 * priv.cache_hit / total) : 0;
		priv.info = "Cache hit ratio";
	}
	if (priv.found != 1) {
		printf("Unknown parameter '%s'\n", param);
		exit(1);
	}

	status = check_thresholds(priv.value);
	printf("VARNISH %s: %s (%'jd)|%s=%jd\n", status_text[status],
	       priv.info, priv.value, param, priv.value);
	exit(status);
}

/*-------------------------------------------------------------------------------*/

static void
help(void)
{

	fprintf(stderr, "usage: "
	    "check_varnish [-lv] [-n varnish_name] [-p param_name [-c N] [-w N]]\n"
	    "\n"
	    "-v              Increase verbosity.\n"
	    "-n varnish_name Specify the Varnish instance name\n"
	    "-p param_name   Specify the parameter to check (see below).\n"
	    "                The default is 'ratio'.\n"
	    "-c [ _æ_ ][lo:]hi   Set critical threshold\n"
	    "-w [ _æ_ ][lo:]hi   Set warning threshold\n"
	    "\n"
	    "All items reported by varnishstat(1) are available - use the\n"
	    "identifier listed in the left column by 'varnishstat -l'.\n"
	    "\n"
	    "Examples for Varnish 3.x:\n"
	    "\n"
	    "uptime          How long the cache has been running (in seconds)\n"
	    "ratio           The cache hit ratio expressed as a percentage of hits to\n"
	    "                hits + misses.  Default thresholds are 95 and 90.\n"
	    "usage           Cache file usage as a percentage of the total cache space.\n"
	    "\n"
	    "Examples for Varnish 4.x:\n"
	    "\n"
	    "ratio           The cache hit ratio expressed as a percentage of hits to\n"
	    "                hits + misses.  Default thresholds are 95 and 90.\n"
	    "MAIN.uptime     How long the cache has been running (in seconds).\n"
	    "MAIN.n_purges   Number of purge operations executed.\n"
	);
	exit(0);
}

static void
usage(void)
{

	fprintf(stderr, "usage: "
	    "check_varnish [-lv] [-n varnish_name] [-p param_name [-c N] [-w N]]\n");
	exit(3);
}


int
main(int argc, char **argv)
{
	struct VSM_data *vd;
	char *param = NULL;
	int opt;

	setlocale(LC_ALL, "");

	vd = VSM_New();
#if defined(HAVE_VARNISHAPI_3)
	VSC_Setup(vd);
#endif

	while ((opt = getopt(argc, argv, VSC_ARGS "c:hn:p:vw:")) != -1) {
		switch (opt) {
		case 'c':
			if (parse_range(optarg, &critical) != 0)
				usage();
			break;
		case 'h':
			help();
			break;
		case 'n':
			VSC_Arg(vd, opt, optarg);
			break;
		case 'p':
			param = strdup(optarg);
			break;
		case 'v':
			++verbose;
			break;
		case 'w':
			if (parse_range(optarg, &warning) != 0)
				usage();
			break;
		default:
			if (VSC_Arg(vd, opt, optarg) > 0)
				break;
			usage();
		}
	}

#if defined(HAVE_VARNISHAPI_4) || defined(HAVE_VARNISHAPI_4_1)
	if (VSM_Open(vd))
		exit(1);
#elif defined(HAVE_VARNISHAPI_3)
	if (VSC_Open(vd, 1))
		exit(1);
#endif

	/* Default: if no param specified, check hit ratio.  If no warning
	 * and critical values are specified either, set these to default.
	 */
	if (param == NULL) {
		param = strdup("ratio");
		if (!warning.defined)
			parse_range("95:", &warning);
		if (!critical.defined)
			parse_range("90:", &critical);
	}

	if (!param)
		usage();

	check_stats(vd, param);

	exit(0);
}