⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel_sunos5.c

📁 ucd-snmp源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*- This is a -*- C -*- compatible code file * * Code for SUNOS5_INSTRUMENTATION * * This file contains includes of standard and local system header files, * includes of other application header files, global variable definitions, * static variable definitions, static function prototypes, and function * definitions. * * This file contains function to obtain statistics from SunOS 5.x kernel * */#include <config.h>#ifdef solaris2/*- * Includes of standard ANSI C header files  */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>/*- * Includes of system header files (wrapped in duplicate include prevention) */#include <fcntl.h>#include <stropts.h>#include <sys/types.h>#include <kvm.h>#include <sys/fcntl.h>#include <kstat.h>#include <errno.h>#include <time.h>#include <sys/sockio.h>#include <sys/socket.h>#include <sys/stream.h>#include <sys/stropts.h>#include <sys/tihdr.h>#include <sys/tiuser.h>#include <inet/common.h>#include <inet/mib2.h>#include <inet/ip.h>#include <net/if.h>#include <netinet/in.h>/*- * Includes of local application header files  */#include "../../snmplib/system.h"#include "asn1.h"#include "snmp_debug.h"#include "mibincl.h"#include "kernel_sunos5.h"kstat_ctl_t *kstat_fd = 0;/*- * Global variable definitions (with initialization) *//*- * Static variable definitions (with initialization) */staticmibcache Mibcache[MIBCACHE_SIZE] = {{MIB_SYSTEM,		0,					(void *)-1, 0, 0, 0, 0},{MIB_INTERFACES,	10*sizeof(mib2_ifEntry_t),		(void *)-1, 0, 10, 0, 0},{MIB_AT,		0,					(void *)-1, 0, 0, 0, 0},{MIB_IP,		sizeof(mib2_ip_t),			(void *)-1, 0, 20, 0, 0},{MIB_IP_ADDR,		20*sizeof(mib2_ipAddrEntry_t),		(void *)-1, 0, 20, 0, 0},{MIB_IP_ROUTE,		200*sizeof(mib2_ipRouteEntry_t),	(void *)-1, 0, 10, 0, 0},{MIB_IP_NET,		100*sizeof(mib2_ipNetToMediaEntry_t),	(void *)-1, 0, 100, 0, 0},{MIB_ICMP,		sizeof(mib2_icmp_t),			(void *)-1, 0, 20, 0, 0},{MIB_TCP,		sizeof(mib2_tcp_t),			(void *)-1, 0, 20, 0, 0},{MIB_TCP_CONN,		1000*sizeof(mib2_tcpConnEntry_t),	(void *)-1, 0, 15, 0, 0},{MIB_UDP,		sizeof(mib2_udp_t),			(void *)-1, 0, 15, 0, 0},{MIB_UDP_LISTEN,	1000*sizeof(mib2_udpEntry_t),		(void *)-1, 0, 15, 0, 0},{MIB_EGP,		0,					(void *)-1, 0, 0, 0, 0},{MIB_CMOT,		0,					(void *)-1, 0, 0, 0, 0},{MIB_TRANSMISSION,	0,					(void *)-1, 0, 0, 0, 0},{MIB_SNMP,		0,					(void *)-1, 0, 0, 0, 0},{0},};staticmibmap	Mibmap[MIBCACHE_SIZE] = {{MIB2_SYSTEM,            0,},{MIB2_INTERFACES,        0,},{MIB2_AT,                0,},{MIB2_IP,                0,},{MIB2_IP,                MIB2_IP_20,},{MIB2_IP,                MIB2_IP_21,},{MIB2_IP,                MIB2_IP_22,},{MIB2_ICMP,              0,},{MIB2_TCP,               0,},{MIB2_TCP,               MIB2_TCP_13,},{MIB2_UDP,               0,},{MIB2_UDP,               MIB2_UDP_5},{MIB2_EGP,               0,},{MIB2_CMOT,              0,},{MIB2_TRANSMISSION,      0,},{MIB2_SNMP,              0,},{0},};static	int		sd = -1;	/* /dev/ip stream descriptor. *//*- * Static function prototypes (use void as argument type if there are none) */static found_egetentry(req_e req_type, void *bufaddr, size_t len, size_t entrysize,	void *resp, int (*comp)(void *, void *), void *arg);static intgetmib(int groupname, int subgroupname, void *statbuf, size_t size,	size_t entrysize, req_e req_type, void *resp, size_t *length,	int (*comp)(void *, void *), void *arg);static intgetif(mib2_ifEntry_t *ifbuf, size_t size, req_e req_type, mib2_ifEntry_t *resp,	size_t *length, int (*comp)(void *, void *), void *arg);static intName_cmp(void *, void *);static voidinit_mibcache_element(mibcache *cp);#define	STREAM_DEV	"/dev/ip"#define	BUFSIZE		40960		/* Buffer for  messages (should be modulo(pagesize) *//*- * Function definitions */#ifdef _STDC_COMPAT#ifdef __cplusplusextern "C" {#endif#endif/* Get various kernel statistics using undocumented Solaris kstat interface.   We need it mainly for getting network interface statistics,   although it is generic enough to be used for any purpose.   It knows about kstat_headers module names and by the name of the statistics   it tries to figure out the rest of necessary information.   Returns 0 in case of success and < 0 if there were any errors.   NOTE: To use this function correctly you have to know the actual   type of the value to be returned, so you may build the test program,   figure out the type and use it. Exposing kstat data types to upper layers   doesn't seem to be reasonable. In any case I'd expect more reasonable kstat   interface. :-(*/int getKstatInt(const char *classname, const char *statname,		const char *varname, int *value){    kstat_t *ks;    kid_t kid;    kstat_named_t *named;    int ret = 0;  /* fail unless ... */    if (kstat_fd == 0) {        kstat_fd = kstat_open();        if (kstat_fd == 0) {            snmp_log(LOG_ERR, "kstat_open(): failed\n");	    goto Return;        }    }    ks = kstat_lookup (kstat_fd, classname, -1, statname);    if (ks == NULL) {	DEBUGMSGTL(("kernel_sunos5", "cannot find kstat %s %s\n",		    classname, statname));      	goto Return;    }    kid = kstat_read (kstat_fd, ks, NULL);    if (kid == -1) {	DEBUGMSGTL(("kernel_sunos5", "cannot read kstat %s %s\n",		    classname, statname));	goto Return;    }    named = kstat_data_lookup(ks, varname);    if (named == NULL) {	DEBUGMSGTL(("kernel_sunos5", "cannot lookup kstat %s %s %s\n",		    classname, statname, varname));	goto Return;    }    ret = 1;	/* maybe successful */    switch (named->data_type) {#ifdef KSTAT_DATA_INT32		/* Solaris 2.6 and up */    case KSTAT_DATA_INT32:      *value = named->value.i32;      break;    case KSTAT_DATA_UINT32:      *value = named->value.ui32;      break;    case KSTAT_DATA_INT64:      *value = named->value.i64;      break;    case KSTAT_DATA_UINT64:      *value = named->value.ui64;      break;#else    case KSTAT_DATA_LONG:      *value = named->value.l;      break;    case KSTAT_DATA_ULONG:      *value = named->value.ul;      break;    case KSTAT_DATA_LONGLONG:      *value = named->value.ll;      break;    case KSTAT_DATA_ULONGLONG:      *value = named->value.ull;      break;#endif    default:      DEBUGMSGTL(("kernel_sunos5", "non-integer type in kstat data: %s %s %s %d\n",	      classname, statname, varname, named->data_type));      ret = 0;  /* fail */      break;    }Return:    return ret;}int getKstatRaw(const char *classname, const char *statname,		size_t len, void *buf){    kstat_t *ks;    kid_t kid;    int ret = 0;  /* fail unless ... */    if (kstat_fd == 0) {        kstat_fd = kstat_open();        if (kstat_fd == 0) {            snmp_log(LOG_ERR, "kstat_open(): failed\n");	    goto Return;        }    }    ks = kstat_lookup (kstat_fd, classname, -1, statname);    if (ks == NULL) {	DEBUGMSGTL(("kernel_sunos5", "cannot find kstat %s %s\n",		    classname, statname));	goto Return;    }    if (ks->ks_type != KSTAT_TYPE_RAW) {	DEBUGMSGTL(("kernel_sunos5", "not raw type kstat %s %s: type %d\n",		    classname, statname, ks->ks_type));	goto Return;    }    kid = kstat_read (kstat_fd, ks, NULL);    if (kid == -1) {	DEBUGMSGTL(("kernel_sunos5", "cannot read kstat %s %s\n",		    classname, statname));	goto Return;    }    if (ks->ks_data_size != len) {	DEBUGMSGTL(("kernel_sunos5", "wrong kstat length %s %s: %d != %d\n",		    classname, statname, ks->ks_data_size, len));	goto Return;    }    memcpy(buf, ks->ks_data, len);    ret = 1;	/* maybe successful */Return:    return ret;}intgetKstat(const char *statname, const char *varname, void *value){  kstat_t	*ks, *kstat_data;  kstat_named_t *d;  size_t	i, instance;  char		module_name[64];  int		ret = -10;  u_longlong_t	val;		/* The largest value */  void		*v;  if (value == NULL)		/* Pretty useless but ... */    v = (void *)&val;  else    v = value;  if (kstat_fd == 0) {      kstat_fd = kstat_open();      if (kstat_fd == 0) {          snmp_log(LOG_ERR, "kstat_open(): failed\n");	  goto Return;      }  }  if (statname == NULL || varname == NULL) {    ret = -20;    goto Return;  }  /* First, get "kstat_headers" statistics. It should     contain all available modules. */  if ((ks = kstat_lookup(kstat_fd, "unix", 0, "kstat_headers")) == NULL) {    DEBUGMSGTL(("kernel_sunos5", "cannot lookup kstat %s %s\n",		"unix", "kstat_headers"));    goto Return;		/* kstat errors */  }  if (kstat_read(kstat_fd, ks, NULL) <= 0) {    DEBUGMSGTL(("kernel_sunos5", "cannot read kstat %s %s\n",		"unix", "kstat_headers"));    goto Return;		/* kstat errors */  }  kstat_data = ks->ks_data;  /* Now, look for the name of our stat in the headers buf */  for (i = 0; i < ks->ks_ndata; i++) {    DEBUGMSGTL(("kernel_sunos5",                "module: %s instance: %d name: %s class: %s type: %d flags: %x\n",                kstat_data[i].ks_module, kstat_data[i].ks_instance,                 kstat_data[i].ks_name, kstat_data[i].ks_class,                kstat_data[i].ks_type, kstat_data[i].ks_flags));    if (strcmp(statname, kstat_data[i].ks_name) == 0) {      strcpy(module_name, kstat_data[i].ks_module);      instance = kstat_data[i].ks_instance;      break;    }  }  if (i == ks->ks_ndata) {    ret = -1;    goto Return;		/* Not found */  }  /* Get the named statistics */  if ((ks = kstat_lookup(kstat_fd, module_name, instance, statname)) == NULL) {    DEBUGMSGTL(("kernel_sunos5", "cannot lookup kstat %s %u %s\n",		module_name, instance, statname));    goto Return;		/* kstat errors */  }  if (kstat_read(kstat_fd, ks, NULL) <= 0) {    DEBUGMSGTL(("kernel_sunos5", "cannot read kstat %s %u %s\n",		module_name, instance, statname));    goto Return;		/* kstat errors */  }  /* This function expects only name/value type of statistics,     so if it is not the case return an error */  if (ks->ks_type != KSTAT_TYPE_NAMED) {    ret = -2;    goto Return;		/* Invalid stat type */  }  for (i = 0, d = KSTAT_NAMED_PTR(ks); i < ks->ks_ndata; i++, d++) {    DEBUGMSGTL(("kernel_sunos5", "variable: %s %d\n", d->name, d->data_type));    if (strcmp(d->name, varname) == 0) {      switch (d->data_type) {      case KSTAT_DATA_CHAR:	*(char *)v = (int)d->value.c;	DEBUGMSGTL(("kernel_sunos5", "value: %d\n", (int)d->value.c));	break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -