File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bmon / src / in_kstat.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 (13 years, 4 months ago) by misho
Branches: bmon, MAIN
CVS tags: v2_1_0p0, v2_1_0, HEAD
bmon

    1: /*
    2:  * in_kstat.c                  libkstat (SunOS)
    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/item.h>
   28: #include <bmon/node.h>
   29: #include <bmon/utils.h>
   30: 
   31: #if defined HAVE_KSTAT && defined SYS_SUNOS
   32: #include <kstat.h>
   33: 
   34: static void kstat_do_read(void)
   35: {
   36: 	kstat_ctl_t *   kc;
   37: 	kstat_t *       kst;
   38: 	kstat_named_t * kn, *kn2;
   39: 	item_t *        i;
   40: 	
   41: 	if (!(kc = kstat_open()))
   42: 		quit("kstat_open() failed");
   43: 	
   44: 	if ((kst = kstat_lookup(kc, NULL, -1, NULL))) {
   45: 		for (; kst; kst = kst->ks_next) {
   46: 			if (strcmp(kst->ks_class, "net"))
   47: 				continue;
   48: 			
   49: 			if (kstat_read(kc, kst, NULL) < 0)
   50: 				continue;
   51: 
   52: 			if (!strcmp(kst->ks_name, "zero_copy"))
   53: 				continue;
   54: 
   55: 			i = lookup_item(get_local_node(), kst->ks_name, 0, 0);
   56: 
   57: 			if (NULL == i)
   58: 				continue;
   59: 
   60: 			i->i_major_attr = BYTES;
   61: 			i->i_minor_attr = PACKETS;
   62: 
   63: #define KSTAT_GET(S) (kstat_named_t *) kstat_data_lookup(kst, #S)
   64: 
   65: 			if ((kn = KSTAT_GET(rbytes64)) && (kn2 = KSTAT_GET(obytes64))) {
   66: 				update_attr(i, BYTES, kn->value.ui64, kn2->value.ui64,
   67: 				    RX_PROVIDED | TX_PROVIDED | IS64BIT);
   68: 			} else if ((kn = KSTAT_GET(rbytes)) && (kn2 = KSTAT_GET(obytes)))
   69: 				update_attr(i, BYTES, kn->value.ui32, kn2->value.ui32,
   70: 				    RX_PROVIDED | TX_PROVIDED);
   71: 
   72: 			if ((kn = KSTAT_GET(ipackets64)) && (kn2 = KSTAT_GET(opackets64))) {
   73: 				update_attr(i, PACKETS, kn->value.ui64, kn2->value.ui64,
   74: 				    RX_PROVIDED | TX_PROVIDED | IS64BIT);
   75: 			} else if ((kn = KSTAT_GET(ipackets)) && (kn2 = KSTAT_GET(opackets)))
   76: 				update_attr(i, PACKETS, kn->value.ui32, kn2->value.ui32,
   77: 				    RX_PROVIDED | TX_PROVIDED);
   78: 
   79: 			if ((kn = KSTAT_GET(ierror)) && (kn2 = KSTAT_GET(oerrors)))
   80: 				update_attr(i, ERRORS, kn->value.ui32, kn2->value.ui32,
   81: 					RX_PROVIDED | TX_PROVIDED);
   82: 
   83: 			if ((kn = KSTAT_GET(multircv64)) && (kn2 = KSTAT_GET(multixmt64)))
   84: 				update_attr(i, MULTICAST, kn->value.ui64, kn2->value.ui64,
   85: 					RX_PROVIDED | TX_PROVIDED);
   86: 			else if ((kn = KSTAT_GET(multircv)) && (kn2 = KSTAT_GET(multixmt)))
   87: 				update_attr(i, MULTICAST, kn->value.ui32, kn2->value.ui32,
   88: 					RX_PROVIDED | TX_PROVIDED);
   89: 
   90: 			if ((kn = KSTAT_GET(brdcstrcv)) && (kn2 = KSTAT_GET(brdcstxmt)))
   91: 				update_attr(i, BROADCAST, kn->value.ui32, kn2->value.ui32,
   92: 					RX_PROVIDED | TX_PROVIDED);
   93: 
   94: #undef KSTAT_GET
   95: 
   96: 			notify_update(i, NULL);
   97: 			increase_lifetime(i, 1);
   98: 		}
   99: 	}
  100: 	
  101: 	kstat_close(kc);
  102: }
  103: 
  104: static void print_help(void)
  105: {
  106: 	printf(
  107: 	"kstat - kstat statistic collector for SunOS\n" \
  108: 	"\n" \
  109: 	"  SunOS statistic collector using libkstat, kindly provides both,\n" \
  110: 	"  32bit and 64bit counters.\n" \
  111: 	"  Author: Thomas Graf <tgraf@suug.ch>\n" \
  112: 	"\n");
  113: }
  114: 
  115: static void kstat_set_opts(tv_t *attrs)
  116: {
  117: 	while (attrs) {
  118: 		if (!strcasecmp(attrs->type, "help")) {
  119: 			print_help();
  120: 			exit(0);
  121: 		}
  122: 		attrs = attrs->next;
  123: 	}
  124: }
  125: 
  126: static int kstat_probe(void)
  127: {
  128: 	kstat_ctl_t *kc = kstat_open();
  129: 
  130: 	if (kc) {
  131: 		kstat_t * kst = kstat_lookup(kc, NULL, -1, NULL);
  132: 		kstat_close(kc);
  133: 
  134: 		if (kst)
  135: 			return 1;
  136: 	}
  137: 
  138: 	return 0;
  139: }
  140: 
  141: static struct input_module kstat_ops = {
  142: 	.im_name = "kstat",
  143: 	.im_read = kstat_do_read,
  144: 	.im_set_opts = kstat_set_opts,
  145: 	.im_probe = kstat_probe,
  146: };
  147: 
  148: static void __init kstat_init(void)
  149: {
  150: 	register_input_module(&kstat_ops);
  151: }
  152: 
  153: #endif

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