Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Jul 2005 12:26:44 GMT
From:      soc-victor <soc-victor@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 80254 for review
Message-ID:  <200507151226.j6FCQi3j087400@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <sys/time.h>	/*for struct timeval*/
 #include <sys/queue.h>  /*for SLIST, TAILQ & friends*/
 
+#include <sys/param.h>  /*for getfsstat*/
+#include <sys/ucred.h>	/*for getfsstat*/
+#include <sys/mount.h>	/*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();
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507151226.j6FCQi3j087400>