X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmultimeter.c;h=6953750525ce34cd2f4e50de26af4cfa6f6949e3;hb=0ba2f3a1a1db4ed7ce6f990b22b9605e19fc7ab9;hp=09073abab2d7449804e764151d45b028238e671a;hpb=3a4405009e53f578c26c22073920cc5b4ffa6eca;p=collectd.git diff --git a/src/multimeter.c b/src/multimeter.c index 09073aba..69537505 100644 --- a/src/multimeter.c +++ b/src/multimeter.c @@ -20,49 +20,22 @@ * Peter Holik * * Used multimeter: Metex M-4650CR - * **/ #include "collectd.h" #include "common.h" #include "plugin.h" -#include -#include -#include - -#define MODULE_NAME "multimeter" - -static char *multimeter_file = "multimeter.rrd"; - -static char *ds_def[] = -{ - "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U", - NULL -}; -static int ds_num = 1; +#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H +# include +# include +# include +#else +# error "No applicable input method." +#endif static int fd = -1; -static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2, - struct timeval *res) -{ - if ((tv1->tv_sec < tv2->tv_sec) || - ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec))) - return (-1); - - res->tv_sec = tv1->tv_sec - tv2->tv_sec; - res->tv_usec = tv1->tv_usec - tv2->tv_usec; - - assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0))); - - while (res->tv_usec < 0) - { - res->tv_usec += 1000000; - res->tv_sec--; - } - return (0); -} #define LINE_LENGTH 14 static int multimeter_read_value(double *value) { @@ -76,8 +49,10 @@ static int multimeter_read_value(double *value) if (gettimeofday (&time_end, NULL) < 0) { - syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s", - strerror (errno)); + char errbuf[1024]; + ERROR ("multimeter plugin: gettimeofday failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } time_end.tv_sec++; @@ -91,18 +66,26 @@ static int multimeter_read_value(double *value) struct timeval timeout; struct timeval time_now; - write(fd, "D", 1); + status = swrite (fd, "D", 1); + if (status < 0) + { + ERROR ("multimeter plugin: swrite failed."); + return (-1); + } FD_ZERO(&rfds); FD_SET(fd, &rfds); if (gettimeofday (&time_now, NULL) < 0) { - syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s", - strerror (errno)); + char errbuf[1024]; + ERROR ("multimeter plugin: " + "gettimeofday failed: %s", + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } - if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1) + if (timeval_cmp (time_end, time_now, &timeout) < 0) break; status = select(fd+1, &rfds, NULL, NULL, &timeout); @@ -151,34 +134,37 @@ static int multimeter_read_value(double *value) } else /* status == -1 */ { - syslog (LOG_ERR, MODULE_NAME": select failed: %s", - strerror (errno)); + char errbuf[1024]; + ERROR ("multimeter plugin: " + "select failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); break; } } } while (--retry); return (-2); /* no value received */ -} +} /* int multimeter_read_value */ -static void multimeter_init (void) +static int multimeter_init (void) { int i; char device[] = "/dev/ttyS "; for (i = 0; i < 10; i++) { - device[strlen(device)-1] = i + '0'; + device[strlen(device)-1] = i + '0'; - if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0) + if ((fd = open(device, O_RDWR | O_NOCTTY)) != -1) { struct termios tios; int rts = TIOCM_RTS; double value; + memset (&tios, 0, sizeof (tios)); tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL; tios.c_iflag = IGNBRK | IGNPAR; - tios.c_oflag = 0; + tios.c_oflag = 0; tios.c_lflag = 0; tios.c_cc[VTIME] = 3; tios.c_cc[VMIN] = LINE_LENGTH; @@ -186,50 +172,70 @@ static void multimeter_init (void) tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &tios); ioctl(fd, TIOCMBIC, &rts); - - if (multimeter_read_value(&value) < -1) + + if (multimeter_read_value (&value) < -1) { - close(fd); + close (fd); fd = -1; } else { - syslog (LOG_INFO, MODULE_NAME" found (%s)", device); - return; + INFO ("multimeter plugin: Device " + "found at %s", device); + return (0); } } } - syslog (LOG_ERR, MODULE_NAME" not found"); + + ERROR ("multimeter plugin: No device found"); + return (-1); } #undef LINE_LENGTH -static void multimeter_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, multimeter_file, val, ds_def, ds_num); -} -#define BUFSIZE 128 -static void multimeter_submit (double *value) +static void multimeter_submit (double value) { - char buf[BUFSIZE]; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].gauge = value; - if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, *value) >= BUFSIZE) - return; + vl.values = values; + vl.values_len = 1; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin)); + sstrncpy (vl.type, "multimeter", sizeof (vl.type)); - plugin_submit (MODULE_NAME, "-", buf); + plugin_dispatch_values (&vl); } -#undef BUFSIZE -static void multimeter_read (void) +static int multimeter_read (void) { double value; - if (fd > -1 && !(multimeter_read_value(&value))) - multimeter_submit (&value); -} + if (fd < 0) + return (-1); -void module_register (void) + if (multimeter_read_value (&value) != 0) + return (-1); + + multimeter_submit (value); + return (0); +} /* int multimeter_read */ + +static int multimeter_shutdown (void) { - plugin_register (MODULE_NAME, multimeter_init, multimeter_read, multimeter_write); + if (fd >= 0) + { + close (fd); + fd = -1; + } + + return (0); } -#undef MODULE_NAME +void module_register (void) +{ + plugin_register_init ("multimeter", multimeter_init); + plugin_register_read ("multimeter", multimeter_read); + plugin_register_shutdown ("multimeter", multimeter_shutdown); +} /* void module_register */