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>
26 #if defined(KERNEL_LINUX)
27 # define WIRELESS_HAVE_READ 1
29 # define WIRELESS_HAVE_READ 0
32 #define WIRELESS_PROC_FILE "/proc/net/wireless"
34 static data_source_t data_source_quality[1] =
36 {"value", DS_TYPE_GAUGE, 0, NAN}
39 static data_set_t quality_ds =
41 "signal_quality", 1, data_source_quality
44 static data_source_t data_source_signal[1] =
46 {"value", DS_TYPE_GAUGE, NAN, 0}
49 static data_set_t power_ds =
51 "signal_power", 1, data_source_signal
54 static data_set_t noise_ds =
56 "signal_noise", 1, data_source_signal
59 #if WIRELESS_HAVE_READ
61 static double wireless_dbm_to_watt (double dbm)
66 * dbm = 10 * log_{10} (1000 * power / W)
67 * power = 10^(dbm/10) * W/1000
70 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
76 static void wireless_submit (const char *plugin_instance, const char *type,
80 value_list_t vl = VALUE_LIST_INIT;
82 values[0].gauge = value;
86 vl.time = time (NULL);
87 strcpy (vl.host, hostname_g);
88 strcpy (vl.plugin, "wireless");
89 strncpy (vl.plugin_instance, plugin_instance,
90 sizeof (vl.plugin_instance));
92 plugin_dispatch_values (type, &vl);
93 } /* void wireless_submit */
95 static int wireless_read (void)
112 /* there are a variety of names for the wireless device */
113 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
116 WARNING ("wireless: fopen: %s",
117 sstrerror (errno, errbuf, sizeof (errbuf)));
122 while (fgets (buffer, sizeof (buffer), fh) != NULL)
124 numfields = strsplit (buffer, fields, 8);
129 len = strlen (fields[0]) - 1;
132 if (fields[0][len] != ':')
134 fields[0][len] = '\0';
137 quality = atof (fields[2]);
138 power = atof (fields[3]);
139 noise = atof (fields[4]);
141 /* Fill in invalid values when conversion failed.. */
143 quality = -1.0; /* quality >= 0 */
146 power = 1.0; /* power <= 0 */
149 noise = 1.0; /* noise <= 0 */
151 wireless_submit (device, "signal_quality", quality);
152 wireless_submit (device, "signal_power", power);
153 wireless_submit (device, "signal_noise", noise);
160 /* If no wireless devices are present return an error, so the plugin
161 * code delays our read function. */
162 if (devices_found == 0)
164 #endif /* KERNEL_LINUX */
167 } /* int wireless_read */
168 #endif /* WIRELESS_HAVE_READ */
170 void module_register (void)
172 plugin_register_data_set (&quality_ds);
173 plugin_register_data_set (&power_ds);
174 plugin_register_data_set (&noise_ds);
176 #if WIRELESS_HAVE_READ
177 plugin_register_read ("wireless", wireless_read);
178 #endif /* WIRELESS_HAVE_READ */