added files
[bcm963xx.git] / userapps / opensource / net-snmp / apps / snmpdf.c
1 /*
2  * snmpget.c - send snmp GET requests to a network entity.
3  *
4  */
5 /***********************************************************************
6         Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
7
8                       All Rights Reserved
9
10 Permission to use, copy, modify, and distribute this software and its 
11 documentation for any purpose and without fee is hereby granted, 
12 provided that the above copyright notice appear in all copies and that
13 both that copyright notice and this permission notice appear in 
14 supporting documentation, and that the name of CMU not be
15 used in advertising or publicity pertaining to distribution of the
16 software without specific, written prior permission.  
17
18 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
19 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
20 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
21 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 SOFTWARE.
25 ******************************************************************/
26 #include <net-snmp/net-snmp-config.h>
27
28 #if HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #if HAVE_STRING_H
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39 #include <sys/types.h>
40 #if HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #include <stdio.h>
44 #include <ctype.h>
45 #if TIME_WITH_SYS_TIME
46 # ifdef WIN32
47 #  include <sys/timeb.h>
48 # else
49 #  include <sys/time.h>
50 # endif
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 #  include <sys/time.h>
55 # else
56 #  include <time.h>
57 # endif
58 #endif
59 #if HAVE_SYS_SELECT_H
60 #include <sys/select.h>
61 #endif
62 #if HAVE_WINSOCK_H
63 #include <winsock.h>
64 #endif
65 #if HAVE_NETDB_H
66 #include <netdb.h>
67 #endif
68 #if HAVE_ARPA_INET_H
69 #include <arpa/inet.h>
70 #endif
71
72 #include <net-snmp/net-snmp-includes.h>
73
74 int             failures = 0;
75
76 void
77 usage(void)
78 {
79     fprintf(stderr, "Usage: snmpdf [-Cu] ");
80     snmp_parse_args_usage(stderr);
81     fprintf(stderr, "\n\n");
82     snmp_parse_args_descriptions(stderr);
83     fprintf(stderr, "\nsnmpdf options:\n");
84     fprintf(stderr,
85             "\t-Cu\tUse UCD-SNMP dskTable to do the calculations.\n");
86     fprintf(stderr,
87             "\t\t[Normally the HOST-RESOURCES-MIB is consulted first.]\n");
88 }
89
90 int             ucd_mib = 0;
91
92 static void
93 optProc(int argc, char *const *argv, int opt)
94 {
95     switch (opt) {
96     case 'C':
97         while (*optarg) {
98             switch (*optarg++) {
99             case 'u':
100                 ucd_mib = 1;
101                 break;
102             default:
103                 fprintf(stderr,
104                         "Unknown flag passed to -C: %c\n", optarg[-1]);
105                 exit(1);
106             }
107         }
108     }
109 }
110
111 struct hrStorageTable {
112     u_long          hrStorageIndex;
113     oid            *hrStorageType;
114     char           *hrStorageDescr;
115     u_long          hrStorageAllocationUnits;
116     u_long          hrStorageSize;
117     u_long          hrStorageUsed;
118 };
119
120 int
121 add(netsnmp_pdu *pdu, const char *mibnodename,
122     oid * index, size_t indexlen)
123 {
124     oid             base[MAX_OID_LEN];
125     size_t          base_length = MAX_OID_LEN;
126
127     memset(base, 0, MAX_OID_LEN * sizeof(oid));
128
129     if (!snmp_parse_oid(mibnodename, base, &base_length)) {
130         snmp_perror(mibnodename);
131         fprintf(stderr, "couldn't find mib node %s, giving up\n",
132                 mibnodename);
133         exit(1);
134     }
135
136     if (index && indexlen) {
137         memcpy(&(base[base_length]), index, indexlen * sizeof(oid));
138         base_length += indexlen;
139     }
140     DEBUGMSGTL(("add", "created: "));
141     DEBUGMSGOID(("add", base, base_length));
142     DEBUGMSG(("add", "\n"));
143     snmp_add_null_var(pdu, base, base_length);
144
145     return base_length;
146 }
147
148 netsnmp_variable_list *
149 collect(netsnmp_session * ss, netsnmp_pdu *pdu,
150         oid * base, size_t base_length)
151 {
152     netsnmp_pdu    *response;
153     int             running = 1;
154     netsnmp_variable_list *saved = NULL, **vlpp = &saved;
155     int             status;
156
157     while (running) {
158         /*
159          * gotta catch em all, gotta catch em all! 
160          */
161         status = snmp_synch_response(ss, pdu, &response);
162         if (status != STAT_SUCCESS || !response) {
163             snmp_sess_perror("snmpdf", ss);
164             exit(1);
165         }
166         if (response && snmp_oid_compare(response->variables->name,
167                                          SNMP_MIN(base_length,
168                                                   response->variables->
169                                                   name_length), base,
170                                          base_length) != 0)
171             running = 0;
172         else {
173             /*
174              * get response 
175              */
176             *vlpp = response->variables;
177             (*vlpp)->next_variable = NULL;      /* shouldn't be any, but just in case */
178
179             /*
180              * create the next request 
181              */
182             pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
183             snmp_add_null_var(pdu, (*vlpp)->name, (*vlpp)->name_length);
184
185             /*
186              * finish loop setup 
187              */
188             vlpp = &((*vlpp)->next_variable);
189             response->variables = NULL; /* ahh, forget about it */
190         }
191         snmp_free_pdu(response);
192     }
193     return saved;
194 }
195
196
197
198 int
199 main(int argc, char *argv[])
200 {
201     netsnmp_session session, *ss;
202     netsnmp_pdu    *pdu;
203     netsnmp_pdu    *response;
204     int             arg;
205     oid             base[MAX_OID_LEN];
206     size_t          base_length;
207     int             status;
208     netsnmp_variable_list *saved = NULL, *vlp = saved, *vlp2;
209     int             count = 0;
210
211     /*
212      * get the common command line arguments 
213      */
214     switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
215     case -2:
216         exit(0);
217     case -1:
218         usage();
219         exit(1);
220     default:
221         break;
222     }
223
224     SOCK_STARTUP;
225
226     /*
227      * Open an SNMP session.
228      */
229     ss = snmp_open(&session);
230     if (ss == NULL) {
231         /*
232          * diagnose snmp_open errors with the input netsnmp_session pointer 
233          */
234         snmp_sess_perror("snmpget", &session);
235         SOCK_CLEANUP;
236         exit(1);
237     }
238
239     printf("%-18s %15s %15s %15s %5s\n", "Description", "size (kB)",
240            "Used", "Available", "Used%");
241     if (ucd_mib == 0) {
242         /*
243          * * Begin by finding all the storage pieces that are of
244          * * type hrStorageFixedDisk, which is a standard disk.
245          */
246         pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
247         base_length =
248             add(pdu, "HOST-RESOURCES-MIB:hrStorageIndex", NULL, 0);
249         memcpy(base, pdu->variables->name, base_length * sizeof(oid));
250
251         vlp = collect(ss, pdu, base, base_length);
252
253         while (vlp) {
254             size_t          units;
255             unsigned long   hssize, hsused;
256             char            descr[SPRINT_MAX_LEN];
257
258             pdu = snmp_pdu_create(SNMP_MSG_GET);
259
260             add(pdu, "HOST-RESOURCES-MIB:hrStorageDescr",
261                 &(vlp->name[base_length]), vlp->name_length - base_length);
262             add(pdu, "HOST-RESOURCES-MIB:hrStorageAllocationUnits",
263                 &(vlp->name[base_length]), vlp->name_length - base_length);
264             add(pdu, "HOST-RESOURCES-MIB:hrStorageSize",
265                 &(vlp->name[base_length]), vlp->name_length - base_length);
266             add(pdu, "HOST-RESOURCES-MIB:hrStorageUsed",
267                 &(vlp->name[base_length]), vlp->name_length - base_length);
268
269             status = snmp_synch_response(ss, pdu, &response);
270             if (status != STAT_SUCCESS || !response) {
271                 snmp_sess_perror("snmpdf", ss);
272                 exit(1);
273             }
274
275             vlp2 = response->variables;
276             memcpy(descr, vlp2->val.string, vlp2->val_len);
277             descr[vlp2->val_len] = '\0';
278
279             vlp2 = vlp2->next_variable;
280             units = vlp2->val.integer ? *(vlp2->val.integer) : 0;
281
282             vlp2 = vlp2->next_variable;
283             hssize = vlp2->val.integer ? *(vlp2->val.integer) : 0;
284
285             vlp2 = vlp2->next_variable;
286             hsused = vlp2->val.integer ? *(vlp2->val.integer) : 0;
287
288             printf("%-18s %15lu %15lu %15lu %4lu%%\n", descr,
289                    units ? hssize * (units / 1024) : hssize,
290                    units ? hsused * (units / 1024) : hsused,
291                    units ? (hssize - hsused) * (units / 1024) : hssize -
292                    hsused, hssize ? 100 * hsused / hssize : hsused);
293
294             vlp = vlp->next_variable;
295             snmp_free_pdu(response);
296             count++;
297         }
298     }
299
300     if (count == 0) {
301         size_t          units = 0;
302         /*
303          * the host resources mib must not be supported.  Lets try the
304          * UCD-SNMP-MIB and its dskTable 
305          */
306
307         pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
308         base_length = add(pdu, "UCD-SNMP-MIB:dskIndex", NULL, 0);
309         memcpy(base, pdu->variables->name, base_length * sizeof(oid));
310
311         vlp = collect(ss, pdu, base, base_length);
312
313         while (vlp) {
314             unsigned long   hssize, hsused;
315             char            descr[SPRINT_MAX_LEN];
316
317             pdu = snmp_pdu_create(SNMP_MSG_GET);
318
319             add(pdu, "UCD-SNMP-MIB:dskPath",
320                 &(vlp->name[base_length]), vlp->name_length - base_length);
321             add(pdu, "UCD-SNMP-MIB:dskTotal",
322                 &(vlp->name[base_length]), vlp->name_length - base_length);
323             add(pdu, "UCD-SNMP-MIB:dskUsed",
324                 &(vlp->name[base_length]), vlp->name_length - base_length);
325
326             status = snmp_synch_response(ss, pdu, &response);
327             if (status != STAT_SUCCESS || !response) {
328                 snmp_sess_perror("snmpdf", ss);
329                 exit(1);
330             }
331
332             vlp2 = response->variables;
333             memcpy(descr, vlp2->val.string, vlp2->val_len);
334             descr[vlp2->val_len] = '\0';
335
336             vlp2 = vlp2->next_variable;
337             hssize = *(vlp2->val.integer);
338
339             vlp2 = vlp2->next_variable;
340             hsused = *(vlp2->val.integer);
341
342             printf("%-18s %15lu %15lu %15lu %4lu%%\n", descr,
343                    units ? hssize * (units / 1024) : hssize,
344                    units ? hsused * (units / 1024) : hsused,
345                    units ? (hssize - hsused) * (units / 1024) : hssize -
346                    hsused, hssize ? 100 * hsused / hssize : hsused);
347
348             vlp = vlp->next_variable;
349             snmp_free_pdu(response);
350             count++;
351         }
352     }
353
354     if (count == 0) {
355         fprintf(stderr, "Failed to locate any partions.\n");
356         exit(1);
357     }
358
359     snmp_close(ss);
360     SOCK_CLEANUP;
361     return 0;
362
363 }                               /* end main() */