2 * collectd - src/wireless.c
3 * Copyright (C) 2006 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
27 #define MODULE_NAME "wireless"
30 #if defined(KERNEL_LINUX)
31 # define WIRELESS_HAVE_READ 1
33 # define WIRELESS_HAVE_READ 0
36 #define WIRELESS_PROC_FILE "/proc/net/wireless"
38 static char *filename_template = "wireless-%s.rrd";
40 static char *ds_def[] =
42 "DS:quality:GAUGE:"COLLECTD_HEARTBEAT":0:U",
43 "DS:power:GAUGE:"COLLECTD_HEARTBEAT":U:0",
44 "DS:noise:GAUGE:"COLLECTD_HEARTBEAT":U:0",
47 static int ds_num = 3;
49 #if WIRELESS_HAVE_READ
50 static int proc_file_found = 0;
53 static void wireless_init (void)
55 #if WIRELESS_HAVE_READ
56 if (access (WIRELESS_PROC_FILE, R_OK) == 0)
65 static void wireless_write (char *host, char *inst, char *val)
70 status = snprintf (file, BUFSIZE, filename_template, inst);
73 else if (status >= BUFSIZE)
76 rrd_update_file (host, file, val, ds_def, ds_num);
79 #if WIRELESS_HAVE_READ
81 static double wireless_dbm_to_watt (double dbm)
86 * dbm = 10 * log_{10} (1000 * power / W)
87 * power = 10^(dbm/10) * W/1000
90 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
96 static void wireless_submit (char *device,
97 double quality, double power, double noise)
102 status = snprintf (buf, BUFSIZE, "%u:%f:%f:%f",
103 (unsigned int) curtime,
104 quality, power, noise);
105 if ((status < 1) || (status >= BUFSIZE))
108 plugin_submit (MODULE_NAME, device, buf);
111 static void wireless_read (void)
116 char buffer[BUFSIZE];
128 if (!proc_file_found)
131 /* there are a variety of names for the wireless device */
132 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
134 syslog (LOG_WARNING, "wireless: fopen: %s", strerror (errno));
138 while (fgets (buffer, BUFSIZE, fh) != NULL)
140 numfields = strsplit (buffer, fields, 8);
145 len = strlen (fields[0]) - 1;
148 if (fields[0][len] != ':')
150 fields[0][len] = '\0';
153 quality = atof (fields[2]);
154 power = atof (fields[3]);
155 noise = atof (fields[4]);
157 /* Fill in invalid values when conversion failed.. */
159 quality = -1.0; /* quality >= 0 */
162 power = 1.0; /* power <= 0 */
165 noise = 1.0; /* noise <= 0 */
167 wireless_submit (device, quality, power, noise);
171 #endif /* KERNEL_LINUX */
174 # define wireless_read NULL
175 #endif /* WIRELESS_HAVE_READ */
177 void module_register (void)
179 plugin_register (MODULE_NAME, wireless_init, wireless_read, wireless_write);