summaryrefslogtreecommitdiffstats
path: root/src/stats/main.c
blob: 15e07338fa8e25f6e33f788430a342657e90a07b (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
/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */

#include "stats-common.h"
#include "restrict-access.h"
#include "ioloop.h"
#include "master-service.h"
#include "master-service-settings.h"
#include "stats-settings.h"
#include "stats-event-category.h"
#include "stats-metrics.h"
#include "stats-service.h"
#include "client-writer.h"
#include "client-reader.h"
#include "client-http.h"

struct stats_metrics *stats_metrics;
time_t stats_startup_time;

static const struct stats_settings *stats_settings;

static bool client_is_writer(const char *path)
{
	const char *name, *suffix;

	name = strrchr(path, '/');
	if (name == NULL)
		name = path;
	else
		name++;

	suffix = strrchr(name, '-');
	if (suffix == NULL)
		suffix = name;
	else
		suffix++;

	return strcmp(suffix, "writer") == 0;
}

static void client_connected(struct master_service_connection *conn)
{
	if (strcmp(conn->name, "http") == 0)
		client_http_create(conn);
	else if (client_is_writer(conn->name))
		client_writer_create(conn->fd);
	else
		client_reader_create(conn->fd);
	master_service_client_connection_accept(conn);
}

static void stats_die(void)
{
	/* just wait for existing stats clients to disconnect from us */
}

static void main_preinit(void)
{
	restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
	restrict_access_allow_coredumps(TRUE);
}

static void main_init(void)
{
	void **sets = master_service_settings_get_others(master_service);
	stats_settings = sets[0];

	stats_startup_time = ioloop_time;
	stats_metrics = stats_metrics_init(stats_settings);
	stats_event_categories_init();
	client_readers_init();
	client_writers_init();
	client_http_init(stats_settings);
	stats_services_init();
}

static void main_deinit(void)
{
	stats_services_deinit();
	client_readers_deinit();
	client_writers_deinit();
	client_http_deinit();
	stats_event_categories_deinit();
	stats_metrics_deinit(&stats_metrics);
}

int main(int argc, char *argv[])
{
	const struct setting_parser_info *set_roots[] = {
		&stats_setting_parser_info,
		NULL
	};
	const enum master_service_flags service_flags =
		MASTER_SERVICE_FLAG_NO_SSL_INIT |
		MASTER_SERVICE_FLAG_DONT_SEND_STATS |
		MASTER_SERVICE_FLAG_NO_IDLE_DIE |
		MASTER_SERVICE_FLAG_UPDATE_PROCTITLE;
	const char *error;

	master_service = master_service_init("stats", service_flags,
					     &argc, &argv, "");
	if (master_getopt(master_service) > 0)
		return FATAL_DEFAULT;
	if (master_service_settings_read_simple(master_service, set_roots,
						&error) < 0)
		i_fatal("Error reading configuration: %s", error);
	master_service_init_log(master_service);
	master_service_set_die_callback(master_service, stats_die);

	main_preinit();

	main_init();
	master_service_init_finish(master_service);
	master_service_run(master_service, client_connected);
	main_deinit();
	master_service_deinit(&master_service);
        return 0;
}