tape plugin: Converted to the new plugin interface.
authorFlorian Forster <sifnfors@faui02b.informatik.uni-erlangen.de>
Mon, 5 Feb 2007 11:30:10 +0000 (12:30 +0100)
committerFlorian Forster <sifnfors@faui02b.informatik.uni-erlangen.de>
Mon, 5 Feb 2007 11:30:10 +0000 (12:30 +0100)
src/tape.c

index b13cdbd..e0b8b51 100644 (file)
 #include "common.h"
 #include "plugin.h"
 
-#define MODULE_NAME "tape"
-
 #if defined(HAVE_LIBKSTAT)
 # define TAPE_HAVE_READ 1
 #else
 # define TAPE_HAVE_READ 0
 #endif
 
-static char *tape_filename_template = "tape-%s.rrd";
+/* 2^34 = 17179869184 = ~17.2GByte/s */
+static data_source_t octets_dsrc[2] =
+{
+       {"read",  DS_TYPE_COUNTER, 0, 17179869183.0},
+       {"write", DS_TYPE_COUNTER, 0, 17179869183.0}
+};
+
+static data_set_t octets_ds =
+{
+       "tape_octets", 2, octets_dsrc
+};
+
+static data_source_t operations_dsrc[2] =
+{
+       {"read",  DS_TYPE_COUNTER, 0, 4294967295.0},
+       {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
+};
+
+static data_set_t operations_ds =
+{
+       "tape_ops", 2, operations_dsrc
+};
+
+static data_source_t merged_dsrc[2] =
+{
+       {"read",  DS_TYPE_COUNTER, 0, 4294967295.0},
+       {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
+};
+
+static data_set_t merged_ds =
+{
+       "tape_merged", 2, merged_dsrc
+};
+
+/* max is 1000000us per second. */
+static data_source_t time_dsrc[2] =
+{
+       {"read",  DS_TYPE_COUNTER, 0, 1000000.0},
+       {"write", DS_TYPE_COUNTER, 0, 1000000.0}
+};
 
-/* 104857600 == 100 MB */
-static char *tape_ds_def[] =
+static data_set_t time_ds =
 {
-       "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
-       NULL
+       "tape_time", 2, time_dsrc
 };
-static int tape_ds_num = 8;
 
+#if TAPE_HAVE_READ
 #if defined(HAVE_LIBKSTAT)
 #define MAX_NUMTAPE 256
 extern kstat_ctl_t *kc;
@@ -56,7 +84,7 @@ static kstat_t *ksp[MAX_NUMTAPE];
 static int numtape = 0;
 #endif /* HAVE_LIBKSTAT */
 
-static void tape_init (void)
+static int tape_init (void)
 {
 #ifdef HAVE_LIBKSTAT
        kstat_t *ksp_chain;
@@ -64,7 +92,7 @@ static void tape_init (void)
        numtape = 0;
 
        if (kc == NULL)
-               return;
+               return (-1);
 
        for (numtape = 0, ksp_chain = kc->kc_chain;
                        (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
@@ -78,52 +106,31 @@ static void tape_init (void)
        }
 #endif
 
-       return;
+       return (0);
 }
 
-static void tape_write (char *host, char *inst, char *val)
+static void tape_submit (const char *plugin_instance,
+               const char *type,
+               counter_t read, counter_t write)
 {
-       char file[512];
-       int status;
+       value_t values[2];
+       value_list_t vl = VALUE_LIST_INIT;
 
-       status = snprintf (file, 512, tape_filename_template, inst);
-       if (status < 1)
-               return;
-       else if (status >= 512)
-               return;
-
-       rrd_update_file (host, file, val, tape_ds_def, tape_ds_num);
-}
+       values[0].counter = read;
+       values[1].counter = write;
 
+       vl.values = values;
+       vl.values_len = 2;
+       vl.time = time (NULL);
+       strcpy (vl.host, hostname);
+       strcpy (vl.plugin, "tape");
+       strncpy (vl.plugin_instance, plugin_instance,
+                       sizeof (vl.plugin_instance));
 
-#if TAPE_HAVE_READ
-#define BUFSIZE 512
-static void tape_submit (char *tape_name,
-               unsigned long long read_count,
-               unsigned long long read_merged,
-               unsigned long long read_bytes,
-               unsigned long long read_time,
-               unsigned long long write_count,
-               unsigned long long write_merged,
-               unsigned long long write_bytes,
-               unsigned long long write_time)
+       plugin_dispatch_values (type, &vl);
+} /* void tape_submit */
 
-{
-       char buf[BUFSIZE];
-
-       if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
-                               (unsigned int) curtime,
-                               read_count, read_merged, read_bytes, read_time,
-                               write_count, write_merged, write_bytes,
-                               write_time) >= BUFSIZE)
-               return;
-
-       plugin_submit (MODULE_NAME, tape_name, buf);
-}
-
-#undef BUFSIZE
-
-static void tape_read (void)
+static int tape_read (void)
 {
 
 #if defined(HAVE_LIBKSTAT)
@@ -131,7 +138,10 @@ static void tape_read (void)
        int i;
 
        if (kc == NULL)
-               return;
+               return (-1);
+
+       if (numtape <= 0)
+               return (-1);
 
        for (i = 0; i < numtape; i++)
        {
@@ -139,19 +149,28 @@ static void tape_read (void)
                        continue;
 
                if (strncmp (ksp[i]->ks_class, "tape", 4) == 0)
-                       tape_submit (ksp[i]->ks_name,
-                                       kio.reads,  0LL, kio.nread,    kio.rtime,
-                                       kio.writes, 0LL, kio.nwritten, kio.wtime);
+               {
+                       tape_submit (ksp[i]->ks_name, "tape_octets", kio.reads, kio.writes);
+                       tape_submit (ksp[i]->ks_name, "tape_ops", kio.nreads, kio.nwrites);
+                       /* FIXME: Convert this to microseconds if necessary */
+                       tape_submit (ksp[i]->ks_name, "tape_time", kio.rtime, kio.wtime);
+               }
        }
 #endif /* defined(HAVE_LIBKSTAT) */
+
+       return (0);
 }
-#else
-# define tape_read NULL
 #endif /* TAPE_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register (MODULE_NAME, tape_init, tape_read, tape_write);
-}
+       plugin_register_data_set (&octets_ds);
+       plugin_register_data_set (&operations_ds);
+       plugin_register_data_set (&merged_ds);
+       plugin_register_data_set (&time_ds);
 
-#undef MODULE_NAME
+#if TAPE_HAVE_READ
+       plugin_register_init ("tape", tape_init);
+       plugin_register_read ("tape", tape_read);
+#endif /* TAPE_HAVE_READ */
+}