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"
36 char *community = NULL;
41 printf(_("\nUsage:\n"));
42 printf(_("\tap-mrtg -i ip -c community -t type [-b bssid] [-n name] "
43 "[-v] [-h] [-r]\n\n"));
44 printf(_("Get stats from AP and return it in MRTG parsable format\n\n"));
45 printf(_("-i ip - AP ip address\n"));
46 printf(_("-c community - SNMP community string\n"));
47 printf(_("-t type - statistics type <w>ireless, <e>thernet, "
48 "associated <s>tations or <l>ink quality (last one will only "));
49 printf(_("work with ATMEL410 MIB devices in AP Client mode)\n"));
50 printf(_("-b bssid - mac address of the AP from which get link quality"
51 ", only if type=l\n"));
52 printf(_("-n name - AP name - for check only\n"));
53 printf(_("-v - report MRTG about problems connecting to AP\n"));
54 printf(_("-r - reset AP when getting LinkQuality stats\n"));
55 printf(_("-h - print this help screen\n\n"));
56 printf(_("ap-mrtg %s Copyright (c) 2002-2003 Roman Festchook\n\n"),
60 int main(int argc, char **argv)
72 unsigned char channel;
74 unsigned char options;
76 unsigned char essid[32];
79 char sysDescr_NWN[] = {
80 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00
82 char sysDescr_ATMEL[] = {
83 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x01, 0x00
85 char bridgeOperationalMode[] = {
86 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01, 0x00
88 char EthRxStatistics[] = {
89 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x01, 0x00
91 char EthTxStatistics[] = {
92 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x02, 0x00
94 char operAccessPointName[] = {
95 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x01, 0x0A, 0x00
97 char wirelessStatistics[] = {
98 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x03, 0x01, 0x00
100 char AssociatedSTAsNum[] = {
101 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01, 0x00
103 char wirelessKnownAPs[] = {
104 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x07, 0x01, 0x00
107 struct EthRxStatistics_s *EthRxStat = NULL;
108 struct EthTxStatistics_s *EthTxStat = NULL;
109 struct wirelessStatistics_s *WirelessStat = NULL;
112 char message[12], bssid_flag, stat_type = 0, *ERR_STR =
113 ERR_STR_N, *bssid = NULL, *name = NULL, *cp;
116 setlocale(LC_ALL, "");
117 bindtextdomain("ap-utils", LOCALEDIR);
118 textdomain("ap-utils");
128 switch (opt = getopt(argc, argv, "i:c:t:b:n:rv")) {
130 for (cp = optarg, i = 0; *cp && (cp = index(cp, '.')); cp++, i++);
131 if (i < 3 || inet_aton(optarg, &ap_ip) == 0) {
132 printf(_("Error: invalid IP-address.\n"));
137 stat_type = optarg[0];
143 community = malloc(strlen(optarg) + 1);
144 strncpy(community, optarg, strlen(optarg) + 1);
147 bssid = malloc(strlen(optarg) + 1);
148 strncpy(bssid, optarg, strlen(optarg) + 1);
151 name = malloc(strlen(optarg) + 1);
152 strncpy(name, optarg, strlen(optarg) + 1);
171 * Part detecting ap_type (ATMEL AP MIB type) follows.
172 * We could use get_mib_details() here with advantage, but it would
173 * have to involve 1. putting it to separate file in lib/ and
174 * 2. patch it so it would not contain curses-related commands (TODO)
177 /* determine private MIB type according to enterprises ID */
178 varbinds[0].oid = sysDescr_NWN;
179 varbinds[0].len_oid = sizeof(sysDescr_NWN);
180 varbinds[0].value = NULL;
181 varbinds[0].len_val = 0;
182 varbinds[0].type = NULL_VALUE;
183 if (snmp(varbinds, 1, GET) > 0) {
186 varbinds[0].oid = sysDescr_ATMEL;
187 varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
188 varbinds[0].value = NULL;
189 varbinds[0].len_val = 0;
190 varbinds[0].type = NULL_VALUE;
191 if (snmp(varbinds, 1, GET) > 0) {
194 sysDescr_ATMEL[5] = 0xE0;
195 sysDescr_ATMEL[6] = 0x3E;
196 varbinds[0].oid = sysDescr_ATMEL;
197 varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
198 varbinds[0].value = NULL;
199 varbinds[0].len_val = 0;
200 varbinds[0].type = NULL_VALUE;
201 if (snmp(varbinds, 1, GET) > 0) {
202 ap_type = ATMEL12350;
204 printf(_("Unable to determine AP MIB type "
205 "(no response from AP)."));
211 if (ap_type == NWN) {
212 printf("NWN devices are not yet supported.");
216 if (ap_type == ATMEL12350) {
217 bridgeOperationalMode[5] = 0xE0;
218 bridgeOperationalMode[6] = 0x3E;
219 EthRxStatistics[5] = 0xE0;
220 EthRxStatistics[6] = 0x3E;
221 EthTxStatistics[5] = 0xE0;
222 EthTxStatistics[6] = 0x3E;
223 operAccessPointName[5] = 0xE0;
224 operAccessPointName[6] = 0x3E;
225 wirelessStatistics[5] = 0xE0;
226 wirelessStatistics[6] = 0x3E;
227 AssociatedSTAsNum[5] = 0xE0;
228 AssociatedSTAsNum[6] = 0x3E;
229 wirelessKnownAPs[5] = 0xE0;
230 wirelessKnownAPs[6] = 0x3E;
236 varbinds[0].oid = EthRxStatistics;
237 varbinds[0].len_oid = sizeof(EthRxStatistics);
238 varbinds[0].value = EthRxStatistics;
239 varbinds[0].len_val = 0;
240 varbinds[0].type = NULL_VALUE;
241 varbinds[1].oid = EthTxStatistics;
242 varbinds[1].len_oid = sizeof(EthTxStatistics);
243 varbinds[1].value = EthTxStatistics;
244 varbinds[1].len_val = 0;
245 varbinds[1].type = NULL_VALUE;
247 if (snmp(varbinds, 2, GET) < 2) {
252 if (varbinds[0].len_val == 64) {
256 (struct EthRxStatistics_s *) malloc(varbinds[0].
258 memcpy(EthRxStat, varbinds[0].value, varbinds[0].len_val);
264 if (varbinds[1].len_val == 56) {
268 (struct EthTxStatistics_s *) malloc(varbinds[1].
270 memcpy(EthTxStat, varbinds[1].value, varbinds[1].len_val);
275 printf("%u\n%u\n", swap4(EthRxStat->TotalBytesRx),
276 swap4(EthTxStat->TotalBytesTx));
283 varbinds[0].oid = wirelessStatistics;
284 varbinds[0].len_oid = sizeof(wirelessStatistics);
285 varbinds[0].value = wirelessStatistics;
286 varbinds[0].len_val = 0;
287 varbinds[0].type = NULL_VALUE;
289 if (snmp(varbinds, 1, GET) <= 0) {
294 if (varbinds[0].len_val == 88 || varbinds[0].len_val == 104) {
296 * 88 ... using traditional ATMEL 12350 MIB
297 * 104 .. using functionally enhanced ATMEL 12350 MIB by EZYNET
302 (struct wirelessStatistics_s *) malloc(varbinds[0].len_val);
303 memcpy(WirelessStat, varbinds[0].value, varbinds[0].len_val);
309 swap4(WirelessStat->UnicastReceivedPackets) +
310 swap4(WirelessStat->BroadcastReceivedPackets) +
311 swap4(WirelessStat->MulticastReceivedPackets),
312 swap4(WirelessStat->UnicastTransmittedPackets) +
313 swap4(WirelessStat->BroadcastTransmittedPackets) +
314 swap4(WirelessStat->MulticastTransmittedPackets));
318 varbinds[0].oid = AssociatedSTAsNum;
319 varbinds[0].len_oid = sizeof(AssociatedSTAsNum);
320 varbinds[0].value = AssociatedSTAsNum;
321 varbinds[0].len_val = 0;
322 varbinds[0].type = NULL_VALUE;
324 if (snmp(varbinds, 1, GET) <= 0) {
329 printf("%u\n0\n", *varbinds[0].value);
333 varbinds[0].oid = bridgeOperationalMode;
334 varbinds[0].len_oid = sizeof(bridgeOperationalMode);
335 varbinds[0].len_val = 0;
336 varbinds[0].type = NULL_VALUE;
338 if (snmp(varbinds, 1, GET) <= 0) {
343 if (!(ap_type == ATMEL410 && *(varbinds[0].value) == 3)) {
356 varbinds[0].oid = wirelessKnownAPs;
357 varbinds[0].len_oid = sizeof(wirelessKnownAPs);
358 varbinds[0].type = NULL_VALUE;
359 varbinds[0].len_val = 0;
361 if (snmp(varbinds, 1, GET) <= 0) {
366 for (i = 0; i < varbinds[0].len_val; i += 48) {
369 app = (struct ap *) malloc(48);
370 memcpy(app, varbinds[0].value + i, 48);
374 sprintf(message, "%02X%02X%02X%02X%02X%02X",
375 app->mac[0] & 0xFF, app->mac[1] & 0xFF,
376 app->mac[2] & 0xFF, app->mac[3] & 0xFF,
377 app->mac[4] & 0xFF, app->mac[5] & 0xFF);
378 if (memcmp(message, bssid, 12))
381 printf("%d\n%d\n", app->q2,
396 if ( name != NULL ) {
397 varbinds[0].oid = operAccessPointName;
398 varbinds[0].len_oid = sizeof(operAccessPointName);
399 varbinds[0].len_val = 0;
400 varbinds[0].type = NULL_VALUE;
401 if (snmp(varbinds, 1, GET) <= 0) {
406 for (i = 0; i < 32 && *(varbinds[0].value + i); i++)
407 putchar(*(varbinds[0].value + i));
409 if (strncmp(name,varbinds[0].value,strlen(name)) ){