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 defined(KERNEL_LINUX)
27 # define WIRELESS_HAVE_READ 1
28 #else
29 # define WIRELESS_HAVE_READ 0
30 #endif
32 #define WIRELESS_PROC_FILE "/proc/net/wireless"
34 #if WIRELESS_HAVE_READ
35 #if 0
36 static double wireless_dbm_to_watt (double dbm)
37 {
38 double watt;
40 /*
41 * dbm = 10 * log_{10} (1000 * power / W)
42 * power = 10^(dbm/10) * W/1000
43 */
45 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
47 return (watt);
48 }
49 #endif
51 static void wireless_submit (const char *plugin_instance, const char *type,
52 double value)
53 {
54 value_t values[1];
55 value_list_t vl = VALUE_LIST_INIT;
57 values[0].gauge = value;
59 vl.values = values;
60 vl.values_len = 1;
61 vl.time = time (NULL);
62 strcpy (vl.host, hostname_g);
63 strcpy (vl.plugin, "wireless");
64 strncpy (vl.plugin_instance, plugin_instance,
65 sizeof (vl.plugin_instance));
67 plugin_dispatch_values (type, &vl);
68 } /* void wireless_submit */
70 #define POWER_MIN -90.0
71 #define POWER_MAX -50.0
72 static double wireless_percent_to_power (double quality)
73 {
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)
80 {
81 #ifdef KERNEL_LINUX
82 FILE *fh;
83 char buffer[1024];
85 char *device;
86 double quality;
87 double power;
88 double noise;
90 char *fields[8];
91 int numfields;
93 int devices_found;
94 int len;
96 /* there are a variety of names for the wireless device */
97 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
98 {
99 char errbuf[1024];
100 WARNING ("wireless: fopen: %s",
101 sstrerror (errno, errbuf, sizeof (errbuf)));
102 return (-1);
103 }
105 devices_found = 0;
106 while (fgets (buffer, sizeof (buffer), fh) != NULL)
107 {
108 char *endptr;
110 numfields = strsplit (buffer, fields, 8);
112 if (numfields < 5)
113 continue;
115 len = strlen (fields[0]) - 1;
116 if (len < 1)
117 continue;
118 if (fields[0][len] != ':')
119 continue;
120 fields[0][len] = '\0';
122 device = fields[0];
124 quality = strtod (fields[2], &endptr);
125 if (fields[2] == endptr)
126 quality = -1.0; /* invalid */
128 /* power [dBm] < 0.0 */
129 power = strtod (fields[3], &endptr);
130 if (fields[3] == endptr)
131 power = 1.0; /* invalid */
132 else if ((power >= 0.0) && (power <= 100.0))
133 power = wireless_percent_to_power (power);
134 else if (power > 100.0)
135 power = 1.0; /* invalid */
137 /* noise [dBm] < 0.0 */
138 noise = strtod (fields[3], &endptr);
139 if (fields[3] == 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)
144 noise = 1.0; /* invalid */
146 wireless_submit (device, "signal_quality", quality);
147 wireless_submit (device, "signal_power", power);
148 wireless_submit (device, "signal_noise", noise);
150 devices_found++;
151 }
153 fclose (fh);
155 /* If no wireless devices are present return an error, so the plugin
156 * code delays our read function. */
157 if (devices_found == 0)
158 return (-1);
159 #endif /* KERNEL_LINUX */
161 return (0);
162 } /* int wireless_read */
163 #endif /* WIRELESS_HAVE_READ */
165 void module_register (void)
166 {
167 #if WIRELESS_HAVE_READ
168 plugin_register_read ("wireless", wireless_read);
169 #endif /* WIRELESS_HAVE_READ */
170 } /* void module_register */