2 * collectd - src/serial.c
3 * Copyright (C) 2005 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>
27 #define MODULE_NAME "serial"
32 static char *serial_filename_template = "serial-%s.rrd";
34 static char *ds_def[] =
36 "DS:incoming:COUNTER:25:0:U",
37 "DS:outgoing:COUNTER:25:0:U",
40 static int ds_num = 2;
42 void serial_init (void)
46 void serial_write (char *host, char *inst, char *val)
51 status = snprintf (file, 512, serial_filename_template, inst);
54 else if (status >= 512)
57 rrd_update_file (host, file, val, ds_def, ds_num);
61 void serial_submit (char *device,
62 unsigned long long incoming,
63 unsigned long long outgoing)
67 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
68 incoming, outgoing) >= BUFSIZE)
71 plugin_submit (MODULE_NAME, device, buf);
75 void serial_read (void)
81 unsigned long long incoming, outgoing;
87 /* there are a variety of names for the serial device */
88 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
89 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
91 syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
95 while (fgets (buffer, 1024, fh) != NULL)
97 int have_rx = 0, have_tx = 0;
99 numfields = strsplit (buffer, fields, 16);
105 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
106 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
108 len = strlen (fields[0]) - 1;
111 if (fields[0][len] != ':')
113 fields[0][len] = '\0';
115 for (i = 1; i < numfields; i++)
117 len = strlen (fields[i]);
121 if (strncmp (fields[i], "tx:", 3) == 0)
123 outgoing = atoll (fields[i] + 3);
126 else if (strncmp (fields[i], "rx:", 3) == 0)
128 incoming = atoll (fields[i] + 3);
133 if ((have_rx == 0) || (have_tx == 0))
136 serial_submit (fields[0], incoming, outgoing);
140 #endif /* KERNEL_LINUX */
143 void module_register (void)
145 plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
149 #endif /* COLLECT_SERIAL */