78210300fd0a234ded51d35d53bef21aaf5772a4
[collectd.git] / src / serial.c
1 /**
2  * collectd - src/serial.c
3  * Copyright (C) 2005  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 "serial.h"
25
26 #if COLLECT_SERIAL
27 #define MODULE_NAME "serial"
28
29 #include "plugin.h"
30 #include "common.h"
31
32 static char *serial_filename_template = "serial-%s.rrd";
33
34 static char *ds_def[] =
35 {
36         "DS:incoming:COUNTER:25:0:U",
37         "DS:outgoing:COUNTER:25:0:U",
38         NULL
39 };
40 static int ds_num = 2;
41
42 void serial_init (void)
43 {
44 }
45
46 void serial_write (char *host, char *inst, char *val)
47 {
48         char file[512];
49         int status;
50
51         status = snprintf (file, 512, serial_filename_template, inst);
52         if (status < 1)
53                 return;
54         else if (status >= 512)
55                 return;
56
57         rrd_update_file (host, file, val, ds_def, ds_num);
58 }
59
60 #define BUFSIZE 512
61 void serial_submit (char *device,
62                 unsigned long long incoming,
63                 unsigned long long outgoing)
64 {
65         char buf[BUFSIZE];
66         
67         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
68                                 incoming, outgoing) >= BUFSIZE)
69                 return;
70
71         plugin_submit (MODULE_NAME, device, buf);
72 }
73 #undef BUFSIZE
74
75 void serial_read (void)
76 {
77 #ifdef KERNEL_LINUX
78
79         FILE *fh;
80         char buffer[1024];
81         unsigned long long incoming, outgoing;
82         
83         char *fields[16];
84         int i, numfields;
85         int len;
86
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)
90         {
91                 syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
92                 return;
93         }
94
95         while (fgets (buffer, 1024, fh) != NULL)
96         {
97                 int have_rx = 0, have_tx = 0;
98
99                 numfields = strsplit (buffer, fields, 16);
100
101                 if (numfields < 6)
102                         continue;
103
104                 /*
105                  * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
106                  * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
107                  */
108                 len = strlen (fields[0]) - 1;
109                 if (len < 1)
110                         continue;
111                 if (fields[0][len] != ':')
112                         continue;
113                 fields[0][len] = '\0';
114
115                 for (i = 1; i < numfields; i++)
116                 {
117                         len = strlen (fields[i]);
118                         if (len < 4)
119                                 continue;
120
121                         if (strncmp (fields[i], "tx:", 3) == 0)
122                         {
123                                 outgoing = atoll (fields[i] + 3);
124                                 have_tx++;
125                         }
126                         else if (strncmp (fields[i], "rx:", 3) == 0)
127                         {
128                                 incoming = atoll (fields[i] + 3);
129                                 have_rx++;
130                         }
131                 }
132
133                 if ((have_rx == 0) || (have_tx == 0))
134                         continue;
135
136                 serial_submit (fields[0], incoming, outgoing);
137         }
138
139         fclose (fh);
140 #endif /* KERNEL_LINUX */
141 }
142
143 void module_register (void)
144 {
145    plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
146 }
147
148 #undef MODULE_NAME
149 #endif /* COLLECT_SERIAL */