2 * snmpget.c - send snmp GET requests to a network entity.
5 /***********************************************************************
6 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
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
25 ******************************************************************/
26 #include <net-snmp/net-snmp-config.h>
39 #include <sys/types.h>
41 #include <netinet/in.h>
45 #if TIME_WITH_SYS_TIME
47 # include <sys/timeb.h>
49 # include <sys/time.h>
54 # include <sys/time.h>
60 #include <sys/select.h>
69 #include <arpa/inet.h>
72 #include <net-snmp/net-snmp-includes.h>
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");
85 "\t-Cu\tUse UCD-SNMP dskTable to do the calculations.\n");
87 "\t\t[Normally the HOST-RESOURCES-MIB is consulted first.]\n");
93 optProc(int argc
, char *const *argv
, int opt
)
104 "Unknown flag passed to -C: %c\n", optarg
[-1]);
111 struct hrStorageTable
{
112 u_long hrStorageIndex
;
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",
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
);
148 netsnmp_variable_list
*
149 collect(netsnmp_session
* ss
, netsnmp_pdu
*pdu
,
150 oid
* base
, size_t base_length
)
152 netsnmp_pdu
*response
;
154 netsnmp_variable_list
*saved
= NULL
, **vlpp
= &saved
;
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
);
166 if (response
&& snmp_oid_compare(response
->variables
->name
,
167 SNMP_MIN(base_length
,
168 response
->variables
->
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
);
188 vlpp
= &((*vlpp
)->next_variable
);
189 response
->variables
= NULL
; /* ahh, forget about it */
191 snmp_free_pdu(response
);
199 main(int argc
, char *argv
[])
201 netsnmp_session session
, *ss
;
203 netsnmp_pdu
*response
;
205 oid base
[MAX_OID_LEN
];
208 netsnmp_variable_list
*saved
= NULL
, *vlp
= saved
, *vlp2
;
212 * get the common command line arguments
214 switch (arg
= snmp_parse_args(argc
, argv
, &session
, "C:", optProc
)) {
227 * Open an SNMP session.
229 ss
= snmp_open(&session
);
232 * diagnose snmp_open errors with the input netsnmp_session pointer
234 snmp_sess_perror("snmpget", &session
);
239 printf("%-18s %15s %15s %15s %5s\n", "Description", "size (kB)",
240 "Used", "Available", "Used%");
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
);
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
);
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
);
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
);
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
);
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
);
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
);
355 fprintf(stderr
, "Failed to locate any partions.\n");