Merge pull request #3001 from usev6/write_graphite_reverse_host
authorPavel Rochnyak <pavel2000@ngs.ru>
Tue, 30 Apr 2019 07:54:53 +0000 (14:54 +0700)
committerGitHub <noreply@github.com>
Tue, 30 Apr 2019 07:54:53 +0000 (14:54 +0700)
write_graphite plugin: Add option to reverse host

configure.ac
src/collectd.conf.pod
src/cpufreq.c

index 5df84b6..f6f9acb 100644 (file)
@@ -6518,6 +6518,7 @@ fi
 # FreeBSD
 
 if test "x$ac_system" = "xFreeBSD"; then
+  plugin_cpufreq="yes"
   plugin_disk="yes"
   plugin_zfs_arc="yes"
 fi
index 353a506..ce7f725 100644 (file)
@@ -1656,15 +1656,24 @@ Defaults to B<true>.
 
 =head2 Plugin C<cpufreq>
 
-This plugin doesn't have any options. It reads
+This plugin is available on Linux and FreeBSD only.  It doesn't have any
+options.  On Linux it reads
 F</sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq> (for the first CPU
 installed) to get the current CPU frequency. If this file does not exist make
 sure B<cpufreqd> (L<http://cpufreqd.sourceforge.net/>) or a similar tool is
 installed and an "cpu governor" (that's a kernel module) is loaded.
 
-If the system has the I<cpufreq-stats> kernel module loaded, this plugin reports
-the rate of p-state (cpu frequency) transitions and the percentage of time spent
-in each p-state.
+On Linux, if the system has the I<cpufreq-stats> kernel module loaded, this
+plugin reports the rate of p-state (cpu frequency) transitions and the
+percentage of time spent in each p-state.
+
+On FreeBSD it does a sysctl dev.cpu.0.freq and submits this as instance 0.
+At this time FreeBSD only has one frequency setting for all cores.
+See the BUGS section in the FreeBSD man page for cpufreq(4) for more details.
+
+On FreeBSD the plugin checks the success of sysctl dev.cpu.0.freq and
+unregisters the plugin when this fails.  A message will be logged to indicate
+this.
 
 =head2 Plugin C<cpusleep>
 
index 35ec07f..f95b282 100644 (file)
 #include "plugin.h"
 #include "utils/common/common.h"
 
+#if KERNEL_FREEBSD
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#endif
+
+#if KERNEL_LINUX
 #define MAX_AVAIL_FREQS 20
 
 static int num_cpu;
@@ -71,8 +77,10 @@ static void cpufreq_stats_init(void) {
   }
   return;
 }
+#endif /* KERNEL_LINUX */
 
 static int cpufreq_init(void) {
+#if KERNEL_LINUX
   char filename[PATH_MAX];
 
   num_cpu = 0;
@@ -96,6 +104,16 @@ static int cpufreq_init(void) {
 
   if (num_cpu == 0)
     plugin_unregister_read("cpufreq");
+#elif KERNEL_FREEBSD
+  char mib[] = "dev.cpu.0.freq";
+  int cpufreq;
+  size_t cf_len = sizeof(cpufreq);
+
+  if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
+    WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
+    plugin_unregister_read("cpufreq");
+  }
+#endif
 
   return 0;
 } /* int cpufreq_init */
@@ -116,6 +134,7 @@ static void cpufreq_submit(int cpu_num, const char *type,
   plugin_dispatch_values(&vl);
 }
 
+#if KERNEL_LINUX
 static void cpufreq_read_stats(int cpu) {
   char filename[PATH_MAX];
   /* Read total transitions for cpu frequency */
@@ -184,8 +203,10 @@ static void cpufreq_read_stats(int cpu) {
   }
   fclose(fh);
 }
+#endif /* KERNEL_LINUX */
 
 static int cpufreq_read(void) {
+#if KERNEL_LINUX
   for (int cpu = 0; cpu < num_cpu; cpu++) {
     char filename[PATH_MAX];
     /* Read cpu frequency */
@@ -206,6 +227,23 @@ static int cpufreq_read(void) {
     if (report_p_stats)
       cpufreq_read_stats(cpu);
   }
+#elif KERNEL_FREEBSD
+  /* FreeBSD currently only has 1 freq setting.  See BUGS in cpufreq(4) */
+  char mib[] = "dev.cpu.0.freq";
+  int cpufreq;
+  size_t cf_len = sizeof(cpufreq);
+
+  if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) {
+    WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib);
+    return 0;
+  }
+
+  value_t v;
+  /* convert Mhz to Hz */
+  v.gauge = cpufreq * 1000000.0;
+
+  cpufreq_submit(0, "cpufreq", NULL, &v);
+#endif
   return 0;
 } /* int cpufreq_read */