int handle_snmp_event(struct oflops_context * ctx, struct snmp_event * se) { char buf[1024]; snprint_variable(buf, sizeof(buf), se->reply->name, se->reply->name_length, se->reply); fprintf(stderr,"SNMP response: %s\n",buf); return 0; }
/* * simple printing of returned data */ int snmpStoreResult (tSession *session, struct snmp_pdu *pdu, char **result) { char buf[RESULTSIZE]; struct variable_list *vp; struct snmp_session *sp = session->sess; unsigned int o = session->current_oid; unsigned int i,j; *result = (char*)calloc(RESULTSIZE+1, sizeof(char)); vp = pdu->variables; if (pdu->errstat == SNMP_ERR_NOERROR) { while (vp) { snprint_value(*result, RESULTSIZE, vp->name, vp->name_length, vp); // tolower & remove spaces and linebreaks for (i=0, j=0; i<strlen(*result); i++) { if ((*result)[i] > 0x22) { if ((*result)[i] >= 0x41 && (*result)[i] <= 0x5A) { (*result)[j] = (*result)[i] + 0x20; } else { (*result)[j] = (*result)[i]; } j++; } } (*result)[j] = 0; // fix lancom snr if (session->oidList.oid[o].id_oidtype == 12 && ( session->oidList.oid[o].id_type == 2 || session->oidList.oid[o].id_type == 5 || session->oidList.oid[o].id_type == 9 || session->oidList.oid[o].id_type == 24 )) { snprintf(*result, RESULTSIZE, "%.0f", atoi(*result) * 0.64); } snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp); syslog(LOG_DEBUG, "snmpStoreResult: %s: %s", sp->peername, buf); vp = vp->next_variable; } return 1; } else { if (vp) { snprint_objid(buf, sizeof(buf), vp->name, vp->name_length); } else { strcpy(buf, "(empty buf)"); } syslog(LOG_ERR, "snmpStoreResult: %s: %s: (%s)", sp->peername, buf, snmp_errstring(pdu->errstat)); if (pdu->errstat == SNMP_ERR_NOSUCHNAME) { snprintf(*result, RESULTSIZE, "U"); return 1; } return 0; } }
void processSnmpGet(char * oid){ read_objid(oid, id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len); int status = snmp_synch_response(session_handle, pdu, &response); for(vars = response->variables; vars; vars = vars->next_variable){ snprint_variable(outbuff, 256, vars->name, vars->name_length, vars); resultString = strrchr(outbuff, ':'); } }
int return_data(int status, struct snmp_session *sp, struct snmp_pdu *pdu) { char buf[1024]; struct variable_list *vp; int ix; char *getdata = NULL; switch (status) { case STAT_SUCCESS: vp = pdu->variables; if (pdu->errstat == SNMP_ERR_NOERROR) { while (vp) { snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp); /*mjpark------------------------------------------*/ getdata = (char *)malloc(1 + vp->val_len); memcpy(getdata, vp->val.string, vp->val_len); getdata[vp->val_len] = '\0'; #if 0 //Data over flow... int nVal; memcpy((void *)&nVal, getdata, 4); #else unsigned long int nVal; memcpy((void *)&nVal, getdata, sizeof(unsigned long int)); #endif gpSnmp->val = nVal; //debug print /* printf("data length(%d), unsigned long int size(%d), int(%d)\n",vp->val_len, sizeof(unsigned long int), sizeof(int)); */ printf("snmpVal(%d), psnmp(%p)\n", nVal, gpSnmp); /*------------------------------------------------*/ vp = vp->next_variable; } free(getdata); } else { for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++); if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length); else strcpy(buf, "(none)"); fprintf(stdout, "%s: %s: %s\n", sp->peername, buf, snmp_errstring(pdu->errstat)); } return 1; case STAT_TIMEOUT: fprintf(stdout, "%s: Timeout\n", sp->peername); return 0; case STAT_ERROR: snmp_perror(sp->peername); return 0; } return 0; }
char *session_query(struct snmp_session *ss, struct snmp_pdu *pdu) { struct snmp_pdu *response; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR ) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL && vars->type <ASN_LONG_LEN) { if (vars->type == ASN_INTEGER) { sprintf(buf,"%ld",*vars->val.integer); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } } else rbuffer = NULL; } 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 */ if (response) snmp_free_pdu(response); // printf("Result : %s\n",rbuffer); return rbuffer; }
char *session_set( struct snmp_session *ss,const char *name, const char *value ) { struct snmp_pdu *pdu, *response; oid anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* create PDU for request */ pdu = snmp_pdu_create(SNMP_MSG_SET); get_node(name, anOID, &anOID_len); snmp_add_var(pdu, anOID, anOID_len,'i',value); // snmp_add_null_var(pdu, anOID, anOID_len); /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL) { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else rbuffer = NULL; } 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("snmpset", ss); } /* Clean up */ /* if (pdu) snmp_free_pdu(pdu); */ if (response) snmp_free_pdu(response); return rbuffer; }
int print_result(int status, struct snmp_session *sp, struct snmp_pdu *pdu) { char buf[1024]; struct variable_list *vp; int ix; struct timeval now; struct timezone tz; struct tm *tm; gettimeofday(&now, &tz); tm = localtime(&now.tv_sec); fprintf(stdout, "%.2d:%.2d:%.2d.%.6d, status: %d, ", tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec, status); switch (status) { case STAT_SUCCESS: vp = pdu->variables; if (pdu->errstat == SNMP_ERR_NOERROR) { while (vp) { snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp); fprintf(stdout, ":%s: %s\n", sp->peername, buf); vp = vp->next_variable; } } else { for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++) ; if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length); else strcpy(buf, "(none)"); fprintf(stdout, ":%s: %s: %s\n", sp->peername, buf, snmp_errstring(pdu->errstat)); } return 1; case STAT_TIMEOUT: fprintf(stdout, ":%s: Timeout\n", sp -> peername); return 0; case STAT_ERROR: snmp_perror(sp->peername); return 0; } return 0; }
int return_data(int status, struct snmp_session *sp, struct snmp_pdu *pdu) /* simple printing of returned data */ { char buf[1024]; struct variable_list *vp; int ix; char *getdata = NULL; unsigned long int nVal; /* unsigned char sVal; */ /* int ipVal; */ switch (status) { case STAT_SUCCESS: vp = pdu->variables; if (pdu->errstat == SNMP_ERR_NOERROR) { while (vp) { snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp); /*mjpark------------------------------------------*/ getdata = (char *)malloc(1 + vp->val_len); memcpy(getdata, vp->val.string, vp->val_len); getdata[vp->val_len] = '\0'; switch(vp->type) { case ASN_OCTET_STR: /* { */ /* memcpy((void *)&sVal, getdata, sizeof(unsigned char)); */ /* gpSnmpstr->val = sVal; */ /* break; */ /* } */ case ASN_GAUGE: case ASN_COUNTER: case ASN_TIMETICKS: case ASN_INTEGER: { memcpy((void *)&nVal, getdata, sizeof(unsigned long int)); gpSnmp->val = nVal; break; } case ASN_IPADDRESS: { /* struct in_addr st_addr; */ /* memcpy((void *)&ipVal, getdata, 4); */ /* st_addr.s_addr = ipVal; */ /* gpSnmpstr->val = inet_ntoa(st_addr); */ break; } } //debug print //printf("data length(%d), unsigned long int size(%d), int(%d)\n",vp->val_len, sizeof(unsigned long int), sizeof(int)); /* printf("snmpVal(%d), psnmp(%p)\n", nVal, gpSnmp); */ /*------------------------------------------------*/ vp = vp->next_variable; } free(getdata); } else { for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++); if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length); else strcpy(buf, "(none)"); fprintf(stdout, "%s: %s: %s\n", sp->peername, buf, snmp_errstring(pdu->errstat)); } return 1; case STAT_TIMEOUT: fprintf(stdout, "%s: Timeout\n", sp->peername); return 0; case STAT_ERROR: snmp_perror(sp->peername); return 0; } return 0; }
static int csnmp_read_value (host_definition_t *host, data_definition_t *data) { struct snmp_pdu *req; struct snmp_pdu *res; struct variable_list *vb; const data_set_t *ds; value_list_t vl = VALUE_LIST_INIT; int status; size_t i; DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name, data->name); if (host->sess_handle == NULL) { DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL"); return (-1); } ds = plugin_get_ds (data->type); if (!ds) { ERROR ("snmp plugin: DataSet `%s' not defined.", data->type); return (-1); } if (ds->ds_num != data->values_len) { ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu", data->type, ds->ds_num, data->values_len); return (-1); } vl.values_len = ds->ds_num; vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len); if (vl.values == NULL) return (-1); for (i = 0; i < vl.values_len; i++) { if (ds->ds[i].type == DS_TYPE_COUNTER) vl.values[i].counter = 0; else vl.values[i].gauge = NAN; } sstrncpy (vl.host, host->name, sizeof (vl.host)); sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin)); sstrncpy (vl.type, data->type, sizeof (vl.type)); sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance)); vl.interval = host->interval; req = snmp_pdu_create (SNMP_MSG_GET); if (req == NULL) { ERROR ("snmp plugin: snmp_pdu_create failed."); sfree (vl.values); return (-1); } for (i = 0; i < data->values_len; i++) snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len); res = NULL; status = snmp_sess_synch_response (host->sess_handle, req, &res); if ((status != STAT_SUCCESS) || (res == NULL)) { char *errstr = NULL; snmp_sess_error (host->sess_handle, NULL, NULL, &errstr); ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s", host->name, (errstr == NULL) ? "Unknown problem" : errstr); if (res != NULL) snmp_free_pdu (res); res = NULL; sfree (errstr); csnmp_host_close_session (host); return (-1); } for (vb = res->variables; vb != NULL; vb = vb->next_variable) { #if COLLECT_DEBUG char buffer[1024]; snprint_variable (buffer, sizeof (buffer), vb->name, vb->name_length, vb); DEBUG ("snmp plugin: Got this variable: %s", buffer); #endif /* COLLECT_DEBUG */ for (i = 0; i < data->values_len; i++) if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len, vb->name, vb->name_length) == 0) vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type, data->scale, data->shift, host->name, data->name); } /* for (res->variables) */ if (res != NULL) snmp_free_pdu (res); res = NULL; DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);"); plugin_dispatch_values (&vl); sfree (vl.values); return (0); } /* int csnmp_read_value */
gchar * snmp_probe(gchar *peer, gint port, gchar *community) { oid sysDescr[MAX_OID_LEN]; size_t sysDescr_length; oid sysObjectID[MAX_OID_LEN]; size_t sysObjectID_length; oid sysUpTime[MAX_OID_LEN]; size_t sysUpTime_length; oid sysContact[MAX_OID_LEN]; size_t sysContact_length; oid sysName[MAX_OID_LEN]; size_t sysName_length; oid sysLocation[MAX_OID_LEN]; size_t sysLocation_length; struct snmp_session session, *ss; struct snmp_pdu *pdu, *response; struct variable_list *vars; int count; int status; char textbuf[1024]; char *result = NULL; char *tmp = NULL; /* transform interesting OIDs */ sysDescr_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysDescr.0", sysDescr, &sysDescr_length)) printf("error parsing oid: system.sysDescr.0\n"); sysObjectID_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysObjectID.0", sysObjectID, &sysObjectID_length)) printf("error parsing oid: system.sysObjectID.0\n"); sysUpTime_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysUpTime.0", sysUpTime, &sysUpTime_length)) printf("error parsing oid: system.sysUpTime.0\n"); sysContact_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysContact.0", sysContact, &sysContact_length)) printf("error parsing oid: system.sysContact.0\n"); sysName_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysName.0", sysName, &sysName_length)) printf("error parsing oid: system.sysName.0\n"); sysLocation_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysLocation.0", sysLocation, &sysLocation_length)) printf("error parsing oid: system.sysLocation.0\n"); /* initialize session to default values */ snmp_sess_init( &session ); session.version = SNMP_VERSION_1; session.community = community; session.community_len = strlen(community); session.peername = peer; #ifdef STREAM session.flags |= SNMP_FLAGS_STREAM_SOCKET; fprintf (stderr, "local port set to: %d\n", session.local_port); #endif /* * Open an SNMP session. */ ss = snmp_open(&session); if (ss == NULL){ fprintf (stderr, "local port set to: %d\n", session.local_port); snmp_sess_perror("snmp_open", &session); exit(1); } /* * Create PDU for GET request and add object names to request. */ pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, sysDescr, sysDescr_length); snmp_add_null_var(pdu, sysObjectID, sysObjectID_length); snmp_add_null_var(pdu, sysUpTime, sysUpTime_length); snmp_add_null_var(pdu, sysContact, sysContact_length); snmp_add_null_var(pdu, sysName, sysName_length); snmp_add_null_var(pdu, sysLocation, sysLocation_length); /* * 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){ /* just render all vars */ for(vars = response->variables; vars; vars = vars->next_variable) { snprint_variable(textbuf, 1023, vars->name, vars->name_length, vars); textbuf[1023] = '\0'; if (result) { tmp = result; result = g_strdup_printf("%s\n%s\n", tmp, textbuf); g_free(tmp); } else { result = g_strdup_printf("%s\n", textbuf); } } } 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++) /*EMPTY*/ ; if (vars) fprint_objid(stderr, vars->name, vars->name_length); fprintf(stderr, "\n"); } /* retry if the errored variable was successfully removed */ 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){ snmp_close(ss); return g_strdup_printf("Timeout: No Response from %s.\n", session.peername); } else { /* status == STAT_ERROR */ fprintf (stderr, "local port set to: %d\n", session.local_port); snmp_sess_perror("STAT_ERROR", ss); snmp_close(ss); return NULL; } /* endif -- STAT_SUCCESS */ if (response) snmp_free_pdu(response); snmp_close(ss); return result; }
/* Handling of results */ static void noit_snmp_log_results(noit_module_t *self, noit_check_t *check, struct snmp_pdu *pdu) { struct check_info *info = check->closure; struct variable_list *vars; struct timeval duration; char buff[128]; stats_t current; int nresults = 0; noit_check_stats_clear(check, ¤t); if(pdu) for(vars = pdu->variables; vars; vars = vars->next_variable) nresults++; gettimeofday(¤t.whence, NULL); sub_timeval(current.whence, check->last_fire_time, &duration); current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; current.available = pdu ? NP_AVAILABLE : NP_UNAVAILABLE; current.state = (nresults == info->noids) ? NP_GOOD : NP_BAD; snprintf(buff, sizeof(buff), "%d/%d gets", nresults, info->noids); current.status = buff; /* We have no results over which to iterate. */ if(!pdu) { noit_check_set_stats(check, ¤t); return; } /* manipulate the information ourselves */ nresults = 0; for(vars = pdu->variables; vars; vars = vars->next_variable) { char *sp; int oid_idx; double float_conv; u_int64_t u64; int64_t i64; char *endptr; char varbuff[256]; /* find the oid to which this is the response */ oid_idx = nresults; /* our current idx is the most likely */ if(info->oids[oid_idx].oidlen != vars->name_length || memcmp(info->oids[oid_idx].oid, vars->name, vars->name_length * sizeof(oid))) { /* Not the most obvious guess */ for(oid_idx = info->noids - 1; oid_idx >= 0; oid_idx--) { if(info->oids[oid_idx].oidlen == vars->name_length && memcmp(info->oids[oid_idx].oid, vars->name, vars->name_length * sizeof(oid))) break; } } if(oid_idx < 0) { snprint_variable(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); noitL(nlerr, "Unexpected oid results to %s`%s`%s: %s\n", check->target, check->module, check->name, varbuff); nresults++; continue; } #define SETM(a,b) noit_stats_set_metric(check, ¤t, \ info->oids[oid_idx].confname, a, b) if(info->oids[oid_idx].type_should_override) { snprint_value(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); sp = strchr(varbuff, ' '); if(sp) sp++; noit_stats_set_metric_coerce(check, ¤t, info->oids[oid_idx].confname, info->oids[oid_idx].type_override, sp); } else { switch(vars->type) { case ASN_OCTET_STR: sp = malloc(1 + vars->val_len); memcpy(sp, vars->val.string, vars->val_len); sp[vars->val_len] = '\0'; SETM(METRIC_STRING, sp); free(sp); break; case ASN_INTEGER: case ASN_GAUGE: SETM(METRIC_INT32, vars->val.integer); break; case ASN_TIMETICKS: case ASN_COUNTER: SETM(METRIC_UINT32, vars->val.integer); break; case ASN_INTEGER64: printI64(varbuff, vars->val.counter64); i64 = strtoll(varbuff, &endptr, 10); SETM(METRIC_INT64, (varbuff == endptr) ? NULL : &i64); break; case ASN_COUNTER64: printU64(varbuff, vars->val.counter64); u64 = strtoull(varbuff, &endptr, 10); SETM(METRIC_UINT64, (varbuff == endptr) ? NULL : &u64); break; case ASN_FLOAT: if(vars->val.floatVal) float_conv = *(vars->val.floatVal); SETM(METRIC_DOUBLE, vars->val.floatVal ? &float_conv : NULL); break; case ASN_DOUBLE: SETM(METRIC_DOUBLE, vars->val.doubleVal); break; case SNMP_NOSUCHOBJECT: case SNMP_NOSUCHINSTANCE: SETM(METRIC_STRING, NULL); break; default: snprint_variable(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); /* Advance passed the first space and use that unless there * is no space or we have no more string left. */ sp = strchr(varbuff, ' '); if(sp) sp++; SETM(METRIC_STRING, (sp && *sp) ? sp : NULL); } } nresults++; } noit_check_set_stats(check, ¤t); }