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 **/
24 #include "serial.h"
26 #if COLLECT_SERIAL
27 #define MODULE_NAME "serial"
29 #include "plugin.h"
30 #include "common.h"
32 static char *serial_filename_template = "serial-%s.rrd";
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;
42 void serial_init (void)
43 {
44 }
46 void serial_write (char *host, char *inst, char *val)
47 {
48 char file[512];
49 int status;
51 status = snprintf (file, 512, serial_filename_template, inst);
52 if (status < 1)
53 return;
54 else if (status >= 512)
55 return;
57 rrd_update_file (host, file, val, ds_def, ds_num);
58 }
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 time_t curtime = time(NULL);
68 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
69 incoming, outgoing) >= BUFSIZE)
70 return;
72 plugin_submit (MODULE_NAME, device, buf);
73 }
74 #undef BUFSIZE
76 void serial_read (void)
77 {
78 #ifdef KERNEL_LINUX
80 FILE *fh;
81 char buffer[1024];
82 unsigned long long incoming, outgoing;
84 char *fields[16];
85 int i, numfields;
86 int len;
88 /* there are a variety of names for the serial device */
89 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
90 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
91 {
92 syslog (LOG_WARNING, "serial: fopen: %s", strerror (errno));
93 return;
94 }
96 while (fgets (buffer, 1024, fh) != NULL)
97 {
98 int have_rx = 0, have_tx = 0;
100 numfields = strsplit (buffer, fields, 16);
102 if (numfields < 6)
103 continue;
105 /*
106 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
107 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
108 */
109 len = strlen (fields[0]) - 1;
110 if (len < 1)
111 continue;
112 if (fields[0][len] != ':')
113 continue;
114 fields[0][len] = '\0';
116 for (i = 1; i < numfields; i++)
117 {
118 len = strlen (fields[i]);
119 if (len < 4)
120 continue;
122 if (strncmp (fields[i], "tx:", 3) == 0)
123 {
124 outgoing = atoll (fields[i] + 3);
125 have_tx++;
126 }
127 else if (strncmp (fields[i], "rx:", 3) == 0)
128 {
129 incoming = atoll (fields[i] + 3);
130 have_rx++;
131 }
132 }
134 if ((have_rx == 0) || (have_tx == 0))
135 continue;
137 serial_submit (fields[0], incoming, outgoing);
138 }
140 fclose (fh);
141 #endif /* KERNEL_LINUX */
142 }
144 void module_register (void)
145 {
146 plugin_register (MODULE_NAME, serial_init, serial_read, serial_write);
147 }
149 #undef MODULE_NAME
150 #endif /* COLLECT_SERIAL */