Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / md.c
index db85c46..016e6b0 100644 (file)
--- a/src/md.c
+++ b/src/md.c
 #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 */