2 * collectd - src/wireless.c
3 * Copyright (C) 2006-2018 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
34 #include <linux/wireless.h>
35 #include <sys/ioctl.h>
37 #error "No applicable input method."
40 #define WIRELESS_PROC_FILE "/proc/net/wireless"
43 static double wireless_dbm_to_watt (double dbm)
48 * dbm = 10 * log_{10} (1000 * power / W)
49 * power = 10^(dbm/10) * W/1000
52 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
58 static void wireless_submit(const char *plugin_instance, const char *type,
60 value_list_t vl = VALUE_LIST_INIT;
62 vl.values = &(value_t){.gauge = value};
64 sstrncpy(vl.plugin, "wireless", sizeof(vl.plugin));
65 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
66 sstrncpy(vl.type, type, sizeof(vl.type));
68 plugin_dispatch_values(&vl);
69 } /* void wireless_submit */
71 #define POWER_MIN -90.0
72 #define POWER_MAX -50.0
73 static double wireless_percent_to_power(double quality) {
74 assert((quality >= 0.0) && (quality <= 100.0));
76 return (quality * (POWER_MAX - POWER_MIN)) + POWER_MIN;
77 } /* double wireless_percent_to_power */
79 static int wireless_read(void) {
95 /* there are a variety of names for the wireless device */
96 if ((fh = fopen(WIRELESS_PROC_FILE, "r")) == NULL) {
97 ERROR("wireless plugin: fopen: %s", STRERRNO);
101 int sock = socket(AF_INET, SOCK_DGRAM, 0);
103 ERROR("wireless plugin: socket: %s", STRERRNO);
109 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
112 numfields = strsplit(buffer, fields, 8);
117 len = strlen(fields[0]) - 1;
120 if (fields[0][len] != ':')
122 fields[0][len] = '\0';
126 quality = strtod(fields[2], &endptr);
127 if (fields[2] == endptr)
128 quality = -1.0; /* invalid */
130 /* power [dBm] < 0.0 */
131 power = strtod(fields[3], &endptr);
132 if (fields[3] == endptr)
133 power = 1.0; /* invalid */
134 else if ((power >= 0.0) && (power <= 100.0))
135 power = wireless_percent_to_power(power);
136 else if ((power > 100.0) && (power <= 256.0))
137 power = power - 256.0;
138 else if (power > 0.0)
139 power = 1.0; /* invalid */
141 /* noise [dBm] < 0.0 */
142 noise = strtod(fields[4], &endptr);
143 if (fields[4] == endptr)
144 noise = 1.0; /* invalid */
145 else if ((noise >= 0.0) && (noise <= 100.0))
146 noise = wireless_percent_to_power(noise);
147 else if ((noise > 100.0) && (noise <= 256.0))
148 noise = noise - 256.0;
149 else if (noise > 0.0)
150 noise = 1.0; /* invalid */
152 wireless_submit(device, "signal_quality", quality);
153 wireless_submit(device, "signal_power", power);
154 wireless_submit(device, "signal_noise", noise);
157 .ifr_ifrn.ifrn_name = {0},
159 sstrncpy(req.ifr_ifrn.ifrn_name, device, sizeof(req.ifr_ifrn.ifrn_name));
160 if (ioctl(sock, SIOCGIWRATE, &req) == -1) {
161 WARNING("wireless plugin: ioctl(SIOCGIWRATE): %s", STRERRNO);
163 wireless_submit(device, "bitrate", (double)req.u.bitrate.value);
172 /* If no wireless devices are present return an error, so the plugin
173 * code delays our read function. */
174 if (devices_found == 0)
176 #endif /* KERNEL_LINUX */
179 } /* int wireless_read */
181 void module_register(void) {
182 plugin_register_read("wireless", wireless_read);
183 } /* void module_register */