From owner-p4-projects@FreeBSD.ORG Fri Jul 15 12:26:45 2005 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 5B8D316A420; Fri, 15 Jul 2005 12:26:45 +0000 (GMT) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 36EDC16A41C for ; Fri, 15 Jul 2005 12:26:45 +0000 (GMT) (envelope-from soc-victor@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id E18E143D4C for ; Fri, 15 Jul 2005 12:26:44 +0000 (GMT) (envelope-from soc-victor@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id j6FCQieR087403 for ; Fri, 15 Jul 2005 12:26:44 GMT (envelope-from soc-victor@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id j6FCQi3j087400 for perforce@freebsd.org; Fri, 15 Jul 2005 12:26:44 GMT (envelope-from soc-victor@freebsd.org) Date: Fri, 15 Jul 2005 12:26:44 GMT Message-Id: <200507151226.j6FCQi3j087400@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to soc-victor@freebsd.org using -f From: soc-victor To: Perforce Change Reviews Cc: Subject: PERFORCE change 80254 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jul 2005 12:26:46 -0000 http://perforce.freebsd.org/chv.cgi?CH=80254 Change 80254 by soc-victor@soc-victor_82.76.158.176 on 2005/07/15 12:26:43 Added support for FS stuff into the hrStorageTable implementation. Next (and last) is the VM part of this SNMP table. Affected files ... .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#5 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#5 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#2 edit Differences ... ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#5 (text+ko) ==== @@ -146,8 +146,8 @@ */ static void hostres_idle_v(void) { - if ( (time(NULL) - hrState_g.hrStorage_tbl_age) > 7 ) { - HR_DPRINTF((stderr, " %s: need refresh\n ",__func__)); + if ( (time(NULL) - hrState_g.hrStorage_tbl_age) > HR_STORAGE_TBL_REFRESH ) { + HR_DPRINTF((stderr, "%s: hrStorageTable needd refresh\n ",__func__)); refresh_hrStorage_tbl_v(); } ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#5 (text+ko) ==== @@ -40,7 +40,11 @@ #include /*for struct timeval*/ #include /*for SLIST, TAILQ & friends*/ +#include /*for getfsstat*/ +#include /*for getfsstat*/ +#include /*for getfsstat*/ + /*a debug macro*/ #ifndef NDEBUG #define HR_DPRINTF(ARGS) do { \ @@ -66,6 +70,10 @@ struct kvm_swap *swap_devs; /*for kvm_getswapinfo*/ size_t swap_devs_len; /*idem */ + + struct statfs *fs_buf; /*for getfsstat*/ + size_t fs_buf_count; /*idem*/ + uint32_t next_hrStorage_index; /*next int availabe for indexing the hrStorageTable*/ time_t hrStorage_tbl_age; @@ -126,10 +134,11 @@ /* * Finalization routine for hrStorageTable. - * It destroys the lists and frees the heap memory + * It destroys the lists and frees any allocated heap memory */ void fini_hrStorage_tbl_v(void); +#define HR_STORAGE_TBL_REFRESH 7 /* * Refresh routine for hrStorageTable. * Usable for polling the system for any changes. ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#2 (text+ko) ==== @@ -48,6 +48,7 @@ static void hrStorage_get_vm_v(void){ + /*FIX ME*/ HR_DPRINTF((stderr, "WARNING: hrStorageTable vm not implmeneted yet.\n ")); } @@ -74,8 +75,10 @@ if (map == NULL) { /* new object - get a new index */ - if (hrState_g.next_hrStorage_index > UINT_MAX) + if (hrState_g.next_hrStorage_index > INT_MAX) { + syslog(LOG_ERR, "%s: hrStorageTable index wrap", __func__ ); errx(1, "hrStorage index wrap"); + } if ((map = malloc(sizeof(*map))) == NULL) { syslog(LOG_ERR, "%s: %m", __func__ ); @@ -142,7 +145,7 @@ void hrStorage_get_swap_v(void) { int nswapdev = 0; int len = sizeof(nswapdev); - struct hrStorageTblEntry *entry, *entry_tmp; + struct hrStorageTblEntry *entry; char swap_w_prefix[255+1]; if ( sysctlbyname("vm.nswapdev", &nswapdev,&len, NULL,0 ) < 0 ) { @@ -164,6 +167,7 @@ assert( hrState_g.swap_devs != NULL ); if ( hrState_g.swap_devs == NULL ) { + hrState_g.swap_devs_len = 0; return; } } @@ -176,9 +180,6 @@ return; } - /*mark each entry as missisng*/ - TAILQ_FOREACH(entry, &storage_tbl, link) - entry->flags &= ~HR_STORAGE_FOUND; for(len = 0; len < nswapdev; len++ ) { @@ -205,29 +206,162 @@ }/*end for*/ - /* - * Purge swap items that disappeared - */ - entry = TAILQ_FIRST(&storage_tbl); - while (entry != NULL) { - entry_tmp = TAILQ_NEXT(entry, link); - if (!(entry->flags & HR_STORAGE_FOUND)) - hrStorageTblEntry_delete_v(entry); - entry = entry_tmp; + + +} + +/* + * Very silly detection for FS storage types. + * Should be changed to use devinfo API. + * FIX ME + */ +static +void hrStrorage_getType_v(const struct statfs *fs_p, struct asn_oid *out_type_p) { + assert(fs_p != NULL); + assert(out_type_p != NULL); + if( !(fs_p->f_flags & MNT_LOCAL) ) { + *out_type_p = (struct asn_oid)OIDX_hrStorageNetworkDisk; + return; + } + + if( strncmp (fs_p->f_fstypename, "procfs", MFSNAMELEN ) == 0 || + strncmp (fs_p->f_fstypename, "devfs", MFSNAMELEN ) == 0 ) { + *out_type_p = (struct asn_oid)OIDX_hrStorageOther; + return; + + } + if( strncmp (fs_p->f_mntfromname, "/dev/fd", strlen("/dev/fd") ) == 0 || + strncmp (fs_p->f_mntfromname, "/dev/afd", strlen("/dev/afd") ) == 0) { + *out_type_p = (struct asn_oid)OIDX_hrStorageFloppyDisk; + return; + } + + if( strncmp (fs_p->f_mntfromname, "/dev/acd", strlen("/dev/acd") ) == 0 || + strncmp (fs_p->f_mntfromname, "/dev/cd", strlen("/dev/cd") ) == 0) { + *out_type_p = (struct asn_oid)OIDX_hrStorageCompactDisc; + return; + } + + + if( strncmp (fs_p->f_mntfromname, "/dev/ad", strlen("/dev/ad") ) == 0 || + strncmp (fs_p->f_mntfromname, "/dev/ar", strlen("/dev/ar") ) == 0 || + strncmp (fs_p->f_mntfromname, "/dev/da", strlen("/dev/da") ) == 0 ) { + *out_type_p = (struct asn_oid)OIDX_hrStorageFixedDisk; + return; } + + *out_type_p = (struct asn_oid)OIDX_hrStorageOther; /*not known*/ + return; } +static +void hrStorage_get_fs_v(void) { + int mounted_fs_count = 0; + int i = 0; + struct hrStorageTblEntry *entry; + char fs_string[255+1]; + uint64_t used_blocks_count = 0; + + if( (mounted_fs_count = getfsstat(NULL, 0, MNT_NOWAIT)) < 0 ) { + syslog(LOG_ERR, "getfsstat(NULL, 0, MNT_NOWAIT) failed: %m\n"); + return; /*out of luck this time*/ + } + + if( mounted_fs_count != (int)hrState_g.fs_buf_count || hrState_g.fs_buf == NULL ) { + hrState_g.fs_buf_count = mounted_fs_count; + hrState_g.fs_buf = (struct statfs *)reallocf(hrState_g.fs_buf, + hrState_g.fs_buf_count * sizeof(struct statfs)); + if(hrState_g.fs_buf == NULL) { + hrState_g.fs_buf_count = 0; + assert(0); + return; + } + } + + if( (mounted_fs_count = getfsstat(hrState_g.fs_buf, + hrState_g.fs_buf_count * sizeof(struct statfs), + MNT_NOWAIT)) < 0 ) { + syslog(LOG_ERR, "getfsstat(, , MNT_NOWAIT) failed: %m \n"); + return; /*out of luck this time*/ + } + + HR_DPRINTF((stderr, "%s: Got %d mounted FS\n ", __func__, mounted_fs_count)); + for(i = 0; i < mounted_fs_count; i++ ) { + + memset(&fs_string[0], '\0', sizeof(fs_string) ); + snprintf(fs_string, 255, "%s, type: %s, dev: %s", hrState_g.fs_buf[i].f_mntonname, + hrState_g.fs_buf[i].f_fstypename, + hrState_g.fs_buf[i].f_mntfromname); + + entry = hrStorageTblEntry_find_by_name(fs_string); + if(entry != NULL) { + entry->flags |= HR_STORAGE_FOUND; + + hrStrorage_getType_v( &hrState_g.fs_buf[i], &entry->type); + + entry->allocationUnits = ( hrState_g.fs_buf[i].f_bsize > (uint64_t)INT_MAX ? + INT_MAX : + (int32_t)hrState_g.fs_buf[i].f_bsize ); /*may overflow the SNMP type*/ + + entry->size = ( hrState_g.fs_buf[i].f_blocks > (uint64_t)INT_MAX ? + INT_MAX : + (int32_t)hrState_g.fs_buf[i].f_blocks ); /*may overflow the SNMP type*/ + + used_blocks_count = hrState_g.fs_buf[i].f_blocks - hrState_g.fs_buf[i].f_bfree; + + entry->used = ( used_blocks_count > (uint64_t)INT_MAX ? + INT_MAX : + used_blocks_count ); /*may overflow the SNMP type*/ + + entry->allocationFailures = 0; + continue; + } + if ((entry = hrStorageTblEntry_create(fs_string)) != NULL) { + entry->flags |= HR_STORAGE_FOUND; + + hrStrorage_getType_v( &hrState_g.fs_buf[i], &entry->type); + + entry->allocationUnits = ( hrState_g.fs_buf[i].f_bsize > (uint64_t)INT_MAX ? + INT_MAX : + (int32_t)hrState_g.fs_buf[i].f_bsize ); /*may overflow the SNMP type*/ + + entry->size = ( hrState_g.fs_buf[i].f_blocks > (uint64_t)INT_MAX ? + INT_MAX : + (int32_t)hrState_g.fs_buf[i].f_blocks ); /*may overflow the SNMP type*/ + + used_blocks_count = hrState_g.fs_buf[i].f_blocks - hrState_g.fs_buf[i].f_bfree; + + entry->used = ( used_blocks_count > (uint64_t)INT_MAX ? + INT_MAX : + used_blocks_count ); /*may overflow the SNMP type*/ + + entry->allocationFailures = 0; + + } + + }/*end for*/ + + +} + void init_hrStorage_tbl_v(void) { hrState_g.swap_devs = NULL; hrState_g.swap_devs_len = 0; + + hrState_g.fs_buf = NULL; + hrState_g.fs_buf_count = 0; + + hrState_g.next_hrStorage_index = 1; hrStorage_get_vm_v(); hrStorage_get_swap_v(); + hrStorage_get_fs_v(); + } void fini_hrStorage_tbl_v(void) { @@ -237,9 +371,15 @@ free( hrState_g.swap_devs ); hrState_g.swap_devs = NULL; } - hrState_g.swap_devs_len = 0; - + + if ( hrState_g.fs_buf != NULL ) { + free( hrState_g.fs_buf ); + hrState_g.fs_buf = NULL; + } + hrState_g.fs_buf_count = 0; + + n1 = STAILQ_FIRST(&storage_name_map); while (n1 != NULL) { n2 = STAILQ_NEXT(n1, link); @@ -259,21 +399,40 @@ void refresh_hrStorage_tbl_v(void) { + + struct hrStorageTblEntry *entry = NULL, *entry_tmp = NULL; + if ( this_tick <= hrState_g.hr_tick) { - HR_DPRINTF((stderr, " %s: no refresh needed\n ",__func__)); + HR_DPRINTF((stderr, "%s: no refresh needed\n ",__func__)); return; } + /*mark each entry as missisng*/ + TAILQ_FOREACH(entry, &storage_tbl, link) + entry->flags &= ~HR_STORAGE_FOUND; hrStorage_get_vm_v(); hrStorage_get_swap_v(); + hrStorage_get_fs_v(); + + /* + * Purge items items that disappeared + */ + entry = TAILQ_FIRST(&storage_tbl); + while (entry != NULL) { + entry_tmp = TAILQ_NEXT(entry, link); + if (!(entry->flags & HR_STORAGE_FOUND)) + hrStorageTblEntry_delete_v(entry); + entry = entry_tmp; + } + hrState_g.hr_tick = this_tick; hrState_g.hrStorage_tbl_age = time(NULL); - HR_DPRINTF((stderr, " %s: refresh DONE\n ",__func__)); + HR_DPRINTF((stderr, "%s: refresh DONE\n ",__func__)); } int op_hrStorageTable(struct snmp_context *ctx __unused, @@ -290,7 +449,7 @@ refresh entries here?! */ if ( (time(NULL) - hrState_g.hrStorage_tbl_age) > 7 ) { - HR_DPRINTF((stderr, " %s: need refresh\n ",__func__)); + HR_DPRINTF((stderr, "%s: need refresh\n ",__func__)); refresh_hrStorage_tbl_v(); }