1 /**
2 * collectd - src/wireless.c
3 * Copyright (C) 2006 Florian octo Forster
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 * Author:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "wireless"
28 #define BUFSIZE 1024
30 #if defined(KERNEL_LINUX)
31 # define WIRELESS_HAVE_READ 1
32 #else
33 # define WIRELESS_HAVE_READ 0
34 #endif
36 #define WIRELESS_PROC_FILE "/proc/net/wireless"
38 static char *filename_template = "wireless-%s.rrd";
40 static char *ds_def[] =
41 {
42 "DS:quality:GAUGE:"COLLECTD_HEARTBEAT":0:U",
43 "DS:power:GAUGE:"COLLECTD_HEARTBEAT":U:0",
44 "DS:noise:GAUGE:"COLLECTD_HEARTBEAT":U:0",
45 NULL
46 };
47 static int ds_num = 3;
49 #if WIRELESS_HAVE_READ
50 static int proc_file_found = 0;
51 #endif
53 static void wireless_init (void)
54 {
55 #if WIRELESS_HAVE_READ
56 if (access (WIRELESS_PROC_FILE, R_OK) == 0)
57 proc_file_found = 1;
58 else
59 proc_file_found = 0;
60 #endif
62 return;
63 }
65 static void wireless_write (char *host, char *inst, char *val)
66 {
67 char file[BUFSIZE];
68 int status;
70 status = snprintf (file, BUFSIZE, filename_template, inst);
71 if (status < 1)
72 return;
73 else if (status >= BUFSIZE)
74 return;
76 rrd_update_file (host, file, val, ds_def, ds_num);
77 }
79 #if WIRELESS_HAVE_READ
80 #if 0
81 static double wireless_dbm_to_watt (double dbm)
82 {
83 double watt;
85 /*
86 * dbm = 10 * log_{10} (1000 * power / W)
87 * power = 10^(dbm/10) * W/1000
88 */
90 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
92 return (watt);
93 }
94 #endif
96 static void wireless_submit (char *device,
97 double quality, double power, double noise)
98 {
99 char buf[BUFSIZE];
100 int status;
102 status = snprintf (buf, BUFSIZE, "%u:%f:%f:%f",
103 (unsigned int) curtime,
104 quality, power, noise);
105 if ((status < 1) || (status >= BUFSIZE))
106 return;
108 plugin_submit (MODULE_NAME, device, buf);
109 }
111 #define POWER_MIN -90.0
112 #define POWER_MAX -50.0
113 static double wireless_percent_to_power (double quality)
114 {
115 assert ((quality >= 0.0) && (quality <= 100.0));
117 return ((quality * (POWER_MAX - POWER_MIN)) + POWER_MIN);
118 } /* double wireless_percent_to_power */
120 static void wireless_read (void)
121 {
122 #ifdef KERNEL_LINUX
124 FILE *fh;
125 char buffer[BUFSIZE];
127 char *device;
128 double quality;
129 double power;
130 double noise;
132 char *fields[8];
133 int numfields;
135 int len;
137 if (!proc_file_found)
138 return;
140 /* there are a variety of names for the wireless device */
141 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
142 {
143 syslog (LOG_WARNING, "wireless: fopen: %s", strerror (errno));
144 return;
145 }
147 while (fgets (buffer, BUFSIZE, fh) != NULL)
148 {
149 char *endptr;
151 numfields = strsplit (buffer, fields, 8);
153 if (numfields < 5)
154 continue;
156 len = strlen (fields[0]) - 1;
157 if (len < 1)
158 continue;
159 if (fields[0][len] != ':')
160 continue;
161 fields[0][len] = '\0';
163 device = fields[0];
165 quality = strtod (fields[2], &endptr);
166 if (fields[2] == endptr)
167 quality = -1.0; /* invalid */
169 /* power [dBm] < 0.0 */
170 power = strtod (fields[3], &endptr);
171 if (fields[3] == endptr)
172 power = 1.0; /* invalid */
173 else if ((power >= 0.0) && (power <= 100.0))
174 power = wireless_percent_to_power (power);
175 else if (power > 100.0)
176 power = 1.0; /* invalid */
178 /* noise [dBm] < 0.0 */
179 noise = strtod (fields[3], &endptr);
180 if (fields[3] == endptr)
181 noise = 1.0; /* invalid */
182 else if ((noise >= 0.0) && (noise <= 100.0))
183 noise = wireless_percent_to_power (noise);
184 else if (noise > 100.0)
185 noise = 1.0; /* invalid */
187 wireless_submit (device, quality, power, noise);
188 }
190 fclose (fh);
191 #endif /* KERNEL_LINUX */
192 }
193 #else
194 # define wireless_read NULL
195 #endif /* WIRELESS_HAVE_READ */
197 void module_register (void)
198 {
199 plugin_register (MODULE_NAME, wireless_init, wireless_read, wireless_write);
200 }
202 #undef BUFSIZE
203 #undef MODULE_NAME