Code Coverage: vinyl-cache/bin/vinylstat/vinylstat.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 * Author: Dag-Erling Smørgrav <des@des.no>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 * Statistics output program
32
 */
33
34
#include "config.h"
35
36
#include <stdarg.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <signal.h>
40
#include <string.h>
41
#include <time.h>
42
#include <unistd.h>
43
#include <math.h>
44
45
#define VOPT_DEFINITION
46
#define VOPT_INC "vinylstat_options.h"
47
48
#include "vapi/voptget.h"
49
#include "vapi/vsl.h"
50
#include "vdef.h"
51
#include "vut.h"
52
53
#include "vinylstat.h"
54
55
static struct VUT *vut;
56
int has_f = 0;
57
58
/*--------------------------------------------------------------------*/
59
60
static int v_matchproto_(VSC_iter_f)
61
do_xml_cb(void *priv, const struct VSC_point * const pt)
62 417
{
63 417
        uint64_t val;
64
65 417
        (void)priv;
66 417
        if (pt == NULL)
67 0
                return (0);
68 417
        AZ(vstrcmp(pt->ctype, "uint64_t"));
69 417
        val = VSC_Value(pt);
70
71 417
        printf("\t<stat>\n");
72 417
        printf("\t\t<name>%s</name>\n", pt->name);
73 417
        printf("\t\t<value>%ju</value>\n", (uintmax_t)val);
74 417
        printf("\t\t<flag>%c</flag>\n", pt->semantics);
75 417
        printf("\t\t<format>%c</format>\n", pt->format);
76 417
        printf("\t\t<description>%s</description>\n", pt->sdesc);
77 417
        printf("\t</stat>\n");
78 417
        return (0);
79 417
}
80
81
static void
82
do_xml(struct vsm *vsm, struct vsc *vsc)
83 1
{
84 1
        char time_stamp[20];
85 1
        time_t now;
86
87 1
        printf("<?xml version=\"1.0\"?>\n");
88 1
        now = time(NULL);
89 1
        (void)strftime(time_stamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&now));
90 1
        printf("<vinylstat timestamp=\"%s\">\n", time_stamp);
91 1
        (void)VSC_Iter(vsc, vsm, do_xml_cb, NULL);
92 1
        printf("</vinylstat>\n");
93 1
}
94
95
96
/*--------------------------------------------------------------------*/
97
98
static int v_matchproto_(VSC_iter_f)
99
do_json_cb(void *priv, const struct VSC_point * const pt)
100 1668
{
101 1668
        const char **sep;
102 1668
        uintmax_t val;
103
104 1668
        if (pt == NULL)
105 0
                return (0);
106
107 1668
        AZ(vstrcmp(pt->ctype, "uint64_t"));
108 1668
        val = (uintmax_t)VSC_Value(pt);
109
110 1668
        sep = priv;
111
112 1668
        printf(
113 1668
            "%s"
114 1668
            "    \"%s\": {\n"
115 1668
            "      \"description\": \"%s\",\n"
116 1668
            "      \"flag\": \"%c\",\n"
117 1668
            "      \"format\": \"%c\",\n"
118 1668
            "      \"value\": %ju\n"
119 1668
            "    }",
120 1668
            *sep, pt->name, pt->sdesc, pt->semantics, pt->format, val);
121
122 1668
        *sep = ",\n";
123 1668
        return (0);
124 1668
}
125
126
static void
127
do_json(struct vsm *vsm, struct vsc *vsc)
128 4
{
129 4
        const char *sep;
130 4
        char time_stamp[20];
131 4
        time_t now;
132
133 4
        sep = "";
134 4
        now = time(NULL);
135
136 4
        (void)strftime(time_stamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&now));
137 4
        printf(
138 4
            "{\n"
139 4
            "  \"version\": 1,\n"
140 4
            "  \"timestamp\": \"%s\",\n"
141 4
            "  \"counters\": {\n", time_stamp);
142 4
        (void)VSC_Iter(vsc, vsm, do_json_cb, &sep);
143 4
        printf(
144 4
            "\n"
145 4
            "  }\n"
146 4
            "}\n");
147 4
}
148
149
150
/*--------------------------------------------------------------------*/
151
152
struct once_priv {
153
        double  up;
154
        int pad;
155
};
156
157
static int v_matchproto_(VSC_iter_f)
158
do_once_cb_first(void *priv, const struct VSC_point * const pt)
159 10
{
160 10
        struct once_priv *op;
161 10
        uint64_t val;
162
163 10
        if (pt == NULL)
164 0
                return (0);
165 10
        op = priv;
166 10
        AZ(vstrcmp(pt->ctype, "uint64_t"));
167 10
        if (vstrcmp(pt->name, "MAIN.uptime"))
168 0
                return (0);
169 10
        val = VSC_Value(pt);
170 10
        op->up = (double)val;
171 10
        return (1);
172 10
}
173
174
static int v_matchproto_(VSC_iter_f)
175
do_once_cb(void *priv, const struct VSC_point * const pt)
176 5507
{
177 5507
        struct once_priv *op;
178 5507
        uint64_t val;
179 5507
        int i;
180
181 5507
        if (pt == NULL)
182 0
                return (0);
183 5507
        op = priv;
184 5507
        AZ(vstrcmp(pt->ctype, "uint64_t"));
185 5507
        val = VSC_Value(pt);
186 5507
        i = 0;
187 5507
        i += printf("%s", pt->name);
188 5507
        if (i >= op->pad)
189 23
                op->pad = i + 1;
190 5507
        printf("%*.*s", op->pad - i, op->pad - i, "");
191 5507
        if (pt->semantics == 'c')
192 5081
                printf("%12ju %12.2f %s\n",
193 5081
                    (uintmax_t)val, op->up ? val / op->up : 0,
194 5081
                    pt->sdesc);
195 426
        else
196 426
                printf("%12ju %12s %s\n",
197 426
                    (uintmax_t)val, ".  ", pt->sdesc);
198 5507
        return (0);
199 5507
}
200
201
static void
202
do_once(struct vsm *vsm, struct vsc *vsc)
203 10
{
204 10
        struct vsc *vsconce = VSC_New();
205 10
        struct once_priv op;
206
207 10
        AN(vsconce);
208 10
        AN(VSC_Arg(vsconce, 'f', "MAIN.uptime"));
209
210 10
        memset(&op, 0, sizeof op);
211 10
        op.pad = 18;
212
213 10
        (void)VSC_Iter(vsconce, vsm, do_once_cb_first, &op);
214 10
        VSC_Destroy(&vsconce, vsm);
215 10
        (void)VSC_Iter(vsc, vsm, do_once_cb, &op);
216 10
}
217
218
/*--------------------------------------------------------------------*/
219
220
static int v_matchproto_(VSC_iter_f)
221
do_list_cb(void *priv, const struct VSC_point * const pt)
222 417
{
223 417
        int i;
224
225 417
        (void)priv;
226
227 417
        if (pt == NULL)
228 0
                return (0);
229
230 417
        i = 0;
231 417
        i += printf("%s", pt->name);
232 417
        if (i < 30)
233 414
                printf("%*s", i - 30, "");
234 417
        printf(" %s\n", pt->sdesc);
235 417
        return (0);
236 417
}
237
238
static void
239
list_fields(struct vsm *vsm, struct vsc *vsc)
240 1
{
241 1
        printf("Vinylstat -f option fields:\n");
242 1
        printf("Field name                     Description\n");
243 1
        printf("----------                     -----------\n");
244
245 1
        (void)VSC_Iter(vsc, vsm, do_list_cb, NULL);
246 1
}
247
248
/*--------------------------------------------------------------------*/
249
250
static void v_noreturn_
251
usage(int status)
252 2
{
253 2
        const char **opt;
254
255 2
        fprintf(stderr, "Usage: %s <options>\n\n", vut->progname);
256 2
        fprintf(stderr, "Options:\n");
257 28
        for (opt = vopt_spec.vopt_usage; *opt != NULL; opt +=2)
258 26
                fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
259 2
        exit(status);
260 2
}
261
262
static int
263
key_bindings(void)
264 1
{
265
266 1
#define BINDING_KEY(chr, name, next)    \
267 25
        printf("<%s>" next, name);
268 1
#define BINDING(name, desc)             \
269 17
        printf("\n%s\n\n", desc);
270 1
#include "vinylstat_bindings.h"
271 1
        return (0);
272 1
}
273
274
int
275
main(int argc, char * const *argv)
276 36
{
277 36
        struct vsm *vd;
278 36
        int once = 0, xml = 0, json = 0, f_list = 0, curses = 0;
279 36
        signed char opt;
280 36
        int i;
281 36
        struct vsc *vsc;
282
283 36
        if (argc == 2 && !vstrcmp(argv[1], "--bindings"))
284 1
                exit(key_bindings());
285
286 35
        vut = VUT_InitProg(argc, argv, &vopt_spec);
287 35
        AN(vut);
288 35
        vd = VSM_New();
289 35
        AN(vd);
290 35
        vsc = VSC_New();
291 35
        AN(vsc);
292
293 107
        while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
294 77
                switch (opt) {
295 14
                case '1':
296 14
                        once = 1;
297 14
                        break;
298 0
                case 'c':
299 0
                        curses = 1;
300 0
                        break;
301 1
                case 'h':
302
                        /* Usage help */
303 1
                        usage(0);
304 0
                        break;
305 1
                case 'l':
306 1
                        f_list = 1;
307 1
                        break;
308 1
                case 'x':
309 1
                        xml = 1;
310 1
                        break;
311 4
                case 'j':
312 4
                        json = 1;
313 4
                        break;
314 7
                case 'I':
315 8
                case 'X':
316 19
                case 'f':
317 19
                        AN(VSC_Arg(vsc, opt, optarg));
318 19
                        has_f = 1;
319 19
                        break;
320 0
                case 'r':
321 0
                        AN(VSC_Arg(vsc, opt, optarg));
322 0
                        break;
323 1
                case 'V':
324 1
                        AN(VUT_Arg(vut, opt, optarg));
325 1
                        break;
326 36
                default:
327 36
                        i = VSM_Arg(vd, opt, optarg);
328 36
                        if (i < 0)
329 3
                                VUT_Error(vut, 1, "%s", VSM_Error(vd));
330 33
                        if (!i)
331 0
                                usage(1);
332 77
                }
333 77
        }
334
335 30
        if (optind != argc)
336 1
                usage(1);
337
338 29
        if (!(curses || xml || json || once || f_list)) {
339 9
                curses = isatty(STDOUT_FILENO);
340 9
                once = !curses;
341 9
        }
342
343 29
        if (VSM_Attach(vd, STDERR_FILENO))
344 2
                VUT_Error(vut, 1, "%s", VSM_Error(vd));
345
346 27
        if (curses) {
347 7
                if (has_f) {
348 2
                        AN(VSC_Arg(vsc, 'R', "MGT.uptime"));
349 2
                        AN(VSC_Arg(vsc, 'R', "MAIN.uptime"));
350 2
                        AN(VSC_Arg(vsc, 'R', "MAIN.cache_hit"));
351 2
                        AN(VSC_Arg(vsc, 'R', "MAIN.cache_miss"));
352 2
                }
353 7
                do_curses(vd, vsc);
354 7
        }
355 20
        else if (xml)
356 1
                do_xml(vd, vsc);
357 19
        else if (json)
358 4
                do_json(vd, vsc);
359 15
        else if (once)
360 10
                do_once(vd, vsc);
361 5
        else if (f_list)
362 1
                list_fields(vd, vsc);
363 4
        else
364 4
                WRONG("undefined vinylstat mode");
365
366 23
        exit(0);
367 27
}