]> git.decadent.org.uk Git - ap-utils.git/blob - src/ap-mrtg.c
296a37ac4f5dbccb45d923eb0cebb51421c0355e
[ap-utils.git] / src / ap-mrtg.c
1 /*
2  *      ap-mrtg.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  *
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.
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
13  * GNU 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  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #if defined (__GLIBC__)
28 #include <libgen.h>
29 #endif
30 #include "ap-utils.h"
31
32 #define ERR_STR_V "\n\n-\n\n"
33 #define ERR_STR_N "999999999\n999999999\n-\n\n"
34
35 short ap_type = ATMEL410;
36 char *community = NULL;
37 int sockfd;
38 struct in_addr ap_ip;
39
40 void usage()
41 {
42     printf(_("\nUsage:\n"));
43     printf(_
44           ("\tap-mrtg -i ip -c community -t type [-b bssid] [-n name] [-a aptype] [-v] [-h] [-r]\n\n"));
45     printf(_
46            ("Get stats from AP and return it in MRTG parsable format\n\n"));
47     printf(_("-i ip        - AP ip address\n"));
48     printf(_("-c community - SNMP community string\n"));
49     printf(_
50            ("-t type      - statistics type <w>ireless, <e>thernet, associated <s>tations or <l>ink quality in client mode\n"));
51     printf(_
52            ("-b bssid     - mac address of the AP to which get link quality, only if type=l\n"));
53     printf(_("-n name     - AP name - for check only\n")); 
54     printf(_("-a aptype        - AP type - 410 (default) or 510 for ATMEL12350's, like the ME-102\n"));
55     printf(_
56            ("-v           - report MRTG about problems connecting to AP\n"));
57     printf(_("-r           - reset AP when getting LinkQuality stats\n"));
58     printf(_("-h           - print this help screen\n\n"));
59     printf(_("ap-mrtg %s Copyright (c) 2002-2003 Roman Festchook\n\n"),
60            VERSION);
61
62 }
63
64
65 int main(int argc, char **argv)
66 {
67     extern char *optarg;
68     extern int optind;
69     extern int opterr;
70     extern int optopt;
71     int opt = 0;
72
73     struct ap {
74         char mac[6];
75         unsigned char q1;
76         unsigned char q2;
77         unsigned char channel;
78         unsigned char x2;
79         unsigned char options;
80         unsigned char x3[5];
81         unsigned char essid[32];
82     } *app = NULL;
83
84     char Wireless[] =
85         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x03, 0x01,
86         0x00
87     };
88     char EthRx[] =
89         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x01,
90         0x00
91     };
92     char EthTx[] =
93         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x07, 0x02,
94         0x00
95     };
96     char operAccessPointName[] =
97         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x01, 0x0A,
98         0x00
99     };
100     char StasNum[] =
101         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x05, 0x01,
102         0x00
103     };
104     char KnownAP[] =
105         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x07,
106         0x01, 0x00
107     };
108     char bridgeOperationalMode[] =
109         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
110         0x01, 0x04, 0x01, 0x00
111     };
112
113     struct EthernetRxStatistics *EthRxStat = NULL;
114     struct EthernetTxStatistics *EthTxStat = NULL;
115     struct WirelessStatistics *WirelessStat = NULL;
116     varbind varbinds[2];
117     int i, reset_flag=0;
118     char message[12], bssid_flag,  stat_type = 0, *ERR_STR =
119         ERR_STR_N, *bssid = NULL, *name = NULL;
120     struct sockaddr_in client;
121
122 #ifdef HAVE_GETTEXT
123     setlocale(LC_ALL, "");
124     bindtextdomain("ap-utils", LOCALEDIR);
125     textdomain("ap-utils");
126 #endif
127
128     memset(&client, 0, sizeof client);
129     client.sin_family = AF_INET;
130     client.sin_port = INADDR_ANY;
131     client.sin_addr.s_addr = INADDR_ANY;
132
133     if (argc < 4) {
134         usage();
135         exit(0);
136     }
137
138     do {
139         opterr = 0;
140         switch (opt = getopt(argc, argv, "i:c:t:b:n:a:rv")) {
141         case 'i':
142             if (inet_aton(optarg, &ap_ip) == 0) {
143                 printf(_("Invalid IP-address\n"));
144                 return 1;
145             }
146             break;
147         case 't':
148             stat_type = optarg[0];
149             break;
150         case 'v':
151             ERR_STR = ERR_STR_V;
152             break;
153         case 'c':
154             community = malloc(strlen(optarg) + 1);
155             strncpy(community, optarg, strlen(optarg) + 1);
156             break;
157         case 'b':
158             bssid = malloc(strlen(optarg) + 1);
159             strncpy(bssid, optarg, strlen(optarg) + 1);
160             break;
161         case 'n':
162             name = malloc(strlen(optarg) + 1);
163             strncpy(name, optarg, strlen(optarg) + 1);
164             break;
165         case 'r':
166             reset_flag=1;
167             break;
168         case 'a':
169             if ( strcmp( optarg, "510\0" ) == 0) {
170               Wireless[5] = 0xE0;
171               Wireless[6] = 0x3E;
172               EthRx[5] = 0xE0;
173               EthRx[6] = 0x3E;
174               EthTx[5] = 0xE0;
175               EthTx[6] = 0x3E;
176               operAccessPointName[5] = 0xE0;
177               operAccessPointName[6] = 0x3E;
178               StasNum[5] = 0xE0;
179               StasNum[6] = 0x3E;
180               KnownAP[5] = 0xE0;
181               KnownAP[6] = 0x3E;
182               bridgeOperationalMode[5] = 0xE0;
183               bridgeOperationalMode[6] = 0x3E;
184             } else if (strcmp(optarg, "410") == 0) {
185               /* nothing - hard-coded defaults are fine */
186             } else {
187               /* Invalid AP-Type */
188               printf(_("Invalid AP-Type '%s' - valid types are 510 or 410\n"), optarg);
189               return 1;
190             }
191             break;
192         case -1:
193             break;
194         default:
195             usage();
196             goto quit;
197         }
198     } while (opt != -1);
199
200     if (!community) {
201         usage();
202         goto quit;
203     }
204
205
206     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
207         perror(_("Create socket error"));
208         return 1;
209     }
210     if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
211         perror(_("Bind socket error"));
212         return 1;
213     }
214
215     switch (stat_type) {
216
217     case 'e':
218
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;
229
230         if (snmp(varbinds, 2, GET) <= 0) {
231             printf(ERR_STR);
232             return 1;
233         }
234
235         if (varbinds[0].len_val == 64) {
236             if (EthRxStat)
237                 free(EthRxStat);
238             EthRxStat =
239                 (struct EthernetRxStatistics *) malloc(varbinds[0].
240                                                        len_val);
241             memcpy(EthRxStat, varbinds[0].value, varbinds[0].len_val);
242         } else {
243             printf(ERR_STR);
244             return 1;
245         }
246
247         if (varbinds[1].len_val == 56) {
248             if (EthTxStat)
249                 free(EthTxStat);
250             EthTxStat =
251                 (struct EthernetTxStatistics *) malloc(varbinds[1].
252                                                        len_val);
253             memcpy(EthTxStat, varbinds[1].value, varbinds[1].len_val);
254         } else {
255             printf(ERR_STR);
256             return 1;
257         }
258         printf("%u\n%u\n", swap4(EthRxStat->TotalBytesRx),
259                swap4(EthTxStat->TotalBytesTx));
260         if (EthRxStat)
261             free(EthRxStat);
262         if (EthTxStat)
263             free(EthTxStat);
264         break;
265     case 'w':
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;
271
272         if (snmp(varbinds, 1, GET) <= 0) {
273             printf(ERR_STR);
274             return 1;
275         }
276
277         if (varbinds[0].len_val == 88) {
278             if (WirelessStat)
279                 free(WirelessStat);
280             WirelessStat =
281                 (struct WirelessStatistics *) malloc(varbinds[0].len_val);
282             memcpy(WirelessStat, varbinds[0].value, varbinds[0].len_val);
283         } else {
284             printf(ERR_STR);
285             return 1;
286         }
287         printf("%u\n%u\n",
288                swap4(WirelessStat->UnicastReceivedPackets) +
289                swap4(WirelessStat->BroadcastReceivedPackets) +
290                swap4(WirelessStat->MulticastReceivedPackets),
291                swap4(WirelessStat->UnicastTransmittedPackets) +
292                swap4(WirelessStat->BroadcastTransmittedPackets) +
293                swap4(WirelessStat->MulticastTransmittedPackets));
294         break;
295
296     case 's':
297         varbinds[0].oid = StasNum;
298         varbinds[0].len_oid = sizeof(StasNum);
299         varbinds[0].value = StasNum;
300         varbinds[0].len_val = 0;
301         varbinds[0].type = NULL_VALUE;
302
303         if (snmp(varbinds, 1, GET) <= 0) {
304             printf(ERR_STR);
305             return 1;
306         }
307
308         printf("%u\n0\n", *varbinds[0].value);
309         break;
310
311     case 'l':
312
313         varbinds[0].oid = bridgeOperationalMode;
314         varbinds[0].len_oid = sizeof(bridgeOperationalMode);
315         varbinds[0].len_val = 0;
316         varbinds[0].type = NULL_VALUE;
317
318         if (snmp(varbinds, 1, GET) <= 0) {
319             printf(ERR_STR);
320             return 1;
321         }
322
323         if (*(varbinds[0].value) != 3) {
324             printf(ERR_STR);
325             return 1;
326         }
327
328         if (reset_flag) {
329                 if (SysReset()) {
330                     printf(ERR_STR);
331                     return 1;
332                 }
333                 sleep(10);
334         }
335                 
336         varbinds[0].oid = KnownAP;
337         varbinds[0].len_oid = sizeof(KnownAP);
338         varbinds[0].type = NULL_VALUE;
339         varbinds[0].len_val = 0;
340
341         if (snmp(varbinds, 1, GET) <= 0) {
342             printf(ERR_STR);
343             return 1;
344         }
345         bssid_flag = 1;
346         for (i = 0; i < varbinds[0].len_val; i += 48) {
347             if (app)
348                 free(app);
349             app = (struct ap *) malloc(48);
350             memcpy(app, varbinds[0].value + i, 48);
351             if (!app->channel)
352                 continue;
353             if (bssid) {
354                 sprintf(message, "%02X%02X%02X%02X%02X%02X",
355                         app->mac[0] & 0xFF, app->mac[1] & 0xFF,
356                         app->mac[2] & 0xFF, app->mac[3] & 0xFF,
357                         app->mac[4] & 0xFF, app->mac[5] & 0xFF);
358                 if (memcmp(message, bssid, 12))
359                     continue;
360             };
361             printf("%d\n%d\n", app->q2,
362                    96 - app->q1);
363             bssid_flag = 0;
364             break;
365         }
366         if (bssid_flag)
367             printf(ERR_STR);
368         break;
369     default:
370         usage();
371         goto quit;
372     }
373
374     printf("-\n");
375
376
377     if ( name != NULL ) {
378         varbinds[0].oid = operAccessPointName;
379         varbinds[0].len_oid = sizeof(operAccessPointName);
380         varbinds[0].len_val = 0;
381         varbinds[0].type = NULL_VALUE;
382         if (snmp(varbinds, 1, GET) <= 0) {
383                 printf("\n");
384                 return 1;
385         }
386
387         for (i = 0; i < 32 && *(varbinds[0].value + i); i++)
388                 putchar(*(varbinds[0].value + i));
389                 putchar('\n');
390           if (strncmp(name,varbinds[0].value,strlen(name)) ){
391                 return 2;
392         }
393     } else {
394         printf("-\n");
395     }
396
397     close(sockfd);
398
399     
400   quit:
401     if (community)
402         free(community);
403     if (app)
404         free(app);
405     if (bssid)
406         free(bssid);
407     if (name)
408         free(name);
409     return 0;
410 }
411