9bd885dbbf0bec28aeacbe8ae06624a06bf0a98e
[collectd.git] / src / serial.c
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #if !KERNEL_LINUX
29 # error "No applicable input method."
30 #endif
31
32 static void serial_submit (const char *type_instance,
33                 derive_t rx, derive_t tx)
34 {
35         value_t values[2];
36         value_list_t vl = VALUE_LIST_INIT;
37
38         values[0].derive = rx;
39         values[1].derive = tx;
40
41         vl.values = values;
42         vl.values_len = 2;
43         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
44         sstrncpy (vl.plugin, "serial", sizeof (vl.plugin));
45         sstrncpy (vl.type, "serial_octets", sizeof (vl.type));
46         sstrncpy (vl.type_instance, type_instance,
47                         sizeof (vl.type_instance));
48
49         plugin_dispatch_values (&vl);
50 }
51
52 static int serial_read (void)
53 {
54         FILE *fh;
55         char buffer[1024];
56
57         derive_t rx = 0;
58         derive_t tx = 0;
59         
60         char *fields[16];
61         int i, numfields;
62         int len;
63
64         /* there are a variety of names for the serial device */
65         if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
66                 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
67         {
68                 char errbuf[1024];
69                 WARNING ("serial: fopen: %s",
70                                 sstrerror (errno, errbuf, sizeof (errbuf)));
71                 return (-1);
72         }
73
74         while (fgets (buffer, sizeof (buffer), fh) != NULL)
75         {
76                 int have_rx = 0, have_tx = 0;
77
78                 numfields = strsplit (buffer, fields, 16);
79
80                 if (numfields < 6)
81                         continue;
82
83                 /*
84                  * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
85                  * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
86                  */
87                 len = strlen (fields[0]) - 1;
88                 if (len < 1)
89                         continue;
90                 if (fields[0][len] != ':')
91                         continue;
92                 fields[0][len] = '\0';
93
94                 for (i = 1; i < numfields; i++)
95                 {
96                         len = strlen (fields[i]);
97                         if (len < 4)
98                                 continue;
99
100                         if (strncmp (fields[i], "tx:", 3) == 0)
101                         {
102                                 tx = atoll (fields[i] + 3);
103                                 have_tx++;
104                         }
105                         else if (strncmp (fields[i], "rx:", 3) == 0)
106                         {
107                                 rx = atoll (fields[i] + 3);
108                                 have_rx++;
109                         }
110                 }
111
112                 if ((have_rx == 0) || (have_tx == 0))
113                         continue;
114
115                 serial_submit (fields[0], rx, tx);
116         }
117
118         fclose (fh);
119         return (0);
120 } /* int serial_read */
121
122 void module_register (void)
123 {
124         plugin_register_read ("serial", serial_read);
125 } /* void module_register */