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 #if defined(KERNEL_LINUX)
29 # define SERIAL_HAVE_READ 1
30 #else
31 # define SERIAL_HAVE_READ 0
32 #endif
34 #if SERIAL_HAVE_READ
35 static void serial_submit (const char *type_instance,
36 counter_t rx, counter_t tx)
37 {
38 value_t values[2];
39 value_list_t vl = VALUE_LIST_INIT;
41 values[0].counter = rx;
42 values[1].counter = tx;
44 vl.values = values;
45 vl.values_len = 2;
46 vl.time = time (NULL);
47 strcpy (vl.host, hostname_g);
48 strcpy (vl.plugin, "serial");
49 strncpy (vl.type_instance, type_instance,
50 sizeof (vl.type_instance));
52 plugin_dispatch_values ("serial_octets", &vl);
53 }
55 static int serial_read (void)
56 {
57 #ifdef KERNEL_LINUX
58 FILE *fh;
59 char buffer[1024];
61 counter_t rx = 0;
62 counter_t tx = 0;
64 char *fields[16];
65 int i, numfields;
66 int len;
68 /* there are a variety of names for the serial device */
69 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
70 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
71 {
72 char errbuf[1024];
73 WARNING ("serial: fopen: %s",
74 sstrerror (errno, errbuf, sizeof (errbuf)));
75 return (-1);
76 }
78 while (fgets (buffer, sizeof (buffer), fh) != NULL)
79 {
80 int have_rx = 0, have_tx = 0;
82 numfields = strsplit (buffer, fields, 16);
84 if (numfields < 6)
85 continue;
87 /*
88 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
89 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
90 */
91 len = strlen (fields[0]) - 1;
92 if (len < 1)
93 continue;
94 if (fields[0][len] != ':')
95 continue;
96 fields[0][len] = '\0';
98 for (i = 1; i < numfields; i++)
99 {
100 len = strlen (fields[i]);
101 if (len < 4)
102 continue;
104 if (strncmp (fields[i], "tx:", 3) == 0)
105 {
106 tx = atoll (fields[i] + 3);
107 have_tx++;
108 }
109 else if (strncmp (fields[i], "rx:", 3) == 0)
110 {
111 rx = atoll (fields[i] + 3);
112 have_rx++;
113 }
114 }
116 if ((have_rx == 0) || (have_tx == 0))
117 continue;
119 serial_submit (fields[0], rx, tx);
120 }
122 fclose (fh);
123 return (0);
124 #endif /* KERNEL_LINUX */
125 } /* int serial_read */
126 #endif /* SERIAL_HAVE_READ */
128 void module_register (void)
129 {
130 #if SERIAL_HAVE_READ
131 plugin_register_read ("serial", serial_read);
132 #endif /* SERIAL_HAVE_READ */
133 } /* void module_register */