Annotation of embedaddon/bmon/src/out_format.c, revision 1.1
1.1 ! misho 1: /*
! 2: * out_format.c Formatted Output
! 3: *
! 4: * Copyright (c) 2001-2004 Thomas Graf <tgraf@suug.ch>
! 5: *
! 6: * Permission is hereby granted, free of charge, to any person obtaining a
! 7: * copy of this software and associated documentation files (the "Software"),
! 8: * to deal in the Software without restriction, including without limitation
! 9: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
! 10: * and/or sell copies of the Software, and to permit persons to whom the
! 11: * Software is furnished to do so, subject to the following conditions:
! 12: *
! 13: * The above copyright notice and this permission notice shall be included
! 14: * in all copies or substantial portions of the Software.
! 15: *
! 16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
! 17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! 18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
! 19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! 20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
! 21: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! 22: * DEALINGS IN THE SOFTWARE.
! 23: */
! 24:
! 25: #include <bmon/bmon.h>
! 26: #include <bmon/graph.h>
! 27: #include <bmon/conf.h>
! 28: #include <bmon/output.h>
! 29: #include <bmon/input.h>
! 30: #include <bmon/node.h>
! 31: #include <bmon/utils.h>
! 32:
! 33: static int c_quit_after = -1;
! 34: static char *c_format;
! 35: static int c_debug = 0;
! 36: static FILE *c_fd;
! 37:
! 38: enum {
! 39: OT_STRING,
! 40: OT_TOKEN
! 41: };
! 42:
! 43: static struct out_token {
! 44: int ot_type;
! 45: char *ot_str;
! 46: } *out_tokens;
! 47:
! 48: static int token_index;
! 49: static int out_tokens_size;
! 50:
! 51: static inline char *itos(b_cnt_t n)
! 52: {
! 53: static char str[128];
! 54: snprintf(str, sizeof(str), "%" PRIu64, n);
! 55: return str;
! 56: }
! 57:
! 58: static char *get_token(node_t *node, item_t *it, const char *token)
! 59: {
! 60: if (!strcasecmp(token, "node:index"))
! 61: return itos(node->n_index);
! 62: else if (!strcasecmp(token, "node:name"))
! 63: return node->n_name;
! 64: else if (!strcasecmp(token, "node:from"))
! 65: return node->n_from;
! 66: else if (!strcasecmp(token, "node:nitems"))
! 67: return itos(node->n_nitems);
! 68: else if (!strcasecmp(token, "node:rx_maj_total"))
! 69: return itos(node->n_rx_maj_total);
! 70: else if (!strcasecmp(token, "node:tx_maj_total"))
! 71: return itos(node->n_tx_maj_total);
! 72: else if (!strcasecmp(token, "node:rx_min_total"))
! 73: return itos(node->n_rx_min_total);
! 74: else if (!strcasecmp(token, "node:tx_min_total"))
! 75: return itos(node->n_tx_min_total);
! 76: else if (!strcasecmp(token, "item:name"))
! 77: return it->i_name;
! 78: else if (!strcasecmp(token, "item:desc"))
! 79: return it->i_desc ? it->i_desc : "none";
! 80: else if (!strcasecmp(token, "item:index"))
! 81: return itos(it->i_index);
! 82: else if (!strcasecmp(token, "item:nattrs"))
! 83: return itos(it->i_nattrs);
! 84: else if (!strcasecmp(token, "item:lifetime"))
! 85: return itos(it->i_lifetime);
! 86: else if (!strcasecmp(token, "item:level"))
! 87: return itos(it->i_level);
! 88: else if (!strcasecmp(token, "item:link"))
! 89: return itos(it->i_link);
! 90: else if (!strcasecmp(token, "item:parent"))
! 91: return itos(it->i_parent);
! 92: else if (!strcasecmp(token, "item:handle"))
! 93: return itos(it->i_handle);
! 94: else if (!strcasecmp(token, "item:rxusage"))
! 95: return it->i_rx_usage == -1 ? "UNK" : itos(it->i_rx_usage);
! 96: else if (!strcasecmp(token, "item:txusage"))
! 97: return it->i_tx_usage == -1 ? "UNK" : itos(it->i_tx_usage);
! 98: else if (!strcasecmp(token, "item:is_child"))
! 99: return itos(!!(it->i_flags & ITEM_FLAG_IS_CHILD));
! 100: else if (!strcasecmp(token, "item:has_childs"))
! 101: return itos(!!(it->i_flags & ITEM_FLAG_HAS_CHILDS));
! 102: else if (!strcasecmp(token, "read:sec"))
! 103: return itos(rtiming.rt_last_read.tv_sec);
! 104: else if (!strcasecmp(token, "read:usec"))
! 105: return itos(rtiming.rt_last_read.tv_usec);
! 106: else if (!strncasecmp(token, "attr:", 5)) {
! 107: int atype;
! 108: char *name = strchr(token+5, ':');
! 109: stat_attr_t *a;
! 110: if (name == NULL) {
! 111: fprintf(stderr, "Invalid attribute field \"%s\"\n",
! 112: token);
! 113: return "UNK";
! 114: }
! 115: atype = name2type(++name);
! 116:
! 117: if (atype == INT_MAX || !(a = lookup_attr(it, atype))) {
! 118: fprintf(stderr, "Unknown attribute \"%s\"\n", name);
! 119: return "UNK";
! 120: }
! 121:
! 122: if (!strncasecmp(token+5, "rx:", 3))
! 123: return itos(attr_get_rx(a));
! 124: else if (!strncasecmp(token+5, "tx:", 3))
! 125: return itos(attr_get_tx(a));
! 126: else if (!strncasecmp(token+5, "rxrate:", 7))
! 127: return itos(attr_get_rx_rate(a));
! 128: else if (!strncasecmp(token+5, "txrate:", 7))
! 129: return itos(attr_get_tx_rate(a));
! 130: else if (!strncasecmp(token+5, "rx64:", 5)) {
! 131: return a->a_rx.r_is64bit ? "1" : "0";
! 132: } else if (!strncasecmp(token+5, "tx64:", 5)) {
! 133: return a->a_tx.r_is64bit ? "1" : "0";
! 134: } else if (!strncasecmp(token+5, "rxoflows:", 9)) {
! 135: return itos(a->a_rx.r_overflows);
! 136: } else if (!strncasecmp(token+5, "txoflows:", 9)) {
! 137: return itos(a->a_tx.r_overflows);
! 138: } else if (!strncasecmp(token+5, "rxenab:", 7))
! 139: return itos(!!(a->a_flags & ATTR_FLAG_RX_ENABLED));
! 140: else if (!strncasecmp(token+5, "txenab:", 7))
! 141: return itos(!!(a->a_flags & ATTR_FLAG_TX_ENABLED));
! 142: }
! 143:
! 144: fprintf(stderr, "Unknown field \"%s\"\n", token);
! 145: return "UNK";
! 146: }
! 147:
! 148: static void format_draw_item(item_t *it, void *arg)
! 149: {
! 150: int i;
! 151: node_t *node = (node_t *) arg;
! 152:
! 153: for (i = 0; i < token_index; i++) {
! 154: if (out_tokens[i].ot_type == OT_STRING) {
! 155: fprintf(c_fd, "%s", out_tokens[i].ot_str);
! 156: } else if (out_tokens[i].ot_type == OT_TOKEN)
! 157: fprintf(c_fd, "%s", get_token(node, it, out_tokens[i].ot_str));
! 158: }
! 159: }
! 160:
! 161: static void format_draw_node(node_t *n, void *arg)
! 162: {
! 163: foreach_item(n, format_draw_item, n);
! 164: }
! 165:
! 166: static void format_draw(void)
! 167: {
! 168: foreach_node(format_draw_node, NULL);
! 169:
! 170: if (c_quit_after > 0)
! 171: if (--c_quit_after == 0)
! 172: exit(0);
! 173: }
! 174:
! 175: static inline void add_token(int type, char *data)
! 176: {
! 177: if (!out_tokens_size) {
! 178: out_tokens_size = 32;
! 179: out_tokens = calloc(out_tokens_size, sizeof(struct out_token));
! 180: if (out_tokens == NULL)
! 181: quit("Cannot allocate out token array\n");
! 182: }
! 183:
! 184: if (out_tokens_size <= token_index) {
! 185: out_tokens_size += 32;
! 186: out_tokens = realloc(out_tokens, out_tokens_size * sizeof(struct out_token));
! 187: if (out_tokens == NULL)
! 188: quit("Cannot reallocate out token array\n");
! 189: }
! 190:
! 191:
! 192: out_tokens[token_index].ot_type = type;
! 193: out_tokens[token_index].ot_str = data;
! 194: token_index++;
! 195: }
! 196:
! 197: static int format_probe(void)
! 198: {
! 199: int new_one = 1;
! 200: char *p, *e;
! 201:
! 202: for (p = c_format; *p; p++) {
! 203: if (*p == '$') {
! 204: char *s = p;
! 205: s++;
! 206: if (*s == '(') {
! 207: s++;
! 208: if (!*s)
! 209: goto unexpected_end;
! 210: e = strchr(s, ')');
! 211: if (e == NULL)
! 212: goto invalid;
! 213:
! 214: *p = '\0';
! 215: *e = '\0';
! 216: add_token(OT_TOKEN, s);
! 217: new_one = 1;
! 218: p = e;
! 219: continue;
! 220: }
! 221: }
! 222:
! 223: if (*p == '\\') {
! 224: char *s = p;
! 225: s++;
! 226: switch (*s) {
! 227: case 'n':
! 228: *s = '\n';
! 229: goto finish_escape;
! 230: case 't':
! 231: *s = '\t';
! 232: goto finish_escape;
! 233: case 'r':
! 234: *s = '\r';
! 235: goto finish_escape;
! 236: case 'v':
! 237: *s = '\v';
! 238: goto finish_escape;
! 239: case 'b':
! 240: *s = '\b';
! 241: goto finish_escape;
! 242: case 'f':
! 243: *s = '\f';
! 244: goto finish_escape;
! 245: case 'a':
! 246: *s = '\a';
! 247: goto finish_escape;
! 248: }
! 249:
! 250: goto out;
! 251:
! 252: finish_escape:
! 253: *p = '\0';
! 254: add_token(OT_STRING, s);
! 255: p = s;
! 256: new_one = 0;
! 257: continue;
! 258: }
! 259:
! 260: out:
! 261: if (new_one) {
! 262: add_token(OT_STRING, p);
! 263: new_one = 0;
! 264: }
! 265: }
! 266:
! 267: if (c_debug) {
! 268: int i;
! 269: for (i = 0; i < token_index; i++)
! 270: printf(">>%s<\n", out_tokens[i].ot_str);
! 271: }
! 272:
! 273: return 1;
! 274:
! 275: unexpected_end:
! 276: fprintf(stderr, "Unexpected end of format string\n");
! 277: return 0;
! 278:
! 279: invalid:
! 280: fprintf(stderr, "Missing ')' in format string\n");
! 281: return 0;
! 282: }
! 283:
! 284: static void print_help(void)
! 285: {
! 286: printf(
! 287: "format - Formatable Output\n" \
! 288: "\n" \
! 289: " Formatable ASCII output for scripts. Calls a drawing function for\n" \
! 290: " every item per node and outputs according to the specified format\n" \
! 291: " string. The format string consists of normal text and placeholders\n" \
! 292: " in the form of $(placeholder).\n" \
! 293: "\n" \
! 294: " Author: Thomas Graf <tgraf@suug.ch>\n" \
! 295: "\n" \
! 296: " Options:\n" \
! 297: " fmt=FORMAT Format string\n" \
! 298: " stderr Write to stderr instead of stdout\n" \
! 299: " quitafter=NUM Quit bmon NUM outputs\n" \
! 300: "\n" \
! 301: " Placeholders:\n" \
! 302: " read:sec time of last read (seconds)\n" \
! 303: " read:usec time of last read (micro seconds)\n" \
! 304: " node:index index of node\n" \
! 305: " node:name name of node\n" \
! 306: " node:from origin this node came from\n" \
! 307: " node:nitems number of items this nodes carries\n" \
! 308: " node:rx_maj_total total RX rate of major attribute\n" \
! 309: " node:tx_maj_total total TX rate of major attribute\n" \
! 310: " node:rx_min_total total RX rate of minor attribute\n" \
! 311: " node:tx_min_total total TX rate of minor attribute\n" \
! 312: " item:name name of item\n" \
! 313: " item:desc description of item\n" \
! 314: " item:index index of item\n" \
! 315: " item:nattrs number of attributes this item carries\n" \
! 316: " item:lifetime lifetime of item\n" \
! 317: " item:level indentation level of item\n" \
! 318: " item:link item index of root parent\n" \
! 319: " item:parent item index of parent\n" \
! 320: " item:rxusage RX usage of item (in percent)\n" \
! 321: " item:txusage TX usage of item (in percent)\n" \
! 322: " item:handle handle of item\n" \
! 323: " item:is_child 1 if item is a child\n" \
! 324: " item:has_childs 1 if item has childs\n" \
! 325: " attr:rx:<name> rx counter of attribute <name>\n" \
! 326: " attr:tx:<name> tx counter of attribute <name>\n" \
! 327: " attr:rxrate:<name> rx rate of attribute <name>\n" \
! 328: " attr:txrate:<name> tx rate of attribute <name>\n" \
! 329: " attr:rx64:<name> 1 if rx counter of attribute <name> is 64bit sized\n" \
! 330: " attr:tx64:<name> 1 if tx counter of attribute <name> is 64bit sized\n" \
! 331: " attr:rxoflows:<name> number of rx counter overflows of attribute <name>\n" \
! 332: " attr:txoflows:<name> number of tx counter overflows of attribute <name>\n" \
! 333: " attr:rxenab:<name> 1 if rx counter of attribute <name> is available\n" \
! 334: " attr:txenab:<name> 1 if rx counter of attribute <name> is available\n" \
! 335: "\n" \
! 336: " Supported Escape Sequences: \\n, \\t, \\r, \\v, \\b, \\f, \\a\n" \
! 337: "\n" \
! 338: " Examples:\n" \
! 339: " \"$(node:name)\\t$(item:name)\\t$(attr:rx:bytes)\\t$(attr:tx:bytes)\\n\"\n" \
! 340: " axs lo 12074 12074\n" \
! 341: "\n" \
! 342: " \"$(item:name) $(attr:rxrate:packets) $(attr:txrate:packets)\\n\"\n" \
! 343: " eth0 33 5\n" \
! 344: "\n" \
! 345: " \"Node: $(node:name)\\nItem: $(item:name)\\nBytes Rate: \" \\\n" \
! 346: " \"$(attr:rxrate:bytes)/$(attr:txrate:bytes)\\nPackets Rate: \" \\\n" \
! 347: " \"$(attr:rxrate:packets)/$(attr:txrate:packets)\\n\"\n" \
! 348: " Node: axs\n" \
! 349: " Item: eth0\n" \
! 350: " Bytes Rate: 49130/2119\n" \
! 351: " Packets Rate: 40/11\n" \
! 352: "\n");
! 353: }
! 354:
! 355: static void format_set_opts(tv_t *attrs)
! 356: {
! 357: while (attrs) {
! 358: if (!strcasecmp(attrs->type, "stderr"))
! 359: c_fd = stderr;
! 360: else if (!strcasecmp(attrs->type, "debug"))
! 361: c_debug = 1;
! 362: else if (!strcasecmp(attrs->type, "fmt")) {
! 363: if (c_format)
! 364: free(c_format);
! 365: c_format = strdup(attrs->value);
! 366: } else if (!strcasecmp(attrs->type, "quitafter") &&
! 367: attrs->value)
! 368: c_quit_after = strtol(attrs->value, NULL, 0);
! 369: else if (!strcasecmp(attrs->type, "help")) {
! 370: print_help();
! 371: exit(0);
! 372: }
! 373:
! 374: attrs = attrs->next;
! 375: }
! 376: }
! 377:
! 378: static struct output_module format_ops = {
! 379: .om_name = "format",
! 380: .om_draw = format_draw,
! 381: .om_probe = format_probe,
! 382: .om_set_opts = format_set_opts,
! 383: };
! 384:
! 385: static void __init ascii_init(void)
! 386: {
! 387: c_fd = stdout;
! 388: c_format = strdup("$(node:name) $(item:name) $(attr:rx:bytes) $(attr:tx:bytes) " \
! 389: "$(attr:rx:packets) $(attr:tx:packets)\\n");
! 390: register_output_module(&format_ops);
! 391: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>