diff --git a/src/serial.c b/src/serial.c
index 0c6050d72c9a9e7533782ba2dd458627268532e0..0878d3c93b1dac2b4d4bc331e4d13e56a358ede9 100644 (file)
--- a/src/serial.c
+++ b/src/serial.c
*
* Authors:
* David Bacher <drbacher at gmail.com>
- * Florian octo Forster <octo at verplant.org>
+ * Florian octo Forster <octo at collectd.org>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
-#define MODULE_NAME "serial"
-
-#if defined(KERNEL_LINUX)
-# define SERIAL_HAVE_READ 1
-#else
-# define SERIAL_HAVE_READ 0
+#if !KERNEL_LINUX
+# error "No applicable input method."
#endif
-static char *serial_filename_template = "serial-%s.rrd";
-
-static char *ds_def[] =
+static void serial_submit (const char *type_instance,
+ derive_t rx, derive_t tx)
{
- "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
- "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
- NULL
-};
-static int ds_num = 2;
-
-static void serial_init (void)
-{
- return;
-}
+ value_t values[2];
+ value_list_t vl = VALUE_LIST_INIT;
-static void serial_write (char *host, char *inst, char *val)
-{
- char file[512];
- int status;
-
- status = snprintf (file, 512, serial_filename_template, inst);
- if (status < 1)
- return;
- else if (status >= 512)
- return;
-
- rrd_update_file (host, file, val, ds_def, ds_num);
-}
+ values[0].derive = rx;
+ values[1].derive = tx;
-#if SERIAL_HAVE_READ
-#define BUFSIZE 512
-static void serial_submit (char *device,
- unsigned long long incoming,
- unsigned long long outgoing)
-{
- char buf[BUFSIZE];
-
- if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
- incoming, outgoing) >= BUFSIZE)
- return;
+ vl.values = values;
+ vl.values_len = 2;
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "serial", sizeof (vl.plugin));
+ sstrncpy (vl.type, "serial_octets", sizeof (vl.type));
+ sstrncpy (vl.type_instance, type_instance,
+ sizeof (vl.type_instance));
- plugin_submit (MODULE_NAME, device, buf);
+ plugin_dispatch_values (&vl);
}
-#undef BUFSIZE
-static void serial_read (void)
+static int serial_read (void)
{
-#ifdef KERNEL_LINUX
-
FILE *fh;
char buffer[1024];
- unsigned long long incoming, outgoing;
-
- char *fields[16];
- int i, numfields;
- int len;
/* there are a variety of names for the serial device */
if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
(fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
{
- syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
- return;
+ char errbuf[1024];
+ WARNING ("serial: fopen: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
}
- while (fgets (buffer, 1024, fh) != NULL)
+ while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
- int have_rx = 0, have_tx = 0;
+ derive_t rx = 0;
+ derive_t tx = 0;
+ _Bool have_rx = 0, have_tx = 0;
+ size_t len;
- /* stupid compiler:
- * serial.c:87: warning: 'incoming' may be used uninitialized in this function
- * serial.c:87: warning: 'outgoing' may be used uninitialized in this function
- */
- incoming = 0ULL;
- outgoing = 0ULL;
-
- numfields = strsplit (buffer, fields, 16);
+ char *fields[16];
+ int numfields;
+ int i;
+ numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
if (numfields < 6)
continue;
* 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
* 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
*/
- len = strlen (fields[0]) - 1;
- if (len < 1)
+ len = strlen (fields[0]);
+ if (len < 2)
continue;
- if (fields[0][len] != ':')
+ if (fields[0][len - 1] != ':')
continue;
- fields[0][len] = '\0';
+ fields[0][len - 1] = 0;
for (i = 1; i < numfields; i++)
{
if (strncmp (fields[i], "tx:", 3) == 0)
{
- outgoing = atoll (fields[i] + 3);
- have_tx++;
+ if (strtoderive (fields[i] + 3, &tx) == 0)
+ have_tx = 1;
}
else if (strncmp (fields[i], "rx:", 3) == 0)
{
- incoming = atoll (fields[i] + 3);
- have_rx++;
+ if (strtoderive (fields[i] + 3, &rx) == 0)
+ have_rx = 1;
}
}
- if ((have_rx == 0) || (have_tx == 0))
- continue;
-
- serial_submit (fields[0], incoming, outgoing);
+ if (have_rx && have_tx)
+ serial_submit (fields[0], rx, tx);
}
fclose (fh);
-#endif /* KERNEL_LINUX */
-}
-#else
-# define serial_read NULL
-#endif /* SERIAL_HAVE_READ */
+ return (0);
+} /* int serial_read */
void module_register (void)
{
- plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
-}
-
-#undef MODULE_NAME
+ plugin_register_read ("serial", serial_read);
+} /* void module_register */