X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fserial.c;h=1c874e52177e919d59056126015da12edf760109;hb=8bc093a2476f2bef751c9d3d9e382a7e62d060b3;hp=20a51e70d91b549e3bb6898af689d43c0910dca9;hpb=89ef5df52d495315cd26b8e3467153b93b2c2742;p=collectd.git diff --git a/src/serial.c b/src/serial.c index 20a51e70..1c874e52 100644 --- a/src/serial.c +++ b/src/serial.c @@ -1,6 +1,6 @@ /** * collectd - src/serial.c - * Copyright (C) 2005 David Bacher + * Copyright (C) 2005,2006 David Bacher * * 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 @@ -25,66 +25,38 @@ #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[] = -{ - "DS:incoming:COUNTER:25:0:U", - "DS:outgoing:COUNTER:25:0:U", - NULL -}; -static int ds_num = 2; - -static void serial_init (void) +static void serial_submit (const char *type_instance, + counter_t rx, counter_t tx) { - return; + value_t values[2]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].counter = rx; + values[1].counter = tx; + + vl.values = values; + vl.values_len = 2; + vl.time = time (NULL); + 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_dispatch_values (&vl); } -static void serial_write (char *host, char *inst, char *val) +static int serial_read (void) { - 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); -} - -#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; - - plugin_submit (MODULE_NAME, device, buf); -} -#undef BUFSIZE - -#if SERIAL_HAVE_READ -static void serial_read (void) -{ -#ifdef KERNEL_LINUX - FILE *fh; char buffer[1024]; - unsigned long long incoming, outgoing; + + counter_t rx = 0; + counter_t tx = 0; char *fields[16]; int i, numfields; @@ -94,21 +66,16 @@ static void serial_read (void) 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; - /* 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); if (numfields < 6) @@ -133,12 +100,12 @@ static void serial_read (void) if (strncmp (fields[i], "tx:", 3) == 0) { - outgoing = atoll (fields[i] + 3); + tx = atoll (fields[i] + 3); have_tx++; } else if (strncmp (fields[i], "rx:", 3) == 0) { - incoming = atoll (fields[i] + 3); + rx = atoll (fields[i] + 3); have_rx++; } } @@ -146,23 +113,14 @@ static void serial_read (void) if ((have_rx == 0) || (have_tx == 0)) continue; - serial_submit (fields[0], incoming, outgoing); + serial_submit (fields[0], rx, tx); } fclose (fh); -#endif /* KERNEL_LINUX */ -} -#endif /* SERIAL_HAVE_READ */ + return (0); +} /* int serial_read */ void module_register (void) { - plugin_register (MODULE_NAME, serial_init, -#if SERIAL_HAVE_READ - serial_read, -#else - NULL, -#endif - serial_write); -} - -#undef MODULE_NAME + plugin_register_read ("serial", serial_read); +} /* void module_register */