dnscrypto-proxy: Update to release 1.3.0
[tomato.git] / release / src / router / snmp / apps / snmpdf.c
blob0ed23ce69c82770cbf27057536bb578a0f5bdba1
1 /*
2 * snmpget.c - send snmp GET requests to a network entity.
4 */
5 /***********************************************************************
6 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
8 All Rights Reserved
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.
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>
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
72 #include <net-snmp/net-snmp-includes.h>
74 int failures = 0;
76 void
77 usage(void)
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");
90 int ucd_mib = 0;
92 static void
93 optProc(int argc, char *const *argv, int opt)
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);
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;
121 add(netsnmp_pdu *pdu, const char *mibnodename,
122 oid * index, size_t indexlen)
124 oid base[MAX_OID_LEN];
125 size_t base_length = MAX_OID_LEN;
127 memset(base, 0, MAX_OID_LEN * sizeof(oid));
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);
136 if (index && indexlen) {
137 memcpy(&(base[base_length]), index, indexlen * sizeof(oid));
138 base_length += indexlen;
140 DEBUGMSGTL(("add", "created: "));
141 DEBUGMSGOID(("add", base, base_length));
142 DEBUGMSG(("add", "\n"));
143 snmp_add_null_var(pdu, base, base_length);
145 return base_length;
148 netsnmp_variable_list *
149 collect(netsnmp_session * ss, netsnmp_pdu *pdu,
150 oid * base, size_t base_length)
152 netsnmp_pdu *response;
153 int running = 1;
154 netsnmp_variable_list *saved = NULL, **vlpp = &saved;
155 int status;
157 while (running) {
159 * gotta catch em all, gotta catch em all!
161 status = snmp_synch_response(ss, pdu, &response);
162 if (status != STAT_SUCCESS || !response) {
163 snmp_sess_perror("snmpdf", ss);
164 exit(1);
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 {
174 * get response
176 *vlpp = response->variables;
177 (*vlpp)->next_variable = NULL; /* shouldn't be any, but just in case */
180 * create the next request
182 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
183 snmp_add_null_var(pdu, (*vlpp)->name, (*vlpp)->name_length);
186 * finish loop setup
188 vlpp = &((*vlpp)->next_variable);
189 response->variables = NULL; /* ahh, forget about it */
191 snmp_free_pdu(response);
193 return saved;
199 main(int argc, char *argv[])
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;
212 * get the common command line arguments
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;
224 SOCK_STARTUP;
227 * Open an SNMP session.
229 ss = snmp_open(&session);
230 if (ss == NULL) {
232 * diagnose snmp_open errors with the input netsnmp_session pointer
234 snmp_sess_perror("snmpget", &session);
235 SOCK_CLEANUP;
236 exit(1);
239 printf("%-18s %15s %15s %15s %5s\n", "Description", "size (kB)",
240 "Used", "Available", "Used%");
241 if (ucd_mib == 0) {
243 * * Begin by finding all the storage pieces that are of
244 * * type hrStorageFixedDisk, which is a standard disk.
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));
251 vlp = collect(ss, pdu, base, base_length);
253 while (vlp) {
254 size_t units;
255 unsigned long hssize, hsused;
256 char descr[SPRINT_MAX_LEN];
258 pdu = snmp_pdu_create(SNMP_MSG_GET);
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);
269 status = snmp_synch_response(ss, pdu, &response);
270 if (status != STAT_SUCCESS || !response) {
271 snmp_sess_perror("snmpdf", ss);
272 exit(1);
275 vlp2 = response->variables;
276 memcpy(descr, vlp2->val.string, vlp2->val_len);
277 descr[vlp2->val_len] = '\0';
279 vlp2 = vlp2->next_variable;
280 units = vlp2->val.integer ? *(vlp2->val.integer) : 0;
282 vlp2 = vlp2->next_variable;
283 hssize = vlp2->val.integer ? *(vlp2->val.integer) : 0;
285 vlp2 = vlp2->next_variable;
286 hsused = vlp2->val.integer ? *(vlp2->val.integer) : 0;
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);
294 vlp = vlp->next_variable;
295 snmp_free_pdu(response);
296 count++;
300 if (count == 0) {
301 size_t units = 0;
303 * the host resources mib must not be supported. Lets try the
304 * UCD-SNMP-MIB and its dskTable
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));
311 vlp = collect(ss, pdu, base, base_length);
313 while (vlp) {
314 unsigned long hssize, hsused;
315 char descr[SPRINT_MAX_LEN];
317 pdu = snmp_pdu_create(SNMP_MSG_GET);
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);
326 status = snmp_synch_response(ss, pdu, &response);
327 if (status != STAT_SUCCESS || !response) {
328 snmp_sess_perror("snmpdf", ss);
329 exit(1);
332 vlp2 = response->variables;
333 memcpy(descr, vlp2->val.string, vlp2->val_len);
334 descr[vlp2->val_len] = '\0';
336 vlp2 = vlp2->next_variable;
337 hssize = *(vlp2->val.integer);
339 vlp2 = vlp2->next_variable;
340 hsused = *(vlp2->val.integer);
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);
348 vlp = vlp->next_variable;
349 snmp_free_pdu(response);
350 count++;
354 if (count == 0) {
355 fprintf(stderr, "Failed to locate any partions.\n");
356 exit(1);
359 snmp_close(ss);
360 SOCK_CLEANUP;
361 return 0;
363 } /* end main() */