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

1.1     ! misho       1: /*
        !             2:  * out_xml_state.c     State 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 int c_nolock = 0;
        !            34: 
        !            35: static void write_attr(stat_attr_t *a, void *arg)
        !            36: {
        !            37:        FILE *fd = (FILE *) arg;
        !            38:        fprintf(fd,
        !            39:            "<attr name=\"%s\" rx=\"%" PRIu64 "\" tx=\"%" PRIu64 "\" />\n",
        !            40:            type2name(a->a_type), attr_get_rx(a), attr_get_tx(a));
        !            41: }
        !            42: 
        !            43: static void write_per_item(item_t *it, void *arg)
        !            44: {
        !            45:        FILE *fd = (FILE *) arg;
        !            46:        fprintf(fd, "<item index=\"%d\" name=\"%s\" handle=\"%d\" " \
        !            47:                    "level=\"%d\" ischild=\"%d\" parent=\"%d\" >\n",
        !            48:                    it->i_index, it->i_name, it->i_handle, it->i_level,
        !            49:                    !!(it->i_flags & ITEM_FLAG_IS_CHILD), it->i_parent);
        !            50:        foreach_attr(it, &write_attr, fd);
        !            51:        fprintf(fd, "</item>\n");
        !            52: }
        !            53: 
        !            54: static void write_per_node(node_t *node, void *arg)
        !            55: {
        !            56:        FILE *fd = (FILE *) arg;
        !            57:        fprintf(fd, "<node index=\"%d\" name=\"%s\" from=\"%s\">\n",
        !            58:                    node->n_index, node->n_name, node->n_from ? : "local");
        !            59:        foreach_item(node, write_per_item, fd);
        !            60:        fprintf(fd, "</node>\n");
        !            61: }
        !            62: 
        !            63: void xml_state_draw(void)
        !            64: {
        !            65:        FILE *fd;
        !            66: 
        !            67:        fd = fopen(c_file, "w");
        !            68:        if (fd == NULL)
        !            69:                return;
        !            70: 
        !            71:        /* We ignore a locking failure and simply try again next time */
        !            72:        if (!c_nolock && flock(fileno(fd), LOCK_EX | LOCK_NB) < 0)
        !            73:                return;
        !            74:        
        !            75:        fprintf(fd,
        !            76:            "<?xml version=\"1.0\"?>\n" \
        !            77:            "<statefile ts_sec=\"%" PRId64 "\" ts_usec=\"%" PRId64 "\">\n",
        !            78:            rtiming.rt_last_read.tv_sec, rtiming.rt_last_read.tv_usec);
        !            79: 
        !            80:        foreach_node(write_per_node, fd);
        !            81:        fprintf(fd, "</statefile>\n");
        !            82: 
        !            83:        if (!c_nolock)
        !            84:                flock(fileno(fd), LOCK_UN | LOCK_NB);
        !            85:        fclose(fd);
        !            86: }
        !            87: 
        !            88: static void print_module_help(void)
        !            89: {
        !            90:        printf(
        !            91:        "xml_state - State Based XML Output\n" \
        !            92:        "\n" \
        !            93:        "  XML based state output, outputs counters as-is as XML\n" \
        !            94:        "  objects into a file. The file is overwritten in each\n" \
        !            95:        "  cycle and locked during this period if nolock is not\n" \
        !            96:        "  specified.\n" \
        !            97:        "  Author: Thomas Graf <tgraf@suug.ch>\n" \
        !            98:        "\n" \
        !            99:        "  Options:\n" \
        !           100:        "    path=FILE        Output file (default: stdout)\n" \
        !           101:        "    nolock           Do not lock the file\n");
        !           102: }
        !           103: 
        !           104: static void xml_state_set_opts(tv_t *attrs)
        !           105: {
        !           106:        while (attrs) {
        !           107:                if (!strcasecmp(attrs->type, "path") && attrs->value)
        !           108:                        c_file = attrs->value;
        !           109:                else if (!strcasecmp(attrs->type, "nolock"))
        !           110:                        c_nolock = 1;
        !           111:                else if (!strcasecmp(attrs->type, "help")) {
        !           112:                        print_module_help();
        !           113:                        exit(0);
        !           114:                }
        !           115:                
        !           116:                attrs = attrs->next;
        !           117:        }
        !           118: }
        !           119: 
        !           120: static int xml_state_probe(void)
        !           121: {
        !           122:        FILE *t;
        !           123: 
        !           124:        if (c_file == NULL) {
        !           125:                fprintf(stderr, "xml_state: You must specify a output file\n");
        !           126:                return 0;
        !           127:        }
        !           128: 
        !           129:        t = fopen(c_file, "w");
        !           130:        if (t == NULL) {
        !           131:                fprintf(stderr, "xml_state: Unable to open output file %s:%s\n",
        !           132:                    c_file, strerror(errno));
        !           133:                return 0;
        !           134:        }
        !           135: 
        !           136:        return 1;
        !           137: }
        !           138: 
        !           139: static struct output_module xml_state_ops = {
        !           140:        .om_name = "xml_state",
        !           141:        .om_draw = xml_state_draw,
        !           142:        .om_set_opts = xml_state_set_opts,
        !           143:        .om_probe = xml_state_probe
        !           144: };
        !           145: 
        !           146: static void __init xml_state_init(void)
        !           147: {
        !           148:        register_secondary_output_module(&xml_state_ops);
        !           149: }

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