Annotation of embedaddon/bmon/src/out_xml_event.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * out_xml_event.c     Event based XML output
        !             3:  *
        !             4:  * Copyright (c) 2001-2005 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/node.h>
        !            27: #include <bmon/output.h>
        !            28: #include <bmon/graph.h>
        !            29: #include <bmon/input.h>
        !            30: #include <bmon/utils.h>
        !            31: 
        !            32: static const char *c_file;
        !            33: static FILE *outfd;
        !            34: 
        !            35: static void write_attr(stat_attr_t *a, void *arg)
        !            36: {
        !            37:        fprintf(outfd,
        !            38:            "<attr name=\"%s\" rx=\"%" PRIu64 "\" tx=\"%" PRIu64 "\" />\n",
        !            39:            type2name(a->a_type), attr_get_rx(a), attr_get_tx(a));
        !            40: }
        !            41: 
        !            42: static void write_per_item(item_t *it, void *arg)
        !            43: {
        !            44:        fprintf(outfd, "<item index=\"%d\" name=\"%s\" handle=\"%d\" " \
        !            45:                       "level=\"%d\" ischild=\"%d\" parent=\"%d\" >\n",
        !            46:                       it->i_index, it->i_name, it->i_handle, it->i_level,
        !            47:                       !!(it->i_flags & ITEM_FLAG_IS_CHILD), it->i_parent);
        !            48:        foreach_attr(it, &write_attr, NULL);
        !            49:        fprintf(outfd, "</item>\n");
        !            50: }
        !            51: 
        !            52: static void write_per_node(node_t *node, void *arg)
        !            53: {
        !            54:        fprintf(outfd, "<node index=\"%d\" name=\"%s\" from=\"%s\">\n",
        !            55:                        node->n_index, node->n_name, node->n_from ? : "local");
        !            56:        foreach_item(node, write_per_item, (void *) node);
        !            57:        fprintf(outfd, "</node>\n");
        !            58: }
        !            59: 
        !            60: void xml_event_draw(void)
        !            61: {
        !            62:        if (outfd == NULL) {
        !            63:                if (c_file) {
        !            64:                        umask(0133);
        !            65:                        outfd = fopen(c_file, "w");
        !            66:                        if (outfd == NULL)
        !            67:                                quit("fopen(%s) failed: %s\n",
        !            68:                                    c_file, strerror(errno));
        !            69:                } else
        !            70:                        outfd = stdout;
        !            71:                
        !            72:                fprintf(outfd,
        !            73:                    "<?xml version=\"1.0\"?>\n" \
        !            74:                    "<eventfile start=\"%llu\">\n",
        !            75:                    (unsigned long long) time(0));
        !            76:        }
        !            77: 
        !            78:        fprintf(outfd,
        !            79:            "<update ts_sec=\"%" PRId64 "\" ts_usec=\"%" PRId64 "\">\n",
        !            80:            rtiming.rt_last_read.tv_sec, rtiming.rt_last_read.tv_usec);
        !            81:        foreach_node(write_per_node, NULL);
        !            82:        fprintf(outfd, "</update>\n");
        !            83: }
        !            84: 
        !            85: static void print_module_help(void)
        !            86: {
        !            87:        printf(
        !            88:        "xml_event - Event Based XML Output\n" \
        !            89:        "\n" \
        !            90:        "  XML based event output, outputs counter as-is in a flow\n" \
        !            91:        "  of XML objects.\n" \
        !            92:        "  Author: Thomas Graf <tgraf@suug.ch>\n" \
        !            93:        "\n" \
        !            94:        "  Options:\n" \
        !            95:        "    path=FILE        Output file (default: stdout)\n");
        !            96: }
        !            97: 
        !            98: static void xml_event_set_opts(tv_t *attrs)
        !            99: {
        !           100:        while (attrs) {
        !           101:                if (!strcasecmp(attrs->type, "path") && attrs->value)
        !           102:                        c_file = attrs->value;
        !           103:                else if (!strcasecmp(attrs->type, "help")) {
        !           104:                        print_module_help();
        !           105:                        exit(0);
        !           106:                }
        !           107:                
        !           108:                attrs = attrs->next;
        !           109:        }
        !           110: }
        !           111: 
        !           112: static void xml_event_shutdown(void)
        !           113: {
        !           114:        if (outfd) {
        !           115:                fprintf(outfd, "</eventfile>");
        !           116:                fclose(outfd);
        !           117:        }
        !           118: }
        !           119: 
        !           120: static int xml_event_probe(void)
        !           121: {
        !           122:        return 1;
        !           123: }
        !           124: 
        !           125: static struct output_module xml_event_ops = {
        !           126:        .om_name = "xml_event",
        !           127:        .om_draw = xml_event_draw,
        !           128:        .om_set_opts = xml_event_set_opts,
        !           129:        .om_probe = xml_event_probe,
        !           130:        .om_shutdown = xml_event_shutdown,
        !           131: };
        !           132: 
        !           133: static void __init xml_event_init(void)
        !           134: {
        !           135:        register_secondary_output_module(&xml_event_ops);
        !           136: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>