2 * collectd - src/multimeter.c
3 * Copyright (C) 2005,2006 Peter Holik
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Peter Holik <peter at holik.at>
22 * Used multimeter: Metex M-4650CR
29 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
31 # include <sys/ioctl.h>
34 # error "No applicable input method."
39 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
42 if ((tv1->tv_sec < tv2->tv_sec) ||
43 ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
46 res->tv_sec = tv1->tv_sec - tv2->tv_sec;
47 res->tv_usec = tv1->tv_usec - tv2->tv_usec;
49 assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
51 while (res->tv_usec < 0)
53 res->tv_usec += 1000000;
59 #define LINE_LENGTH 14
60 static int multimeter_read_value(double *value)
62 int retry = 3; /* sometimes we receive garbadge */
66 struct timeval time_end;
68 tcflush(fd, TCIFLUSH);
70 if (gettimeofday (&time_end, NULL) < 0)
73 ERROR ("multimeter plugin: gettimeofday failed: %s",
74 sstrerror (errno, errbuf,
82 char buf[LINE_LENGTH];
86 struct timeval timeout;
87 struct timeval time_now;
94 if (gettimeofday (&time_now, NULL) < 0)
97 ERROR ("multimeter plugin: "
98 "gettimeofday failed: %s",
99 sstrerror (errno, errbuf,
103 if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
106 status = select(fd+1, &rfds, NULL, NULL, &timeout);
108 if (status > 0) /* usually we succeed */
110 status = read(fd, buf, LINE_LENGTH);
112 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
115 /* Format: "DC 00.000mV \r" */
116 if (status > 0 && status == LINE_LENGTH)
118 *value = strtod(buf + 2, &range);
120 if ( range > (buf + 6) )
126 case 'p': *value *= 1.0E-12; break;
127 case 'n': *value *= 1.0E-9; break;
128 case 'u': *value *= 1.0E-6; break;
129 case 'm': *value *= 1.0E-3; break;
130 case 'k': *value *= 1.0E3; break;
131 case 'M': *value *= 1.0E6; break;
132 case 'G': *value *= 1.0E9; break;
136 return (-1); /* Overflow */
138 return (0); /* value received */
142 else if (!status) /* Timeout */
146 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
150 else /* status == -1 */
153 ERROR ("multimeter plugin: "
155 sstrerror (errno, errbuf, sizeof (errbuf)));
161 return (-2); /* no value received */
162 } /* int multimeter_read_value */
164 static int multimeter_init (void)
167 char device[] = "/dev/ttyS ";
169 for (i = 0; i < 10; i++)
171 device[strlen(device)-1] = i + '0';
173 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
179 tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
180 tios.c_iflag = IGNBRK | IGNPAR;
183 tios.c_cc[VTIME] = 3;
184 tios.c_cc[VMIN] = LINE_LENGTH;
186 tcflush(fd, TCIFLUSH);
187 tcsetattr(fd, TCSANOW, &tios);
188 ioctl(fd, TIOCMBIC, &rts);
190 if (multimeter_read_value (&value) < -1)
197 INFO ("multimeter plugin: Device "
198 "found at %s", device);
204 ERROR ("multimeter plugin: No device found");
209 static void multimeter_submit (double value)
212 value_list_t vl = VALUE_LIST_INIT;
214 values[0].gauge = value;
218 vl.time = time (NULL);
219 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
220 sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin));
222 plugin_dispatch_values ("multimeter", &vl);
225 static int multimeter_read (void)
232 if (multimeter_read_value (&value) != 0)
235 multimeter_submit (value);
237 } /* int multimeter_read */
239 static int multimeter_shutdown (void)
250 void module_register (void)
252 plugin_register_init ("multimeter", multimeter_init);
253 plugin_register_read ("multimeter", multimeter_read);
254 plugin_register_shutdown ("multimeter", multimeter_shutdown);
255 } /* void module_register */