cpufreq plugin: Converted to the new plugin interface.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Wed, 31 Jan 2007 08:27:25 +0000 (09:27 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Wed, 31 Jan 2007 08:27:25 +0000 (09:27 +0100)
src/cpufreq.c

index ba0149a..e53e495 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/cpufreq.c
- * Copyright (C) 2005,2006  Peter Holik
+ * Copyright (C) 2005-2007  Peter Holik
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
 # define CPUFREQ_HAVE_READ 0
 #endif
 
-static char *cpufreq_file = "cpufreq-%s.rrd";
+static data_source_t data_source[1] =
+{
+       {"value", DS_TYPE_GAUGE, 0, NAN}
+};
 
-static char *ds_def[] =
+static data_set_t data_set =
 {
-       "DS:value:GAUGE:"COLLECTD_HEARTBEAT":0:U",
-       NULL
+       "cpufreq", 1, data_source
 };
-static int ds_num = 1;
 
+#if CPUFREQ_HAVE_READ
 #ifdef KERNEL_LINUX
 static int num_cpu = 0;
 #endif
 
-#define BUFSIZE 256
-
-static void cpufreq_init (void)
+static int cpufreq_init (void)
 {
 #ifdef KERNEL_LINUX
         int status;
-       char filename[BUFSIZE];
+       char filename[256];
 
        num_cpu = 0;
 
        while (1)
        {
-               status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", num_cpu);
-               if (status < 1 || status >= BUFSIZE)
+               status = snprintf (filename, sizeof (filename),
+                               "/sys/devices/system/cpu/cpu%d/cpufreq/"
+                               "scaling_cur_freq", num_cpu);
+               if (status < 1 || status >= sizeof (filename))
                        break;
 
-               if (access(filename, R_OK))
+               if (access (filename, R_OK))
                        break;
 
                num_cpu++;
        }
 
-       syslog (LOG_INFO, MODULE_NAME" found %d cpu(s)", num_cpu);
+       syslog (LOG_INFO, "cpufreq plugin: Found %d CPU%s", num_cpu,
+                       (num_cpu == 1) ? "" : "s");
+
+       if (num_cpu == 0)
+               plugin_unregister_read ("cpufreq");
 #endif /* defined(KERNEL_LINUX) */
 
-       return;
-}
+       return (0);
+} /* int cpufreq_init */
 
-static void cpufreq_write (char *host, char *inst, char *val)
+static void cpufreq_submit (int cpu_num, double value)
 {
-        int status;
-        char file[BUFSIZE];
-
-        status = snprintf (file, BUFSIZE, cpufreq_file, inst);
-        if (status < 1 || status >= BUFSIZE)
-                return;
-
-       rrd_update_file (host, file, val, ds_def, ds_num);
-}
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
 
-#if CPUFREQ_HAVE_READ
-static void cpufreq_submit (int cpu_num, unsigned long long val)
-{
-       char buf[BUFSIZE];
-       char cpu[16];
+       values[0].gauge = value;
 
-       if (snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, val) >= BUFSIZE)
-               return;
-        snprintf (cpu, 16, "%i", cpu_num);
+       vl.values = values;
+       vl.values_len = 1;
+       vl.time = time (NULL);
+       strcpy (vl.host, hostname);
+       strcpy (vl.plugin, "cpufreq");
+       snprintf (vl.type_instance, sizeof (vl.type_instance),
+                       "%i", cpu_num);
 
-       plugin_submit (MODULE_NAME, cpu, buf);
+       plugin_dispatch_values ("cpufreq", &vl);
 }
 
-static void cpufreq_read (void)
+static int cpufreq_read (void)
 {
 #ifdef KERNEL_LINUX
         int status;
        unsigned long long val;
        int i = 0;
        FILE *fp;
-       char filename[BUFSIZE];
+       char filename[256];
        char buffer[16];
 
        for (i = 0; i < num_cpu; i++)
        {
-               status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
-               if (status < 1 || status >= BUFSIZE)
-                       return;
+               status = snprintf (filename, sizeof (filename),
+                               "/sys/devices/system/cpu/cpu%d/cpufreq/"
+                               "scaling_cur_freq", i);
+               if (status < 1 || status >= sizeof (filename))
+                       return (-1);
 
                if ((fp = fopen (filename, "r")) == NULL)
                {
                        syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
-                       return;
+                       return (-1);
                }
 
                if (fgets (buffer, 16, fp) == NULL)
                {
                        syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
                        fclose (fp);
-                       return;
+                       return (-1);
                }
 
                if (fclose (fp))
@@ -137,16 +138,17 @@ static void cpufreq_read (void)
        }
 #endif /* defined(KERNEL_LINUX) */
 
-       return;
-}
-#else
-#define cpufreq_read NULL
-#endif
+       return (0);
+} /* int cpufreq_read */
+#endif /* CPUFREQ_HAVE_READ */
 #undef BUFSIZE
 
 void module_register (void)
 {
-       plugin_register (MODULE_NAME, cpufreq_init, cpufreq_read, cpufreq_write);
-}
+       plugin_register_data_set (&data_set);
 
-#undef MODULE_NAME
+#if CPUFREQ_HAVE_READ
+       plugin_register_init ("cpufreq", cpufreq_init);
+       plugin_register_read ("cpufreq", cpufreq_read);
+#endif /* CPUFREQ_HAVE_READ */
+}