summaryrefslogtreecommitdiffstats
path: root/src/out_ascii.c
blob: c9d2e91c87198ee5235f6f1c5566cfdf5d2496cc (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
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
293
294
295
296
297
298
299
/*
 * out_ascii.c           ASCII Output
 *
 * Copyright (c) 2001-2013 Thomas Graf <tgraf@suug.ch>
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <bmon/bmon.h>
#include <bmon/conf.h>
#include <bmon/output.h>
#include <bmon/group.h>
#include <bmon/element.h>
#include <bmon/attr.h>
#include <bmon/graph.h>
#include <bmon/history.h>
#include <bmon/utils.h>

typedef enum diagram_type_e {
	D_LIST,
	D_GRAPH,
	D_DETAILS
} diagram_type_t;

static struct graph_cfg graph_cfg = {
	.gc_foreground = '*',
	.gc_background = ' ',
	.gc_noise = '.',
	.gc_unknown = '?',
	.gc_height = 6,
};

static diagram_type_t c_diagram_type = D_LIST;
static char *c_hist = "second";
static int c_quit_after = -1;

static void print_list(struct element *e)
{
	char *rxu1 = "", *txu1 = "", *rxu2 = "", *txu2 = "";
	double rx1 = 0.0f, tx1 = 0.0f, rx2 = 0.0f, tx2 = 0.0f;
	int rx1prec = 0, tx1prec = 0, rx2prec = 0, tx2prec = 0;
	char pad[IFNAMSIZ + 32];
	struct attr *a;

	if (e->e_key_attr[GT_MAJOR] &&
	    (a = attr_lookup(e, e->e_key_attr[GT_MAJOR]->ad_id)))
		attr_rate2float(a, &rx1, &rxu1, &rx1prec,
				   &tx1, &txu1, &tx1prec);

	if (e->e_key_attr[GT_MINOR] &&
	    (a = attr_lookup(e, e->e_key_attr[GT_MINOR]->ad_id)))
		attr_rate2float(a, &rx2, &rxu2, &rx2prec,
				   &tx2, &txu2, &tx2prec);

	memset(pad, 0, sizeof(pad));
	memset(pad, ' ', e->e_level < 15 ? e->e_level : 15);

	strncat(pad, e->e_name, sizeof(pad) - strlen(pad) - 1);

	if (e->e_description) {
		strncat(pad, " (", sizeof(pad) - strlen(pad) - 1);
		strncat(pad, e->e_description, sizeof(pad) - strlen(pad) - 1);
		strncat(pad, ")", sizeof(pad) - strlen(pad) - 1);
	}

	printf("  %-36s %8.*f%-3s %8.*f%-3s ",
	       pad, rx1prec, rx1, rxu1, rx2prec, rx2, rxu2);

	if (e->e_rx_usage == FLT_MAX)
		printf("   ");
	else
		printf("%2.0f%%", e->e_rx_usage);

	printf("  %8.*f%-3s %8.*f%-3s ", tx1prec, tx1, txu1, tx2prec, tx2, txu2);

	if (e->e_tx_usage == FLT_MAX)
		printf("   \n");
	else
		printf("%2.0f%%\n", e->e_tx_usage);
}

static void print_attr_detail(struct element *e, struct attr *a, void *arg)
{
	char *rx_u, *tx_u;
	int rxprec, txprec;

	double rx = unit_value2str(rate_get_total(&a->a_rx_rate),
				   a->a_def->ad_unit,
				   &rx_u, &rxprec);
	double tx = unit_value2str(rate_get_total(&a->a_tx_rate),
				   a->a_def->ad_unit,
				   &tx_u, &txprec);

	printf("  %-36s %12.*f%-3s %12.*f%-3s\n",
	       a->a_def->ad_description, rxprec, rx, rx_u, txprec, tx, tx_u);
}

static void print_details(struct element *e)
{
	printf(" %s", e->e_name);

	if (e->e_id)
		printf(" (%u)", e->e_id);

	printf("\n");

	element_foreach_attr(e, print_attr_detail, NULL);

	printf("\n");
}

static void print_table(struct graph *g, struct graph_table *tbl, const char *hdr)
{
	int i;

	if (!tbl->gt_table)
		return;

	printf("%s   %s\n", hdr, tbl->gt_y_unit);

	for (i = (g->g_cfg.gc_height - 1); i >= 0; i--)
		printf("%8.2f %s\n", tbl->gt_scale[i],
		    tbl->gt_table + (i * graph_row_size(&g->g_cfg)));
	
	printf("         1   5   10   15   20   25   30   35   40   " \
		"45   50   55   60\n");
}

static void __print_graph(struct element *e, struct attr *a, void *arg)
{
	struct history *h;
	struct graph *g;

	if (!(a->a_flags & ATTR_DOING_HISTORY))
		return;

	graph_cfg.gc_unit = a->a_def->ad_unit;

	list_for_each_entry(h, &a->a_history_list, h_list) {
		if (strcasecmp(c_hist, h->h_definition->hd_name))
			continue;

		g = graph_alloc(h, &graph_cfg);
		graph_refill(g, h);

		printf("Interface: %s\n", e->e_name);
		printf("Attribute: %s\n", a->a_def->ad_description);

		print_table(g, &g->g_rx, "RX");
		print_table(g, &g->g_tx, "TX");

		graph_free(g);

	}
}

static void print_graph(struct element *e)
{
	element_foreach_attr(e, __print_graph, NULL);
}

static void ascii_draw_element(struct element_group *g, struct element *e,
			       void *arg)
{
	switch (c_diagram_type) {
		case D_LIST:
			print_list(e);
			break;

		case D_DETAILS:
			print_details(e);
			break;

		case D_GRAPH:
			print_graph(e);
			break;
	}
}

static void ascii_draw_group(struct element_group *g, void *arg)
{
	if (c_diagram_type == D_LIST)
		printf("%-37s%10s %11s      %%%10s %11s      %%\n",
		       g->g_hdr->gh_title,
		       g->g_hdr->gh_column[0],
		       g->g_hdr->gh_column[1],
		       g->g_hdr->gh_column[2],
		       g->g_hdr->gh_column[3]);
	else
		printf("%s\n", g->g_hdr->gh_title);

	group_foreach_element(g, ascii_draw_element, NULL);
}

static void ascii_draw(void)
{
	group_foreach(ascii_draw_group, NULL);

	if (c_quit_after > 0)
		if (--c_quit_after == 0)
			exit(0);
}

static void print_help(void)
{
	printf(
	"ascii - ASCII Output\n" \
	"\n" \
	"  Plain configurable ASCII output.\n" \
	"\n" \
	"  scriptable: (output graph for eth1 10 times)\n" \
	"      bmon -p eth1 -o 'ascii:diagram=graph;quitafter=10'\n" \
	"  show details for all ethernet interfaces:\n" \
	"      bmon -p 'eth*' -o 'ascii:diagram=details;quitafter=1'\n" \
	"\n" \
	"  Author: Thomas Graf <tgraf@suug.ch>\n" \
	"\n" \
	"  Options:\n" \
	"    diagram=TYPE   Diagram type\n" \
	"    fgchar=CHAR    Foreground character (default: '*')\n" \
	"    bgchar=CHAR    Background character (default: '.')\n" \
	"    nchar=CHAR     Noise character (default: ':')\n" \
	"    uchar=CHAR     Unknown character (default: '?')\n" \
	"    height=NUM     Height of graph (default: 6)\n" \
	"    xunit=UNIT     X-Axis Unit (default: seconds)\n" \
	"    yunit=UNIT     Y-Axis Unit (default: dynamic)\n" \
	"    quitafter=NUM  Quit bmon after NUM outputs\n");
}

static void ascii_parse_opt(const char *type, const char *value)
{
	if (!strcasecmp(type, "diagram") && value) {
		if (tolower(*value) == 'l')
			c_diagram_type = D_LIST;
		else if (tolower(*value) == 'g')
			c_diagram_type = D_GRAPH;
		else if (tolower(*value) == 'd')
			c_diagram_type = D_DETAILS;
		else