|
|
@@ -1,9 +1,19 @@
|
|
|
-
|
|
|
#include <net-snmp/net-snmp-config.h>
|
|
|
#include <net-snmp/net-snmp-includes.h>
|
|
|
-//#include <net-snmp/varbind.h>
|
|
|
#include "snmp_nms.h"
|
|
|
#include "sys.h"
|
|
|
+#include "elog.h"
|
|
|
+
|
|
|
+#if 0
|
|
|
+ #define LOGD log_d
|
|
|
+ #define LOGE log_e
|
|
|
+ #define LOGW log_w
|
|
|
+#else
|
|
|
+ #define LOGD printf
|
|
|
+ #define LOGE printf
|
|
|
+ #define LOGW printf
|
|
|
+#endif
|
|
|
+
|
|
|
|
|
|
#define NETSNMP_DS_APP_DONT_FIX_PDUS 0
|
|
|
|
|
|
@@ -23,45 +33,43 @@ int snmp_nms_init(void)
|
|
|
|
|
|
|
|
|
//snmp_get("public","192.168.1.133","1.3.6.1.2.1.1.1.0");
|
|
|
-static char* snmp_get(char *community, char *ip, char *oid_array, int msgtype)
|
|
|
+static char* snmp_getx(int version, char *community, char *ip, char *oid_str, int msgtype)
|
|
|
{
|
|
|
int count;
|
|
|
- int current_name = 0;
|
|
|
- char *names[SNMP_MAX_CMDLINE_OIDS];
|
|
|
oid name[MAX_OID_LEN];
|
|
|
- size_t name_length;
|
|
|
+ size_t name_length=MAX_OID_LEN;
|
|
|
int status;
|
|
|
- int exitval = 0;
|
|
|
- netsnmp_pdu *pdu;
|
|
|
- netsnmp_pdu *response;
|
|
|
+ netsnmp_pdu *pdu=NULL;
|
|
|
+ netsnmp_pdu *rsp=NULL;
|
|
|
netsnmp_variable_list *vars=NULL;
|
|
|
-
|
|
|
- netsnmp_session *ss;
|
|
|
+ netsnmp_session *ss=NULL;
|
|
|
netsnmp_session session;
|
|
|
|
|
|
snmp_sess_init(&session);
|
|
|
- session.version = SNMP_VERSION_2c;
|
|
|
+ session.version = version;
|
|
|
session.peername = ip;
|
|
|
session.community = (unsigned char*)community;
|
|
|
session.community_len = strlen(community);
|
|
|
-
|
|
|
+
|
|
|
ss = snmp_open(&session);
|
|
|
if (ss == NULL) {
|
|
|
+ LOGE("___snmp_open failed\n");
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- names[0] = oid_array;
|
|
|
- current_name = 1;
|
|
|
-
|
|
|
pdu = snmp_pdu_create(msgtype);
|
|
|
+ if(!pdu) {
|
|
|
+ LOGE("___snmp_pdu_create failed\n");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
if(msgtype==SNMP_MSG_GETBULK) {
|
|
|
- pdu->errstat = 10;//non_repeaters;
|
|
|
+ pdu->errstat = 0;//non_repeaters;
|
|
|
pdu->errindex = 10;//max_repetitions;
|
|
|
snmp_add_null_var(pdu, name, name_length);
|
|
|
}
|
|
|
else {
|
|
|
- name_length = MAX_OID_LEN;
|
|
|
- if (!snmp_parse_oid(oid_array, name, &name_length)) {
|
|
|
+ if (!snmp_parse_oid(oid_str, name, &name_length)) {
|
|
|
//failures++;
|
|
|
}
|
|
|
else {
|
|
|
@@ -70,36 +78,34 @@ static char* snmp_get(char *community, char *ip, char *oid_array, int msgtype)
|
|
|
}
|
|
|
|
|
|
retry:
|
|
|
- status = snmp_synch_response(ss, pdu, &response);
|
|
|
+ status = snmp_synch_response(ss, pdu, &rsp);
|
|
|
if (status == STAT_SUCCESS) {
|
|
|
- if (response->errstat == SNMP_ERR_NOERROR) {
|
|
|
- for (vars = response->variables; vars;
|
|
|
+ if (rsp->errstat == SNMP_ERR_NOERROR) {
|
|
|
+ for (vars = rsp->variables; vars;
|
|
|
vars = vars->next_variable)
|
|
|
print_variable(vars->name, vars->name_length, vars);
|
|
|
|
|
|
} else {
|
|
|
- fprintf(stderr, "Error in packet\nReason: %s\n",
|
|
|
- snmp_errstring(response->errstat));
|
|
|
+ LOGE("Error in packet, Reason: %s\n", snmp_errstring(rsp->errstat));
|
|
|
|
|
|
- if (response->errindex != 0) {
|
|
|
+ if (rsp->errindex != 0) {
|
|
|
fprintf(stderr, "Failed object: ");
|
|
|
- for (count = 1, vars = response->variables;
|
|
|
- vars && count != response->errindex;
|
|
|
+ for (count = 1, vars = rsp->variables;
|
|
|
+ vars && count != rsp->errindex;
|
|
|
vars = vars->next_variable, count++)
|
|
|
- /*EMPTY*/;
|
|
|
+
|
|
|
if (vars) {
|
|
|
fprint_objid(stderr, vars->name, vars->name_length);
|
|
|
- }
|
|
|
+ }
|
|
|
fprintf(stderr, "\n");
|
|
|
}
|
|
|
- exitval = 2;
|
|
|
|
|
|
//retry if the errored variable was successfully removed
|
|
|
if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
|
|
|
NETSNMP_DS_APP_DONT_FIX_PDUS)) {
|
|
|
- pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
|
|
|
- snmp_free_pdu(response);
|
|
|
- response = NULL;
|
|
|
+ pdu = snmp_fix_pdu(rsp, SNMP_MSG_GET);
|
|
|
+ snmp_free_pdu(rsp);
|
|
|
+ rsp = NULL;
|
|
|
if (pdu != NULL) {
|
|
|
goto retry;
|
|
|
}
|
|
|
@@ -107,50 +113,42 @@ static char* snmp_get(char *community, char *ip, char *oid_array, int msgtype)
|
|
|
}
|
|
|
|
|
|
} else if (status == STAT_TIMEOUT) {
|
|
|
- fprintf(stderr, "Timeout: No Response from %s.\n", session.peername);
|
|
|
- exitval = 1;
|
|
|
-
|
|
|
+ LOGE("Timeout: No Response from %s.\n", session.peername);
|
|
|
} else { /* status == STAT_ERROR */
|
|
|
snmp_sess_perror("snmpget", ss);
|
|
|
- exitval = 1;
|
|
|
}
|
|
|
|
|
|
- if (response) {
|
|
|
- snmp_free_pdu(response);
|
|
|
+ if (rsp) {
|
|
|
+ snmp_free_pdu(rsp);
|
|
|
}
|
|
|
|
|
|
snmp_close(ss);
|
|
|
- netsnmp_cleanup_session(&session);
|
|
|
+ //netsnmp_cleanup_session(&session);
|
|
|
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
-//SNMP_VERSION_1 SNMP_VERSION_2c SNMP_VERSION_3
|
|
|
-//SNMP_MSG_GET SNMP_MSG_GETNEXT SNMP_MSG_GETBULK SNMP_MSG_INFORM SNMP_MSG_SET
|
|
|
-char* snmp_nms_get(snmp_req_t *req)
|
|
|
+/*
|
|
|
+ SNMP_VERSION_1 SNMP_VERSION_2c SNMP_VERSION_3
|
|
|
+ SNMP_MSG_GET SNMP_MSG_GETNEXT SNMP_MSG_GETBULK SNMP_MSG_INFORM SNMP_MSG_SET
|
|
|
+
|
|
|
+ 1.get:获取一个指定OID的信息;
|
|
|
+ 2.getnext:指定最终节点的上一层,获取该层及以下子类的信息;
|
|
|
+ 3.getbulk:和getnext类似,但是getnext只能指定最终层的父节点,而getbulk没有限制;
|
|
|
+ 4.set:向对方发起控制指令;
|
|
|
+ 5.getresponse:获取回应;
|
|
|
+*/
|
|
|
+static char* snmp_get(int version, char *community, char *ip, char *oid)
|
|
|
{
|
|
|
- if(!req) {
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- return snmp_get(req->community, req->ip, req->oid, SNMP_MSG_GET);
|
|
|
+ return snmp_getx(version, community, ip, oid, SNMP_MSG_GET);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-char* snmp_nms_getnext(snmp_req_t *req)
|
|
|
+static char* snmp_getnext(int version, char *community, char *ip, char *oid)
|
|
|
{
|
|
|
- if(!req) {
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- return snmp_get(req->community, req->ip, req->oid, SNMP_MSG_GETNEXT);
|
|
|
+ return snmp_getx(version, community, ip, oid, SNMP_MSG_GETNEXT);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-char* snmp_nms_getbulk(snmp_req_t *req)
|
|
|
+static char* snmp_getbulk(int version, char *community, char *ip, char *oid)
|
|
|
{
|
|
|
- if(!req) {
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- return snmp_get(req->community, req->ip, req->oid, SNMP_MSG_GETBULK);
|
|
|
+ return snmp_getx(version, community, ip, oid, SNMP_MSG_GETBULK);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -164,10 +162,28 @@ typedef struct {
|
|
|
switch_port_t port[SW_PORT_MAX];
|
|
|
}switch_data_t;
|
|
|
|
|
|
-
|
|
|
-int switch_port_scan(switch_data_t *sw)
|
|
|
+int snmp_nms_test(void)
|
|
|
{
|
|
|
- //snmp_get();
|
|
|
+ int i,j;
|
|
|
+ char *ipAddr=".1.3.6.1.2.1.4.22.1.3.0";
|
|
|
+ char *phyAddr=".1.3.6.1.2.1.4.22.1.2.0";
|
|
|
+ char *icmp="1.3.6.1.2.1.5";
|
|
|
+ char temp[100];
|
|
|
+
|
|
|
+ SOCK_STARTUP;
|
|
|
+
|
|
|
+#if 0
|
|
|
+ for(i=0; i<SW_PORT_MAX; i++) {
|
|
|
+ sprintf(temp, "%s.%d", phyAddr, i+99329); //LOGD("getnext: %s\n", temp);
|
|
|
+ snmp_get(SNMP_VERSION_1, "public", "192.168.0.1", temp);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ snmp_get(SNMP_VERSION_1, "public", "192.168.0.1", ipAddr);
|
|
|
+ snmp_get(SNMP_VERSION_1, "public", "192.168.0.1", phyAddr);
|
|
|
+
|
|
|
+ SOCK_CLEANUP;
|
|
|
+ exit(0);
|
|
|
|
|
|
return 0;
|
|
|
}
|