1 /**
2 * collectd - src/serial.c
3 * Copyright (C) 2005,2006 David Bacher
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 * David Bacher <drbacher at gmail.com>
21 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #define MODULE_NAME "serial"
30 #if defined(KERNEL_LINUX)
31 # define SERIAL_HAVE_READ 1
32 #else
33 # define SERIAL_HAVE_READ 0
34 #endif
36 static char *serial_filename_template = "serial-%s.rrd";
38 static char *ds_def[] =
39 {
40 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
41 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
42 NULL
43 };
44 static int ds_num = 2;
46 static void serial_init (void)
47 {
48 return;
49 }
51 static void serial_write (char *host, char *inst, char *val)
52 {
53 char file[512];
54 int status;
56 status = snprintf (file, 512, serial_filename_template, inst);
57 if (status < 1)
58 return;
59 else if (status >= 512)
60 return;
62 rrd_update_file (host, file, val, ds_def, ds_num);
63 }
65 #if SERIAL_HAVE_READ
66 #define BUFSIZE 512
67 static void serial_submit (char *device,
68 unsigned long long incoming,
69 unsigned long long outgoing)
70 {
71 char buf[BUFSIZE];
73 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
74 incoming, outgoing) >= BUFSIZE)
75 return;
77 plugin_submit (MODULE_NAME, device, buf);
78 }
79 #undef BUFSIZE
81 static void serial_read (void)
82 {
83 #ifdef KERNEL_LINUX
85 FILE *fh;
86 char buffer[1024];
87 unsigned long long incoming, outgoing;
89 char *fields[16];
90 int i, numfields;
91 int len;
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)
96 {
97 syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
98 return;
99 }
101 while (fgets (buffer, 1024, fh) != NULL)
102 {
103 int have_rx = 0, have_tx = 0;
105 /* stupid compiler:
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
108 */
109 incoming = 0ULL;
110 outgoing = 0ULL;
112 numfields = strsplit (buffer, fields, 16);
114 if (numfields < 6)
115 continue;
117 /*
118 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
119 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
120 */
121 len = strlen (fields[0]) - 1;
122 if (len < 1)
123 continue;
124 if (fields[0][len] != ':')
125 continue;
126 fields[0][len] = '\0';
128 for (i = 1; i < numfields; i++)
129 {
130 len = strlen (fields[i]);
131 if (len < 4)
132 continue;
134 if (strncmp (fields[i], "tx:", 3) == 0)
135 {
136 outgoing = atoll (fields[i] + 3);
137 have_tx++;
138 }
139 else if (strncmp (fields[i], "rx:", 3) == 0)
140 {
141 incoming = atoll (fields[i] + 3);
142 have_rx++;
143 }
144 }
146 if ((have_rx == 0) || (have_tx == 0))
147 continue;
149 serial_submit (fields[0], incoming, outgoing);
150 }
152 fclose (fh);
153 #endif /* KERNEL_LINUX */
154 }
155 #else
156 # define serial_read NULL
157 #endif /* SERIAL_HAVE_READ */
159 void module_register (void)
160 {
161 plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
162 }
164 #undef MODULE_NAME