From 9cbb3b91f11ad0c4944a1428d609201c054cffab Mon Sep 17 00:00:00 2001 From: "James Dominic P. Guana" Date: Tue, 26 May 2020 10:57:48 +0800 Subject: Handle LVM and RAID --- glibtop.h | 2 +- include/glibtop/disk.h | 10 +- sysdeps/common/default.c | 4 +- sysdeps/linux/disk.c | 218 +++++++++++++++++++++++++++++++++++++--- sysdeps/linux/glibtop_private.c | 27 +++-- sysdeps/linux/glibtop_private.h | 4 +- 6 files changed, 229 insertions(+), 36 deletions(-) diff --git a/glibtop.h b/glibtop.h index 804294da..c196818c 100644 --- a/glibtop.h +++ b/glibtop.h @@ -80,7 +80,7 @@ struct _glibtop int socket; /* Accepted connection of a socket */ int ncpu; /* Number of CPUs, zero if single-processor */ int real_ncpu; /* Real number of CPUs. Only ncpu are monitored */ - int ndisk; /* Number of DISKs, zero if single-disk */ + int ndisk; /* Number of DISKs, zero if single-disk */ int real_ndisk; /* Number of PHYSICAL DISKs. Only ncpu is monitored */ unsigned long os_version_code; /* Version code of the operating system */ const char *name; /* Program name for error messages */ diff --git a/include/glibtop/disk.h b/include/glibtop/disk.h index 8843112c..cdde7796 100644 --- a/include/glibtop/disk.h +++ b/include/glibtop/disk.h @@ -42,6 +42,14 @@ G_BEGIN_DECLS typedef struct _glibtop_disk glibtop_disk; +struct _partition_info +{ + char name[256]; + char type[256]; + char raid_num[256]; + int max; +}; + struct _glibtop_disk { guint64 xdisk_sectors_read [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_SECTORS_READ */ @@ -50,7 +58,7 @@ struct _glibtop_disk guint64 xdisk_time_write [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_TIME_WRITE */ }; -void glibtop_get_disk(glibtop_disk *buf); +void glibtop_get_disk (glibtop_disk *buf); #if GLIBTOP_SUID_DISK #define glibtop_get_disk_r glibtop_get_disk_p diff --git a/sysdeps/common/default.c b/sysdeps/common/default.c index c20f2938..653a4f5a 100644 --- a/sysdeps/common/default.c +++ b/sysdeps/common/default.c @@ -72,9 +72,9 @@ glibtop_get_cpu(glibtop_cpu *buf) * */ void -glibtop_get_cpu(glibtop_cpu *buf) +glibtop_get_cpu (glibtop_cpu *buf) { - glibtop_get_cpu_l(glibtop_global_server, buf); + glibtop_get_cpu_l (glibtop_global_server, buf); } diff --git a/sysdeps/linux/disk.c b/sysdeps/linux/disk.c index 159f8f12..3ad74bcc 100644 --- a/sysdeps/linux/disk.c +++ b/sysdeps/linux/disk.c @@ -39,58 +39,244 @@ _glibtop_init_disk_s (glibtop *server) /* Provides information about disk usage. */ -#define FILENAME "/proc/diskstats" //kernel reports sectors by 512 bytes even for AF 4kn +// Linux kernel reports sectors by 512 bytes even for AF 4kn // + +#define FILENAME "/proc/diskstats" +#define CMD_PIPE "lsblk --output NAME,TYPE -i -n | sed 's/`-//'|sed 's/|-//'|sed 's/|//'| sed -e 's/^[ \t]*//'|tr -s ' '" #define STAT_BUFSIZ 81920 +// Handle LVM and RAID // + +void +find_primary_part (_partition_info *primary_part, const char *m) +{ + int n = 0, tlvl = 0; + char name[256]="",type[256]=""; + + primary_part->max = 0; + + //scan by tree level + //0 = disk (to lvl 0) + //0 = disk, 1 = part (to lvl 1) + //0 = disk, 1 = part, 2 = lvm or raid (to lvl 2) + //0 = disk, 1 = part, 2 = raid, 3 = lvm (to lvl 3) + + while (sscanf(m, "%s %s", name, type) == 2) + { + + if (tlvl == 0) { + + if (strcmp (type, "disk") == 0) { + + primary_part->max++; + + } + else if ((strcmp (type, "part") == 0)){ + + tlvl = 1; + + } + + } + else if(tlvl == 1){ + + if (strcmp (type, "disk") == 0) { + + n--; + tlvl = 0; + primary_part->max++; + + } + else if ((strcmp (type, "part") == 0)) { + + n--; + + } + else if ((strcmp (type, "lvm") == 0)) { + + tlvl = 2; + primary_part->max++; + + } + else if ((strncmp (type, "raid", 4) == 0)) { + + tlvl = 2; + primary_part->max++; + + } + + } + else if( tlvl == 2){ + + if (strcmp(type, "disk") == 0) { + + n--; + tlvl = 0; + primary_part->max++; + + } + else if ((strcmp (primary_part[n-1].type, "lvm") == 0) && (strcmp (type, "lvm") == 0)) { + + n--; + + } + else if ((strcmp (primary_part[n-1].type, "raid") == 0) && (strncmp (type, "raid", 4) == 0)) { + + n--; + + } + else if ((strcmp (primary_part[n-1].type, "lvm") == 0) && (strcmp (type, "part") == 0)) { + + n--; + tlvl = 1; + + } + else if ((strcmp (primary_part[n-1].type, "raid") == 0) && (strcmp (type, "part") == 0)) { + + n--; + tlvl = 1; + + } + else if ((strcmp (primary_part[n-1].type, "raid") == 0) && (strcmp (type, "lvm") == 0)){ + + tlvl = 3; + primary_part->max++; + + } + + } + else if (tlvl == 3) { + + if (strcmp (type, "disk") == 0) { + + n--; + tlvl = 0; + primary_part->max++; + + } + else if ((strcmp (type, "lvm") == 0)) { + + n--; + + } + else if ((strncmp (type, "raid", 4) == 0)) { + + n--; + tlvl = 2; + + } + else if ((strcmp (type, "part") == 0)) { + + n--; + tlvl = 1; + + } + } + + + strcpy (primary_part[n].name, name); + strncpy (primary_part[n].type, type, 4); + + if (strcmp (primary_part[n].type, "raid") == 0) { + + strcpy (primary_part[n].raid_num, type + 4); + + } + + m = skip_line (m); + n++; + + } +} + +int +is_virtual_drive (_partition_info *primary_part, const char *p) +{ + int i; + char name[256]; + int test = 1; + sscanf (p, "%s", name); + + if (*p) { + + for (i=0; imax; i++) { + + if (strcmp (primary_part[i].name, name) == 0) { + + test = 0; + break; + + } + + } + + } + else { + + test = 0; + + } + + return test; +} + void glibtop_get_disk_s (glibtop *server, glibtop_disk *buf) { - char buffer [STAT_BUFSIZ], *p; + _partition_info primary_part[GLIBTOP_NDISK]; + char buffer [STAT_BUFSIZ], *p, map_buffer [STAT_BUFSIZ], *m; int i; memset (buf, 0, sizeof (glibtop_disk)); - file_to_buffer(server, buffer, sizeof buffer, FILENAME); + file_to_buffer (server, buffer, sizeof buffer, FILENAME); + + get_from_pipe (map_buffer, CMD_PIPE); + + server->ndisk = GLIBTOP_NDISK; /* * GLOBAL */ p = buffer; /* "disk" */ + m = map_buffer; /* * PER DISK */ + find_primary_part (primary_part, m); + for (i = 0; i <= server->ndisk; i++) { - p = skip_multiple_token(p,2); + p = skip_multiple_token (p,2); - // skip if disk is the raw device - if(!check_alphanumeric_word(p)){ + // skip if disk is the raw device + if (!is_virtual_drive (primary_part, p)) { - p = skip_line(p); /* move to ^ */ - p = skip_multiple_token(p,2); + p = skip_line (p); /* move to ^ */ + p = skip_multiple_token (p, 2); - } + } - if (!check_disk_line_warn(server, p, i)) + if (!check_disk_line_warn (server, p, i)) break; - p = skip_token(p); /* prev xdisk_name */ - p = skip_token(p); /* prev xdisk_reads_completed */ - p = skip_token(p); /* prev xdisk_reads_merged */ + p = skip_token (p); /* prev xdisk_name */ + p = skip_token (p); /* prev xdisk_reads_completed */ + p = skip_token (p); /* prev xdisk_reads_merged */ buf->xdisk_sectors_read [i] = strtoull (p, &p, 0); buf->xdisk_time_read [i] = strtoull (p, &p, 0); - p = skip_token(p); /* prev xdisk_writes_completed */ - p = skip_token(p); /* prev xdisk_writes_merged */ + p = skip_token (p); /* prev xdisk_writes_completed */ + p = skip_token (p); /* prev xdisk_writes_merged */ buf->xdisk_sectors_write [i] = strtoull (p, &p, 0); buf->xdisk_time_write [i] = strtoull (p, &p, 0); - p = skip_line(p); /* move to ^ */ + p = skip_line (p); /* move to ^ */ } } diff --git a/sysdeps/linux/glibtop_private.c b/sysdeps/linux/glibtop_private.c index 7b36561c..7bca42a5 100644 --- a/sysdeps/linux/glibtop_private.c +++ b/sysdeps/linux/glibtop_private.c @@ -57,21 +57,20 @@ skip_token (const char *p) } -int -check_alphanumeric_word (const char *p) +void +get_from_pipe (char *buffer, const char *cmd) { - int test = 0; - p = next_token(p); - while (*p && !g_ascii_isspace(*p)) { - if(g_ascii_isalpha(*p)){ - test = 0; - }else if(g_ascii_isdigit(*p)){ - test = 1; - } - p++; - }; - p = next_token(p); - return test; + FILE* fp; + long psize; + + fp = popen (cmd, "r"); + + fseek (fp, 0, SEEK_END); + psize = ftell (fp); + fseek (fp, 0, SEEK_SET); + fread(buffer,1,psize,fp); + + pclose (fp); } diff --git a/sysdeps/linux/glibtop_private.h b/sysdeps/linux/glibtop_private.h index d42ee669..66fed54c 100644 --- a/sysdeps/linux/glibtop_private.h +++ b/sysdeps/linux/glibtop_private.h @@ -61,8 +61,8 @@ skip_line (const char *p) return (char *) (*p ? p+1 : p); } -int -check_alphanumeric_word (const char *p); +void +get_from_pipe (char *buffer, const char *cmd) /* * Smart strtoul which handles binary suffixes -- cgit v1.2.1