File: o2hbmonitor.c

package info (click to toggle)
ocfs2-tools 1.8.4-4+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • sloc: ansic: 85,325; sh: 5,702; python: 2,380; makefile: 1,265
file content (431 lines) | stat: -rw-r--r-- 8,575 bytes parent folder | download | duplicates (2)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * o2hbmonitor.c
 *
 * Monitors o2hb
 *
 * Copyright (C) 2010 Oracle.  All rights reserved.
 *
 * This program 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 2 of the License, or (at your option) any later version.
 *
 * 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.
 */

/*
 * This utility requires the o2hb debugfs file elapsed_time_in_ms which shows
 * the time since the o2hb heartbeat timer was last armed.  This file was added
 * in the mainline kernel via commit 43695d095dfaf266a8a940d9b07eed7f46076b49.
 *
 * This utility scans the configfs to see if the cluster is up. If not up, it
 * checks again after CONFIG_POLL_IN_SECS.
 *
 * If up, it loads the dead threshold and then scans the debugfs file,
 * elapsed_time_in_ms, of each heartbeat region. If the elapsed time is
 * greater than the warn threshold, it logs a message in syslog.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <linux/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <libgen.h>
#include <syslog.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#define SYS_CONFIG_DIR			"/sys/kernel/config"
#define O2HB_CLUSTER_DIR		SYS_CONFIG_DIR"/cluster"
#define O2HB_HEARTBEAT_DIR		O2HB_CLUSTER_DIR"/%s/heartbeat"
#define O2HB_DEAD_THRESHOLD		O2HB_HEARTBEAT_DIR"/threshold"
#define O2HB_DEVICE			O2HB_HEARTBEAT_DIR"/%s/dev"

#define SYS_DEBUG_DIR			"/sys/kernel/debug"
#define O2HB_DEBUG_DIR			SYS_DEBUG_DIR"/o2hb"
#define O2HB_ELAPSED_TIME		O2HB_DEBUG_DIR"/%s/elapsed_time_in_ms"

#define DEAD_THRESHOLD_IN_MSECS(a)	(((a) - 1) * 2000)
#define WARN_THRESHOLD_PERCENT		50

#define CONFIG_POLL_IN_SECS		60
#define SLOW_POLL_IN_SECS		10
#define FAST_POLL_IN_SECS		2

#define O2HB_SEM_MAGIC_KEY		0x6F326862

char *progname;
int interactive;
int warn_threshold_percent;
int verbose;

char *cluster_name;
unsigned long dead_threshold_in_ms;
unsigned long warn_threshold_in_ms;
unsigned long poll_in_secs;

static void show_version(void)
{
	fprintf(stderr, "%s %s\n", progname, VERSION);
}

static char *do_strchomp(char *str)
{
	int len = strlen(str);
	char *p;

	if (!len)
		return str;

	p = str + len - 1;
	while ((len--) && (isspace(*p) || (*p == '\n')))
		*p-- = '\0';

	return str;
}

static int get_value(char *path, char *value, int count)
{
	int fd = -1, ret = -1;
	char *p = value;

	fd = open(path, O_RDONLY);
	if (fd > 0)
		ret = read(fd, value, count);
	if (ret > 0) {
		p += ret;
		*p = '\0';
		ret = 0;
	}

	if (!ret)
		do_strchomp(value);

	if (fd > -1)
		close(fd);
	return ret;
}

static void get_device_name(char *region, char **device)
{
	int ret;
	char val[255];
	char path[PATH_MAX];

	sprintf(path, O2HB_DEVICE, cluster_name, region);
	ret = get_value(path, val, sizeof(val));
	if (ret)
		goto bail;
	*device = strdup(val);

bail:
	return ;
}

static void process_elapsed_time(char *region, unsigned long elapsed)
{
	int warn = 0;
	char *device = NULL;

	if (elapsed >= warn_threshold_in_ms)
		warn++;

	if (!verbose && !warn)
		return;

	get_device_name(region, &device);

	if (verbose)
		fprintf(stdout, "Last ping %lu msecs ago on /dev/%s, %s\n",
		       elapsed, device, region);

	if (warn) {
		poll_in_secs = FAST_POLL_IN_SECS;
		syslog(LOG_WARNING, "Last ping %lu msecs ago on /dev/%s, %s\n",
		       elapsed, device, region);
	}

	if (device)
		free(device);
}

static int read_elapsed_time(char *region, unsigned long *elapsed)
{
	int ret;
	char val[32];
	char path[PATH_MAX];

	*elapsed = 0;

	sprintf(path, O2HB_ELAPSED_TIME, region);
	ret = get_value(path, val, sizeof(val));
	if (ret)
		goto bail;
	*elapsed = strtoul(val, NULL, 0);

	ret = 0;

bail:
	return ret;
}

static void scan_heartbeat_regions(void)
{
	int ret = -1;
	DIR *dir = NULL;
	struct dirent *ent;
	char path[PATH_MAX];
	unsigned long elapsed;

	sprintf(path, O2HB_DEBUG_DIR);

	dir = opendir(path);
	if (!dir)
		return;

	do {
		ent = readdir(dir);
		if (ent && ent->d_type == DT_DIR && strcmp(ent->d_name, ".") &&
		    strcmp(ent->d_name, "..")) {
			ret = read_elapsed_time(ent->d_name, &elapsed);
			if (!ret)
				process_elapsed_time(ent->d_name, elapsed);
		}
	} while (ent);

	if (dir)
		closedir(dir);
}

static int populate_thresholds(void)
{
	int ret;
	char val[32];
	char path[PATH_MAX];

	sprintf(path, O2HB_DEAD_THRESHOLD, cluster_name);
	ret = get_value(path, val, sizeof(val));
	if (!ret) {
		dead_threshold_in_ms =
			DEAD_THRESHOLD_IN_MSECS(strtoul(val, NULL, 0));
		warn_threshold_in_ms =
			(dead_threshold_in_ms * warn_threshold_percent / 100);
	}

	return ret;
}

static int populate_cluster(void)
{
	DIR *dir;
	struct dirent *ent;

	if (cluster_name) {
		free(cluster_name);
		cluster_name = NULL;
	}

	dir = opendir(O2HB_CLUSTER_DIR);
	if (!dir)
		return -1;

	do {
		ent = readdir(dir);
		if (ent && ent->d_type == 4 && strcmp(ent->d_name, ".") &&
		    strcmp(ent->d_name, "..")) {
			cluster_name = strdup(ent->d_name);
			break;
		}
	} while (ent);

	closedir(dir);

	if (cluster_name)
		return 0;

	return -1;
}

static int is_cluster_up(void)
{
	struct stat buf;
	int status;
	static int warn_count = 0;

	status = stat(O2HB_CLUSTER_DIR, &buf);
	if (status)
		return 0;

	status = stat(O2HB_DEBUG_DIR, &buf);
	if (status) {
		if (!(warn_count++ % 10))
			syslog(LOG_WARNING,
			       "mount debugfs at /sys/kernel/debug");
		return 0;
	}

	return 1;
}

static void monitor(void)
{
	int ret;

	while (1) {
		if (!is_cluster_up()) {
			sleep(CONFIG_POLL_IN_SECS);
			continue;
		}

		ret = populate_cluster();
		if (!ret)
			ret = populate_thresholds();
		if (ret) {
			sleep(CONFIG_POLL_IN_SECS);
			continue;
		}

		poll_in_secs = SLOW_POLL_IN_SECS;

		scan_heartbeat_regions();

		sleep(poll_in_secs);
	}
}

static int islocked(void)
{
	int semid;
	struct sembuf trylock[1] = {
		{.sem_num = 0, .sem_op = 0, .sem_flg = SEM_UNDO|IPC_NOWAIT},
	};

	semid = semget(O2HB_SEM_MAGIC_KEY, 1, 0);
	if (semid < 0)
		return 0;
	if (semop(semid, trylock, 1) < 0)
		return 1;
	return 0;
}

static int getlock(void)
{
	int semid, vals[1] = { 0 };
	struct sembuf trylock[2] = {
		{.sem_num = 0, .sem_op = 0, .sem_flg = SEM_UNDO|IPC_NOWAIT},
		{.sem_num = 0, .sem_op = 1, .sem_flg = SEM_UNDO|IPC_NOWAIT},
	};

	semid = semget(O2HB_SEM_MAGIC_KEY, 1, 0);
	if (semid < 0) {
		semid = semget(O2HB_SEM_MAGIC_KEY, 1,
			       IPC_CREAT|IPC_EXCL|S_IRUSR);
		if (semid < 0)
			goto out;
		semctl(semid, 0, SETALL, vals);
		if (semop(semid, trylock, 2) < 0)
			goto out;
		else
			return 0;
	}
	if (semop(semid, trylock, 2) < 0)
		goto out;
	return 0;
out:
	if (errno == EAGAIN) {
		syslog(LOG_WARNING, "Another instance of %s is already running."
		       " Aborting.\n", progname);
		return 1;
	}
	return 0;
}

static void usage(void)
{
	fprintf(stderr, "usage: %s [-w percent] -[ivV]\n", progname);
	fprintf(stderr, "\t -w, Warn threshold percent (default 50%%)\n");
	fprintf(stderr, "\t -i, Interactive\n");
	fprintf(stderr, "\t -v, Verbose\n");
	fprintf(stderr, "\t -V, Version\n");
	exit(1);
}

int main(int argc, char **argv)
{
	int c, ret, version = 0;

	/* init globals */
	progname = basename(argv[0]);
	interactive = 0;
	warn_threshold_percent = WARN_THRESHOLD_PERCENT;
	verbose = 0;
	cluster_name = NULL;

	while (1) {
		c = getopt(argc, argv, "w:i?hvV");
		if (c == -1)
			break;
		switch (c) {
		case 'i':
			interactive = 1;
			break;
		case 'v':
			++verbose;
			break;
		case 'w':
			warn_threshold_percent = strtoul(optarg, NULL, 0);
			if (warn_threshold_percent < 1 ||
			    warn_threshold_percent > 99)
				warn_threshold_percent = WARN_THRESHOLD_PERCENT;
			break;
		case 'V':
			version = 1;
			break;
		case '?':
		case 'h':
		default:
			usage();
			break;
		}
	}

	if (version)
		show_version();

	if (islocked()) {
		fprintf(stderr, "Another instance of %s is already running. "
		       "Aborting.\n", progname);
		return 1;
	}

	if (!interactive) {
		ret = daemon(0, verbose);
		if (ret)
			fprintf(stderr, "Unable to daemonize, %s\n",
				strerror(errno));
	}

	openlog(progname, LOG_CONS|LOG_NDELAY, LOG_DAEMON);
	ret = getlock();
	if (ret) {
		closelog();
		return ret;
	}

	syslog(LOG_INFO, "Starting\n");
	monitor();
	closelog();

	return 0;
}