1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/*
* snmpstatus.c - send snmp GET requests to a network entity.
*
*/
/***********************************************************
Copyright 1988, 1989 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#ifdef linux
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#endif
#include "snmp.h"
#include "snmp_impl.h"
#include "asn1.h"
#include "snmp_client.h"
#include "snmp_api.h"
#include "mib.h"
int snmp_dump_packet = 0;
oid objid_sysDescr[] = {1, 3, 6, 1, 2, 1, 1, 1, 0};
int length_sysDescr = sizeof(objid_sysDescr)/sizeof(oid);
oid objid_sysUpTime[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
int length_sysUpTime = sizeof(objid_sysUpTime)/sizeof(oid);
oid objid_ifOperStatus[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 8};
int length_ifOperStatus = sizeof(objid_ifOperStatus)/sizeof(oid);
oid objid_ifInUCastPkts[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 11};
int length_ifInUCastPkts = sizeof(objid_ifInUCastPkts)/sizeof(oid);
oid objid_ifInNUCastPkts[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 12};
int length_ifInNUCastPkts = sizeof(objid_ifInNUCastPkts)/sizeof(oid);
oid objid_ifOutUCastPkts[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 17};
int length_ifOutUCastPkts = sizeof(objid_ifOutUCastPkts)/sizeof(oid);
oid objid_ifOutNUCastPkts[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 18};
int length_ifOutNUCastPkts = sizeof(objid_ifOutNUCastPkts)/sizeof(oid);
oid objid_ipInReceives[] = {1, 3, 6, 1, 2, 1, 4, 3, 0};
int length_ipInReceives = sizeof(objid_ipInReceives)/sizeof(oid);
oid objid_ipOutRequests[] = {1, 3, 6, 1, 2, 1, 4, 10, 0};
int length_ipOutRequests = sizeof(objid_ipOutRequests)/sizeof(oid);
char *
uptime_string(timeticks, buf)
u_long timeticks;
char *buf;
{
int seconds, minutes, hours, days;
timeticks /= 100;
days = timeticks / (60 * 60 * 24);
timeticks %= (60 * 60 * 24);
hours = timeticks / (60 * 60);
timeticks %= (60 * 60);
minutes = timeticks / 60;
seconds = timeticks % 60;
if (days == 0){
sprintf(buf, "%d:%02d:%02d", hours, minutes, seconds);
} else if (days == 1) {
sprintf(buf, "%d day, %d:%02d:%02d", days, hours, minutes, seconds);
} else {
sprintf(buf, "%d days, %d:%02d:%02d", days, hours, minutes, seconds);
}
return buf;
}
int
main(argc, argv)
int argc;
char *argv[];
{
struct snmp_session session, *ss;
struct snmp_pdu *pdu, *response;
struct variable_list *vars;
int arg;
char *gateway = NULL;
char *community = NULL;
char name[256];
char buf[64];
int good_var, index;
int status, count;
u_long ipackets = 0, opackets = 0, down_interfaces = 0;
u_long ipin = 0, ipout = 0;
u_long uptime = 0;
/*
* usage: snmpstatus gateway-name [community-name]
*/
for(arg = 1; arg < argc; arg++){
if (argv[arg][0] == '-'){
switch(argv[arg][1]){
case 'd':
snmp_dump_packet++;
break;
default:
printf("invalid option: -%c\n", argv[arg][1]);
break;
}
continue;
}
if (gateway == NULL){
gateway = argv[arg];
} else if (community == NULL){
community = argv[arg];
} else {
printf("usage: snmpstatus gateway-name [community-name]\n");
exit(1);
}
}
if (!(gateway)){
printf("usage: snmpstatus gateway-name [community-name]\n");
exit(1);
}
bzero((char *)&session, sizeof(struct snmp_session));
session.peername = gateway;
session.community = (u_char *)community;
if (community == NULL){
session.community_len = SNMP_DEFAULT_COMMUNITY_LEN;
} else {
session.community_len = strlen(community);
}
session.retries = 4;
session.timeout = 500000;
session.authenticator = NULL;
snmp_synch_setup(&session);
ss = snmp_open(&session);
if (ss == NULL){
printf("Couldn't open snmp\n");
exit(-1);
}
strcpy(name, "No System Description Available");
pdu = snmp_pdu_create(GET_REQ_MSG);
snmp_add_null_var(pdu, objid_sysDescr, length_sysDescr);
snmp_add_null_var(pdu, objid_sysUpTime, length_sysUpTime);
snmp_add_null_var(pdu, objid_ipInReceives, length_ipInReceives);
snmp_add_null_var(pdu, objid_ipOutRequests, length_ipOutRequests);
retry:
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS){
if (response->errstat == SNMP_ERR_NOERROR){
for(vars = response->variables; vars; vars = vars->next_variable){
if (vars->name_length == length_sysDescr &&
!bcmp((char *)objid_sysDescr, (char*)vars->name, sizeof(objid_sysDescr))){
bcopy((char *)vars->val.string, name, vars->val_len);
name[vars->val_len] = '\0';
}
if (vars->name_length == length_sysUpTime &&
!bcmp((char *)objid_sysUpTime, (char*)vars->name, sizeof(objid_sysUpTime))){
uptime = *vars->val.integer;
}
if (vars->name_length == length_ipInReceives &&
!bcmp((char *)objid_ipInReceives, (char*)vars->name, sizeof(objid_ipInReceives))){
ipin = *vars->val.integer;
}
if (vars->name_length == length_ipOutRequests &&
!bcmp((char *)objid_ipOutRequests, (char*)vars->name, sizeof(objid_ipOutRequests))){
ipout = *vars->val.integer;
}
}
} else {
printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
if (response->errstat == SNMP_ERR_NOSUCHNAME){
printf("This name doesn't exist: ");
for(count = 1, vars = response->variables; vars && count != response->errindex;
vars = vars->next_variable, count++)
;
if (vars)
print_objid(vars->name, vars->name_length);
printf("\n");
}
if ((pdu = snmp_fix_pdu(response, GET_REQ_MSG)) != NULL)
goto retry;
}
} else if (status == STAT_TIMEOUT){
printf("No Response from %s\n", gateway);
exit(1);
} else { /* status == STAT_ERROR */
printf("An error occurred, Quitting\n");
exit(2);
}
#ifdef HAVE_IPX
printf("[%s]=>[%s] Up: %s\n", (response->address.sa_family == AF_IPX
? ipx_ntoa((ipxaddr *)&response->address)
: inet_ntoa((*(ipaddr *)&response->address).sin_addr)), name,
uptime_string(uptime, buf));
#else
printf("[%s]=>[%s] Up: %s\n",
inet_ntoa((*(ipaddr *)&response->address).sin_addr), name,
uptime_string(uptime, buf));
#endif
if (response)
snmp_free_pdu(response);
pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
snmp_add_null_var(pdu, objid_ifOperStatus, length_ifOperStatus);
snmp_add_null_var(pdu, objid_ifInUCastPkts, length_ifInUCastPkts);
snmp_add_null_var(pdu, objid_ifInNUCastPkts, length_ifInNUCastPkts);
snmp_add_null_var(pdu, objid_ifOutUCastPkts, length_ifOutUCastPkts);
snmp_add_null_var(pdu, objid_ifOutNUCastPkts, length_ifOutNUCastPkts);
good_var = 5;
while(good_var == 5){
good_var = 0;
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS){
if (response->errstat == SNMP_ERR_NOERROR){
pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
index = 0;
for(vars = response->variables; vars; vars = vars->next_variable){
if (index == 0 && vars->name_length >= length_ifOperStatus &&
!bcmp((char *)objid_ifOperStatus, (char *)vars->name,
sizeof(objid_ifOperStatus))){
if (*vars->val.integer != MIB_IFSTATUS_UP)
down_interfaces++;
snmp_add_null_var(pdu, vars->name, vars->name_length);
good_var++;
} else if (index == 1 && vars->name_length >= length_ifInUCastPkts &&
!bcmp((char *)objid_ifInUCastPkts, (char *)vars->name,
sizeof(objid_ifInUCastPkts))){
ipackets += *vars->val.integer;
snmp_add_null_var(pdu, vars->name, vars->name_length);
good_var++;
} else if (index == 2 && vars->name_length >= length_ifInNUCastPkts &&
!bcmp((char *)objid_ifInNUCastPkts, (char *)vars->name,
sizeof(objid_ifInNUCastPkts))){
ipackets += *vars->val.integer;
snmp_add_null_var(pdu, vars->name, vars->name_length);
good_var++;
} else if (index == 3 && vars->name_length >= length_ifOutUCastPkts &&
!bcmp((char *)objid_ifOutUCastPkts, (char *)vars->name,
sizeof(objid_ifOutUCastPkts))){
opackets += *vars->val.integer;
snmp_add_null_var(pdu, vars->name, vars->name_length);
good_var++;
} else if (index == 4 && vars->name_length >= length_ifOutNUCastPkts &&
!bcmp((char *)objid_ifOutNUCastPkts, (char *)vars->name,
sizeof(objid_ifOutNUCastPkts))){
opackets += *vars->val.integer;
snmp_add_null_var(pdu, vars->name, vars->name_length);
good_var++;
}
index++;
}
} else {
printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
if (response->errstat == SNMP_ERR_NOSUCHNAME){
printf("This name doesn't exist: ");
for(count = 1, vars = response->variables; vars && count != response->errindex;
vars = vars->next_variable, count++)
;
if (vars)
print_objid(vars->name, vars->name_length);
printf("\n");
}
}
} else if (status == STAT_TIMEOUT){
printf("No Response from %s\n", gateway);
} else { /* status == STAT_ERROR */
printf("An error occurred, Quitting\n");
}
if (response)
snmp_free_pdu(response);
}
printf("Recv/Trans packets: Interfaces: %ld/%ld | IP: %ld/%ld\n", ipackets, opackets, ipin, ipout);
if (down_interfaces > 0){
printf("%ld interface%s down!\n", down_interfaces, down_interfaces > 1 ? "s are": " is" );
}
return 0;
}
|