2 * ap-mrtg.c from Access Point SNMP Utils for Linux
4 * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 from
8 * June 1991 as published by the Free Software Foundation.
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
13 * GNU General Public License for more details.
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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #if defined (__GLIBC__)
32 #define ERR_STR_V "\n\n-\n\n"
33 #define ERR_STR_N "999999999\n999999999\n-\n\n"
35 short ap_type = ATMEL410;
36 char *community = NULL;
42 printf(_("\nUsage:\n"));
44 ("\tap-rrd -i ip -c community -t type -d db_file [-b bssid] [-n name] [-a aptype] [-h] [-r] \n\n"));
46 ("Get stats from AP and put it in to specified RRDtool database\n\n"));
47 printf(_("-i ip - AP ip address\n"));
48 printf(_("-c community - SNMP community string\n"));
50 ("-t type - statistics type <w>ireless, <e>thernet, associated <s>tations or <l>ink quality in client mode\n"));
51 printf(_("-d db_file - RRD database file with full path\n"));
53 ("-b bssid - mac address of the AP to which get link quality, only if type=l\n"));
54 printf(_("-n name - AP name - for check only\n"));
55 printf(_("-a aptype - AP type - 410 (default) or 510 for ATMEL12350's, like the ME-102\n"));
56 printf(_("-r - reset AP when getting LinkQuality stats\n"));
57 printf(_("-h - print this help screen\n\n"));
58 printf(_("ap-rrd %s Copyright (c) 2002-2004 Roman Festchook\n\n"),
64 int main(int argc, char **argv)
76 unsigned char channel;
78 unsigned char options;
80 unsigned char essid[32];
84 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x03, 0x01,
88 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x01,
92 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x02,
95 char operAccessPointName[] =
96 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x01, 0x0A,
100 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01,
104 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x07,
107 char bridgeOperationalMode[] =
108 { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
109 0x01, 0x04, 0x01, 0x00
112 struct EthRxStatistics_s *EthRxStat = NULL;
113 struct EthTxStatistics_s *EthTxStat = NULL;
114 struct wirelessStatistics_s *WirelessStat = NULL;
117 char message[12], bssid_flag, stat_type = 0, *ERR_STR =
118 ERR_STR_N, *bssid = NULL, *name = NULL, *rrd_file = NULL, cmd[1024];
119 struct sockaddr_in client;
122 setlocale(LC_ALL, "");
123 bindtextdomain("ap-utils", LOCALEDIR);
124 textdomain("ap-utils");
127 memset(&client, 0, sizeof client);
128 client.sin_family = AF_INET;
129 client.sin_port = INADDR_ANY;
130 client.sin_addr.s_addr = INADDR_ANY;
139 switch (opt = getopt(argc, argv, "i:c:t:b:n:a:d:r")) {
141 if (inet_aton(optarg, &ap_ip) == 0) {
142 printf(_("Invalid IP-address\n"));
147 stat_type = optarg[0];
150 community = malloc(strlen(optarg) + 1);
151 strncpy(community, optarg, strlen(optarg) + 1);
154 bssid = malloc(strlen(optarg) + 1);
155 strncpy(bssid, optarg, strlen(optarg) + 1);
158 rrd_file = malloc(strlen(optarg) + 1);
159 strncpy(rrd_file, optarg, strlen(optarg) + 1);
162 name = malloc(strlen(optarg) + 1);
163 strncpy(name, optarg, strlen(optarg) + 1);
169 if ( strcmp( optarg, "510\0" ) == 0) {
176 operAccessPointName[5] = 0xE0;
177 operAccessPointName[6] = 0x3E;
182 bridgeOperationalMode[5] = 0xE0;
183 bridgeOperationalMode[6] = 0x3E;
184 } else if (strcmp(optarg, "410") == 0) {
185 /* nothing - hard-coded defaults are fine */
187 /* Invalid AP-Type */
188 printf(_("Invalid AP-Type '%s' - valid types are 510 or 410\n"), optarg);
206 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
207 perror(_("Create socket error"));
210 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
211 perror(_("Bind socket error"));
219 varbinds[0].oid = EthRx;
220 varbinds[0].len_oid = sizeof(EthRx);
221 varbinds[0].value = EthRx;
222 varbinds[0].len_val = 0;
223 varbinds[0].type = NULL_VALUE;
224 varbinds[1].oid = EthTx;
225 varbinds[1].len_oid = sizeof(EthTx);
226 varbinds[1].value = EthTx;
227 varbinds[1].len_val = 0;
228 varbinds[1].type = NULL_VALUE;
230 if (snmp(varbinds, 2, GET) <= 0) {
235 if (varbinds[0].len_val == 64) {
239 (struct EthRxStatistics_s *) malloc(varbinds[0].
241 memcpy(EthRxStat, varbinds[0].value, varbinds[0].len_val);
247 if (varbinds[1].len_val == 56) {
251 (struct EthTxStatistics_s *) malloc(varbinds[1].
253 memcpy(EthTxStat, varbinds[1].value, varbinds[1].len_val);
258 sprintf(cmd, "rrdtool update %s N:%u:%u", rrd_file, swap4(EthRxStat->TotalBytesRx), swap4(EthTxStat->TotalBytesTx));
266 varbinds[0].oid = Wireless;
267 varbinds[0].len_oid = sizeof(Wireless);
268 varbinds[0].value = Wireless;
269 varbinds[0].len_val = 0;
270 varbinds[0].type = NULL_VALUE;
272 if (snmp(varbinds, 1, GET) <= 0) {
277 if (varbinds[0].len_val == 88) {
281 (struct wirelessStatistics_s *) malloc(varbinds[0].len_val);
282 memcpy(WirelessStat, varbinds[0].value, varbinds[0].len_val);
287 sprintf(cmd, "rrdtool update %s N:%u:%u", rrd_file,
288 swap4(WirelessStat->UnicastReceivedPackets) +
289 swap4(WirelessStat->BroadcastReceivedPackets) +
290 swap4(WirelessStat->MulticastReceivedPackets),
291 swap4(WirelessStat->UnicastTransmittedPackets) +
292 swap4(WirelessStat->BroadcastTransmittedPackets) +
293 swap4(WirelessStat->MulticastTransmittedPackets));
298 varbinds[0].oid = StasNum;
299 varbinds[0].len_oid = sizeof(StasNum);
300 varbinds[0].value = StasNum;
301 varbinds[0].len_val = 0;
302 varbinds[0].type = NULL_VALUE;
304 if (snmp(varbinds, 1, GET) <= 0) {
309 sprintf(cmd, "rrdtool update %s N:%u", rrd_file, *varbinds[0].value);
315 varbinds[0].oid = bridgeOperationalMode;
316 varbinds[0].len_oid = sizeof(bridgeOperationalMode);
317 varbinds[0].len_val = 0;
318 varbinds[0].type = NULL_VALUE;
320 if (snmp(varbinds, 1, GET) <= 0) {
325 if (*(varbinds[0].value) != 3) {
338 varbinds[0].oid = KnownAP;
339 varbinds[0].len_oid = sizeof(KnownAP);
340 varbinds[0].type = NULL_VALUE;
341 varbinds[0].len_val = 0;
343 if (snmp(varbinds, 1, GET) <= 0) {
348 for (i = 0; i < varbinds[0].len_val; i += 48) {
351 app = (struct ap *) malloc(48);
352 memcpy(app, varbinds[0].value + i, 48);
356 sprintf(message, "%02X%02X%02X%02X%02X%02X",
357 app->mac[0] & 0xFF, app->mac[1] & 0xFF,
358 app->mac[2] & 0xFF, app->mac[3] & 0xFF,
359 app->mac[4] & 0xFF, app->mac[5] & 0xFF);
360 if (memcmp(message, bssid, 12))
363 sprintf(cmd, "rrdtool update %s N:%u:%u", rrd_file, app->q2, 96 - app->q1);
379 if ( name != NULL ) {
380 varbinds[0].oid = operAccessPointName;
381 varbinds[0].len_oid = sizeof(operAccessPointName);
382 varbinds[0].len_val = 0;
383 varbinds[0].type = NULL_VALUE;
384 if (snmp(varbinds, 1, GET) <= 0) {
389 for (i = 0; i < 32 && *(varbinds[0].value + i); i++)
390 putchar(*(varbinds[0].value + i));
392 if (strncmp(name,varbinds[0].value,strlen(name)) ){