X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fmd.c;h=016e6b0cbe5ef5370465f392453413b50f42054a;hp=db85c46c2891d55195bd2c4d480423f280cfeee5;hb=da11ce02eb202b3e01d3e2d1b40f248a84430973;hpb=e1bfa71aca1f37c2f293dc9adb44065c6e7a9ad9 diff --git a/src/md.c b/src/md.c index db85c46c..016e6b0c 100644 --- a/src/md.c +++ b/src/md.c @@ -37,105 +37,85 @@ #define PROC_DISKSTATS "/proc/diskstats" #define DEV_DIR "/dev" -static const char *config_keys[] = -{ - "Device", - "IgnoreSelected" -}; -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); +static const char *config_keys[] = {"Device", "IgnoreSelected"}; +static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); static ignorelist_t *ignorelist = NULL; -static int md_config (const char *key, const char *value) -{ +static int md_config(const char *key, const char *value) { if (ignorelist == NULL) - ignorelist = ignorelist_create (/* invert = */ 1); + ignorelist = ignorelist_create(/* invert = */ 1); if (ignorelist == NULL) - return (1); - - if (strcasecmp (key, "Device") == 0) - { - ignorelist_add (ignorelist, value); - } - else if (strcasecmp (key, "IgnoreSelected") == 0) - { - ignorelist_set_invert (ignorelist, IS_TRUE (value) ? 0 : 1); - } - else - { - return (-1); + return 1; + + if (strcasecmp(key, "Device") == 0) { + ignorelist_add(ignorelist, value); + } else if (strcasecmp(key, "IgnoreSelected") == 0) { + ignorelist_set_invert(ignorelist, IS_TRUE(value) ? 0 : 1); + } else { + return -1; } - return (0); + return 0; } -static void md_submit (const int minor, const char *type_instance, - gauge_t value) -{ +static void md_submit(const int minor, const char *type_instance, + gauge_t value) { value_list_t vl = VALUE_LIST_INIT; - vl.values = &(value_t) { .gauge = value }; + vl.values = &(value_t){.gauge = value}; vl.values_len = 1; - sstrncpy (vl.host, hostname_g, sizeof (vl.host)); - sstrncpy (vl.plugin, "md", sizeof (vl.plugin)); - ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), - "%i", minor); - sstrncpy (vl.type, "md_disks", sizeof (vl.type)); - sstrncpy (vl.type_instance, type_instance, - sizeof (vl.type_instance)); - - plugin_dispatch_values (&vl); + sstrncpy(vl.plugin, "md", sizeof(vl.plugin)); + snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", minor); + sstrncpy(vl.type, "md_disks", sizeof(vl.type)); + sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); + + plugin_dispatch_values(&vl); } /* void md_submit */ -static void md_process (const int minor, const char *path) -{ +static void md_process(const int minor, const char *path) { char errbuf[1024]; int fd; struct stat st; mdu_array_info_t array; gauge_t disks_missing; - fd = open (path, O_RDONLY); - if (fd < 0) - { - WARNING ("md: open(%s): %s", path, - sstrerror (errno, errbuf, sizeof (errbuf))); + fd = open(path, O_RDONLY); + if (fd < 0) { + WARNING("md: open(%s): %s", path, sstrerror(errno, errbuf, sizeof(errbuf))); return; } - if (fstat (fd, &st) < 0) - { - WARNING ("md: Unable to fstat file descriptor for %s: %s", path, - sstrerror (errno, errbuf, sizeof (errbuf))); - close (fd); + if (fstat(fd, &st) < 0) { + WARNING("md: Unable to fstat file descriptor for %s: %s", path, + sstrerror(errno, errbuf, sizeof(errbuf))); + close(fd); return; } - if (! S_ISBLK (st.st_mode)) - { - WARNING ("md: %s is no block device", path); - close (fd); + if (!S_ISBLK(st.st_mode)) { + WARNING("md: %s is no block device", path); + close(fd); return; } - if (st.st_rdev != makedev (MD_MAJOR, minor)) - { - WARNING ("md: Major/minor of %s are %i:%i, should be %i:%i", - path, (int)major(st.st_rdev), (int)minor(st.st_rdev), - (int)MD_MAJOR, minor); - close (fd); + if (st.st_rdev != makedev(MD_MAJOR, minor)) { + WARNING("md: Major/minor of %s are %i:%i, should be %i:%i", path, + (int)major(st.st_rdev), (int)minor(st.st_rdev), (int)MD_MAJOR, + minor); + close(fd); return; } /* Retrieve md information */ - if (ioctl (fd, GET_ARRAY_INFO, &array) < 0) { - WARNING ("md: Unable to retrieve array info from %s: %s", path, - sstrerror (errno, errbuf, sizeof (errbuf))); - close (fd); + if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { + WARNING("md: Unable to retrieve array info from %s: %s", path, + sstrerror(errno, errbuf, sizeof(errbuf))); + close(fd); return; } - close (fd); + close(fd); /* * The mdu_array_info_t structure contains numbers of disks in the array. @@ -150,51 +130,48 @@ static void md_process (const int minor, const char *path) * disks are missing and smaller than "nr" when spare disks are * around. */ - md_submit (minor, "active", (gauge_t) array.active_disks); - md_submit (minor, "failed", (gauge_t) array.failed_disks); - md_submit (minor, "spare", (gauge_t) array.spare_disks); + md_submit(minor, "active", (gauge_t)array.active_disks); + md_submit(minor, "failed", (gauge_t)array.failed_disks); + md_submit(minor, "spare", (gauge_t)array.spare_disks); disks_missing = 0.0; if (array.raid_disks > array.nr_disks) - disks_missing = (gauge_t) (array.raid_disks - array.nr_disks); - md_submit (minor, "missing", disks_missing); + disks_missing = (gauge_t)(array.raid_disks - array.nr_disks); + md_submit(minor, "missing", disks_missing); } /* void md_process */ -static int md_read (void) -{ +static int md_read(void) { FILE *fh; char buffer[1024]; - fh = fopen (PROC_DISKSTATS, "r"); + fh = fopen(PROC_DISKSTATS, "r"); if (fh == NULL) { char errbuf[1024]; - WARNING ("md: Unable to open %s: %s", - PROC_DISKSTATS , - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); + WARNING("md: Unable to open %s: %s", PROC_DISKSTATS, + sstrerror(errno, errbuf, sizeof(errbuf))); + return -1; } /* Iterate md devices */ - while (fgets (buffer, sizeof (buffer), fh) != NULL) - { + while (fgets(buffer, sizeof(buffer), fh) != NULL) { char path[PATH_MAX]; char *fields[4]; char *name; int major, minor; /* Extract interesting fields */ - if (strsplit (buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3) + if (strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3) continue; - major = atoi (fields[0]); + major = atoi(fields[0]); if (major != MD_MAJOR) continue; - minor = atoi (fields[1]); + minor = atoi(fields[1]); name = fields[2]; - if (ignorelist_match (ignorelist, name)) + if (ignorelist_match(ignorelist, name)) continue; /* FIXME: Don't hardcode path. Walk /dev collecting major, @@ -203,18 +180,17 @@ static int md_read (void) * major/minor, but that again can be tricky if the filesystem * with the device file is mounted using the "nodev" option. */ - ssnprintf (path, sizeof (path), "%s/%s", DEV_DIR, name); + snprintf(path, sizeof(path), "%s/%s", DEV_DIR, name); - md_process (minor, path); + md_process(minor, path); } - fclose (fh); + fclose(fh); - return (0); + return 0; } /* int md_read */ -void module_register (void) -{ - plugin_register_config ("md", md_config, config_keys, config_keys_num); - plugin_register_read ("md", md_read); +void module_register(void) { + plugin_register_config("md", md_config, config_keys, config_keys_num); + plugin_register_read("md", md_read); } /* void module_register */