Tree wide: Use compound literals when dealing with value_t.
[collectd.git] / src / wireless.c
1 /**
2  * collectd - src/wireless.c
3  * Copyright (C) 2006,2007  Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if !KERNEL_LINUX
33 # error "No applicable input method."
34 #endif
35
36 #define WIRELESS_PROC_FILE "/proc/net/wireless"
37
38 #if 0
39 static double wireless_dbm_to_watt (double dbm)
40 {
41         double watt;
42
43         /*
44          * dbm = 10 * log_{10} (1000 * power / W)
45          * power = 10^(dbm/10) * W/1000
46          */
47
48         watt = pow (10.0, (dbm / 10.0)) / 1000.0;
49
50         return (watt);
51 }
52 #endif
53
54 static void wireless_submit (const char *plugin_instance, const char *type,
55                 double value)
56 {
57         value_list_t vl = VALUE_LIST_INIT;
58
59         vl.values = &(value_t) { .gauge = value };
60         vl.values_len = 1;
61         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
62         sstrncpy (vl.plugin, "wireless", sizeof (vl.plugin));
63         sstrncpy (vl.plugin_instance, plugin_instance,
64                         sizeof (vl.plugin_instance));
65         sstrncpy (vl.type, type, sizeof (vl.type));
66
67         plugin_dispatch_values (&vl);
68 } /* void wireless_submit */
69
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));
75
76         return ((quality * (POWER_MAX - POWER_MIN)) + POWER_MIN);
77 } /* double wireless_percent_to_power */
78
79 static int wireless_read (void)
80 {
81 #ifdef KERNEL_LINUX
82         FILE *fh;
83         char buffer[1024];
84
85         char   *device;
86         double  quality;
87         double  power;
88         double  noise;
89
90         char *fields[8];
91         int   numfields;
92
93         int devices_found;
94         int len;
95
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         }
104
105         devices_found = 0;
106         while (fgets (buffer, sizeof (buffer), fh) != NULL)
107         {
108                 char *endptr;
109
110                 numfields = strsplit (buffer, fields, 8);
111
112                 if (numfields < 5)
113                         continue;
114
115                 len = strlen (fields[0]) - 1;
116                 if (len < 1)
117                         continue;
118                 if (fields[0][len] != ':')
119                         continue;
120                 fields[0][len] = '\0';
121
122                 device  = fields[0];
123
124                 quality = strtod (fields[2], &endptr);
125                 if (fields[2] == endptr)
126                         quality = -1.0; /* invalid */
127
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) && (power <= 256.0))
135                         power = power - 256.0;
136                 else if (power > 0.0)
137                         power = 1.0; /* invalid */
138
139                 /* noise [dBm] < 0.0 */
140                 noise = strtod (fields[4], &endptr);
141                 if (fields[4] == endptr)
142                         noise = 1.0; /* invalid */
143                 else if ((noise >= 0.0) && (noise <= 100.0))
144                         noise = wireless_percent_to_power (noise);
145                 else if ((noise > 100.0) && (noise <= 256.0))
146                         noise = noise - 256.0;
147                 else if (noise > 0.0)
148                         noise = 1.0; /* invalid */
149
150                 wireless_submit (device, "signal_quality", quality);
151                 wireless_submit (device, "signal_power", power);
152                 wireless_submit (device, "signal_noise", noise);
153
154                 devices_found++;
155         }
156
157         fclose (fh);
158
159         /* If no wireless devices are present return an error, so the plugin
160          * code delays our read function. */
161         if (devices_found == 0)
162                 return (-1);
163 #endif /* KERNEL_LINUX */
164
165         return (0);
166 } /* int wireless_read */
167
168 void module_register (void)
169 {
170         plugin_register_read ("wireless", wireless_read);
171 } /* void module_register */