- 论坛徽章:
- 0
|
这样的,我在机器上安装了net-snmp的最新版本,5.2,然后用它提供的APIs来编程,写一些管理进程的应用。但是处理一个最简单的例子时,遇到了一个困难,下面是net-snmp本身提供的例子的源代码:
- #include <net-snmp/net-snmp-config.h>;
- #include <net-snmp/net-snmp-includes.h>;
- #include <string.h>;
- #include <stdlib.h>;
- #include <stdio.h>;
- /* change the word "define" to "undef" to try the (insecure) SNMPv1 version */
- #define DEMO_USE_SNMP_VERSION_3
- #ifdef DEMO_USE_SNMP_VERSION_3
- const char *our_v3_passphrase = "The UCD Demo Password";
- #endif
- int
- main (int argc, char **argv)
- {
- struct snmp_session session, *ss;
- struct snmp_pdu *pdu;
- struct snmp_pdu *response;
- oid anOID[MAX_OID_LEN];
- size_t anOID_len = MAX_OID_LEN;
- struct variable_list *vars;
- int status;
- int count = 1;
- /*
- * Initialize the SNMP library
- */
- init_snmp ("snmpapp");
- /*
- * Initialize a "session" that defines who we're going to talk to
- */
- snmp_sess_init (&session); /* set up defaults */
- session.peername = strdup ("test.net-snmp.org");
- /* set up the authentication parameters for talking to the server */
- #ifdef DEMO_USE_SNMP_VERSION_3
- /* Use SNMPv3 to talk to the experimental server */
- /* set the SNMP version number */
- session.version = SNMP_VERSION_3;
- /* set the SNMPv3 user name */
- session.securityName = strdup ("MD5User");
- session.securityNameLen = strlen (session.securityName);
- /* set the security level to authenticated, but not encrypted */
- session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
- /* set the authentication method to MD5 */
- session.securityAuthProto = usmHMACMD5AuthProtocol;
- session.securityAuthProtoLen =
- sizeof (usmHMACMD5AuthProtocol) / sizeof (oid);
- session.securityAuthKeyLen = USM_AUTH_KU_LEN;
- /* set the authentication key to a MD5 hashed version of our
- passphrase "The UCD Demo Password" (which must be at least 8
- characters long) */
- if (generate_Ku (session.securityAuthProto,
- session.securityAuthProtoLen,
- (u_char *) our_v3_passphrase, strlen (our_v3_passphrase),
- session.securityAuthKey,
- &session.securityAuthKeyLen) != SNMPERR_SUCCESS)
- {
- snmp_perror (argv[0]);
- snmp_log (LOG_ERR,
- "Error generating Ku from authentication pass phrase. \n");
- exit (1);
- }
- #else /* we'll use the insecure (but simplier) SNMPv1 */
- /* set the SNMP version number */
- session.version = SNMP_VERSION_1;
- /* set the SNMPv1 community name used for authentication */
- session.community = "demopublic";
- session.community_len = strlen (session.community);
- #endif /* SNMPv1 */
- /*
- * Open the session
- */
- SOCK_STARTUP;
- ss = snmp_open (&session); /* establish the session */
- if (!ss)/*看这里,ss的返回值总是NULL,所以通不过*/
- {
- snmp_perror ("ack");
- snmp_log (LOG_ERR, "something horrible happened!!!\n");
- exit (2);
- }
- /*
- * Create the PDU for the data for our request.
- * 1) We're going to GET the system.sysDescr.0 node.
- */
- pdu = snmp_pdu_create (SNMP_MSG_GET);
- read_objid (".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
- #if OTHER_METHODS
- get_node ("sysDescr.0", anOID, &anOID_len);
- read_objid ("system.sysDescr.0", anOID, &anOID_len);
- #endif
- snmp_add_null_var (pdu, anOID, anOID_len);
- /*
- * Send the Request out.
- */
- status = snmp_synch_response (ss, pdu, &response);
- /*
- * Process the response.
- */
- if (status == STAT_SUCCESS && response->;errstat == SNMP_ERR_NOERROR)
- {
- /*
- * SUCCESS: Print the result variables
- */
- for (vars = response->;variables; vars; vars = vars->;next_variable)
- print_variable (vars->;name, vars->;name_length, vars);
- /* manipuate the information ourselves */
- for (vars = response->;variables; vars; vars = vars->;next_variable)
- {
- if (vars->;type == ASN_OCTET_STR)
- {
- char *sp = (char *) malloc (1 + vars->;val_len);
- memcpy (sp, vars->;val.string, vars->;val_len);
- sp[vars->;val_len] = '\0';
- printf ("value #%d is a string: %s\n", count++, sp);
- free (sp);
- }
- else
- printf ("value #%d is NOT a string! Ack!\n", count++);
- }
- }
- else
- {
- /*
- * FAILURE: print what went wrong!
- */
- if (status == STAT_SUCCESS)
- fprintf (stderr, "Error in packet\nReason: %s\n",
- snmp_errstring (response->;errstat));
- else
- snmp_sess_perror ("snmpget", ss);
- }
- /*
- * Clean up:
- * 1) free the response.
- * 2) close the session.
- */
- if (response)
- snmp_free_pdu (response);
- snmp_close (ss);
- SOCK_CLEANUP;
- return (0);
- } /* main() */
复制代码
上面我标出了ss = snmp_open(&session)一句,我试了好多次,返回值总是NULL。本来正确的返回值应该是新malloc了一个struct snmp_session的指针,这肯定是snmp_open()中调用了malloc函数。唉,说不清楚,看看输出吧:
- [root@localhost example]# ./snmpdemoapp
- ack: Out of memory (malloc failure)
- something horrible happened!!!
复制代码
Out of memory这句不是我打印的,定是某个net-snmp提供的函数间接打印的,但是我find了net-snmp的源码包,没找到snmp_open()函数的源代码,而且/usr/include/net-snmp/目录下也找不到,只有ldd能显示用了那些库,但是又没办法看。
另外,程序中的agent指定为test.net-snmp.org是没问题的,我用snmpget、snmpwalk等程序试验过,都好使的。
大侠救命啊!![list=] |
|