Line data Source code
1 : /*
2 : * Copyright (c) 2022 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <prom/prom.h>
17 :
18 : static uword
19 0 : unformat_stats_patterns (unformat_input_t *input, va_list *args)
20 : {
21 0 : u8 ***patterns = va_arg (*args, u8 ***);
22 : u8 *pattern;
23 :
24 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
25 : {
26 0 : if (unformat (input, "%s", &pattern))
27 0 : vec_add1 (*patterns, pattern);
28 : else
29 0 : return 0;
30 : }
31 0 : return 1;
32 : }
33 :
34 : static clib_error_t *
35 0 : prom_patterns_command_fn (vlib_main_t *vm, unformat_input_t *input,
36 : vlib_cli_command_t *cmd)
37 : {
38 0 : unformat_input_t _line_input, *line_input = &_line_input;
39 0 : u8 is_clear = 0, is_show = 0, **pattern = 0;
40 0 : clib_error_t *error = 0;
41 :
42 0 : if (!unformat_user (input, unformat_line_input, line_input))
43 0 : return 0;
44 :
45 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46 : {
47 0 : if (unformat (line_input, "show"))
48 0 : is_show = 1;
49 0 : else if (unformat (line_input, "clear"))
50 0 : is_clear = 1;
51 0 : else if (unformat (line_input, "add %U", unformat_stats_patterns,
52 : &pattern))
53 : {
54 0 : prom_stat_patterns_add (pattern);
55 0 : vec_free (pattern);
56 : }
57 : else
58 : {
59 0 : error = clib_error_return (0, "unknown input `%U'",
60 : format_unformat_error, line_input);
61 0 : break;
62 : }
63 : }
64 0 : unformat_free (line_input);
65 :
66 0 : if (error)
67 0 : return error;
68 :
69 0 : if (is_clear)
70 0 : prom_stat_patterns_free ();
71 :
72 0 : if (is_show)
73 : {
74 0 : u8 **patterns = prom_stat_patterns_get ();
75 0 : vec_foreach (pattern, patterns)
76 0 : vlib_cli_output (vm, " %v\n", *pattern);
77 : }
78 :
79 0 : return 0;
80 : }
81 :
82 44949 : VLIB_CLI_COMMAND (prom_patterns_command, static) = {
83 : .path = "prom patterns",
84 : .short_help = "prom patterns [show] [clear] [add <patterns>...]",
85 : .function = prom_patterns_command_fn,
86 : };
87 :
88 : static clib_error_t *
89 0 : prom_command_fn (vlib_main_t *vm, unformat_input_t *input,
90 : vlib_cli_command_t *cmd)
91 : {
92 0 : unformat_input_t _line_input, *line_input = &_line_input;
93 0 : u8 **patterns = 0, *stat_name_prefix = 0;
94 0 : prom_main_t *pm = prom_get_main ();
95 0 : clib_error_t *error = 0;
96 0 : u8 is_enable = 0;
97 :
98 0 : if (!unformat_user (input, unformat_line_input, line_input))
99 0 : goto no_input;
100 :
101 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102 : {
103 0 : if (unformat (line_input, "enable"))
104 0 : is_enable = 1;
105 0 : else if (unformat (line_input, "min-scrape-interval %f",
106 : &pm->min_scrape_interval))
107 : ;
108 0 : else if (unformat (line_input, "used-only"))
109 0 : prom_report_used_only (1 /* used only */);
110 0 : else if (unformat (line_input, "all-stats"))
111 0 : prom_report_used_only (0 /* used only */);
112 0 : else if (unformat (line_input, "stat-name-prefix %_%v%_",
113 : &stat_name_prefix))
114 0 : prom_stat_name_prefix_set (stat_name_prefix);
115 0 : else if (unformat (line_input, "stat-patterns %U",
116 : unformat_stats_patterns, &patterns))
117 0 : prom_stat_patterns_set (patterns);
118 : else
119 : {
120 0 : error = clib_error_return (0, "unknown input `%U'",
121 : format_unformat_error, line_input);
122 0 : break;
123 : }
124 : }
125 :
126 0 : unformat_free (line_input);
127 :
128 0 : if (error)
129 0 : return error;
130 :
131 0 : no_input:
132 :
133 0 : if (is_enable && !pm->is_enabled)
134 0 : prom_enable (vm);
135 :
136 0 : return 0;
137 : }
138 :
139 44949 : VLIB_CLI_COMMAND (prom_enable_command, static) = {
140 : .path = "prom",
141 : .short_help = "prom [enable] [min-scrape-interval <n>] [used-only] "
142 : "[all-stats] [stat-name-prefix <prefix>] "
143 : "[stat-patterns <patterns>...]",
144 : .function = prom_command_fn,
145 : };
146 :
147 : /*
148 : * fd.io coding-style-patch-verification: ON
149 : *
150 : * Local Variables:
151 : * eval: (c-set-style "gnu")
152 : * End:
153 : */
|