2 * collectd - src/wireless.c
3 * Copyright (C) 2006,2007 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; only version 2 of the License is applicable.
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.
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
19 * Florian octo Forster <octo at verplant.org>
27 # error "No applicable input method."
30 #define WIRELESS_PROC_FILE "/proc/net/wireless"
33 static double wireless_dbm_to_watt (double dbm)
38 * dbm = 10 * log_{10} (1000 * power / W)
39 * power = 10^(dbm/10) * W/1000
42 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
48 static void wireless_submit (const char *plugin_instance, const char *type,
52 value_list_t vl = VALUE_LIST_INIT;
54 values[0].gauge = value;
58 vl.time = time (NULL);
59 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
60 sstrncpy (vl.plugin, "wireless", sizeof (vl.plugin));
61 sstrncpy (vl.plugin_instance, plugin_instance,
62 sizeof (vl.plugin_instance));
63 sstrncpy (vl.type, type, sizeof (vl.type));
65 plugin_dispatch_values (&vl);
66 } /* void wireless_submit */
68 #define POWER_MIN -90.0
69 #define POWER_MAX -50.0
70 static double wireless_percent_to_power (double quality)
72 assert ((quality >= 0.0) && (quality <= 100.0));
74 return ((quality * (POWER_MAX - POWER_MIN)) + POWER_MIN);
75 } /* double wireless_percent_to_power */
77 static int wireless_read (void)
94 /* there are a variety of names for the wireless device */
95 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
98 WARNING ("wireless: fopen: %s",
99 sstrerror (errno, errbuf, sizeof (errbuf)));
104 while (fgets (buffer, sizeof (buffer), fh) != NULL)
108 numfields = strsplit (buffer, fields, 8);
113 len = strlen (fields[0]) - 1;
116 if (fields[0][len] != ':')
118 fields[0][len] = '\0';
122 quality = strtod (fields[2], &endptr);
123 if (fields[2] == endptr)
124 quality = -1.0; /* invalid */
126 /* power [dBm] < 0.0 */
127 power = strtod (fields[3], &endptr);
128 if (fields[3] == endptr)
129 power = 1.0; /* invalid */
130 else if ((power >= 0.0) && (power <= 100.0))
131 power = wireless_percent_to_power (power);
132 else if ((power > 100.0) && (power <= 256.0))
133 power = power - 256.0;
134 else if (power > 0.0)
135 power = 1.0; /* invalid */
137 /* noise [dBm] < 0.0 */
138 noise = strtod (fields[4], &endptr);
139 if (fields[4] == endptr)
140 noise = 1.0; /* invalid */
141 else if ((noise >= 0.0) && (noise <= 100.0))
142 noise = wireless_percent_to_power (noise);
143 else if ((noise > 100.0) && (noise <= 256.0))
144 noise = noise - 256.0;
145 else if (noise > 0.0)
146 noise = 1.0; /* invalid */
148 wireless_submit (device, "signal_quality", quality);
149 wireless_submit (device, "signal_power", power);
150 wireless_submit (device, "signal_noise", noise);
157 /* If no wireless devices are present return an error, so the plugin
158 * code delays our read function. */
159 if (devices_found == 0)
161 #endif /* KERNEL_LINUX */
164 } /* int wireless_read */
166 void module_register (void)
168 plugin_register_read ("wireless", wireless_read);
169 } /* void module_register */