stats.c 6.76 KB
Newer Older
1
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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

    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 3 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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <inttypes.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <urcu.h>

#include "contrib/files.h"
#include "knot/common/stats.h"
#include "knot/common/log.h"
#include "knot/nameserver/query_module.h"

struct {
	bool active_dumper;
	pthread_t dumper;
	uint32_t timer;
	server_t *server;
} stats = { 0 };

35 36 37 38 39 40 41
typedef struct {
	FILE *fd;
	const list_t *query_modules;
	const knot_dname_t *zone;
	bool zone_emitted;
} dump_ctx_t;

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
#define DUMP_STR(fd, level, name, ...) do { \
	fprintf(fd, "%-.*s"name": %s\n", level, "    ", ##__VA_ARGS__); \
	} while (0)
#define DUMP_CTR(fd, level, name, ...) do { \
	fprintf(fd, "%-.*s"name": %"PRIu64"\n", level, "    ", ##__VA_ARGS__); \
	} while (0)

uint64_t server_zone_count(server_t *server)
{
	return knot_zonedb_size(server->zone_db);
}

const stats_item_t server_stats[] = {
	{ "zone-count", server_zone_count },
	{ 0 }
};

static void dump_counters(FILE *fd, int level, mod_ctr_t *ctr)
{
	for (uint32_t j = 0; j < ctr->count; j++) {
		// Skip empty counters.
		if (ctr->counters[j] == 0) {
			continue;
		}

		if (ctr->idx_to_str != NULL) {
			char *str = ctr->idx_to_str(j, ctr->count);
			if (str != NULL) {
				DUMP_CTR(fd, level, "%s", str, ctr->counters[j]);
				free(str);
			}
		} else {
			DUMP_CTR(fd, level, "%u", j, ctr->counters[j]);
		}
	}
}

79
static void dump_modules(dump_ctx_t *ctx)
80
{
81
	int level = 0;
82
	knotd_mod_t *mod = NULL;
83
	WALK_LIST(mod, *ctx->query_modules) {
84 85 86 87 88 89
		// Skip modules without statistics.
		if (mod->stats_count == 0) {
			continue;
		}

		// Dump zone name.
90
		if (ctx->zone != NULL) {
91
			// Prevent from zone section override.
92 93 94
			if (!ctx->zone_emitted) {
				DUMP_STR(ctx->fd, 0, "zone", "");
				ctx->zone_emitted = true;
95
			}
96
			level = 1;
97 98

			char name[KNOT_DNAME_TXT_MAXLEN + 1];
99
			if (knot_dname_to_str(name, ctx->zone, sizeof(name)) == NULL) {
100 101
				return;
			}
102
			DUMP_STR(ctx->fd, level++, "\"%s\"", name, "");
103 104 105 106 107
		} else {
			level = 0;
		}

		// Dump module counters.
108
		DUMP_STR(ctx->fd, level, "%s", mod->id->name + 1, "");
109 110 111 112 113 114 115 116
		for (int i = 0; i < mod->stats_count; i++) {
			mod_ctr_t *ctr = mod->stats + i;
			if (ctr->name == NULL) {
				// Empty counter.
				continue;
			}
			if (ctr->count == 1) {
				// Simple counter.
117
				DUMP_CTR(ctx->fd, level + 1, "%s", ctr->name, ctr->counter);
118 119
			} else {
				// Array of counters.
120 121
				DUMP_STR(ctx->fd, level + 1, "%s", ctr->name, "");
				dump_counters(ctx->fd, level + 2, ctr);
122 123 124 125 126
			}
		}
	}
}

127
static void zone_stats_dump(zone_t *zone, dump_ctx_t *ctx)
128 129 130 131 132
{
	if (EMPTY_LIST(zone->query_modules)) {
		return;
	}

133 134 135 136
	ctx->query_modules = &zone->query_modules;
	ctx->zone = zone->name;

	dump_modules(ctx);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

static void dump_to_file(FILE *fd, server_t *server)
{
	char date[64] = "";

	// Get formated current time string.
	struct tm tm;
	time_t now = time(NULL);
	localtime_r(&now, &tm);
	strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%S%z", &tm);

	// Get the server identity.
	conf_val_t val = conf_get(conf(), C_SRV, C_IDENT);
	const char *ident = conf_str(&val);
152
	if (ident == NULL || ident[0] == '\0') {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		ident = conf()->hostname;
	}

	// Dump record header.
	fprintf(fd,
	        "---\n"
	        "time: %s\n"
	        "identity: %s\n",
	        date, ident);

	// Dump server statistics.
	DUMP_STR(fd, 0, "server", "");
	for (const stats_item_t *item = server_stats; item->name != NULL; item++) {
		DUMP_CTR(fd, 1, "%s", item->name, item->val(server));
	}

169 170
	dump_ctx_t ctx = {
		.fd = fd,
171
		.query_modules = conf()->query_modules,
172 173
	};

174
	// Dump global statistics.
175
	dump_modules(&ctx);
176 177

	// Dump zone statistics.
178
	knot_zonedb_foreach(server->zone_db, zone_stats_dump, &ctx);
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
}

static void dump_stats(server_t *server)
{
	conf_val_t val = conf_get(conf(), C_SRV, C_RUNDIR);
	char *rundir = conf_abs_path(&val, NULL);
	val = conf_get(conf(), C_STATS, C_FILE);
	char *file_name = conf_abs_path(&val, rundir);
	free(rundir);

	val = conf_get(conf(), C_STATS, C_APPEND);
	bool append = conf_bool(&val);

	// Open or create output file.
	FILE *fd = NULL;
	char *tmp_name = NULL;
	if (append) {
		fd = fopen(file_name, "a");
		if (fd == NULL) {
			log_error("stats, failed to append file '%s' (%s)",
			          file_name, knot_strerror(knot_map_errno()));
			free(file_name);
			return;
		}
	} else {
		int ret = open_tmp_file(file_name, &tmp_name, &fd,
		                        S_IRUSR | S_IWUSR | S_IRGRP);
		if (ret != KNOT_EOK) {
			log_error("stats, failed to open file '%s' (%s)",
			          file_name, knot_strerror(ret));
			free(file_name);
			return;
		}
	}
	assert(fd);

	// Dump stats into the file.
	dump_to_file(fd, server);

	fflush(fd);
	fclose(fd);

	// Switch the file contents.
	if (!append) {
		int ret = rename(tmp_name, file_name);
		if (ret != 0) {
			log_error("stats, failed to access file '%s' (%s)",
			          file_name, knot_strerror(knot_map_errno()));
			unlink(tmp_name);
		}
		free(tmp_name);
	}

	log_debug("stats, dumped into file '%s'", file_name);
	free(file_name);
}

static void *dumper(void *data)
{
	while (true) {
		assert(stats.timer > 0);
		sleep(stats.timer);

		pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
		rcu_read_lock();
		dump_stats(stats.server);
		rcu_read_unlock();
		pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	}

	return NULL;
}

void stats_reconfigure(conf_t *conf, server_t *server)
{
	if (conf == NULL || server == NULL) {
		return;
	}

	// Update server context.
	stats.server = server;

	conf_val_t val = conf_get(conf, C_STATS, C_TIMER);
	stats.timer = conf_int(&val);
	if (stats.timer > 0) {
		// Check if dumping is already running.
		if (stats.active_dumper) {
			return;
		}

		int ret = pthread_create(&stats.dumper, NULL, dumper, NULL);
		if (ret != 0) {
			log_error("stats, failed to launch periodic dumping (%s)",
			          knot_strerror(knot_map_errno_code(ret)));
		} else {
			stats.active_dumper = true;
		}
	// Stop current dumping.
	} else if (stats.active_dumper) {
		pthread_cancel(stats.dumper);
		pthread_join(stats.dumper, NULL);
		stats.active_dumper = false;
	}
}

void stats_deinit(void)
{
	if (stats.active_dumper) {
		pthread_cancel(stats.dumper);
		pthread_join(stats.dumper, NULL);
	}

	memset(&stats, 0, sizeof(stats));
}