03d124b28837dc76e19c643d4d6b80af5c6968df
1 /**
2 * collectd - src/wireless.c
3 * Copyright (C) 2006 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; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #include <math.h>
29 #define MODULE_NAME "wireless"
30 #define BUFSIZE 1024
32 #if defined(KERNEL_LINUX)
33 # define WIRELESS_HAVE_READ 1
34 #else
35 # define WIRELESS_HAVE_READ 0
36 #endif
38 #define WIRELESS_PROC_FILE "/proc/net/wireless"
40 static char *filename_template = "wireless-%s.rrd";
42 static char *ds_def[] =
43 {
44 "DS:quality:GAUGE:"COLLECTD_HEARTBEAT":0:U",
45 "DS:power:GAUGE:"COLLECTD_HEARTBEAT":0:U",
46 "DS:noise:GAUGE:"COLLECTD_HEARTBEAT":0:U",
47 NULL
48 };
49 static int ds_num = 3;
51 #if WIRELESS_HAVE_READ
52 static int proc_file_found = 0;
53 #endif
55 static void wireless_init (void)
56 {
57 #if WIRELESS_HAVE_READ
58 if (access (WIRELESS_PROC_FILE, R_OK) == 0)
59 proc_file_found = 1;
60 else
61 proc_file_found = 0;
62 #endif
64 return;
65 }
67 static void wireless_write (char *host, char *inst, char *val)
68 {
69 char file[BUFSIZE];
70 int status;
72 status = snprintf (file, BUFSIZE, filename_template, inst);
73 if (status < 1)
74 return;
75 else if (status >= BUFSIZE)
76 return;
78 rrd_update_file (host, file, val, ds_def, ds_num);
79 }
81 #if WIRELESS_HAVE_READ
82 static double wireless_dbm_to_watt (double dbm)
83 {
84 double watt;
86 /*
87 * dbm = 10 * log_{10} (1000 * power / W)
88 * power = 10^(dbm/10) * W/1000
89 */
91 watt = pow (10.0, (dbm / 10.0)) / 1000.0;
93 return (watt);
94 }
96 static void wireless_submit (char *device,
97 double quality, double power, double noise)
98 {
99 char buf[BUFSIZE];
100 int status;
102 status = snprintf (buf, BUFSIZE, "%u:%f:%f:%f",
103 (unsigned int) curtime,
104 quality, power, noise);
105 if ((status < 1) || (status >= BUFSIZE))
106 return;
108 plugin_submit (MODULE_NAME, device, buf);
109 }
111 static void wireless_read (void)
112 {
113 #ifdef KERNEL_LINUX
115 FILE *fh;
116 char buffer[BUFSIZE];
118 char *device;
119 double quality;
120 double power;
121 double noise;
123 char *fields[8];
124 int numfields;
126 int len;
128 if (!proc_file_found)
129 return;
131 /* there are a variety of names for the wireless device */
132 if ((fh = fopen (WIRELESS_PROC_FILE, "r")) == NULL)
133 {
134 syslog (LOG_WARNING, "wireless: fopen: %s", strerror (errno));
135 return;
136 }
138 while (fgets (buffer, BUFSIZE, fh) != NULL)
139 {
140 numfields = strsplit (buffer, fields, 8);
142 if (numfields < 5)
143 continue;
145 len = strlen (fields[0]) - 1;
146 if (len < 1)
147 continue;
148 if (fields[0][len] != ':')
149 continue;
150 fields[0][len] = '\0';
152 device = fields[0];
153 quality = atof (fields[2]);
154 power = atof (fields[3]);
155 noise = atof (fields[4]);
157 if (quality == 0.0)
158 quality = -1.0;
160 if (power >= 0.0)
161 power = -1.0;
162 else
163 power = wireless_dbm_to_watt (power);
165 if (noise >= 0.0)
166 noise = -1.0;
167 else
168 noise = wireless_dbm_to_watt (noise);
170 wireless_submit (device, quality, power, noise);
171 }
173 fclose (fh);
174 #endif /* KERNEL_LINUX */
175 }
176 #else
177 # define wireless_read NULL
178 #endif /* WIRELESS_HAVE_READ */
180 void module_register (void)
181 {
182 plugin_register (MODULE_NAME, wireless_init, wireless_read, wireless_write);
183 }
185 #undef BUFSIZE
186 #undef MODULE_NAME