summaryrefslogtreecommitdiff
path: root/usr.sbin/rpki-client/output-ometric.c
blob: df6ef097671151def361680bc52785f908c9cb2c (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
/*	$OpenBSD: output-ometric.c,v 1.1 2022/12/15 12:02:29 claudio Exp $ */
/*
 * Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "extern.h"
#include "ometric.h"
#include "version.h"

struct ometric *rpki_info, *rpki_completion_time, *rpki_duration;
struct ometric *rpki_repo, *rpki_obj, *rpki_ta_obj;
struct ometric *rpki_repo_obj, *rpki_repo_duration, *rpki_repo_state;

static const char * const repo_states[2] = { "failed", "synced" };

static void
set_common_stats(const struct repostats *in, struct ometric *metric,
    struct olabels *ol)
{
	ometric_set_int_with_labels(metric, in->certs,
	    OKV("type", "state"), OKV("cert", "valid"), ol);
	ometric_set_int_with_labels(metric, in->certs_fail,
	    OKV("type", "state"), OKV("cert", "failed parse"), ol);

	ometric_set_int_with_labels(metric, in->mfts,
	    OKV("type", "state"), OKV("manifest", "valid"), ol);
	ometric_set_int_with_labels(metric, in->mfts_fail,
	    OKV("type", "state"), OKV("manifest", "failed parse"), ol);
	ometric_set_int_with_labels(metric, in->mfts_stale,
	    OKV("type", "state"), OKV("manifest", "stale"), ol);

	ometric_set_int_with_labels(metric, in->roas,
	    OKV("type", "state"), OKV("roa", "valid"), ol);
	ometric_set_int_with_labels(metric, in->roas_fail,
	    OKV("type", "state"), OKV("roa", "failed parse"), ol);
	ometric_set_int_with_labels(metric, in->roas_invalid,
	    OKV("type", "state"), OKV("roa", "invalid"), ol);

	ometric_set_int_with_labels(metric, in->aspas,
	    OKV("type", "state"), OKV("aspa", "valid"), ol);
	ometric_set_int_with_labels(metric, in->aspas_fail,
	    OKV("type", "state"), OKV("aspa", "failed parse"), ol);
	ometric_set_int_with_labels(metric, in->aspas_invalid,
	    OKV("type", "state"), OKV("aspa", "invalid"), ol);

	ometric_set_int_with_labels(metric, in->brks,
	    OKV("type", "state"), OKV("router_key", "valid"), ol);
	ometric_set_int_with_labels(metric, in->crls,
	    OKV("type", "state"), OKV("crl", "valid"), ol);
	ometric_set_int_with_labels(metric, in->gbrs,
	    OKV("type", "state"), OKV("gbr", "valid"), ol);
	ometric_set_int_with_labels(metric, in->taks,
	    OKV("type", "state"), OKV("tak", "valid"), ol);

	ometric_set_int_with_labels(metric, in->vrps,
	    OKV("type", "state"), OKV("vrp", "total"), ol);
	ometric_set_int_with_labels(metric, in->vrps_uniqs,
	    OKV("type", "state"), OKV("vrp", "unique"), ol);

	ometric_set_int_with_labels(metric, in->vaps,
	    OKV("type", "state"), OKV("vap", "total"), ol);
	ometric_set_int_with_labels(metric, in->vaps_uniqs,
	    OKV("type", "state"), OKV("vap", "unique"), ol);
	ometric_set_int_with_labels(metric, in->vaps_pas,
	    OKV("type", "state"), OKV("vap providers", "both"), ol);
	ometric_set_int_with_labels(metric, in->vaps_pas4,
	    OKV("type", "state"), OKV("vap providers", "IPv4 only"), ol);
	ometric_set_int_with_labels(metric, in->vaps_pas6,
	    OKV("type", "state"), OKV("vap providers", "IPv6 only"), ol);
}

static void
ta_stats(int id)
{
	struct olabels *ol;
	const char *keys[2] = { "name", NULL };
	const char *values[2];

	values[0] = taldescs[id];
	values[1] = NULL;

	ol = olabels_new(keys, values);
	set_common_stats(&talstats[id], rpki_ta_obj, ol);
	olabels_free(ol);
}

static void
repo_stats(const struct repo *rp, const struct repostats *in, void *arg)
{
	struct olabels *ol;
	const char *keys[4] = { "name", "carepo", "notify", NULL };
	const char *values[4];

	values[0] = taldescs[repo_talid(rp)];
	repo_fetch_uris(rp, &values[1], &values[2]);
	values[3] = NULL;

	ol = olabels_new(keys, values);
	set_common_stats(in, rpki_repo_obj, ol);
	ometric_set_timespec(rpki_repo_duration, &in->sync_time, ol);
	ometric_set_state(rpki_repo_state, repo_states[repo_synced(rp)], ol);
	olabels_free(ol);
}

int
output_ometric(FILE *out, struct vrp_tree *vrps, struct brk_tree *brks,
    struct vap_tree *vaps, struct stats *st)
{
	struct olabels *ol;
	const char *keys[4] = { "nodename", "domainname", "release", NULL };
	const char *values[4];
	char hostname[HOST_NAME_MAX + 1];
	char *domainname;
	struct timespec now_time;
	int rv, i;

	rpki_info = ometric_new(OMT_INFO, "rpki_client",
	    "rpki-client information");
	rpki_completion_time = ometric_new(OMT_GAUGE,
	    "rpki_client_job_completion_time",
	    "end of this run as epoch timestamp");

	rpki_repo = ometric_new(OMT_GAUGE, "rpki_client_repository",
	    "total number of repositories");
	rpki_obj = ometric_new(OMT_GAUGE, "rpki_client_objects",
	    "total number of objects");

	rpki_duration = ometric_new(OMT_GAUGE, "rpki_client_duration",
	    "duration in seconds");

	rpki_ta_obj = ometric_new(OMT_GAUGE, "rpki_client_ta_objects",
	    "total number of objects per TAL");
	rpki_repo_obj = ometric_new(OMT_GAUGE, "rpki_client_repository_objects",
	    "total number of objects per repository");
	rpki_repo_duration = ometric_new(OMT_GAUGE,
	    "rpki_client_repository_duration",
	    "duration used to sync this repository in seconds");
	rpki_repo_state = ometric_new_state(repo_states,
	    sizeof(repo_states) / sizeof(repo_states[0]),
	    "rpki_client_repository_state",
	    "repository state");

	/*
	 * Dump statistics
	 */
	if (gethostname(hostname, sizeof(hostname)))
		err(1, "gethostname");
	if ((domainname = strchr(hostname, '.')))
		*domainname++ = '\0';

	values[0] = hostname;
	values[1] = domainname;
	values[2] = RPKI_VERSION;
	values[3] = NULL;

	ol = olabels_new(keys, values);
	ometric_set_info(rpki_info, NULL, NULL, ol);
	olabels_free(ol);

	repo_stats_collect(repo_stats, NULL);
	for (i = 0; i < talsz; i++)
		ta_stats(i);
	set_common_stats(&st->repo_stats, rpki_obj, NULL);

	ometric_set_int(rpki_repo, st->repos, NULL);
	ometric_set_int_with_labels(rpki_repo, st->rsync_repos,
	    OKV("type", "state"), OKV("rsync", "synced"), NULL);
	ometric_set_int_with_labels(rpki_repo, st->rsync_fails,
	    OKV("type", "state"), OKV("rsync", "failed"), NULL);
	ometric_set_int_with_labels(rpki_repo, st->http_repos,
	    OKV("type", "state"), OKV("http", "synced"), NULL);
	ometric_set_int_with_labels(rpki_repo, st->http_fails,
	    OKV("type", "state"), OKV("http", "failed"), NULL);
	ometric_set_int_with_labels(rpki_repo, st->rrdp_repos,
	    OKV("type", "state"), OKV("rrdp", "synced"), NULL);
	ometric_set_int_with_labels(rpki_repo, st->rrdp_fails,
	    OKV("type", "state"), OKV("rrdp", "failed"), NULL);

	ometric_set_timespec_with_labels(rpki_duration, &st->elapsed_time,
	    OKV("type"), OKV("elapsed"), NULL);
	ometric_set_timespec_with_labels(rpki_duration, &st->user_time,
	    OKV("type"), OKV("user"), NULL);
	ometric_set_timespec_with_labels(rpki_duration, &st->system_time,
	    OKV("type"), OKV("system"), NULL);

	clock_gettime(CLOCK_REALTIME, &now_time);
	ometric_set_timespec(rpki_completion_time, &now_time, NULL);

	rv = ometric_output_all(out);
	ometric_free_all();

	return rv;
}