nfs plugin: Ported to the new plugin structure.
[collectd.git] / src / multimeter.c
1 /**
2  * collectd - src/multimeter.c
3  * Copyright (C) 2005,2006  Peter Holik
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  *
22  * Used multimeter: Metex M-4650CR
23  *
24  **/
25
26 #include <termios.h>
27 #include <sys/ioctl.h>
28 #include <math.h>
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32
33 #define MODULE_NAME "multimeter"
34
35 static char *multimeter_file = "multimeter.rrd";
36
37 static char *ds_def[] =
38 {
39         "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
40         NULL
41 };
42 static int ds_num = 1;
43
44 static int fd = -1;
45
46 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
47                 struct timeval *res)
48 {
49         if ((tv1->tv_sec < tv2->tv_sec) ||
50             ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
51                 return (-1);
52
53         res->tv_sec  = tv1->tv_sec  - tv2->tv_sec;
54         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
55
56         assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
57
58         while (res->tv_usec < 0)
59         {
60                 res->tv_usec += 1000000;
61                 res->tv_sec--;
62         }
63         return (0);
64 }
65 #define LINE_LENGTH 14
66 static int multimeter_read_value(double *value)
67 {
68         int retry = 3; /* sometimes we receive garbadge */
69
70         do
71         {
72                 struct timeval time_end;
73
74                 tcflush(fd, TCIFLUSH);
75
76                 if (gettimeofday (&time_end, NULL) < 0)
77                 {
78                         syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
79                                 strerror (errno));
80                         return (-1);
81                 }
82                 time_end.tv_sec++;      
83
84                 while (1)
85                 {
86                         char buf[LINE_LENGTH];
87                         char *range;
88                         int status;
89                         fd_set rfds;
90                         struct timeval timeout;
91                         struct timeval time_now;
92
93                         write(fd, "D", 1);
94
95                         FD_ZERO(&rfds);
96                         FD_SET(fd, &rfds);
97
98                         if (gettimeofday (&time_now, NULL) < 0)
99                         {
100                                 syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
101                                         strerror (errno));
102                                 return (-1);
103                         }
104                         if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
105                                 break;
106
107                         status = select(fd+1, &rfds, NULL, NULL, &timeout);
108
109                         if (status > 0) /* usually we succeed */
110                         {
111                                 status = read(fd, buf, LINE_LENGTH);
112
113                                 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
114                                         continue;
115
116                                 /* Format: "DC 00.000mV  \r" */
117                                 if (status > 0 && status == LINE_LENGTH)
118                                 {
119                                         *value = strtod(buf + 2, &range);
120
121                                         if ( range > (buf + 6) )
122                                         {
123                                                 range = buf + 9;
124
125                                                 switch ( *range )
126                                                 {
127                                                         case 'p': *value *= 1.0E-12; break;
128                                                         case 'n': *value *= 1.0E-9; break;
129                                                         case 'u': *value *= 1.0E-6; break;
130                                                         case 'm': *value *= 1.0E-3; break;
131                                                         case 'k': *value *= 1.0E3; break;
132                                                         case 'M': *value *= 1.0E6; break;
133                                                         case 'G': *value *= 1.0E9; break;
134                                                 }
135                                         }
136                                         else
137                                                 return (-1); /* Overflow */
138
139                                         return (0); /* value received */
140                                 }
141                                 else break;
142                         }
143                         else if (!status) /* Timeout */
144                         {
145                                 break;
146                         }
147                         else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
148                         {
149                                 continue;
150                         }
151                         else /* status == -1 */
152                         {
153                                 syslog (LOG_ERR, MODULE_NAME": select failed: %s",
154                                         strerror (errno));
155                                 break;
156                         }
157                 }
158         } while (--retry);
159
160         return (-2);  /* no value received */
161 }
162
163 static void multimeter_init (void)
164 {
165         int i;
166         char device[] = "/dev/ttyS ";
167
168         for (i = 0; i < 10; i++)
169         {
170                 device[strlen(device)-1] = i + '0'; 
171
172                 if ((fd = open(device, O_RDWR | O_NOCTTY)) > 0)
173                 {
174                         struct termios tios;
175                         int rts = TIOCM_RTS;
176                         double value;
177
178                         tios.c_cflag = B1200 | CS7 | CSTOPB | CREAD | CLOCAL;
179                         tios.c_iflag = IGNBRK | IGNPAR;
180                         tios.c_oflag = 0;
181                         tios.c_lflag = 0;
182                         tios.c_cc[VTIME] = 3;
183                         tios.c_cc[VMIN]  = LINE_LENGTH;
184
185                         tcflush(fd, TCIFLUSH);
186                         tcsetattr(fd, TCSANOW, &tios);
187                         ioctl(fd, TIOCMBIC, &rts);
188                         
189                         if (multimeter_read_value(&value) < -1)
190                         {
191                                 close(fd);
192                                 fd = -1;
193                         }
194                         else
195                         {
196                                 syslog (LOG_INFO, MODULE_NAME" found (%s)", device);
197                                 return;
198                         }
199                 }
200         }
201         syslog (LOG_ERR, MODULE_NAME" not found");
202 }
203 #undef LINE_LENGTH
204
205 static void multimeter_write (char *host, char *inst, char *val)
206 {
207         rrd_update_file (host, multimeter_file, val, ds_def, ds_num);
208 }
209 #define BUFSIZE 128
210 static void multimeter_submit (double *value)
211 {
212         char buf[BUFSIZE];
213
214         if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, *value) >= BUFSIZE)
215                 return;
216
217         plugin_submit (MODULE_NAME, NULL, buf);
218 }
219 #undef BUFSIZE
220
221 static void multimeter_read (void)
222 {
223         double value;
224
225         if (fd > -1 && !(multimeter_read_value(&value)))
226                 multimeter_submit (&value);
227 }
228
229 void module_register (void)
230 {
231         plugin_register (MODULE_NAME, multimeter_init, multimeter_read, multimeter_write);
232 }
233
234 #undef MODULE_NAME