Merge branch 'master' into collectd-4
[collectd.git] / src / wireless.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if defined(KERNEL_LINUX)
27 # define WIRELESS_HAVE_READ 1
28 #else
29 # define WIRELESS_HAVE_READ 0
30 #endif
31
32 #define WIRELESS_PROC_FILE "/proc/net/wireless"
33
34 static data_source_t data_source_quality[1] =
35 {
36         {"value", DS_TYPE_GAUGE, 0, NAN}
37 };
38
39 static data_set_t quality_ds =
40 {
41         "signal_quality", 1, data_source_quality
42 };
43
44 static data_source_t data_source_signal[1] =
45 {
46         {"value", DS_TYPE_GAUGE, NAN, 0}
47 };
48
49 static data_set_t power_ds =
50 {
51         "signal_power", 1, data_source_signal
52 };
53
54 static data_set_t noise_ds =
55 {
56         "signal_noise", 1, data_source_signal
57 };
58
59 #if WIRELESS_HAVE_READ
60 #if 0
61 static double wireless_dbm_to_watt (double dbm)
62 {
63         double watt;
64
65         /*
66          * dbm = 10 * log_{10} (1000 * power / W)
67          * power = 10^(dbm/10) * W/1000 
68          */
69
70         watt = pow (10.0, (dbm / 10.0)) / 1000.0;
71
72         return (watt);
73 }
74 #endif
75
76 static void wireless_submit (const char *plugin_instance, const char *type,
77                 double value)
78 {
79         value_t values[1];
80         value_list_t vl = VALUE_LIST_INIT;
81
82         values[0].gauge = value;
83
84         vl.values = values;
85         vl.values_len = 1;
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));
91
92         plugin_dispatch_values (type, &vl);
93 } /* void wireless_submit */
94
95 static int wireless_read (void)
96 {
97 #ifdef KERNEL_LINUX
98         FILE *fh;
99         char buffer[1024];
100
101         char   *device;
102         double  quality;
103         double  power;
104         double  noise;
105         
106         char *fields[8];
107         int   numfields;
108
109         int devices_found;
110         int len;
111
112         /* there are a variety of names for the wireless device */
113         if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
114         {
115                 char errbuf[1024];
116                 WARNING ("wireless: fopen: %s",
117                                 sstrerror (errno, errbuf, sizeof (errbuf)));
118                 return (-1);
119         }
120
121         devices_found = 0;
122         while (fgets (buffer, sizeof (buffer), fh) != NULL)
123         {
124                 numfields = strsplit (buffer, fields, 8);
125
126                 if (numfields < 5)
127                         continue;
128
129                 len = strlen (fields[0]) - 1;
130                 if (len < 1)
131                         continue;
132                 if (fields[0][len] != ':')
133                         continue;
134                 fields[0][len] = '\0';
135
136                 device  = fields[0];
137                 quality = atof (fields[2]);
138                 power   = atof (fields[3]);
139                 noise   = atof (fields[4]);
140
141                 /* Fill in invalid values when conversion failed.. */
142                 if (quality == 0.0)
143                         quality = -1.0; /* quality >= 0 */
144
145                 if (power == 0.0)
146                         power = 1.0; /* power <= 0 */
147
148                 if (noise == 0.0)
149                         noise = 1.0; /* noise <= 0 */
150
151                 wireless_submit (device, "signal_quality", quality);
152                 wireless_submit (device, "signal_power", power);
153                 wireless_submit (device, "signal_noise", noise);
154
155                 devices_found++;
156         }
157
158         fclose (fh);
159
160         /* If no wireless devices are present return an error, so the plugin
161          * code delays our read function. */
162         if (devices_found == 0)
163                 return (-1);
164 #endif /* KERNEL_LINUX */
165
166         return (0);
167 } /* int wireless_read */
168 #endif /* WIRELESS_HAVE_READ */
169
170 void module_register (modreg_e load)
171 {
172         if (load & MR_DATASETS)
173         {
174                 plugin_register_data_set (&quality_ds);
175                 plugin_register_data_set (&power_ds);
176                 plugin_register_data_set (&noise_ds);
177         }
178
179 #if WIRELESS_HAVE_READ
180         if (load & MR_READ)
181                 plugin_register_read ("wireless", wireless_read);
182 #endif /* WIRELESS_HAVE_READ */
183 } /* void module_register */