File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bmon / src / in_dummy.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:19:56 2012 UTC (12 years, 4 months ago) by misho
Branches: bmon, MAIN
CVS tags: v2_1_0p0, v2_1_0, HEAD
bmon

    1: /*
    2:  * in_dummy.c                Dummy Input Method
    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: 
   31: static b_cnt_t c_rx_b_inc = 1000000000;
   32: static b_cnt_t c_tx_b_inc = 80000000;
   33: static b_cnt_t c_rx_p_inc = 1000;
   34: static b_cnt_t c_tx_p_inc = 800;
   35: static int c_numdev = 5;
   36: static int c_randomize = 0;
   37: static int c_mtu = 1540;
   38: static int c_maxpps = 100000;
   39: 
   40: static void dummy_read(void)
   41: {
   42: 	int n;
   43: 
   44: 	for (n = 0; n < c_numdev; n++) {
   45: 		char ifname[IFNAMSIZ];
   46: 		item_t *it;
   47: 
   48: 		snprintf(ifname, sizeof(ifname), "dummy%d", n);
   49: 		
   50: 		it = lookup_item(get_local_node(), ifname, 0, 0);
   51: 		if (it == NULL)
   52: 			return;
   53: 
   54: 		it->i_major_attr = BYTES;
   55: 		it->i_minor_attr = PACKETS;
   56: 
   57: 		if (c_randomize) {
   58: 			b_cnt_t rx = rand() % c_maxpps;
   59: 			b_cnt_t tx = rand() % c_maxpps;
   60: 			
   61: 			update_attr(it, PACKETS, rx, tx, RX_PROVIDED | TX_PROVIDED);
   62: 			update_attr(it, BYTES, rx * (rand() % c_mtu),
   63: 				    tx * (rand() % c_mtu), RX_PROVIDED | TX_PROVIDED);
   64: 		} else {
   65: 			update_attr(it, PACKETS, c_rx_p_inc, c_tx_p_inc,
   66: 				    RX_PROVIDED | TX_PROVIDED);
   67: 			update_attr(it, BYTES, c_rx_b_inc, c_tx_b_inc,
   68: 				    RX_PROVIDED | TX_PROVIDED);
   69: 		}
   70: 
   71: 		notify_update(it, NULL);
   72: 		increase_lifetime(it, 1);
   73: 	}
   74: }
   75: 
   76: static void print_help(void)
   77: {
   78: 	printf(
   79: 	"dummy - Statistic generator module (dummy)\n" \
   80: 	"\n" \
   81: 	"  Basic statistic generator for testing purposes. Can produce a\n" \
   82: 	"  constant or random statistic flow with configurable parameters.\n" \
   83: 	"  Author: Thomas Graf <tgraf@suug.ch>\n" \
   84: 	"\n" \
   85: 	"  Options:\n" \
   86: 	"    rxb=NUM        RX bytes increment amount (default: 10^9)\n" \
   87: 	"    txb=NUM        TX bytes increment amount (default: 8*10^7)\n" \
   88: 	"    rxp=NUM        RX packets increment amount (default: 1K)\n" \
   89: 	"    txp=NUM        TX packets increment amount (default: 800)\n" \
   90: 	"    num=NUM        Number of devices (default: 5)\n" \
   91: 	"    randomize      Randomize counters (default: off)\n" \
   92: 	"    seed=NUM       Seed for randomizer (default: time(0))\n" \
   93: 	"    mtu=NUM        Maximal Transmission Unit (default: 1540)\n" \
   94: 	"    maxpps=NUM     Upper limit for packets per second (default: 100K)\n" \
   95: 	"\n" \
   96: 	"  Randomizer:\n" \
   97: 	"    RX-packets := Rand() %% maxpps\n" \
   98: 	"    TX-packets := Rand() %% maxpps\n" \
   99: 	"    RX-bytes   := RX-packets * (Rand() %% mtu)\n" \
  100: 	"    TX-bytes   := TX-packets * (Rand() %% mtu)\n");
  101: }
  102: 
  103: static void dummy_set_opts(tv_t *attrs)
  104: {
  105: 	while (attrs) {
  106: 		if (!strcasecmp(attrs->type, "rxb") && attrs->value)
  107: 			c_rx_b_inc = strtol(attrs->value, NULL, 0);
  108: 		else if (!strcasecmp(attrs->type, "txb") && attrs->value)
  109: 			c_tx_b_inc = strtol(attrs->value, NULL, 0);
  110: 		else if (!strcasecmp(attrs->type, "rxp") && attrs->value)
  111: 			c_rx_p_inc = strtol(attrs->value, NULL, 0);
  112: 		else if (!strcasecmp(attrs->type, "txp") && attrs->value)
  113: 			c_tx_p_inc = strtol(attrs->value, NULL, 0);
  114: 		else if (!strcasecmp(attrs->type, "num") && attrs->value)
  115: 			c_numdev = strtol(attrs->value, NULL, 0);
  116: 		else if (!strcasecmp(attrs->type, "randomize")) {
  117: 			c_randomize = 1;
  118: 			srand(time(0));
  119: 		} else if (!strcasecmp(attrs->type, "seed") && attrs->value)
  120: 			srand(strtol(attrs->value, NULL, 0));
  121: 		else if (!strcasecmp(attrs->type, "mtu") && attrs->value)
  122: 			c_mtu = strtol(attrs->value, NULL, 0);
  123: 		else if (!strcasecmp(attrs->type, "maxpps") && attrs->value)
  124: 			c_maxpps = strtol(attrs->value, NULL, 0);
  125: 		else if (!strcasecmp(attrs->type, "help")) {
  126: 			print_help();
  127: 			exit(0);
  128: 		}
  129: 		
  130: 		attrs = attrs->next;
  131: 	}
  132: }
  133: 
  134: static int dummy_probe(void)
  135: {
  136: 	return 1;
  137: }
  138: 
  139: static struct input_module dummy_ops = {
  140: 	.im_name = "dummy",
  141: 	.im_read = dummy_read,
  142: 	.im_set_opts = dummy_set_opts,
  143: 	.im_probe = dummy_probe,
  144: 	.im_no_default = 1,
  145: };
  146: 
  147: static void __init dummy_init(void)
  148: {
  149: 	register_input_module(&dummy_ops);
  150: }

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