9fb26df1ea2aee15febba70d6b2be08fe762b97f
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 static data_source_t octets_dsrc[2] =
35 {
36 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
37 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
38 };
40 static data_set_t octets_ds =
41 {
42 "serial_octets", 2, octets_dsrc
43 };
45 #if SERIAL_HAVE_READ
46 static void serial_submit (const char *type_instance,
47 counter_t rx, counter_t tx)
48 {
49 value_t values[2];
50 value_list_t vl = VALUE_LIST_INIT;
52 values[0].counter = rx;
53 values[1].counter = tx;
55 vl.values = values;
56 vl.values_len = 2;
57 vl.time = time (NULL);
58 strcpy (vl.host, hostname_g);
59 strcpy (vl.plugin, "serial");
60 strncpy (vl.type_instance, type_instance,
61 sizeof (vl.type_instance));
63 plugin_dispatch_values ("serial_octets", &vl);
64 }
66 static int serial_read (void)
67 {
68 #ifdef KERNEL_LINUX
69 FILE *fh;
70 char buffer[1024];
72 counter_t rx = 0;
73 counter_t tx = 0;
75 char *fields[16];
76 int i, numfields;
77 int len;
79 /* there are a variety of names for the serial device */
80 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
81 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
82 {
83 char errbuf[1024];
84 WARNING ("serial: fopen: %s",
85 sstrerror (errno, errbuf, sizeof (errbuf)));
86 return (-1);
87 }
89 while (fgets (buffer, sizeof (buffer), fh) != NULL)
90 {
91 int have_rx = 0, have_tx = 0;
93 numfields = strsplit (buffer, fields, 16);
95 if (numfields < 6)
96 continue;
98 /*
99 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
100 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
101 */
102 len = strlen (fields[0]) - 1;
103 if (len < 1)
104 continue;
105 if (fields[0][len] != ':')
106 continue;
107 fields[0][len] = '\0';
109 for (i = 1; i < numfields; i++)
110 {
111 len = strlen (fields[i]);
112 if (len < 4)
113 continue;
115 if (strncmp (fields[i], "tx:", 3) == 0)
116 {
117 tx = atoll (fields[i] + 3);
118 have_tx++;
119 }
120 else if (strncmp (fields[i], "rx:", 3) == 0)
121 {
122 rx = atoll (fields[i] + 3);
123 have_rx++;
124 }
125 }
127 if ((have_rx == 0) || (have_tx == 0))
128 continue;
130 serial_submit (fields[0], rx, tx);
131 }
133 fclose (fh);
134 return (0);
135 #endif /* KERNEL_LINUX */
136 } /* int serial_read */
137 #endif /* SERIAL_HAVE_READ */
139 void module_register (void)
140 {
141 plugin_register_data_set (&octets_ds);
143 #if SERIAL_HAVE_READ
144 plugin_register_read ("serial", serial_read);
145 #endif /* SERIAL_HAVE_READ */
146 }