--- embedaddon/bmon/src/out_format.c 2012/02/21 22:19:56 1.1.1.1 +++ embedaddon/bmon/src/out_format.c 2014/07/30 07:55:27 1.1.1.2 @@ -1,7 +1,8 @@ /* * out_format.c Formatted Output * - * Copyright (c) 2001-2004 Thomas Graf + * Copyright (c) 2001-2013 Thomas Graf + * 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"), @@ -26,9 +27,11 @@ #include #include #include +#include +#include #include -#include #include +#include static int c_quit_after = -1; static char *c_format; @@ -48,124 +51,123 @@ static struct out_token { static int token_index; static int out_tokens_size; -static inline char *itos(b_cnt_t n) +static char *get_token(struct element_group *g, struct element *e, + const char *token, char *buf, size_t len) { - static char str[128]; - snprintf(str, sizeof(str), "%" PRIu64, n); - return str; -} + if (!strncasecmp(token, "group:", 6)) { + const char *n = token + 6; -static char *get_token(node_t *node, item_t *it, const char *token) -{ - if (!strcasecmp(token, "node:index")) - return itos(node->n_index); - else if (!strcasecmp(token, "node:name")) - return node->n_name; - else if (!strcasecmp(token, "node:from")) - return node->n_from; - else if (!strcasecmp(token, "node:nitems")) - return itos(node->n_nitems); - else if (!strcasecmp(token, "node:rx_maj_total")) - return itos(node->n_rx_maj_total); - else if (!strcasecmp(token, "node:tx_maj_total")) - return itos(node->n_tx_maj_total); - else if (!strcasecmp(token, "node:rx_min_total")) - return itos(node->n_rx_min_total); - else if (!strcasecmp(token, "node:tx_min_total")) - return itos(node->n_tx_min_total); - else if (!strcasecmp(token, "item:name")) - return it->i_name; - else if (!strcasecmp(token, "item:desc")) - return it->i_desc ? it->i_desc : "none"; - else if (!strcasecmp(token, "item:index")) - return itos(it->i_index); - else if (!strcasecmp(token, "item:nattrs")) - return itos(it->i_nattrs); - else if (!strcasecmp(token, "item:lifetime")) - return itos(it->i_lifetime); - else if (!strcasecmp(token, "item:level")) - return itos(it->i_level); - else if (!strcasecmp(token, "item:link")) - return itos(it->i_link); - else if (!strcasecmp(token, "item:parent")) - return itos(it->i_parent); - else if (!strcasecmp(token, "item:handle")) - return itos(it->i_handle); - else if (!strcasecmp(token, "item:rxusage")) - return it->i_rx_usage == -1 ? "UNK" : itos(it->i_rx_usage); - else if (!strcasecmp(token, "item:txusage")) - return it->i_tx_usage == -1 ? "UNK" : itos(it->i_tx_usage); - else if (!strcasecmp(token, "item:is_child")) - return itos(!!(it->i_flags & ITEM_FLAG_IS_CHILD)); - else if (!strcasecmp(token, "item:has_childs")) - return itos(!!(it->i_flags & ITEM_FLAG_HAS_CHILDS)); - else if (!strcasecmp(token, "read:sec")) - return itos(rtiming.rt_last_read.tv_sec); - else if (!strcasecmp(token, "read:usec")) - return itos(rtiming.rt_last_read.tv_usec); - else if (!strncasecmp(token, "attr:", 5)) { - int atype; - char *name = strchr(token+5, ':'); - stat_attr_t *a; - if (name == NULL) { + if (!strcasecmp(n, "nelements")) { + snprintf(buf, len, "%u", g->g_nelements); + return buf; + } else if (!strcasecmp(n, "name")) + return g->g_name; + else if (!strcasecmp(n, "title")) + return g->g_hdr->gh_title; + + } else if (!strncasecmp(token, "element:", 8)) { + const char *n = token + 8; + + if (!strcasecmp(n, "name")) + return e->e_name; + else if (!strcasecmp(n, "description")) + return e->e_description; + else if (!strcasecmp(n, "nattrs")) { + snprintf(buf, len, "%u", e->e_nattrs); + return buf; + } else if (!strcasecmp(n, "lifecycles")) { + snprintf(buf, len, "%u", e->e_lifecycles); + return buf; + } else if (!strcasecmp(n, "level")) { + snprintf(buf, len, "%u", e->e_level); + return buf; + } else if (!strcasecmp(n, "parent")) + return e->e_parent ? e->e_parent->e_name : ""; + else if (!strcasecmp(n, "id")) { + snprintf(buf, len, "%u", e->e_id); + return buf; + } else if (!strcasecmp(n, "rxusage")) { + snprintf(buf, len, "%2.0f", + e->e_rx_usage == FLT_MAX ? e->e_rx_usage : 0.0f); + return buf; + } else if (!strcasecmp(n, "txusage")) { + snprintf(buf, len, "%2.0f", + e->e_tx_usage == FLT_MAX ? e->e_tx_usage : 0.0f); + return buf; + } else if (!strcasecmp(n, "haschilds")) { + snprintf(buf, len, "%u", + list_empty(&e->e_childs) ? 0 : 1); + return buf; + } + } else if (!strncasecmp(token, "attr:", 5)) { + const char *type = token + 5; + char *name = strchr(type, ':'); + struct attr_def *def; + struct attr *a; + + if (!name) { fprintf(stderr, "Invalid attribute field \"%s\"\n", - token); - return "UNK"; + type); + goto out; } - atype = name2type(++name); - if (atype == INT_MAX || !(a = lookup_attr(it, atype))) { - fprintf(stderr, "Unknown attribute \"%s\"\n", name); - return "UNK"; + name++; + + def = attr_def_lookup(name); + if (!def) { + fprintf(stderr, "Undefined attribute \"%s\"\n", name); + goto out; } - if (!strncasecmp(token+5, "rx:", 3)) - return itos(attr_get_rx(a)); - else if (!strncasecmp(token+5, "tx:", 3)) - return itos(attr_get_tx(a)); - else if (!strncasecmp(token+5, "rxrate:", 7)) - return itos(attr_get_rx_rate(a)); - else if (!strncasecmp(token+5, "txrate:", 7)) - return itos(attr_get_tx_rate(a)); - else if (!strncasecmp(token+5, "rx64:", 5)) { - return a->a_rx.r_is64bit ? "1" : "0"; - } else if (!strncasecmp(token+5, "tx64:", 5)) { - return a->a_tx.r_is64bit ? "1" : "0"; - } else if (!strncasecmp(token+5, "rxoflows:", 9)) { - return itos(a->a_rx.r_overflows); - } else if (!strncasecmp(token+5, "txoflows:", 9)) { - return itos(a->a_tx.r_overflows); - } else if (!strncasecmp(token+5, "rxenab:", 7)) - return itos(!!(a->a_flags & ATTR_FLAG_RX_ENABLED)); - else if (!strncasecmp(token+5, "txenab:", 7)) - return itos(!!(a->a_flags & ATTR_FLAG_TX_ENABLED)); + if (!(a = attr_lookup(e, def->ad_id))) { + fprintf(stderr, "Unable to find attribute %u (%s)\n", + def->ad_id, name); + goto out; + } + + if (!strncasecmp(type, "rx:", 3)) { + snprintf(buf, len, "%llu", a->a_rx_rate.r_total); + return buf; + } else if (!strncasecmp(type, "tx:", 3)) { + snprintf(buf, len, "%llu", a->a_tx_rate.r_total); + return buf; + } else if (!strncasecmp(type, "rxrate:", 7)) { + snprintf(buf, len, "%.2f", a->a_rx_rate.r_rate); + return buf; + } else if (!strncasecmp(token+5, "txrate:", 7)) + snprintf(buf, len, "%.2f", a->a_tx_rate.r_rate); + return buf; } fprintf(stderr, "Unknown field \"%s\"\n", token); - return "UNK"; +out: + return "unknown"; } -static void format_draw_item(item_t *it, void *arg) +static void draw_element(struct element_group *g, struct element *e, void *arg) { int i; - node_t *node = (node_t *) arg; for (i = 0; i < token_index; i++) { - if (out_tokens[i].ot_type == OT_STRING) { - fprintf(c_fd, "%s", out_tokens[i].ot_str); - } else if (out_tokens[i].ot_type == OT_TOKEN) - fprintf(c_fd, "%s", get_token(node, it, out_tokens[i].ot_str)); + char buf[128]; + char *p; + + if (out_tokens[i].ot_type == OT_STRING) + p = out_tokens[i].ot_str; + else if (out_tokens[i].ot_type == OT_TOKEN) + p = get_token(g, e, out_tokens[i].ot_str, + buf, sizeof(buf)); + else + BUG(); + + if (p) + fprintf(c_fd, "%s", p); } } -static void format_draw_node(node_t *n, void *arg) -{ - foreach_item(n, format_draw_item, n); -} - static void format_draw(void) { - foreach_node(format_draw_node, NULL); + group_foreach_recursive(draw_element, NULL); if (c_quit_after > 0) if (--c_quit_after == 0) @@ -296,96 +298,76 @@ static void print_help(void) " Options:\n" \ " fmt=FORMAT Format string\n" \ " stderr Write to stderr instead of stdout\n" \ - " quitafter=NUM Quit bmon NUM outputs\n" \ + " quitafter=NUM Quit bmon after NUM outputs\n" \ "\n" \ " Placeholders:\n" \ - " read:sec time of last read (seconds)\n" \ - " read:usec time of last read (micro seconds)\n" \ - " node:index index of node\n" \ - " node:name name of node\n" \ - " node:from origin this node came from\n" \ - " node:nitems number of items this nodes carries\n" \ - " node:rx_maj_total total RX rate of major attribute\n" \ - " node:tx_maj_total total TX rate of major attribute\n" \ - " node:rx_min_total total RX rate of minor attribute\n" \ - " node:tx_min_total total TX rate of minor attribute\n" \ - " item:name name of item\n" \ - " item:desc description of item\n" \ - " item:index index of item\n" \ - " item:nattrs number of attributes this item carries\n" \ - " item:lifetime lifetime of item\n" \ - " item:level indentation level of item\n" \ - " item:link item index of root parent\n" \ - " item:parent item index of parent\n" \ - " item:rxusage RX usage of item (in percent)\n" \ - " item:txusage TX usage of item (in percent)\n" \ - " item:handle handle of item\n" \ - " item:is_child 1 if item is a child\n" \ - " item:has_childs 1 if item has childs\n" \ - " attr:rx: rx counter of attribute \n" \ - " attr:tx: tx counter of attribute \n" \ - " attr:rxrate: rx rate of attribute \n" \ - " attr:txrate: tx rate of attribute \n" \ - " attr:rx64: 1 if rx counter of attribute is 64bit sized\n" \ - " attr:tx64: 1 if tx counter of attribute is 64bit sized\n" \ - " attr:rxoflows: number of rx counter overflows of attribute \n" \ - " attr:txoflows: number of tx counter overflows of attribute \n" \ - " attr:rxenab: 1 if rx counter of attribute is available\n" \ - " attr:txenab: 1 if rx counter of attribute is available\n" \ + " group:nelements Number of elements this group\n" \ + " :name Name of group\n" \ + " :title Title of group\n" \ + " element:name Name of element\n" \ + " :desc Description of element\n" \ + " :nattrs Number of attributes\n" \ + " :lifecycles Number of lifecycles\n" \ + " :level Indentation level\n" \ + " :parent Name of parent element\n" \ + " :rxusage RX usage in percent\n" \ + " :txusage TX usage in percent)\n" \ + " :id ID of element\n" \ + " :haschilds Indicate if element has children (0|1)\n" \ + " attr:rx: RX counter of attribute \n" \ + " :tx: TX counter of attribute \n" \ + " :rxrate: RX rate of attribute \n" \ + " :txrate: TX rate of attribute \n" \ "\n" \ " Supported Escape Sequences: \\n, \\t, \\r, \\v, \\b, \\f, \\a\n" \ "\n" \ " Examples:\n" \ - " \"$(node:name)\\t$(item:name)\\t$(attr:rx:bytes)\\t$(attr:tx:bytes)\\n\"\n" \ - " axs lo 12074 12074\n" \ + " \"$(element:name)\\t$(attr:rx:bytes)\\t$(attr:tx:bytes)\\n\"\n" \ + " lo 12074 12074\n" \ "\n" \ - " \"$(item:name) $(attr:rxrate:packets) $(attr:txrate:packets)\\n\"\n" \ + " \"$(element:name) $(attr:rxrate:packets) $(attr:txrate:packets)\\n\"\n" \ " eth0 33 5\n" \ "\n" \ - " \"Node: $(node:name)\\nItem: $(item:name)\\nBytes Rate: \" \\\n" \ + " \"Element: $(element:name)\\nBytes Rate: \" \\\n" \ " \"$(attr:rxrate:bytes)/$(attr:txrate:bytes)\\nPackets Rate: \" \\\n" \ " \"$(attr:rxrate:packets)/$(attr:txrate:packets)\\n\"\n" \ - " Node: axs\n" \ " Item: eth0\n" \ " Bytes Rate: 49130/2119\n" \ " Packets Rate: 40/11\n" \ "\n"); } -static void format_set_opts(tv_t *attrs) +static void format_parse_opt(const char *type, const char *value) { - while (attrs) { - if (!strcasecmp(attrs->type, "stderr")) - c_fd = stderr; - else if (!strcasecmp(attrs->type, "debug")) - c_debug = 1; - else if (!strcasecmp(attrs->type, "fmt")) { - if (c_format) - free(c_format); - c_format = strdup(attrs->value); - } else if (!strcasecmp(attrs->type, "quitafter") && - attrs->value) - c_quit_after = strtol(attrs->value, NULL, 0); - else if (!strcasecmp(attrs->type, "help")) { - print_help(); - exit(0); - } - - attrs = attrs->next; + if (!strcasecmp(type, "stderr")) + c_fd = stderr; + else if (!strcasecmp(type, "debug")) + c_debug = 1; + else if (!strcasecmp(type, "fmt")) { + if (c_format) + free(c_format); + c_format = strdup(value); + } else if (!strcasecmp(type, "quitafter") && + value) + c_quit_after = strtol(value, NULL, 0); + else if (!strcasecmp(type, "help")) { + print_help(); + exit(0); } } -static struct output_module format_ops = { - .om_name = "format", - .om_draw = format_draw, - .om_probe = format_probe, - .om_set_opts = format_set_opts, +static struct bmon_module format_ops = { + .m_name = "format", + .m_do = format_draw, + .m_probe = format_probe, + .m_parse_opt = format_parse_opt, }; static void __init ascii_init(void) { c_fd = stdout; - c_format = strdup("$(node:name) $(item:name) $(attr:rx:bytes) $(attr:tx:bytes) " \ + c_format = strdup("$(element:name) $(attr:rx:bytes) $(attr:tx:bytes) " \ "$(attr:rx:packets) $(attr:tx:packets)\\n"); - register_output_module(&format_ops); + + output_register(&format_ops); }