Merge branch 'collectd-4.0'
[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 !KERNEL_LINUX
27 # error "No applicable input method."
28 #endif
29
30 #define WIRELESS_PROC_FILE "/proc/net/wireless"
31
32 #if 0
33 static double wireless_dbm_to_watt (double dbm)
34 {
35         double watt;
36
37         /*
38          * dbm = 10 * log_{10} (1000 * power / W)
39          * power = 10^(dbm/10) * W/1000 
40          */
41
42         watt = pow (10.0, (dbm / 10.0)) / 1000.0;
43
44         return (watt);
45 }
46 #endif
47
48 static void wireless_submit (const char *plugin_instance, const char *type,
49                 double value)
50 {
51         value_t values[1];
52         value_list_t vl = VALUE_LIST_INIT;
53
54         values[0].gauge = value;
55
56         vl.values = values;
57         vl.values_len = 1;
58         vl.time = time (NULL);
59         strcpy (vl.host, hostname_g);
60         strcpy (vl.plugin, "wireless");
61         strncpy (vl.plugin_instance, plugin_instance,
62                         sizeof (vl.plugin_instance));
63
64         plugin_dispatch_values (type, &vl);
65 } /* void wireless_submit */
66
67 static int wireless_read (void)
68 {
69 #ifdef KERNEL_LINUX
70         FILE *fh;
71         char buffer[1024];
72
73         char   *device;
74         double  quality;
75         double  power;
76         double  noise;
77         
78         char *fields[8];
79         int   numfields;
80
81         int devices_found;
82         int len;
83
84         /* there are a variety of names for the wireless device */
85         if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
86         {
87                 char errbuf[1024];
88                 WARNING ("wireless: fopen: %s",
89                                 sstrerror (errno, errbuf, sizeof (errbuf)));
90                 return (-1);
91         }
92
93         devices_found = 0;
94         while (fgets (buffer, sizeof (buffer), fh) != NULL)
95         {
96                 numfields = strsplit (buffer, fields, 8);
97
98                 if (numfields < 5)
99                         continue;
100
101                 len = strlen (fields[0]) - 1;
102                 if (len < 1)
103                         continue;
104                 if (fields[0][len] != ':')
105                         continue;
106                 fields[0][len] = '\0';
107
108                 device  = fields[0];
109                 quality = atof (fields[2]);
110                 power   = atof (fields[3]);
111                 noise   = atof (fields[4]);
112
113                 /* Fill in invalid values when conversion failed.. */
114                 if (quality == 0.0)
115                         quality = -1.0; /* quality >= 0 */
116
117                 if (power == 0.0)
118                         power = 1.0; /* power <= 0 */
119
120                 if (noise == 0.0)
121                         noise = 1.0; /* noise <= 0 */
122
123                 wireless_submit (device, "signal_quality", quality);
124                 wireless_submit (device, "signal_power", power);
125                 wireless_submit (device, "signal_noise", noise);
126
127                 devices_found++;
128         }
129
130         fclose (fh);
131
132         /* If no wireless devices are present return an error, so the plugin
133          * code delays our read function. */
134         if (devices_found == 0)
135                 return (-1);
136 #endif /* KERNEL_LINUX */
137
138         return (0);
139 } /* int wireless_read */
140
141 void module_register (void)
142 {
143         plugin_register_read ("wireless", wireless_read);
144 } /* void module_register */