2 * collectd - src/serial.c
3 * Copyright (C) 2005,2006 David Bacher
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 * David Bacher <drbacher at gmail.com>
21 * Florian octo Forster <octo at verplant.org>
28 #define MODULE_NAME "serial"
30 #if defined(KERNEL_LINUX)
31 # define SERIAL_HAVE_READ 1
33 # define SERIAL_HAVE_READ 0
36 static char *serial_filename_template = "serial-%s.rrd";
38 static char *ds_def[] =
40 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
41 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
44 static int ds_num = 2;
46 static void serial_init (void)
51 static void serial_write (char *host, char *inst, char *val)
56 status = snprintf (file, 512, serial_filename_template, inst);
59 else if (status >= 512)
62 rrd_update_file (host, file, val, ds_def, ds_num);
67 static void serial_submit (char *device,
68 unsigned long long incoming,
69 unsigned long long outgoing)
73 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
74 incoming, outgoing) >= BUFSIZE)
77 plugin_submit (MODULE_NAME, device, buf);
81 static void serial_read (void)
87 unsigned long long incoming, outgoing;
93 /* there are a variety of names for the serial device */
94 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
95 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
97 syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
101 while (fgets (buffer, 1024, fh) != NULL)
103 int have_rx = 0, have_tx = 0;
106 * serial.c:87: warning: 'incoming' may be used uninitialized in this function
107 * serial.c:87: warning: 'outgoing' may be used uninitialized in this function
112 numfields = strsplit (buffer, fields, 16);
118 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
119 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
121 len = strlen (fields[0]) - 1;
124 if (fields[0][len] != ':')
126 fields[0][len] = '\0';
128 for (i = 1; i < numfields; i++)
130 len = strlen (fields[i]);
134 if (strncmp (fields[i], "tx:", 3) == 0)
136 outgoing = atoll (fields[i] + 3);
139 else if (strncmp (fields[i], "rx:", 3) == 0)
141 incoming = atoll (fields[i] + 3);
146 if ((have_rx == 0) || (have_tx == 0))
149 serial_submit (fields[0], incoming, outgoing);
153 #endif /* KERNEL_LINUX */
156 # define serial_read NULL
157 #endif /* SERIAL_HAVE_READ */
159 void module_register (void)
161 plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);