Merge branch 'collectd-5.7' into collectd-5.8
authorFlorian Forster <octo@collectd.org>
Thu, 18 Oct 2018 06:18:25 +0000 (08:18 +0200)
committerFlorian Forster <octo@collectd.org>
Thu, 18 Oct 2018 06:18:25 +0000 (08:18 +0200)
1  2 
src/disk.c
src/powerdns.c

diff --combined src/disk.c
@@@ -120,9 -120,6 +120,9 @@@ static struct gmesh geom_tree
  /* #endif KERNEL_FREEBSD */
  
  #elif HAVE_LIBKSTAT
 +#if HAVE_KSTAT_H
 +#include <kstat.h>
 +#endif
  #define MAX_NUMDISK 1024
  extern kstat_ctl_t *kc;
  static kstat_t *ksp[MAX_NUMDISK];
@@@ -142,7 -139,7 +142,7 @@@ static int pnumdisk
  #error "No applicable input method."
  #endif
  
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
  #include <libudev.h>
  
  static char *conf_udev_name_attr = NULL;
@@@ -159,7 -156,7 +159,7 @@@ static int disk_config(const char *key
    if (ignorelist == NULL)
      ignorelist = ignorelist_create(/* invert = */ 1);
    if (ignorelist == NULL)
 -    return (1);
 +    return 1;
  
    if (strcasecmp("Disk", key) == 0) {
      ignorelist_add(ignorelist, value);
              "on Mach / Mac OS X and will be ignored.");
  #endif
    } else if (strcasecmp("UdevNameAttr", key) == 0) {
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
      if (conf_udev_name_attr != NULL) {
        free(conf_udev_name_attr);
        conf_udev_name_attr = NULL;
      }
      if ((conf_udev_name_attr = strdup(value)) == NULL)
 -      return (1);
 +      return 1;
  #else
      WARNING("disk plugin: The \"UdevNameAttr\" option is only supported "
              "if collectd is built with libudev support");
  #endif
    } else {
 -    return (-1);
 +    return -1;
    }
  
 -  return (0);
 +  return 0;
  } /* int disk_config */
  
  static int disk_init(void) {
    if (status != kIOReturnSuccess) {
      ERROR("IOMasterPort failed: %s", mach_error_string(status));
      io_master_port = MACH_PORT_NULL;
 -    return (-1);
 +    return -1;
    }
  /* #endif HAVE_IOKIT_IOKITLIB_H */
  
  #elif KERNEL_LINUX
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
    if (conf_udev_name_attr != NULL) {
      handle_udev = udev_new();
      if (handle_udev == NULL) {
        ERROR("disk plugin: udev_new() failed!");
 -      return (-1);
 +      return -1;
      }
    }
 -#endif /* HAVE_LIBUDEV */
 +#endif /* HAVE_UDEV_H */
  /* #endif KERNEL_LINUX */
  
  #elif KERNEL_FREEBSD
    rv = geom_gettree(&geom_tree);
    if (rv != 0) {
      ERROR("geom_gettree() failed, returned %d", rv);
 -    return (-1);
 +    return -1;
    }
    rv = geom_stats_open();
    if (rv != 0) {
      ERROR("geom_stats_open() failed, returned %d", rv);
 -    return (-1);
 +    return -1;
    }
  /* #endif KERNEL_FREEBSD */
  
    numdisk = 0;
  
    if (kc == NULL)
 -    return (-1);
 +    return -1;
  
    for (numdisk = 0, ksp_chain = kc->kc_chain;
         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
    }
  #endif /* HAVE_LIBKSTAT */
  
 -  return (0);
 +  return 0;
  } /* int disk_init */
  
  static int disk_shutdown(void) {
  #if KERNEL_LINUX
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
    if (handle_udev != NULL)
      udev_unref(handle_udev);
 -#endif /* HAVE_LIBUDEV */
 +#endif /* HAVE_UDEV_H */
  #endif /* KERNEL_LINUX */
 -  return (0);
 +  return 0;
  } /* int disk_shutdown */
  
  static void disk_submit(const char *plugin_instance, const char *type,
@@@ -324,11 -321,11 +324,11 @@@ static counter_t disk_calc_time_incr(co
    double avg_time = ((double)delta_time) / ((double)delta_ops);
    double avg_time_incr = interval * avg_time;
  
 -  return ((counter_t)(avg_time_incr + .5));
 +  return (counter_t)(avg_time_incr + .5);
  }
  #endif
  
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
  /**
   * Attempt to provide an rename disk instance from an assigned udev attribute.
   *
@@@ -366,7 -363,7 +366,7 @@@ static signed long long dict_get_value(
                                        kCFStringEncodingASCII);
    if (key_obj == NULL) {
      DEBUG("CFStringCreateWithCString (%s) failed.", key);
 -    return (-1LL);
 +    return -1LL;
    }
  
    /* get => we don't need to release (== free) the object */
  
    if (val_obj == NULL) {
      DEBUG("CFDictionaryGetValue (%s) failed.", key);
 -    return (-1LL);
 +    return -1LL;
    }
  
    if (!CFNumberGetValue(val_obj, kCFNumberSInt64Type, &val_int)) {
      DEBUG("CFNumberGetValue (%s) failed.", key);
 -    return (-1LL);
 +    return -1LL;
    }
  
 -  return (val_int);
 +  return val_int;
  }
  #endif /* HAVE_IOKIT_IOKITLIB_H */
  
@@@ -411,7 -408,7 +411,7 @@@ static int disk_read(void) 
            io_master_port, IOServiceMatching(kIOBlockStorageDriverClass),
            &disk_list) != kIOReturnSuccess) {
      ERROR("disk plugin: IOServiceGetMatchingServices failed.");
 -    return (-1);
 +    return -1;
    }
  
    while ((disk = IOIteratorNext(disk_list)) != 0) {
          sstrncpy(disk_name, props_disk_name_bsd, sizeof(disk_name));
        else {
          ERROR("disk plugin: can't find bsd disk name.");
 -        ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major,
 -                  disk_minor);
 +        snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
        }
      } else
 -      ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
 +      snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor);
  
      DEBUG("disk plugin: disk_name = \"%s\"", disk_name);
  
      snap = geom_stats_snapshot_get();
      if (snap == NULL) {
        ERROR("disk plugin: geom_stats_snapshot_get() failed.");
 -      return (-1);
 +      return -1;
      }
  
      /* Check if we have dirty read from this snapshot */
          if (geom_gettree(&geom_tree) != 0) {
            ERROR("disk plugin: geom_gettree() failed");
            geom_stats_snapshot_free(snap);
 -          return (-1);
 +          return -1;
          }
          geom_id = geom_lookupid(&geom_tree, snap_iter->id);
        }
      fh = fopen("/proc/partitions", "r");
      if (fh == NULL) {
        ERROR("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
 -      return (-1);
 +      return -1;
      }
  
      /* Kernel is 2.4.* */
  
      numfields = strsplit(buffer, fields, 32);
  
-     if ((numfields != (14 + fieldshift)) && (numfields != 7))
+     /* need either 7 fields (partition)
+      * or at least 14 fields (15 on Linux 2.4) */
+     if ((numfields != 7) && (numfields < (14 + fieldshift)))
        continue;
  
      minor = atoll(fields[1]);
        read_sectors = atoll(fields[4]);
        write_ops = atoll(fields[5]);
        write_sectors = atoll(fields[6]);
-     } else if (numfields == (14 + fieldshift)) {
+     } else {
+       assert(numfields >= (14 + fieldshift));
        read_ops = atoll(fields[3 + fieldshift]);
        write_ops = atoll(fields[7 + fieldshift]);
  
          io_time = atof(fields[12 + fieldshift]);
          weighted_time = atof(fields[13 + fieldshift]);
        }
-     } else {
-       DEBUG("numfields = %i; => unknown file format.", numfields);
-       continue;
      }
  
      {
  
      output_name = disk_name;
  
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
      char *alt_name = NULL;
      if (conf_udev_name_attr != NULL) {
        alt_name =
  #endif
  
      if (ignorelist_match(ignorelist, output_name) != 0) {
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
        /* release udev-based alternate name, if allocated */
        sfree(alt_name);
  #endif
          submit_io_time(output_name, io_time, weighted_time);
      } /* if (is_disk) */
  
 -#if HAVE_LIBUDEV
 +#if HAVE_UDEV_H
      /* release udev-based alternate name, if allocated */
      sfree(alt_name);
  #endif
    static kstat_io_t kio;
  
    if (kc == NULL)
 -    return (-1);
 +    return -1;
  
    for (int i = 0; i < numdisk; i++) {
      if (kstat_read(kc, ksp[i], &kio) == -1)
    char name[DATA_MAX_NAME_LEN];
  
    if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
 -    return (0);
 +    return 0;
  
    for (int counter = 0; counter < disks; counter++) {
      strncpy(name, ds->disk_name, sizeof(name));
      char errbuf[1024];
      WARNING("disk plugin: perfstat_disk: %s",
              sstrerror(errno, errbuf, sizeof(errbuf)));
 -    return (-1);
 +    return -1;
    }
  
    if (numdisk != pnumdisk || stat_disk == NULL) {
      char errbuf[1024];
      WARNING("disk plugin: perfstat_disk : %s",
              sstrerror(errno, errbuf, sizeof(errbuf)));
 -    return (-1);
 +    return -1;
    }
  
    for (int i = 0; i < rnumdisk; i++) {
    }
  #endif /* defined(HAVE_PERFSTAT) */
  
 -  return (0);
 +  return 0;
  } /* int disk_read */
  
  void module_register(void) {
diff --combined src/powerdns.c
@@@ -282,7 -282,7 +282,7 @@@ static statname_lookup_t lookup_table[
          {"ipv6-questions", "dns_question", "incoming-ipv6"},
          {"malloc-bytes", "gauge", "malloc_bytes"},
          {"max-mthread-stack", "gauge", "max_mthread_stack"},
-         {"no-packet-error", "gauge", "no_packet_error"},
+         {"no-packet-error", "errors", "no_packet_error"},
          {"noedns-outqueries", "dns_question", "outgoing-noedns"},
          {"noping-outqueries", "dns_question", "outgoing-noping"},
          {"over-capacity-drops", "dns_question", "incoming-over_capacity"},
@@@ -395,7 -395,7 +395,7 @@@ static int powerdns_get_data_dgram(list
    sd = socket(PF_UNIX, item->socktype, 0);
    if (sd < 0) {
      FUNC_ERROR("socket");
 -    return (-1);
 +    return -1;
    }
  
    sa_unix.sun_family = AF_UNIX;
    if ((status != 0) && (errno != ENOENT)) {
      SOCK_ERROR("unlink", sa_unix.sun_path);
      close(sd);
 -    return (-1);
 +    return -1;
    }
  
    do /* while (0) */
    unlink(sa_unix.sun_path);
  
    if (status != 0)
 -    return (-1);
 +    return -1;
  
    assert(buffer_size > 0);
    buffer = malloc(buffer_size);
    if (buffer == NULL) {
      FUNC_ERROR("malloc");
 -    return (-1);
 +    return -1;
    }
  
    memcpy(buffer, temp, buffer_size - 1);
    buffer[buffer_size - 1] = 0;
  
    *ret_buffer = buffer;
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_get_data_dgram */
  
  static int powerdns_get_data_stream(list_item_t *item, char **ret_buffer) {
    sd = socket(PF_UNIX, item->socktype, 0);
    if (sd < 0) {
      FUNC_ERROR("socket");
 -    return (-1);
 +    return -1;
    }
  
    struct timeval timeout;
    if (status != 0) {
      FUNC_ERROR("setsockopt");
      close(sd);
 -    return (-1);
 +    return -1;
    }
  
    status =
    if (status != 0) {
      SOCK_ERROR("connect", item->sockaddr.sun_path);
      close(sd);
 -    return (-1);
 +    return -1;
    }
  
    /* strlen + 1, because we need to send the terminating NULL byte, too. */
    if (status < 0) {
      SOCK_ERROR("send", item->sockaddr.sun_path);
      close(sd);
 -    return (-1);
 +    return -1;
    }
  
    while (42) {
  
  static int powerdns_get_data(list_item_t *item, char **ret_buffer) {
    if (item->socktype == SOCK_DGRAM)
 -    return (powerdns_get_data_dgram(item, ret_buffer));
 +    return powerdns_get_data_dgram(item, ret_buffer);
    else if (item->socktype == SOCK_STREAM)
 -    return (powerdns_get_data_stream(item, ret_buffer));
 +    return powerdns_get_data_stream(item, ret_buffer);
    else {
      ERROR("powerdns plugin: Unknown socket type: %i", (int)item->socktype);
 -    return (-1);
 +    return -1;
    }
  } /* int powerdns_get_data */
  
@@@ -574,14 -574,14 +574,14 @@@ static int powerdns_read_server(list_it
      item->command = strdup(SERVER_COMMAND);
    if (item->command == NULL) {
      ERROR("powerdns plugin: strdup failed.");
 -    return (-1);
 +    return -1;
    }
  
    char *buffer = NULL;
    int status = powerdns_get_data(item, &buffer);
    if (status != 0) {
      ERROR("powerdns plugin: powerdns_get_data failed.");
 -    return (status);
 +    return status;
    }
    if (buffer == NULL) {
      return EINVAL;
  
    sfree(buffer);
  
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_read_server */
  
  /*
@@@ -644,7 -644,7 +644,7 @@@ static int powerdns_update_recursor_com
    int status;
  
    if (li == NULL)
 -    return (0);
 +    return 0;
  
    if (li->fields_num < 1) {
      sstrncpy(buffer, RECURSOR_COMMAND, sizeof(buffer));
                       /* seperator = */ " ");
      if (status < 0) {
        ERROR("powerdns plugin: strjoin failed.");
 -      return (-1);
 +      return -1;
      }
      buffer[sizeof(buffer) - 1] = 0;
      size_t len = strlen(buffer);
    li->command = strdup(buffer);
    if (li->command == NULL) {
      ERROR("powerdns plugin: strdup failed.");
 -    return (-1);
 +    return -1;
    }
  
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_update_recursor_command */
  
  static int powerdns_read_recursor(list_item_t *item) /* {{{ */
      status = powerdns_update_recursor_command(item);
      if (status != 0) {
        ERROR("powerdns plugin: powerdns_update_recursor_command failed.");
 -      return (-1);
 +      return -1;
      }
  
      DEBUG("powerdns plugin: powerdns_read_recursor: item->command = %s;",
    status = powerdns_get_data(item, &buffer);
    if (status != 0) {
      ERROR("powerdns plugin: powerdns_get_data failed.");
 -    return (-1);
 +    return -1;
    }
  
    keys_list = strdup(item->command);
    if (keys_list == NULL) {
      FUNC_ERROR("strdup");
      sfree(buffer);
 -    return (-1);
 +    return -1;
    }
  
    key_saveptr = NULL;
    sfree(buffer);
    sfree(keys_list);
  
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_read_recursor */
  
  static int powerdns_config_add_collect(list_item_t *li, /* {{{ */
    if (ci->values_num < 1) {
      WARNING("powerdns plugin: The `Collect' option needs "
              "at least one argument.");
 -    return (-1);
 +    return -1;
    }
  
    for (int i = 0; i < ci->values_num; i++)
      if (ci->values[i].type != OCONFIG_TYPE_STRING) {
        WARNING("powerdns plugin: Only string arguments are allowed to "
                "the `Collect' option.");
 -      return (-1);
 +      return -1;
      }
  
    temp =
        realloc(li->fields, sizeof(char *) * (li->fields_num + ci->values_num));
    if (temp == NULL) {
      WARNING("powerdns plugin: realloc failed.");
 -    return (-1);
 +    return -1;
    }
    li->fields = temp;
  
    /* Invalidate a previously computed command */
    sfree(li->command);
  
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_config_add_collect */
  
  static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */
    if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
      WARNING("powerdns plugin: `%s' needs exactly one string argument.",
              ci->key);
 -    return (-1);
 +    return -1;
    }
  
    item = calloc(1, sizeof(*item));
    if (item == NULL) {
      ERROR("powerdns plugin: calloc failed.");
 -    return (-1);
 +    return -1;
    }
  
    item->instance = strdup(ci->values[0].value.string);
    if (item->instance == NULL) {
      ERROR("powerdns plugin: strdup failed.");
      sfree(item);
 -    return (-1);
 +    return -1;
    }
  
    /*
    } else {
      /* We must never get here.. */
      assert(0);
 -    return (-1);
 +    return -1;
    }
  
    status = 0;
    if (status != 0) {
      sfree(socket_temp);
      sfree(item);
 -    return (-1);
 +    return -1;
    }
  
    DEBUG("powerdns plugin: Add server: instance = %s;", item->instance);
  
    sfree(socket_temp);
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_config_add_server */
  
  static int powerdns_config(oconfig_item_t *ci) /* {{{ */
  
      if (list == NULL) {
        ERROR("powerdns plugin: `llist_create' failed.");
 -      return (-1);
 +      return -1;
      }
    }
  
        } else {
          char *temp = strdup(option->values[0].value.string);
          if (temp == NULL)
 -          return (1);
 +          return 1;
          sfree(local_sockpath);
          local_sockpath = temp;
        }
      }
    } /* for (i = 0; i < ci->children_num; i++) */
  
 -  return (0);
 +  return 0;
  } /* }}} int powerdns_config */
  
  static int powerdns_read(void) {
      item->func(item);
    }
  
 -  return (0);
 +  return 0;
  } /* static int powerdns_read */
  
  static int powerdns_shutdown(void) {
    if (list == NULL)
 -    return (0);
 +    return 0;
  
    for (llentry_t *e = llist_head(list); e != NULL; e = e->next) {
      list_item_t *item = (list_item_t *)e->value;
    llist_destroy(list);
    list = NULL;
  
 -  return (0);
 +  return 0;
  } /* static int powerdns_shutdown */
  
  void module_register(void) {
    plugin_register_read("powerdns", powerdns_read);
    plugin_register_shutdown("powerdns", powerdns_shutdown);
  } /* void module_register */
 -
 -/* vim: set sw=2 sts=2 ts=8 fdm=marker : */