int main(int argc, char *argv[]) { char *varname, *value; if (parse_args (argc, argv)) usage(); /* if (argc > 1) { */ /* if (strcmp(argv[1], "-q") == 0) { */ /* opt_quotes = 1; */ /* if (argc == 2) */ /* opt_prall = 1; */ /* } */ /* } else */ /* opt_prall = 1; */ if (opt_printall) { varname = (char *)readbenv(0); while (varname != NULL) { print_variable (varname, readbenv(2)); varname = (char *)readbenv(1); } } else if (opt_variable != NULL) { value = (char *)getbenv(opt_variable); print_variable (opt_variable, value); } return(0); }
void printDeclList (struct DECLARATION *d, int num) { if (d == NULL) { //fprintf (stderr, "Declaration list does not exist.\n"); return; } declNum = 0; if (num == 0) print_list (&funclist); if (d->prev != NULL) printDeclList (d->prev, num+1); if (d->t == eInt) { fprintf (fp, "int "); print_variable (d->id, true); } else { fprintf (fp, "float "); print_variable (d->id, false); } printIdenList (d->id, 0); fprintf (fp, ";\n"); }
static void print_scenario(Context *ctx) { GList *itr; printf("=== Scenario ===\n"); for (itr = ctx->inputs; itr; itr = itr->next) print_variable(itr->data); for (itr = ctx->outputs; itr; itr = itr->next) print_variable(itr->data); for (itr = ctx->expectations; itr; itr = itr->next) print_expectation(itr->data); printf("====================\n"); }
void print_ins(const int *ins_type, TVariable *addr1, TVariable *addr2, TVariable *addr3) { printf("expr: "); print_ins_type(ins_type); printf(" "); if (*ins_type != INS_CALL) { print_variable(addr1); } else { htab_item *item; item = (htab_item*)addr1; printf(" %s", item->data.function->name); } print_variable(addr2); print_variable(addr3); printf("\n"); }
void print_variable (struct IDENTIFIER *i, bool t) { if (i->prev != NULL) print_variable (i->prev, t); declNum++; if (t == true) fprintf (st, "%10d int%10s%10d Variable\n", declNum, i->ID, i->intnum); else fprintf (st, "%10d float%10s%10d Variable\n", declNum, i->ID, i->intnum); }
std::ostream& node(const ast::abstract::Node& node) { switch(node) { case ast::ExternNode: print_extern(dynamic_cast<const ast::Extern&>(node)); break; case ast::FuncNode: print_func(dynamic_cast<const ast::Func&>(node)); break; case ast::VariableNode: print_variable(dynamic_cast<const ast::Variable&>(node)); break; case ast::ConstantNode: print_constant(dynamic_cast<const ast::Constant&>(node)); break; case ast::CastNode: print_cast(dynamic_cast<const ast::Cast&>(node)); break; case ast::UnOpNode: print_un_op(dynamic_cast<const ast::UnOp&>(node)); break; case ast::BinOpNode: print_bin_op(dynamic_cast<const ast::BinOp&>(node)); break; case ast::CallNode: print_call(dynamic_cast<const ast::Call&>(node)); break; case ast::ReturnNode: print_return(dynamic_cast<const ast::Return&>(node)); break; case ast::BranchNode: print_branch(dynamic_cast<const ast::Branch&>(node)); break; case ast::AssignmentNode: print_assignment(dynamic_cast<const ast::Assignment&>(node)); break; case ast::WhileNode: print_while(dynamic_cast<const ast::While&>(node)); break; case ast::VoidContextNode: print_void_context(dynamic_cast<const ast::VoidContext&>(node)); break; default: stream << "[Unknown Node] ??"; } return stream; }
/*! Show a expression. Set "expand" to 1 if you want variable definitions inside the displayed value expanded. */ bool dbg_cmd_show_exp (char *psz_varname, bool expand) { if (!psz_varname || 0==strlen(psz_varname)) { printf(_("You need to supply a variable name.\n")); return false; } else { variable_t *p_v; variable_set_t *p_set = NULL; variable_set_list_t *p_file_vars = NULL; if (p_stack && p_stack->p_target && p_stack->p_target->name) { const char *psz_target = p_stack->p_target->name; file_t *p_target = lookup_file (psz_target); if (p_target) { initialize_file_variables (p_target, 0); set_file_variables (p_target); p_file_vars = p_target->variables; p_set = p_file_vars->set; } } if (p_set) { p_v = lookup_variable_in_set(psz_varname, strlen(psz_varname), p_set); if (!p_v) /* May be a global variable. */ p_v = lookup_variable (psz_varname, strlen (psz_varname)); } else { p_v = lookup_variable (psz_varname, strlen (psz_varname)); } if (p_v) { if (expand) { print_variable_expand(p_v); } else print_variable(p_v); } else { if (expand) printf("%s\n", variable_expand_set(psz_varname, p_file_vars)); else { try_without_dollar(psz_varname); return false; } } } return true; }
int main(void) { netsnmp_session session; netsnmp_pdu *pdu; netsnmp_variable_list *vars; int status=45,ctr; pdu = (netsnmp_pdu *) calloc(1, sizeof(netsnmp_pdu)); if (pdu) { pdu->version = SNMP_VERSION_1; pdu->command = SNMP_MSG_RESPONSE; pdu->errstat = SNMP_DEFAULT_ERRSTAT; pdu->errindex = SNMP_DEFAULT_ERRINDEX; pdu->securityModel = SNMP_DEFAULT_SECMODEL; pdu->transport_data = NULL; pdu->transport_data_length = 0; pdu->securityNameLen = 0; pdu->contextNameLen = 0; pdu->time = 0; pdu->reqid = snmp_get_next_reqid(); pdu->msgid = snmp_get_next_msgid(); } printf("Message before parsing"); for(ctr=0;ctr<pktlen;ctr++) printf("%x",pkt[ctr]); printf("\n\n"); status=snmp_parse(&session,pdu,pkt,pktlen); printf("Status %d",status); for(vars = pdu->variables; vars; vars = vars->next_variable) { print_variable(vars->name, vars->name_length, vars); printf("printing.."); } return (0); }
/*....................................................................... * Print the value of a variable to a given output stream. * * Input: * sc Script * The parent script. * stream OutputStream * The stream to write to. * var Variable * The variable who's value is to be displayed. * Output: * return int 0 - OK. * 1 - Error. */ int print_variable(Script *sc, OutputStream *output, Variable *var) { /* * Check arguments. */ if(!sc || !var || !output) { lprintf(stderr, "print_variable: Invalid arguments.\n"); return 1; }; /* * Lists are not concrete datatypes, so include special code to * display them. */ if(var->type->is_list) { ListNode *node; /* A node in the list of variables */ /* * Enclose the list in braces. */ if(write_OutputStream(output, "{")) return 1; /* * Display list members separated by commas. */ for(node=LIST_VARIABLE(var)->list->head; node; node=node->next) { Variable *member = (Variable* )node->data; if(print_variable(sc, output, member)) return 1; if(node->next && write_OutputStream(output, ", ")) return 1; }; if(write_OutputStream(output, "}")) return 1; /* * Defer display of concrete datatypes to appropriate datatype * method function. */ } else { if(var->type->dt->print_fn(sc, output, var)) return 1; }; return 0; }
static void snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len) { netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; int status; pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, theoid, theoid_len); status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { for (vars = response->variables; vars; vars = vars->next_variable) { numprinted++; print_variable(vars->name, vars->name_length, vars); } } if (response) { snmp_free_pdu(response); } }
QVariant QSnmp::get(const QString &oid) { struct snmp_pdu *pdu; struct snmp_pdu *response; // oid anOID[MAX_OID_LEN]; u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; int status; struct variable_list *vars; pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid(oid.toUtf8().constData(), anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { qDebug("QSnmp: failed to get an oid"); if (response) snmp_free_pdu(response); return QVariant(); } for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OCTET_STR) { // string QByteArray res(vars->val_len, '\0'); memcpy(res.data(), vars->val.string, vars->val_len); snmp_free_pdu(response); return res; } if (vars->type == ASN_INTEGER) { int res = (int)*vars->val.integer; snmp_free_pdu(response); return res; } print_variable(vars->name, vars->name_length, vars); qDebug("QSnmp: unknown var type %d", vars->type); } snmp_free_pdu(response); return QVariant(); }
void slang_print_function(const slang_function *f, GLboolean body) { GLuint i; #if 0 if (_mesa_strcmp((char *) f->header.a_name, "main") != 0) return; #endif printf("FUNCTION %s ( scope=%p\n", (char *) f->header.a_name, (void *) f->parameters); for (i = 0; i < f->param_count; i++) { print_variable(f->parameters->variables[i], 3); } printf(") param scope = %p\n", (void *) f->parameters); if (body && f->body) slang_print_tree(f->body, 0); }
static void print_entity(const entity_t *entity) { print_indent(); switch (entity->kind) { case ENTITY_FUNCTION: print_function(&entity->function); break; case ENTITY_CONCEPT: print_concept(&entity->concept); break; case ENTITY_VARIABLE: print_variable(&entity->variable); break; case ENTITY_TYPEALIAS: print_typealias(&entity->typealias); break; case ENTITY_CONSTANT: print_constant(&entity->constant); break; case ENTITY_CONCEPT_FUNCTION: case ENTITY_FUNCTION_PARAMETER: case ENTITY_ERROR: // TODO fprintf(out, "some entity of type '%s'\n", get_entity_kind_name(entity->kind)); break; case ENTITY_TYPE_VARIABLE: case ENTITY_LABEL: break; case ENTITY_INVALID: fprintf(out, "invalid entity (%s)\n", get_entity_kind_name(entity->kind)); break; } }
int mp_snmp_values_fetch1(netsnmp_session *ss, const mp_snmp_query_cmd *values) { netsnmp_pdu *request; netsnmp_pdu *response; const netsnmp_variable_list *var; const mp_snmp_query_cmd *vp; int rc; /* * set-up request */ request = snmp_pdu_create(SNMP_MSG_GET); for (vp = values; vp->oid_len && vp->oid; vp++) { snmp_add_null_var(request, vp->oid, vp->oid_len); } /* * commence request */ do { rc = snmp_synch_response(ss, request, &response); if (mp_verbose > 3) printf("snmp_synch_response(): rc=%d\n", rc); /* no result, so something went wrong ... */ if (!response) return STAT_ERROR; if ((rc == STAT_SUCCESS) && (response->errindex == 0)) break; /* rety with fixed request */ request = snmp_fix_pdu(response, SNMP_MSG_GET); snmp_free_pdu(response); response = NULL; } while (request && (rc == STAT_SUCCESS)); /* * process results */ if ((rc == STAT_SUCCESS) && response) { if (response->errstat == SNMP_ERR_NOERROR) { /* * extract values from response */ for(var = response->variables; var; var = var->next_variable) { for (vp = values; vp->oid_len && vp->oid; vp++) { if (snmp_oid_compare(var->name, var->name_length, vp->oid, vp->oid_len) == 0) { if (mp_verbose > 1) print_variable(var->name, var->name_length, var); /* copy value, if not erroneous */ if ((var->type != SNMP_NOSUCHOBJECT) && (var->type != SNMP_NOSUCHINSTANCE) && (var->type != SNMP_ENDOFMIBVIEW)) copy_value(var, vp->type, vp->target_len, vp->target); else if (mp_verbose > 2) printf("OID not available: type=0x%X\n", var->type); /* short-circuit to next result variable */ break; } } } } else if ((ss->version == SNMP_VERSION_1) && (response->errstat == SNMP_ERR_NOSUCHNAME)) { if (mp_verbose > 3) printf("SNMP-V1: end of tree\n"); } else { /* * some other error occured */ if (mp_verbose > 0) printf("SNMP error: respose->errstat = %ld", response->errstat); rc = STAT_ERROR; } } else { /* * no response (i.e. all vars have been removed by * snmp_pid_fixup()) go ahead an assume an error */ rc = STAT_ERROR; } if (response) snmp_free_pdu(response); return rc; }
int mp_snmp_query(netsnmp_session *ss, const mp_snmp_query_cmd *querycmd) { netsnmp_pdu *pdu; netsnmp_pdu *response; netsnmp_variable_list *vars; int status; const mp_snmp_query_cmd *p; pdu = snmp_pdu_create(SNMP_MSG_GET); for(p = querycmd; p->oid_len; p++) { snmp_add_null_var(pdu, p->oid, p->oid_len); } /* Send the SNMP Query */ do { status = snmp_synch_response(ss, pdu, &response); if (mp_verbose > 3) printf("snmp_synch_response() rc=%d\n", status); if (!response) return STAT_ERROR; if (status == STAT_SUCCESS && response->errindex == 0) break; if (mp_verbose > 3) printf(" errindex=%ld\n", response->errindex); pdu = snmp_fix_pdu(response, SNMP_MSG_GET); snmp_free_pdu(response); response = NULL; } while (status == STAT_SUCCESS && pdu); if (!response) return status; /* Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { for(vars = response->variables; vars; vars = vars->next_variable) { if (mp_verbose > 1) print_variable(vars->name, vars->name_length, vars); // Skip non existing vars if (vars->type == SNMP_NOSUCHOBJECT || vars->type == SNMP_NOSUCHINSTANCE || vars->type == SNMP_ENDOFMIBVIEW) continue; for(p = querycmd; p->oid_len; p++) { if (snmp_oid_compare(vars->name, vars->name_length, p->oid, p->oid_len) == 0) { copy_value(vars, p->type, p->target_len, p->target); break; } } } } else if (status != STAT_SUCCESS) { char *err; snmp_error(ss, NULL, NULL, &err); if (response) snmp_free_pdu(response); mp_snmp_deinit(); critical("SNMP Error: %s", err); } if (response) snmp_free_pdu(response); return status; }
int main (int argc, char *argv[]) { netsnmp_session session, *ss; netsnmp_pdu *pdu; netsnmp_pdu *response; netsnmp_variable_list *vars; int arg; int count; int status; int exitval = 0; /* * get the common command line arguments */ switch (arg = snmp_parse_args (argc, argv, &session, "C:", optProc)) { case NETSNMP_PARSE_ARGS_ERROR: exit (1); case NETSNMP_PARSE_ARGS_SUCCESS_EXIT: exit (0); case NETSNMP_PARSE_ARGS_ERROR_USAGE: usage (); exit (1); default: break; } names = argc - arg; if (names < non_repeaters) { fprintf (stderr, "snmpbulkget: need more objects than <nonrep>\n"); exit (1); } namep = name = (struct nameStruct *) calloc (names, sizeof (*name)); while (arg < argc) { namep->name_len = MAX_OID_LEN; if (snmp_parse_oid (argv[arg], namep->name, &namep->name_len) == NULL) { snmp_perror (argv[arg]); exit (1); } arg++; namep++; } SOCK_STARTUP; /* * open an SNMP session */ ss = snmp_open (&session); if (ss == NULL) { /* * diagnose snmp_open errors with the input netsnmp_session pointer */ snmp_sess_perror ("snmpbulkget", &session); SOCK_CLEANUP; exit (1); } /* * create PDU for GETBULK request and add object name to request */ pdu = snmp_pdu_create (SNMP_MSG_GETBULK); pdu->non_repeaters = non_repeaters; pdu->max_repetitions = max_repetitions; /* fill the packet */ for (arg = 0; arg < names; arg++) snmp_add_null_var (pdu, name[arg].name, name[arg].name_len); /* * do the request */ status = snmp_synch_response (ss, pdu, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { /* * check resulting variables */ for (vars = response->variables; vars; vars = vars->next_variable) print_variable (vars->name, vars->name_length, vars); } else { /* * error in response, print it */ if (response->errstat == SNMP_ERR_NOSUCHNAME) { printf ("End of MIB.\n"); } else { fprintf (stderr, "Error in packet.\nReason: %s\n", snmp_errstring (response->errstat)); if (response->errindex != 0) { fprintf (stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && (count != response->errindex); vars = vars->next_variable, count++) /*EMPTY*/; if (vars) fprint_objid (stderr, vars->name, vars->name_length); fprintf (stderr, "\n"); } exitval = 2; } } } else if (status == STAT_TIMEOUT) { fprintf (stderr, "Timeout: No Response from %s\n", session.peername); exitval = 1; } else { /* status == STAT_ERROR */ snmp_sess_perror ("snmpbulkget", ss); exitval = 1; } if (response) snmp_free_pdu (response); snmp_close (ss); SOCK_CLEANUP; return exitval; }
int main(int argc, char ** argv) { netsnmp_session session, *ss; netsnmp_pdu *pdu; netsnmp_pdu *response; oid anOID[MAX_OID_LEN]; size_t anOID_len; netsnmp_variable_list *vars; int status; int count=1; char type='i'; /* * Initialize the SNMP library */ init_snmp("snmpapp_set"); /* * Initialize a "session" that defines who we're going to talk to */ snmp_sess_init( &session ); /* set up defaults */ session.peername = strdup("10.1.5.142"); /* 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("mijoy"); session.securityNameLen = strlen(session.securityName); /* set the security level to authenticated, but not encrypted */ session.securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; /* 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 Net-SNMP 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); } #endif /*-------------------------------------------------------------------------------------------- */ /* #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 = "admin"; */ /* session.community_len = strlen(session.community); */ /* #endif /\* SNMPv1 *\/ */ /* * Open the session */ SOCK_STARTUP; ss = snmp_open(&session); /* establish the session */ if (!ss) { snmp_sess_perror("ack", &session); SOCK_CLEANUP; exit(1); } /* * Create set request and add object names and values. */ pdu = snmp_pdu_create(SNMP_MSG_SET); anOID_len = MAX_OID_LEN; /*------------------------------------- * char oid[40]; * char value[40]; * printf("Enter OID Name : "); * scanf("%s", &oid); * printf("Enter Value : "); * scanf("%s", &value); ---------------------------------------- */ if (!snmp_parse_oid("sPDUOutletCtl.8", anOID, &anOID_len)) { snmp_perror("read_objid failed\n"); SOCK_CLEANUP; exit(1); } if (snmp_parse_oid("sPDUOutletCtl.8", anOID, &anOID_len) == NULL) { snmp_perror("read_objid failed\n"); SOCK_CLEANUP; exit(1); } if (snmp_add_var(pdu, anOID, anOID_len, type, "1")) { snmp_perror("read_objid failed\n"); } /* * 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); } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", session.peername); else snmp_sess_perror("snmpdemoapp", 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() */
int main(int argc, char ** argv) { netsnmp_session session, *ss; netsnmp_pdu *pdu; netsnmp_pdu *response; oid anOID[MAX_OID_LEN]; oid newOID[MAX_OID_LEN]; size_t anOID_len; size_t newOID_len; netsnmp_variable_list *vars; int status; int count=1; int i=1; /* * Initialize the SNMP library */ init_snmp("snmpdemoapp"); /* * Initialize a "session" that defines who we're going to talk to */ snmp_sess_init( &session ); /* set up defaults */ session.peername = strdup("192.168.1.17"); /* set the SNMP version number */ session.version = SNMP_VERSION_1; /* set the SNMPv1 community name used for authentication */ session.community = "public"; session.community_len = strlen(session.community); /* * Open the session */ SOCK_STARTUP; ss = snmp_open(&session); /* establish the session */ if (!ss) { snmp_sess_perror("ack", &session); SOCK_CLEANUP; exit(1); } /* * 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_GETNEXT); anOID_len = MAX_OID_LEN; if (!snmp_parse_oid(".1.3.6.1.2.1.4.20.1.1", anOID, &anOID_len)) { snmp_perror(".1.3.6.1.2.1.4.20.1.1"); SOCK_CLEANUP; exit(1); } #if OTHER_METHODS /* * These are alternatives to the 'snmp_parse_oid' call above, * e.g. specifying the OID by name rather than numerically. */ read_objid(".1.3.6.1.2.1.4.20.1.1", anOID, &anOID_len); 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_IPADDRESS) { int *sp = (int *)malloc(1 + vars->val_len); memcpy(sp, vars->val.string, vars->val_len); sp[vars->val_len] = '\0'; oid *newOID = (oid *)malloc(1+vars->name_length); memcpy(newOID, vars->name, vars->name_length); printf("value #%d is a string: %d.%d.%d.%d \n", count++, sp[0], sp[1], sp[2], sp[3]); 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 if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", session.peername); else snmp_sess_perror("snmpdemoapp", ss); } /* * Clean up: * 1) free the response. */ if (response) snmp_free_pdu(response); pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); newOID_len = MAX_OID_LEN; if (!snmp_parse_oid(".1.3.6.1.2.1.4.20.1.1.127.0.0.1", newOID, &newOID_len)) { snmp_perror(".1.3.6.1.2.1.4.20.1.1"); SOCK_CLEANUP; exit(1); } snmp_add_null_var(pdu, newOID, newOID_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_IPADDRESS) { char *sp = (char *)malloc(1 + vars->val_len); memcpy(sp, vars->val.string, vars->val_len); printf("value #%d is a string: %d.%d.%u.%u\n", count++, sp[0], sp[1], sp[2], sp[3] ); //newOID = vars->name; // oid *newOID = (oid *)malloc(1+vars->name_length); //memcpy(newOID, vars->name, vars->name_length); sp[vars->val_len] = '\0'; 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 if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", session.peername); else snmp_sess_perror("snmpdemoapp", ss); } /* * Clean up: * 1) free the response. * 2) close the session. */ // if (response) // snmp_free_pdu(response); // snmp_free_pdu(pdu); //snmp_close(ss); } /* main() */
/* * Print a description of the network interfaces. */ void intpro(int interval) { oid varname[MAX_OID_LEN], *instance, *ifentry; size_t varname_len; int ifnum, cfg_nnets; oid curifip [4]; struct variable_list *var; struct snmp_pdu *request, *response; int status; int ifindex, oldindex = 0; struct _if_info { int ifindex; char name[128]; char ip[128], route[128]; char ioctets[20], ierrs[20], ooctets[20], oerrs[20], outqueue[20]; int operstatus; u_long netmask; struct in_addr ifip, ifroute; } *if_table, *cur_if; int max_name = 4, max_route = 7, max_ip = 7, max_ioctets = 7, max_ooctets = 7; int i; if (interval) { sidewaysintpr((unsigned)interval); return; } var = getvarbyname(Session, oid_cfg_nnets, sizeof(oid_cfg_nnets) / sizeof(oid)); if (var && var->val.integer) { cfg_nnets = *var->val.integer; snmp_free_var(var); } else { fprintf (stderr, "No response when requesting number of interfaces.\n"); return; } DEBUGMSGTL (("netstat:if", "cfg_nnets = %d\n", cfg_nnets)); memset (curifip, 0, sizeof (curifip)); if_table = (struct _if_info *) calloc (cfg_nnets, sizeof (*if_table)); cur_if = if_table; for (ifnum = 1; ifnum <= cfg_nnets; ifnum++) { register char *cp; request = snmp_pdu_create (SNMP_MSG_GETNEXT); memmove (varname, oid_ipadentaddr, sizeof (oid_ipadentaddr)); varname_len = sizeof (oid_ipadentaddr) / sizeof (oid); instance = varname + 9; memmove (varname + 10, curifip, sizeof (curifip)); *instance = IPIFINDEX; snmp_add_null_var (request, varname, varname_len); *instance = IPADDR; snmp_add_null_var (request, varname, varname_len); *instance = IPNETMASK; snmp_add_null_var (request, varname, varname_len); status = snmp_synch_response (Session, request, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { fprintf (stderr, "SNMP request failed for interface %d, variable %ld out of %d interfaces (IP)\n", ifnum, response->errindex, cfg_nnets); cfg_nnets = ifnum; break; } for (var = response->variables; var; var = var->next_variable) { if (snmp_get_do_debugging()) { print_variable (var->name, var->name_length, var); } switch (var->name [9]) { case IPIFINDEX: ifindex = *var->val.integer; for (cur_if = if_table; cur_if->ifindex != ifindex && cur_if->ifindex != 0; cur_if++); cur_if->ifindex = ifindex; break; case IPADDR: memmove (curifip, var->name+10, sizeof (curifip)); memmove (&cur_if->ifip, var->val.string, sizeof (u_long)); break; case IPNETMASK: memmove (&cur_if->netmask, var->val.string, sizeof (u_long)); } } cur_if->ifroute.s_addr = cur_if->ifip.s_addr & cur_if->netmask; if (cur_if->ifroute.s_addr) strcpy(cur_if->route, netname (cur_if->ifroute, cur_if->netmask)); else strcpy(cur_if->route, "none"); if ((i = strlen(cur_if->route)) > max_route) max_route = i; if (cur_if->ifip.s_addr) strcpy(cur_if->ip, routename (cur_if->ifip)); else strcpy(cur_if->ip, "none"); if ((i = strlen(cur_if->ip)) > max_ip) max_ip = i; snmp_free_pdu (response); memmove (varname, oid_ifname, sizeof(oid_ifname)); varname_len = sizeof(oid_ifname) / sizeof(oid); ifentry = varname + 9; instance = varname + 10; request = snmp_pdu_create (SNMP_MSG_GETNEXT); *instance = oldindex; *ifentry = IFINDEX; snmp_add_null_var (request, varname, varname_len); *ifentry = IFNAME; snmp_add_null_var (request, varname, varname_len); *ifentry = IFOPERSTATUS; snmp_add_null_var (request, varname, varname_len); *ifentry = INOCTETS; snmp_add_null_var (request, varname, varname_len); *ifentry = OUTOCTETS; snmp_add_null_var (request, varname, varname_len); while ((status = snmp_synch_response (Session, request, &response)) == STAT_SUCCESS) { if (response->errstat != SNMP_ERR_NOSUCHNAME) break; if ((request = snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) == NULL) break; snmp_free_pdu(response); } if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { fprintf (stderr, "SNMP request failed for interface %d, variable %ld out of %d interfaces (IF)\n", ifnum, response->errindex, cfg_nnets); cfg_nnets = ifnum; break; } for (var = response->variables; var; var = var->next_variable) { if (snmp_get_do_debugging()) { print_variable (var->name, var->name_length, var); } if (!var->val.integer) continue; switch (var->name [9]) { case IFINDEX: ifindex = *var->val.integer; for (cur_if = if_table; cur_if->ifindex != ifindex && cur_if->ifindex != 0; cur_if++); cur_if->ifindex = ifindex; break; case INOCTETS: sprintf(cur_if->ioctets, "%lu", *var->val.integer); i = strlen(cur_if->ioctets); if (i > max_ioctets) max_ioctets = i; break; case OUTOCTETS: sprintf(cur_if->ooctets, "%lu", *var->val.integer); i = strlen(cur_if->ooctets); if (i > max_ooctets) max_ooctets = i; break; case IFNAME: oldindex = var->name[10]; if (var->val_len >= sizeof(cur_if->name)) var->val_len = sizeof(cur_if->name) - 1; memmove (cur_if->name, var->val.string, var->val_len); cur_if->name [var->val_len] = 0; if ((i = strlen(cur_if->name)+1) > max_name) max_name = i; break; case IFOPERSTATUS: cur_if->operstatus = *var->val.integer; break; } } snmp_free_pdu (response); if (intrface != NULL && strcmp(cur_if->name, intrface) != 0) { cur_if->name [0] = 0; continue; } if (cur_if->operstatus != MIB_IFSTATUS_UP) { cp = strchr(cur_if->name, '\0'); *cp++ = '*'; *cp = '\0'; } } printf("%*.*s %*.*s %*.*s %*.*s %*.*s ", -max_name, max_name, "Name", -max_route, max_route, "Network", -max_ip, max_ip, "Address", max_ioctets, max_ioctets, "Ioctets", max_ooctets, max_ooctets, "Ooctets"); putchar('\n'); for (ifnum = 0, cur_if = if_table; ifnum < cfg_nnets; ifnum++, cur_if++) { if (cur_if->name [0] == 0) continue; printf("%*.*s ", -max_name, max_name, cur_if->name); printf("%*.*s ", -max_route, max_route, cur_if->route); printf("%*.*s ", -max_ip, max_ip, cur_if->ip); printf("%*s %*s", max_ioctets, cur_if->ioctets, max_ioctets, cur_if->ooctets); putchar('\n'); } free(if_table); }
static void print_declaration_statement(const declaration_statement_t *statement) { print_variable(&statement->entity); }
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 = 0; int rc = -1; int running = 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 ("ran151"); /* set up the authentication parameters for talking to the server */ /* set the SNMP version number */ session.version = SNMP_VERSION_2c; /* set the SNMPv1 community name used for authentication */ session.community = "public"; session.community_len = strlen (session.community); /* * Open the session */ SOCK_STARTUP; ss = snmp_open (&session); /* establish the session */ if (!ss) { snmp_perror ("ack"); snmp_log (LOG_ERR, "something horrible happened!!!\n"); exit (2); } /* * Create the PDU for the data for our request. */ read_objid ("HPI-MIB::saHpiEntry", anOID, &anOID_len); while (running) { pdu = snmp_pdu_create (SNMP_MSG_GETNEXT); 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) { if ((vars->type != SNMP_ENDOFMIBVIEW) && (vars->type != SNMP_NOSUCHOBJECT) && (vars->type != SNMP_NOSUCHINSTANCE)) { // // The index is a three tuple. The number pre-index is the // column. We are going to extract the column number and see if // it has increased. If it has, that means we are on the next row // -- which is not something we want to do. if (vars->name[vars->name_length - 4] != 1) { running = 0; continue; } // Or another way - if we have the count, we cna check by that. /* count++; if (count > 3) { running = 0; continue; } */ print_variable (vars->name, vars->name_length, vars); /* * 10:26:54.726810 morbo.val.pok.ibm.com.33372 > ran151.val.pok.ibm.com.snmp: GetNextRequest(28) X:90.1.4.1 (DF) * 10:26:54.729151 ran151.val.pok.ibm.com.snmp > morbo.val.pok.ibm.com.33372: GetResponse(33) X:90.1.4.1.1.0.1.1=0 (DF) */ // We take the response OID - 90.1.4.1.1.0.1.1 and that will be the next // OID we wil lask for. memmove (anOID, vars->name, vars->name_length * sizeof (oid)); anOID_len = vars->name_length; } else // Quit hte loop. running = 0; } /* * FAILURE: print what went wrong! */ } else { 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 (rc); } /* main() */
void print_variable_list(struct variable_list *V ) { print_variable(V->name, V->name_length, V); }
static void parse_args(int argc, char **argv) { int ch, i; while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw", longopts, NULL)) != -1) { switch (ch) { case 'a': aflag++; break; case 'A': Aflag++; break; case 'b': bflag++; break; case 'd': dflag++; break; case 'D': Dflag++; break; case 'g': gflag++; break; case 'H': Hflag++; break; case 'l': lflag++; break; case 'L': Lflag++; break; case 'n': varname = optarg; break; case 'N': Nflag++; break; case 'O': load_opt_flag++; break; case 'p': pflag++; break; case 'R': Rflag++; break; case 't': attrib = strtoul(optarg, NULL, 16); break; case 'u': uflag++; break; case 'w': wflag++; break; case 'f': case 0: errx(1, "unknown or unimplemented option\n"); break; default: usage(); } } argc -= optind; argv += optind; if (argc == 1) varname = argv[0]; if (aflag + Dflag + wflag > 1) { warnx("Can only use one of -a (--append), " "-D (--delete) and -w (--write)"); usage(); } if (aflag + Dflag + wflag > 0 && varname == NULL) { warnx("Must specify a variable for -a (--append), " "-D (--delete) or -w (--write)"); usage(); } if (aflag) append_variable(varname, NULL); else if (Dflag) delete_variable(varname); else if (wflag) write_variable(varname, NULL); else if (Lflag) print_known_guid(); else if (varname) { pflag++; print_variable(varname); } else if (argc > 0) { pflag++; for (i = 0; i < argc; i++) print_variable(argv[i]); } else print_variables(); }
int main(int argc, char ** argv) { netsnmp_session session, *ss; netsnmp_pdu *pdu; netsnmp_pdu *response; oid anOID[MAX_OID_LEN]; size_t anOID_len; netsnmp_variable_list *vars; int status; int count=1; /* * Initialize the SNMP library */ init_snmp("snmpdemoapp"); /* * 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 Net-SNMP 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) { snmp_sess_perror("ack", &session); SOCK_CLEANUP; exit(1); } /* * 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); anOID_len = MAX_OID_LEN; if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len)) { snmp_perror(".1.3.6.1.2.1.1.1.0"); SOCK_CLEANUP; exit(1); } #if OTHER_METHODS /* * These are alternatives to the 'snmp_parse_oid' call above, * e.g. specifying the OID by name rather than numerically. */ read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len); 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 if (status == STAT_TIMEOUT) fprintf(stderr, "Timeout: No response from %s.\n", session.peername); else snmp_sess_perror("snmpdemoapp", 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() */
int main(int argc, char *argv[]) { netsnmp_session session, *ss; netsnmp_pdu *pdu; netsnmp_pdu *response; netsnmp_variable_list *vars; int arg; int count; int current_name = 0; char *names[128]; oid name[MAX_OID_LEN]; size_t name_length; int status; int exitval = 0; /* * get the common command line arguments */ switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) { case -2: exit(0); case -1: usage(); exit(1); default: break; } if (arg >= argc) { fprintf(stderr, "Missing object name\n"); usage(); exit(1); } /* * get the object names */ for (; arg < argc; arg++) names[current_name++] = argv[arg]; SOCK_STARTUP; /* * Open an SNMP session. */ ss = snmp_open(&session); if (ss == NULL) { /* * diagnose snmp_open errors with the input netsnmp_session pointer */ snmp_sess_perror("snmpget", &session); SOCK_CLEANUP; exit(1); } /* * Create PDU for GET request and add object names to request. */ pdu = snmp_pdu_create(SNMP_MSG_GET); for (count = 0; count < current_name; count++) { name_length = MAX_OID_LEN; if (!snmp_parse_oid(names[count], name, &name_length)) { snmp_perror(names[count]); failures++; } else snmp_add_null_var(pdu, name, name_length); } if (failures) { SOCK_CLEANUP; exit(1); } /* * Perform the request. * * If the Get Request fails, note the OID that caused the error, * "fix" the PDU (removing the error-prone OID) and retry. */ 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) print_variable(vars->name, vars->name_length, vars); } else { fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errindex != 0) { fprintf(stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && count != response->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; if (pdu != NULL) { goto retry; } } } /* endif -- SNMP_ERR_NOERROR */ } else if (status == STAT_TIMEOUT) { fprintf(stderr, "Timeout: No Response from %s.\n", session.peername); exitval = 1; } else { /* status == STAT_ERROR */ snmp_sess_perror("snmpget", ss); exitval = 1; } /* endif -- STAT_SUCCESS */ if (response) snmp_free_pdu(response); snmp_close(ss); SOCK_CLEANUP; return exitval; } /* end main() */
int main(int argc, char *argv[]) { struct snmp_session session, *ss; struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; int arg; int count; int current_name = 0; char *names[128]; oid name[MAX_OID_LEN]; int name_length; int status; /* get the common command line arguments */ arg = snmp_parse_args(argc, argv, &session); /* get the object names */ for(; arg < argc; arg++) names[current_name++] = argv[arg]; SOCK_STARTUP; /* open an SNMP session */ snmp_synch_setup(&session); ss = snmp_open(&session); if (ss == NULL){ snmp_perror("snmpget"); SOCK_CLEANUP; exit(1); } /* create PDU for GET request and add object names to request */ pdu = snmp_pdu_create(SNMP_MSG_GET); for(count = 0; count < current_name; count++){ name_length = MAX_OID_LEN; if (!snmp_parse_oid(names[count], name, &name_length)) { fprintf(stderr, "Invalid object identifier: %s\n", names[count]); failures++; } else snmp_add_null_var(pdu, name, name_length); } if (failures) { SOCK_CLEANUP; exit(1); } /* do the request */ 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) print_variable(vars->name, vars->name_length, vars); } else { fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errstat == SNMP_ERR_NOSUCHNAME){ fprintf(stderr, "This name doesn't exist: "); for(count = 1, vars = response->variables; vars && count != response->errindex; vars = vars->next_variable, count++) ; if (vars) fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); } if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) goto retry; } } else if (status == STAT_TIMEOUT){ fprintf(stderr,"Timeout: No Response from %s.\n", session.peername); snmp_close(ss); SOCK_CLEANUP; exit(1); } else { /* status == STAT_ERROR */ snmp_perror("snmpget"); snmp_close(ss); SOCK_CLEANUP; exit(1); } if (response) snmp_free_pdu(response); snmp_close(ss); SOCK_CLEANUP; exit (0); }
int mp_snmp_subtree_query(netsnmp_session *ss, const oid *subtree_oid, const size_t subtree_len, mp_snmp_subtree *subtree) { oid last_oid[MAX_OID_LEN]; size_t last_len; netsnmp_pdu *request = NULL; netsnmp_pdu *response = NULL; netsnmp_variable_list *var; size_t alloc_size = 0; int rc; /* prepare result */ memset(subtree, '\0', sizeof(*subtree)); subtree->vars = NULL; memcpy(last_oid, subtree_oid, subtree_len * sizeof(oid)); last_len = subtree_len; for (;;) { /* * setup request */ if (ss->version == SNMP_VERSION_1) { request = snmp_pdu_create(SNMP_MSG_GETNEXT); } else { request = snmp_pdu_create(SNMP_MSG_GETBULK); request->non_repeaters = 0; request->max_repetitions = 16; } snmp_add_null_var(request, last_oid, last_len); /* * commence request */ if (response) { snmp_free_pdu(response); response = NULL; } if (mp_verbose > 2) { char buf[128]; snprint_objid((char *) &buf, sizeof(buf), last_oid, last_len); printf("Fetching next from OID %s\n", buf); } rc = snmp_synch_response(ss, request, &response); if (mp_verbose > 3) printf("snmp_synch_response(): rc=%d, errstat=%ld\n", rc, response->errstat); if ((rc == STAT_SUCCESS) && response) { if (response->errstat == SNMP_ERR_NOERROR) { /* * loop over results (may only be one result in case of SNMP v1) */ for (var = response->variables; var; var = var->next_variable) { /* * check, if OIDs are incresing to prevent infinite * loop with broken SNMP agents */ if (snmp_oidtree_compare(var->name, var->name_length, last_oid, last_len) < 0) { if (response) snmp_free_pdu(response); mp_snmp_deinit(); critical("SNMP error: OIDs are not incresing"); } /* * terminate, if oid does not belong to subtree anymore */ if ((var->type == SNMP_ENDOFMIBVIEW) || (snmp_oidtree_compare(subtree_oid, subtree_len, var->name, var->name_length) != 0)) { snmp_free_pdu(response); return rc; } if (mp_verbose > 2) print_variable(var->name, var->name_length, var); if (var->type != SNMP_NOSUCHOBJECT || var->type != SNMP_NOSUCHINSTANCE) { if (alloc_size <= subtree->size) { alloc_size += 16; subtree->vars = mp_realloc(subtree->vars, alloc_size * sizeof(netsnmp_variable_list*)); } subtree->vars[subtree->size] = mp_malloc(sizeof(netsnmp_variable_list)); snmp_clone_var(var, subtree->vars[subtree->size]); subtree->size++; } /* * save last fetched oid */ memcpy(last_oid, var->name, var->name_length * sizeof(oid)); last_len = var->name_length; } /* for */ } else if ((ss->version == SNMP_VERSION_1) && (response->errstat == SNMP_ERR_NOSUCHNAME)) { if (mp_verbose > 3) printf("SNMP-V1: end of tree\n"); } else { /* * some other error occured */ if (mp_verbose > 0) printf("SNMP error: respose->errstat = %ld", response->errstat); rc = STAT_ERROR; //goto done; break; } } else { /* no response, assume an error */ rc = STAT_ERROR; break; } } if (response) snmp_free_pdu(response); return rc; }
int main(int argc, char *argv[]) { netsnmp_session session, *ss; netsnmp_pdu *pdu, *response = NULL; netsnmp_variable_list *vars; int arg; int count; int current_name = 0; int current_type = 0; int current_value = 0; char *names[SNMP_MAX_CMDLINE_OIDS]; char types[SNMP_MAX_CMDLINE_OIDS]; char *values[SNMP_MAX_CMDLINE_OIDS]; oid name[MAX_OID_LEN]; size_t name_length; int status; int exitval = 0; putenv(strdup("POSIXLY_CORRECT=1")); /* * get the common command line arguments */ switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) { case -2: exit(0); case -1: usage(); exit(1); default: break; } if (arg >= argc) { fprintf(stderr, "Missing object name\n"); usage(); exit(1); } if ((argc - arg) > 3*SNMP_MAX_CMDLINE_OIDS) { fprintf(stderr, "Too many assignments specified. "); fprintf(stderr, "Only %d allowed in one request.\n", SNMP_MAX_CMDLINE_OIDS); usage(); exit(1); } /* * get object names, types, and values */ for (; arg < argc; arg++) { DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %s %s %s\n", arg,argv[arg], arg+1 < argc ? argv[arg+1] : NULL, arg+2 < argc ? argv[arg+2] : NULL)); names[current_name++] = argv[arg++]; if (arg < argc) { switch (*argv[arg]) { case '=': case 'i': case 'u': case 't': case 'a': case 'o': case 's': case 'x': case 'd': case 'b': #ifdef OPAQUE_SPECIAL_TYPES case 'I': case 'U': case 'F': case 'D': #endif /* OPAQUE_SPECIAL_TYPES */ types[current_type++] = *argv[arg++]; break; default: fprintf(stderr, "%s: Bad object type: %c\n", argv[arg - 1], *argv[arg]); exit(1); } } else { fprintf(stderr, "%s: Needs type and value\n", argv[arg - 1]); exit(1); } if (arg < argc) values[current_value++] = argv[arg]; else { fprintf(stderr, "%s: Needs value\n", argv[arg - 2]); exit(1); } } SOCK_STARTUP; /* * open an SNMP session */ ss = snmp_open(&session); if (ss == NULL) { /* * diagnose snmp_open errors with the input netsnmp_session pointer */ snmp_sess_perror("snmpset", &session); SOCK_CLEANUP; exit(1); } /* * create PDU for SET request and add object names and values to request */ pdu = snmp_pdu_create(SNMP_MSG_SET); for (count = 0; count < current_name; count++) { name_length = MAX_OID_LEN; if (snmp_parse_oid(names[count], name, &name_length) == NULL) { snmp_perror(names[count]); failures++; } else if (snmp_add_var (pdu, name, name_length, types[count], values[count])) { snmp_perror(names[count]); failures++; } } if (failures) { SOCK_CLEANUP; exit(1); } /* * do the request */ status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { if (!quiet) { for (vars = response->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)); if (response->errindex != 0) { fprintf(stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && (count != response->errindex); vars = vars->next_variable, count++); if (vars) fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); } exitval = 2; } } else if (status == STAT_TIMEOUT) { fprintf(stderr, "Timeout: No Response from %s\n", session.peername); exitval = 1; } else { /* status == STAT_ERROR */ snmp_sess_perror("snmpset", ss); exitval = 1; } if (response) snmp_free_pdu(response); snmp_close(ss); SOCK_CLEANUP; return exitval; }
static void handle_frame_stats (GdkFrameClock *frame_clock) { static int num_stats = 0; static double last_print_time = 0; static int frames_since_last_print = 0; static gint64 frame_counter; static gint64 last_handled_frame = -1; static Variable latency = VARIABLE_INIT; double current_time; current_time = g_get_monotonic_time (); if (current_time >= last_print_time + 1000000 * statistics_time) { if (frames_since_last_print) { if (num_stats == 0 && machine_readable) { g_print ("# load_factor frame_rate latency\n"); } num_stats++; if (machine_readable) g_print ("%g ", load_factor); print_double ("Frame rate ", frames_since_last_print / ((current_time - last_print_time) / 1000000.)); print_variable ("Latency", &latency); g_print ("\n"); } last_print_time = current_time; frames_since_last_print = 0; variable_reset (&latency); if (num_stats == max_stats) gtk_main_quit (); } frames_since_last_print++; for (frame_counter = last_handled_frame; frame_counter < gdk_frame_clock_get_frame_counter (frame_clock); frame_counter++) { GdkFrameTimings *timings = gdk_frame_clock_get_timings (frame_clock, frame_counter); GdkFrameTimings *previous_timings = gdk_frame_clock_get_timings (frame_clock, frame_counter - 1); if (!timings || gdk_frame_timings_get_complete (timings)) last_handled_frame = frame_counter; if (timings && gdk_frame_timings_get_complete (timings) && previous_timings && gdk_frame_timings_get_presentation_time (timings) != 0 && gdk_frame_timings_get_presentation_time (previous_timings) != 0) { double display_time = (gdk_frame_timings_get_presentation_time (timings) - gdk_frame_timings_get_presentation_time (previous_timings)) / 1000.; double frame_latency = (gdk_frame_timings_get_presentation_time (previous_timings) - gdk_frame_timings_get_frame_time (previous_timings)) / 1000. + display_time / 2; variable_add_weighted (&latency, frame_latency, display_time); } } }
int main(int argc, char *argv[]) { netsnmp_session session, *ss; netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; int arg; oid name[MAX_OID_LEN]; size_t name_length; oid root[MAX_OID_LEN]; size_t rootlen; int count; int running; int status; int check; int exitval = 0; netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_INCLUDE_REQUESTED); netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "printStatistics", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_PRINT_STATISTICS); netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "dontCheckOrdering", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC); /* * get the common command line arguments */ switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) { case NETSNMP_PARSE_ARGS_ERROR: exit(1); case NETSNMP_PARSE_ARGS_SUCCESS_EXIT: exit(0); case NETSNMP_PARSE_ARGS_ERROR_USAGE: usage(); exit(1); default: break; } /* * get the initial object and subtree */ if (arg < argc) { /* * specified on the command line */ rootlen = MAX_OID_LEN; if (snmp_parse_oid(argv[arg], root, &rootlen) == NULL) { snmp_perror(argv[arg]); exit(1); } } else { /* * use default value */ memmove(root, objid_mib, sizeof(objid_mib)); rootlen = sizeof(objid_mib) / sizeof(oid); } SOCK_STARTUP; /* * open an SNMP session */ ss = snmp_open(&session); if (ss == NULL) { /* * diagnose snmp_open errors with the input netsnmp_session pointer */ snmp_sess_perror("snmpbulkwalk", &session); SOCK_CLEANUP; exit(1); } /* * setup initial object name */ memmove(name, root, rootlen * sizeof(oid)); name_length = rootlen; running = 1; check = !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC); if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_INCLUDE_REQUESTED)) { snmp_get_and_print(ss, root, rootlen); } while (running) { /* * create PDU for GETBULK request and add object name to request */ pdu = snmp_pdu_create(SNMP_MSG_GETBULK); pdu->non_repeaters = non_reps; pdu->max_repetitions = reps; /* fill the packet */ snmp_add_null_var(pdu, name, name_length); /* * do the request */ status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { /* * check resulting variables */ for (vars = response->variables; vars; vars = vars->next_variable) { if ((vars->name_length < rootlen) || (memcmp(root, vars->name, rootlen * sizeof(oid)) != 0)) { /* * not part of this subtree */ running = 0; continue; } numprinted++; print_variable(vars->name, vars->name_length, vars); if ((vars->type != SNMP_ENDOFMIBVIEW) && (vars->type != SNMP_NOSUCHOBJECT) && (vars->type != SNMP_NOSUCHINSTANCE)) { /* * not an exception value */ if (check && snmp_oid_compare(name, name_length, vars->name, vars->name_length) >= 0) { fprintf(stderr, "Error: OID not increasing: "); fprint_objid(stderr, name, name_length); fprintf(stderr, " >= "); fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); running = 0; exitval = 1; } /* * Check if last variable, and if so, save for next request. */ if (vars->next_variable == NULL) { memmove(name, vars->name, vars->name_length * sizeof(oid)); name_length = vars->name_length; } } else { /* * an exception value, so stop */ running = 0; } } } else { /* * error in response, print it */ running = 0; if (response->errstat == SNMP_ERR_NOSUCHNAME) { printf("End of MIB\n"); } else { fprintf(stderr, "Error in packet.\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errindex != 0) { fprintf(stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && count != response->errindex; vars = vars->next_variable, count++) /*EMPTY*/; if (vars) fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); } exitval = 2; } } } else if (status == STAT_TIMEOUT) { fprintf(stderr, "Timeout: No Response from %s\n", session.peername); running = 0; exitval = 1; } else { /* status == STAT_ERROR */ snmp_sess_perror("snmpbulkwalk", ss); running = 0; exitval = 1; } if (response) snmp_free_pdu(response); } if (numprinted == 0 && status == STAT_SUCCESS) { /* * no printed successful results, which may mean we were * pointed at an only existing instance. Attempt a GET, just * for get measure. */ snmp_get_and_print(ss, root, rootlen); } snmp_close(ss); if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_PRINT_STATISTICS)) { printf("Variables found: %d\n", numprinted); } SOCK_CLEANUP; return exitval; }