Blame SOURCES/net-snmp-5.7.2-diskio-whitelist.patch

b5ae06
1092308 - backport diskio device filtering
b5ae06
b5ae06
Backported from:
b5ae06
b5ae06
commit 5be210c90870ff6bab193d497d401b92c1d50db9
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Thu Mar 6 13:26:30 2014 +0100
b5ae06
b5ae06
    CHANGES: snmpd: add new snmpd.conf option 'diskio' to monitor only selected disks.
b5ae06
b5ae06
    On machines with thousands of block devices, parsing /proc/diskstats is really
b5ae06
    slow. The new option enables monitoring of selected devices, saving lot of CPU
b5ae06
    time.
b5ae06
    
b5ae06
diff -up net-snmp-5.7.2/agent/mibgroup/ucd-snmp/diskio.c.test net-snmp-5.7.2/agent/mibgroup/ucd-snmp/diskio.c
b5ae06
--- net-snmp-5.7.2/agent/mibgroup/ucd-snmp/diskio.c.test	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ net-snmp-5.7.2/agent/mibgroup/ucd-snmp/diskio.c	2015-06-18 15:14:57.164891695 +0200
b5ae06
@@ -27,11 +27,18 @@
b5ae06
 
b5ae06
 #include <math.h>
b5ae06
 
b5ae06
+#if defined (linux)
b5ae06
+/* for stat() */
b5ae06
+#include <ctype.h>
b5ae06
+#include <sys/stat.h>
b5ae06
+#endif
b5ae06
+
b5ae06
 #include <net-snmp/net-snmp-includes.h>
b5ae06
 #include <net-snmp/agent/net-snmp-agent-includes.h>
b5ae06
 
b5ae06
 #include "util_funcs/header_simple_table.h"
b5ae06
 
b5ae06
+#include "struct.h"
b5ae06
 /*
b5ae06
  * include our .h file 
b5ae06
  */
b5ae06
@@ -95,6 +102,66 @@ static int ps_numdisks;			/* number of d
b5ae06
 #if defined (linux)
b5ae06
 #define DISKIO_SAMPLE_INTERVAL 5
b5ae06
 void devla_getstats(unsigned int regno, void * dummy);
b5ae06
+static void diskio_parse_config_disks(const char *token, char *cptr);
b5ae06
+static void diskio_free_config(void);
b5ae06
+static int get_sysfs_stats(void);
b5ae06
+
b5ae06
+struct diskiopart {
b5ae06
+    char            syspath[STRMAX];	/* full stat path */
b5ae06
+    char            name[STRMAX];	/* name as provided */
b5ae06
+    char            shortname[STRMAX];	/* short name for output */
b5ae06
+    int             major;
b5ae06
+    int             minor;
b5ae06
+};
b5ae06
+
b5ae06
+static int             numdisks;
b5ae06
+static int             maxdisks = 0;
b5ae06
+static struct diskiopart *disks;
b5ae06
+
b5ae06
+#define DISK_INCR 2
b5ae06
+
b5ae06
+typedef struct linux_diskio
b5ae06
+{
b5ae06
+    int major;
b5ae06
+    int  minor;
b5ae06
+    unsigned long  blocks;
b5ae06
+    char name[256];
b5ae06
+    unsigned long  rio;
b5ae06
+    unsigned long  rmerge;
b5ae06
+    unsigned long  rsect;
b5ae06
+    unsigned long  ruse;
b5ae06
+    unsigned long  wio;
b5ae06
+    unsigned long  wmerge;
b5ae06
+    unsigned long  wsect;
b5ae06
+    unsigned long  wuse;
b5ae06
+    unsigned long  running;
b5ae06
+    unsigned long  use;
b5ae06
+    unsigned long  aveq;
b5ae06
+} linux_diskio;
b5ae06
+
b5ae06
+/* disk load averages */
b5ae06
+typedef struct linux_diskio_la
b5ae06
+{
b5ae06
+    unsigned long use_prev;
b5ae06
+    double la1, la5, la15;
b5ae06
+} linux_diskio_la;
b5ae06
+
b5ae06
+typedef struct linux_diskio_header
b5ae06
+{
b5ae06
+    linux_diskio* indices;
b5ae06
+    int length;
b5ae06
+    int alloc;
b5ae06
+} linux_diskio_header;
b5ae06
+
b5ae06
+typedef struct linux_diskio_la_header
b5ae06
+{
b5ae06
+    linux_diskio_la * indices;
b5ae06
+    int length;
b5ae06
+} linux_diskio_la_header;
b5ae06
+
b5ae06
+static linux_diskio_header head;
b5ae06
+static linux_diskio_la_header la_head;
b5ae06
+
b5ae06
 #endif /* linux */
b5ae06
 
b5ae06
 #if defined (darwin)
b5ae06
@@ -228,6 +295,8 @@ init_diskio(void)
b5ae06
     devla_getstats(0, NULL);
b5ae06
     /* collect LA data regularly */
b5ae06
     snmp_alarm_register(DISKIO_SAMPLE_INTERVAL, SA_REPEAT, devla_getstats, NULL);
b5ae06
+    snmpd_register_config_handler("diskio", diskio_parse_config_disks,
b5ae06
+        diskio_free_config, "path | device");
b5ae06
 #endif
b5ae06
 
b5ae06
 
b5ae06
@@ -870,49 +939,134 @@ var_diskio(struct variable * vp,
b5ae06
 
b5ae06
 #ifdef linux
b5ae06
 
b5ae06
-#define DISK_INCR 2
b5ae06
-
b5ae06
-typedef struct linux_diskio
b5ae06
+static void
b5ae06
+diskio_free_config()
b5ae06
+ {
b5ae06
+    if (la_head.length) {
b5ae06
+        /* reset any usage stats, we may get different list of devices from config */
b5ae06
+        free(la_head.indices);
b5ae06
+        la_head.length = 0;
b5ae06
+        la_head.indices = NULL;
b5ae06
+    }
b5ae06
+    if (numdisks > 0) {
b5ae06
+        int i;
b5ae06
+        head.length = 0;
b5ae06
+        numdisks = 0;
b5ae06
+        for (i = 0; i < maxdisks; i++) {    /* init/erase disk db */
b5ae06
+            disks[i].syspath[0] = 0;
b5ae06
+            disks[i].name[0] = 0;
b5ae06
+            disks[i].shortname[0] = 0;
b5ae06
+            disks[i].major = -1;
b5ae06
+            disks[i].minor = -1;
b5ae06
+        }
b5ae06
+    }
b5ae06
+}
b5ae06
+static int
b5ae06
+disk_exists(char *path) 
b5ae06
 {
b5ae06
-    int major;
b5ae06
-    int  minor;
b5ae06
-    unsigned long  blocks;
b5ae06
-    char name[256];
b5ae06
-    unsigned long  rio;
b5ae06
-    unsigned long  rmerge;
b5ae06
-    unsigned long  rsect;
b5ae06
-    unsigned long  ruse;
b5ae06
-    unsigned long  wio;
b5ae06
-    unsigned long  wmerge;
b5ae06
-    unsigned long  wsect;
b5ae06
-    unsigned long  wuse;
b5ae06
-    unsigned long  running;
b5ae06
-    unsigned long  use;
b5ae06
-    unsigned long  aveq;
b5ae06
-} linux_diskio;
b5ae06
+    int index;
b5ae06
+    for(index = 0; index < numdisks; index++) {
b5ae06
+        DEBUGMSGTL(("ucd-snmp/disk", "Checking for %s. Found %s at %d\n", path, disks[index].syspath, index));
b5ae06
+        if(strcmp(path, disks[index].syspath) == 0) {
b5ae06
+            return index;
b5ae06
+        }
b5ae06
+    }
b5ae06
+    return -1;
b5ae06
+}
b5ae06
 
b5ae06
-/* disk load averages */
b5ae06
-typedef struct linux_diskio_la
b5ae06
-{
b5ae06
-    unsigned long use_prev;
b5ae06
-    double la1, la5, la15;
b5ae06
-} linux_diskio_la;
b5ae06
+static void
b5ae06
+add_device(char *path, int addNewDisks ) 
b5ae06
+ {
b5ae06
+    int index;
b5ae06
+    char device[STRMAX];
b5ae06
+    char syspath[STRMAX];
b5ae06
+    char *basename;
b5ae06
+    struct stat stbuf;
b5ae06
 
b5ae06
-typedef struct linux_diskio_header
b5ae06
-{
b5ae06
-    linux_diskio* indices;
b5ae06
-    int length;
b5ae06
-    int alloc;
b5ae06
-} linux_diskio_header;
b5ae06
+    if (!path || !strcmp(path, "none")) {
b5ae06
+        DEBUGMSGTL(("ucd-snmp/diskio", "Skipping null path device (%s)\n", path));
b5ae06
+        return;
b5ae06
+    }
b5ae06
+    if (numdisks == maxdisks) {
b5ae06
+        if (maxdisks == 0) {
b5ae06
+            maxdisks = 50;
b5ae06
+            disks = malloc(maxdisks * sizeof(struct diskiopart));
b5ae06
+            if (!disks) {
b5ae06
+                config_perror("malloc failed for new disko allocation.");
b5ae06
+	            netsnmp_config_error("\tignoring:  %s", path);
b5ae06
+                return;
b5ae06
+            }
b5ae06
+            memset(disks, 0, maxdisks * sizeof(struct diskiopart));
b5ae06
+        } else {
b5ae06
+            maxdisks *= 2;
b5ae06
+            disks = realloc(disks, maxdisks * sizeof(struct diskiopart));
b5ae06
+            if (!disks) {
b5ae06
+                config_perror("malloc failed for new disko allocation.");
b5ae06
+	            netsnmp_config_error("\tignoring:  %s", path);
b5ae06
+                return;
b5ae06
+            }
b5ae06
+            memset(disks + maxdisks/2, 0, maxdisks/2 * sizeof(struct diskiopart));
b5ae06
+        }
b5ae06
+    }
b5ae06
 
b5ae06
-typedef struct linux_diskio_la_header
b5ae06
-{
b5ae06
-    linux_diskio_la * indices;   
b5ae06
-    int length;
b5ae06
-} linux_diskio_la_header;
b5ae06
+    /* first find the path for this device */
b5ae06
+    device[0]='\0';
b5ae06
+    if ( *path != '/' ) {
b5ae06
+        strlcpy(device, "/dev/", STRMAX - 1 );
b5ae06
+    }
b5ae06
+    strncat(device, path, STRMAX - 1 );
b5ae06
+
b5ae06
+    /* check for /dev existence */
b5ae06
+    if ( stat(device,&stbuf)!=0 ) { /* ENOENT */
b5ae06
+        config_perror("diskio path does not exist.");
b5ae06
+        netsnmp_config_error("\tignoring:  %s", path);
b5ae06
+        return;
b5ae06
+    }
b5ae06
+    else if ( ! S_ISBLK(stbuf.st_mode) ) { /* ENODEV */
b5ae06
+        config_perror("diskio path is not a device.");
b5ae06
+        netsnmp_config_error("\tignoring:  %s", path);
b5ae06
+        return;
b5ae06
+    }
b5ae06
 
b5ae06
-static linux_diskio_header head;
b5ae06
-static linux_diskio_la_header la_head;
b5ae06
+    /* either came with a slash or we just put one there, so the following always works */
b5ae06
+    basename = strrchr(device, '/' )+1;
b5ae06
+    /* construct a sys path using the device numbers to avoid having to disambiguate the various text forms */
b5ae06
+    snprintf( syspath, STRMAX - 1, "/sys/dev/block/%d:%d/stat", major(stbuf.st_rdev), minor(stbuf.st_rdev) );
b5ae06
+    DEBUGMSGTL(("ucd-snmp/diskio", " monitoring sys path (%s)\n", syspath));
b5ae06
+
b5ae06
+    index = disk_exists(syspath);
b5ae06
+
b5ae06
+    if(index == -1 && addNewDisks){
b5ae06
+        /* The following buffers are cleared above, no need to add '\0' */
b5ae06
+        strlcpy(disks[numdisks].syspath, syspath, sizeof(disks[numdisks].syspath) - 1);
b5ae06
+        strlcpy(disks[numdisks].name, path, sizeof(disks[numdisks].name) - 1);
b5ae06
+        strlcpy(disks[numdisks].shortname, basename, sizeof(disks[numdisks].shortname) - 1);
b5ae06
+        disks[numdisks].major = major(stbuf.st_rdev);
b5ae06
+        disks[numdisks].minor = minor(stbuf.st_rdev);
b5ae06
+        numdisks++;  
b5ae06
+    }
b5ae06
+}
b5ae06
+
b5ae06
+static void 
b5ae06
+diskio_parse_config_disks(const char *token, char *cptr)
b5ae06
+ {
b5ae06
+#if HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS
b5ae06
+    char path[STRMAX];
b5ae06
+
b5ae06
+
b5ae06
+    /*
b5ae06
+     * read disk path (eg, /1 or /usr) 
b5ae06
+     */
b5ae06
+    copy_nword(cptr, path, sizeof(path));
b5ae06
+
b5ae06
+    /* TODO: we may include regular expressions in future */
b5ae06
+    /*
b5ae06
+     * check if the disk already exists, if so then modify its
b5ae06
+     * parameters. if it does not exist then add it
b5ae06
+     */
b5ae06
+    add_device(path, 1);
b5ae06
+#endif /* HAVE_FSTAB_H || HAVE_GETMNTENT || HAVE_STATFS */
b5ae06
+}
b5ae06
 
b5ae06
 void devla_getstats(unsigned int regno, void * dummy) {
b5ae06
 
b5ae06
@@ -976,6 +1130,47 @@ int is_excluded(const char *name)
b5ae06
     return 0;
b5ae06
 }
b5ae06
 
b5ae06
+static int get_sysfs_stats()
b5ae06
+{
b5ae06
+    int i;
b5ae06
+    char buffer[1024];
b5ae06
+
b5ae06
+    head.length  = 0;
b5ae06
+
b5ae06
+    for(i = 0; i < numdisks; i++) {
b5ae06
+        FILE *f = fopen(disks[i].syspath, "r");
b5ae06
+        if ( f == NULL ) {
b5ae06
+            DEBUGMSGTL(("ucd-snmp/diskio", "Can't open %s, skipping", disks[i].syspath));
b5ae06
+            continue;
b5ae06
+        }
b5ae06
+        if (fgets(buffer, sizeof(buffer), f) == NULL) {
b5ae06
+            DEBUGMSGTL(("ucd-snmp/diskio", "Can't read %s, skipping", disks[i].syspath));
b5ae06
+            fclose(f);
b5ae06
+            continue;
b5ae06
+        }
b5ae06
+
b5ae06
+        linux_diskio* pTemp;
b5ae06
+        if (head.length == head.alloc) {
b5ae06
+            head.alloc += DISK_INCR;
b5ae06
+            head.indices = (linux_diskio *) realloc(head.indices, head.alloc*sizeof(linux_diskio));
b5ae06
+        }
b5ae06
+        pTemp = &head.indices[head.length];
b5ae06
+        pTemp->major = disks[i].major;
b5ae06
+        pTemp->minor = disks[i].minor;
b5ae06
+        strlcpy( pTemp->name, disks[i].shortname, sizeof(pTemp->name) - 1 );
b5ae06
+        if (sscanf (buffer, "%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu\n",
b5ae06
+                &pTemp->rio, &pTemp->rmerge, &pTemp->rsect, &pTemp->ruse,
b5ae06
+                &pTemp->wio, &pTemp->wmerge, &pTemp->wsect, &pTemp->wuse,
b5ae06
+                &pTemp->running, &pTemp->use, &pTemp->aveq) != 11)
b5ae06
+            sscanf (buffer, "%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu%*[ \n\t]%lu\n",
b5ae06
+                &pTemp->rio, &pTemp->rsect,
b5ae06
+                &pTemp->wio, &pTemp->wsect);
b5ae06
+        head.length++;
b5ae06
+        fclose(f);
b5ae06
+    }
b5ae06
+    return 0;
b5ae06
+}
b5ae06
+
b5ae06
 static int
b5ae06
 getstats(void)
b5ae06
 {
b5ae06
@@ -995,6 +1189,14 @@ getstats(void)
b5ae06
 
b5ae06
     memset(head.indices, 0, head.alloc*sizeof(linux_diskio));
b5ae06
 
b5ae06
+    if (numdisks>0) {
b5ae06
+        /* 'diskio' configuration is used - go through the whitelist only and
b5ae06
+         * read /sys/dev/block/xxx */
b5ae06
+        cache_time = now;
b5ae06
+        return get_sysfs_stats();
b5ae06
+    }
b5ae06
+    /* 'diskio' configuration is not used - report all devices */
b5ae06
+
b5ae06
     /* Is this a 2.6 kernel? */
b5ae06
     parts = fopen("/proc/diskstats", "r");
b5ae06
     if (parts) {
b5ae06
@@ -1111,13 +1313,22 @@ var_diskio(struct variable * vp,
b5ae06
       long_ret = head.indices[indx].wio & 0xffffffff;
b5ae06
       return (u_char *) & long_ret;
b5ae06
     case DISKIO_LA1:
b5ae06
-      long_ret = la_head.indices[indx].la1;
b5ae06
+      if (la_head.length > indx)
b5ae06
+          long_ret = la_head.indices[indx].la1;
b5ae06
+      else
b5ae06
+          long_ret = 0;
b5ae06
       return (u_char *) & long_ret;
b5ae06
     case DISKIO_LA5:
b5ae06
-      long_ret = la_head.indices[indx].la5;
b5ae06
+      if (la_head.length > indx)
b5ae06
+          long_ret = la_head.indices[indx].la5;
b5ae06
+      else
b5ae06
+          long_ret = 0;
b5ae06
       return (u_char *) & long_ret;
b5ae06
     case DISKIO_LA15:
b5ae06
-      long_ret = la_head.indices[indx].la15;
b5ae06
+      if (la_head.length > indx)
b5ae06
+          long_ret = la_head.indices[indx].la15;
b5ae06
+      else
b5ae06
+          long_ret = 0;
b5ae06
       return (u_char *) & long_ret;
b5ae06
     case DISKIO_NREADX:
b5ae06
       *var_len = sizeof(struct counter64);
b5ae06
diff -up net-snmp-5.7.2/man/snmpd.conf.5.def.test net-snmp-5.7.2/man/snmpd.conf.5.def
b5ae06
--- net-snmp-5.7.2/man/snmpd.conf.5.def.test	2015-06-18 15:13:31.249470179 +0200
b5ae06
+++ net-snmp-5.7.2/man/snmpd.conf.5.def	2015-06-18 15:16:45.481423115 +0200
b5ae06
@@ -715,6 +715,15 @@ e.g. "loop0"
b5ae06
 .IP "diskio_exclude_ram yes"
b5ae06
 Excludes all LInux ramdisk block devices, whose names start with "ram", e.g.
b5ae06
 "ram0"
b5ae06
+.PP
b5ae06
+On Linux systems, it is possible to report only explicitly whitelisted
b5ae06
+devices. It may take significant amount of time to process diskIOTable data
b5ae06
+on systems with tens of thousands of block devices and whitelisting only the
b5ae06
+important ones avoids large CPU consumption.
b5ae06
+.IP "diskio <device>"
b5ae06
+Enables whitelisting of devices and adds the device to the whitelist. Only
b5ae06
+explicitly whitelisted devices will be reported. This option may be used
b5ae06
+multiple times.
b5ae06
 .SS System Load Monitoring
b5ae06
 This requires that the agent was built with support for either the
b5ae06
 \fIucd\-snmp/loadave\fR module or the \fIucd\-snmp/memory\fR module
b5ae06
b5ae06
b5ae06
commit 59f9f3387dab4238114804a0be9e4c15667d868c
b5ae06
Author: Jan Safranek <jsafranek@users.sourceforge.net>
b5ae06
Date:   Fri Jun 19 09:29:06 2015 +0200
b5ae06
b5ae06
    Fixed memory leak on realloc failure.
b5ae06
    
b5ae06
    Found by Coverity.
b5ae06
b5ae06
diff --git a/agent/mibgroup/ucd-snmp/diskio.c b/agent/mibgroup/ucd-snmp/diskio.c
b5ae06
index f04d5c5..58163d8 100644
b5ae06
--- a/agent/mibgroup/ucd-snmp/diskio.c
b5ae06
+++ b/agent/mibgroup/ucd-snmp/diskio.c
b5ae06
@@ -405,13 +405,17 @@ add_device(char *path, int addNewDisks )
b5ae06
             }
b5ae06
             memset(disks, 0, maxdisks * sizeof(struct diskiopart));
b5ae06
         } else {
b5ae06
+            struct diskiopart *newdisks;
b5ae06
             maxdisks *= 2;
b5ae06
-            disks = realloc(disks, maxdisks * sizeof(struct diskiopart));
b5ae06
-            if (!disks) {
b5ae06
+            newdisks = realloc(disks, maxdisks * sizeof(struct diskiopart));
b5ae06
+            if (!newdisks) {
b5ae06
+                free(disks);
b5ae06
+                disks = NULL;
b5ae06
                 config_perror("malloc failed for new disko allocation.");
b5ae06
 	            netsnmp_config_error("\tignoring:  %s", path);
b5ae06
                 return;
b5ae06
             }
b5ae06
+            disks = newdisks;
b5ae06
             memset(disks + maxdisks/2, 0, maxdisks/2 * sizeof(struct diskiopart));
b5ae06
         }
b5ae06
     }