1 /**
2 * collectd - src/wireless.c
3 * Copyright (C) 2006,2007 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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if !KERNEL_LINUX
27 # error "No applicable input method."
28 #endif
30 #define WIRELESS_PROC_FILE "/proc/net/wireless"
32 #if 0
33 static double wireless_dbm_to_watt (double dbm)
34 {
35 double watt;
37 /*
38 * dbm = 10 * log_{10} (1000 * power / W)
39 * power = 10^(dbm/10) * W/1000
40 */
42 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
44 return (watt);
45 }
46 #endif
48 static void wireless_submit (const char *plugin_instance, const char *type,
49 double value)
50 {
51 value_t values[1];
52 value_list_t vl = VALUE_LIST_INIT;
54 values[0].gauge = value;
56 vl.values = values;
57 vl.values_len = 1;
58 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
59 sstrncpy (vl.plugin, "wireless", sizeof (vl.plugin));
60 sstrncpy (vl.plugin_instance, plugin_instance,
61 sizeof (vl.plugin_instance));
62 sstrncpy (vl.type, type, sizeof (vl.type));
64 plugin_dispatch_values (&vl);
65 } /* void wireless_submit */
67 #define POWER_MIN -90.0
68 #define POWER_MAX -50.0
69 static double wireless_percent_to_power (double quality)
70 {
71 assert ((quality >= 0.0) && (quality <= 100.0));
73 return ((quality * (POWER_MAX - POWER_MIN)) + POWER_MIN);
74 } /* double wireless_percent_to_power */
76 static int wireless_read (void)
77 {
78 #ifdef KERNEL_LINUX
79 FILE *fh;
80 char buffer[1024];
82 char *device;
83 double quality;
84 double power;
85 double noise;
87 char *fields[8];
88 int numfields;
90 int devices_found;
91 int len;
93 /* there are a variety of names for the wireless device */
94 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
95 {
96 char errbuf[1024];
97 WARNING ("wireless: fopen: %s",
98 sstrerror (errno, errbuf, sizeof (errbuf)));
99 return (-1);
100 }
102 devices_found = 0;
103 while (fgets (buffer, sizeof (buffer), fh) != NULL)
104 {
105 char *endptr;
107 numfields = strsplit (buffer, fields, 8);
109 if (numfields < 5)
110 continue;
112 len = strlen (fields[0]) - 1;
113 if (len < 1)
114 continue;
115 if (fields[0][len] != ':')
116 continue;
117 fields[0][len] = '\0';
119 device = fields[0];
121 quality = strtod (fields[2], &endptr);
122 if (fields[2] == endptr)
123 quality = -1.0; /* invalid */
125 /* power [dBm] < 0.0 */
126 power = strtod (fields[3], &endptr);
127 if (fields[3] == endptr)
128 power = 1.0; /* invalid */
129 else if ((power >= 0.0) && (power <= 100.0))
130 power = wireless_percent_to_power (power);
131 else if ((power > 100.0) && (power <= 256.0))
132 power = power - 256.0;
133 else if (power > 0.0)
134 power = 1.0; /* invalid */
136 /* noise [dBm] < 0.0 */
137 noise = strtod (fields[4], &endptr);
138 if (fields[4] == endptr)
139 noise = 1.0; /* invalid */
140 else if ((noise >= 0.0) && (noise <= 100.0))
141 noise = wireless_percent_to_power (noise);
142 else if ((noise > 100.0) && (noise <= 256.0))
143 noise = noise - 256.0;
144 else if (noise > 0.0)
145 noise = 1.0; /* invalid */
147 wireless_submit (device, "signal_quality", quality);
148 wireless_submit (device, "signal_power", power);
149 wireless_submit (device, "signal_noise", noise);
151 devices_found++;
152 }
154 fclose (fh);
156 /* If no wireless devices are present return an error, so the plugin
157 * code delays our read function. */
158 if (devices_found == 0)
159 return (-1);
160 #endif /* KERNEL_LINUX */
162 return (0);
163 } /* int wireless_read */
165 void module_register (void)
166 {
167 plugin_register_read ("wireless", wireless_read);
168 } /* void module_register */