Annotation of embedaddon/bmon/src/in_proc.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * in_proc.c                  /proc/net/dev Input (Linux)
                      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/input.h>
                     27: #include <bmon/node.h>
                     28: #include <bmon/item.h>
                     29: #include <bmon/utils.h>
                     30: #include <inttypes.h>
                     31: 
                     32: static char *c_path = "/proc/net/dev";
                     33: 
                     34: static void proc_read(void)
                     35: {
                     36:        FILE *  fd;
                     37:        char    buf[512], *p, *s;
                     38:        int     w;
                     39:        item_t *it;
                     40:        
                     41:        if (!(fd = fopen(c_path, "r")))
                     42:                quit("Unable to open file %s: %s\n", c_path, strerror(errno));
                     43:        
                     44:        fgets(buf, sizeof(buf), fd);
                     45:        fgets(buf, sizeof(buf), fd);
                     46:        
                     47:        for (; fgets(buf, sizeof(buf), fd);) {
                     48:                b_cnt_t rx_errors, rx_drop, rx_fifo, rx_frame, rx_compressed;
                     49:                b_cnt_t rx_multicast, tx_errors, tx_drop, tx_fifo, tx_frame;
                     50:                b_cnt_t tx_compressed, tx_multicast, rx_bytes, tx_bytes;
                     51:                b_cnt_t rx_packets, tx_packets;
                     52:                
                     53:                if (buf[0] == '\r' || buf[0] == '\n')
                     54:                        continue;
                     55:                
                     56:                if (!(p = strchr(buf, ':')))
                     57:                        continue;
                     58:                *p = '\0';
                     59:                s = (p + 1);
                     60:                
                     61:                for (p = &buf[0]; *p == ' '; p++);
                     62: 
                     63:                /*
                     64:                 * XXX: get_show_only_running
                     65:                 */
                     66:                
                     67:                if ((it = lookup_item(get_local_node(), p, 0, 0)) == NULL)
                     68:                        continue;
                     69:                
                     70:                w = sscanf(s, "%" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " "
                     71:                              "%" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " "
                     72:                              "%" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " "
                     73:                              "%" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 "\n",
                     74:                    &rx_bytes, &rx_packets, &rx_errors, &rx_drop, &rx_fifo,
                     75:                    &rx_frame, &rx_compressed, &rx_multicast, &tx_bytes,
                     76:                    &tx_packets, &tx_errors, &tx_drop, &tx_fifo,
                     77:                    &tx_frame, &tx_compressed, &tx_multicast);
                     78:                
                     79:                if (w != 16)
                     80:                        continue;
                     81:        
                     82:                it->i_major_attr = BYTES;
                     83:                it->i_minor_attr = PACKETS;
                     84:        
                     85:                update_attr(it, BYTES, rx_bytes, tx_bytes, RX_PROVIDED|TX_PROVIDED);
                     86:                update_attr(it, PACKETS, rx_packets, tx_packets, RX_PROVIDED|TX_PROVIDED);
                     87:                update_attr(it, ERRORS, rx_errors, tx_errors, RX_PROVIDED|TX_PROVIDED);
                     88:                update_attr(it, DROP, rx_drop, tx_drop, RX_PROVIDED|TX_PROVIDED);
                     89:                update_attr(it, FIFO, rx_fifo, tx_fifo, RX_PROVIDED|TX_PROVIDED);
                     90:                update_attr(it, FRAME, rx_frame, tx_frame, RX_PROVIDED|TX_PROVIDED);
                     91:                update_attr(it, COMPRESSED, rx_compressed, tx_compressed, RX_PROVIDED|TX_PROVIDED);
                     92:                update_attr(it, MULTICAST, rx_multicast, tx_multicast, RX_PROVIDED|TX_PROVIDED);
                     93: 
                     94:                notify_update(it, NULL);
                     95:                increase_lifetime(it, 1);
                     96:        }
                     97:        
                     98:        fclose(fd);
                     99: }
                    100: 
                    101: static void print_help(void)
                    102: {
                    103:        printf(
                    104:        "proc - procfs statistic collector for Linux" \
                    105:        "\n" \
                    106:        "  Reads statistics from procfs (/proc/net/dev)\n" \
                    107:        "  Author: Thomas Graf <tgraf@suug.ch>\n" \
                    108:        "\n" \
                    109:        "  Options:\n" \
                    110:        "    file=PATH      Path to statistics file (default: /proc/net/dev)\n");
                    111: }
                    112: 
                    113: static void proc_set_opts(tv_t *attrs)
                    114: {
                    115:        while (attrs) {
                    116:                if (!strcasecmp(attrs->type, "file") && attrs->value)
                    117:                        c_path = attrs->value;
                    118:                else if (!strcasecmp(attrs->type, "help")) {
                    119:                        print_help();
                    120:                        exit(0);
                    121:                }
                    122:                attrs = attrs->next;
                    123:        }
                    124: }
                    125: 
                    126: static int proc_probe(void)
                    127: {
                    128:        FILE *fd = fopen(c_path, "r");
                    129: 
                    130:        if (fd) {
                    131:                fclose(fd);
                    132:                return 1;
                    133:        }
                    134:        return 0;
                    135: }
                    136: 
                    137: static struct input_module proc_ops = {
                    138:        .im_name = "proc",
                    139:        .im_read = proc_read,
                    140:        .im_set_opts = proc_set_opts,
                    141:        .im_probe = proc_probe,
                    142: };
                    143: 
                    144: static void __init proc_init(void)
                    145: {
                    146:        register_input_module(&proc_ops);
                    147: }

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