示例#1
0
文件: gsnmpgo.go 项目: soniah/gsnmpgo
// querySync - do an gsnmp library sync_* query
//
// Results are returned in C form, use convertResults() to convert to a Go struct.
func querySync(session *_Ctype_GNetSnmp, vbl *_Ctype_GList, uritype _Ctype_GNetSnmpUriType,
	version SnmpVersion) (*_Ctype_GList, error) {
	var gerror *C.GError
	var out *_Ctype_GList

	if Debug {
		applog.Debugf("Starting a %s", uritype)
	}
	switch UriType(uritype) {
	case GNET_SNMP_URI_GET:
		out = C.gnet_snmp_sync_get(session, vbl, &gerror)
	case GNET_SNMP_URI_NEXT:
		out = C.gnet_snmp_sync_getnext(session, vbl, &gerror)
	case GNET_SNMP_URI_WALK:
		out = C.gnet_snmp_sync_walk(session, vbl, &gerror)
		/* TODO gnet_snmp_sync_walk is just a series of 'getnexts'
		if version == GNET_SNMP_V1 {
			out = C.gnet_snmp_sync_walk(session, vbl, &gerror)
		} else {
			// do a proper bulkwalk
		}
		*/
	default:
		return nil, fmt.Errorf("%s: querySync(): unknown uritype", libname())
	}

	/*
		Originally error handling was done at this point, like
		gsnmp-0.3.0/examples/gsnmp-get.c. However in production too many results
		were being discarded. Hence just return out, and convertResults() will
		convert any errors in out to nil values.
	*/

	return out, nil
}
示例#2
0
// Do an gsnmp library sync_* query
//
// Results are returned in C form, use convertResults() to convert to a Go struct.
func querySync(session *_Ctype_GNetSnmp, vbl *_Ctype_GList,
	uritype _Ctype_GNetSnmpUriType) (*_Ctype_GList, error) {
	var gerror *C.GError
	var out *_Ctype_GList

	switch UriType(uritype) {
	case GNET_SNMP_URI_GET:
		out = C.gnet_snmp_sync_get(session, vbl, &gerror)
	case GNET_SNMP_URI_NEXT:
		out = C.gnet_snmp_sync_getnext(session, vbl, &gerror)
	case GNET_SNMP_URI_WALK:
		out = C.gnet_snmp_sync_walk(session, vbl, &gerror)
	default:
		return nil, fmt.Errorf("%s: querySync(): unknown uritype", libname())
	}

	// error handling
	if gerror != nil {
		err_string := C.GoString((*_Ctype_char)(gerror.message))
		C.g_clear_error(&gerror)
		return out, fmt.Errorf("%s: %s", libname(), err_string)
	}
	err_status := PduError(session.error_status)
	switch UriType(uritype) {
	case GNET_SNMP_URI_WALK:
		if err_status != GNET_SNMP_PDU_ERR_NOERROR && err_status != GNET_SNMP_PDU_ERR_NOSUCHNAME {
			es := C.get_err_label(session.error_status)
			err_string := C.GoString((*_Ctype_char)(es))
			return out, fmt.Errorf("%s: %s for uri %d", libname(), err_string, session.error_index)
		}
	default:
		if err_status != GNET_SNMP_PDU_ERR_NOERROR {
			es := C.get_err_label(session.error_status)
			err_string := C.GoString((*_Ctype_char)(es))
			return out, fmt.Errorf("%s: %s for uri %d", libname(), err_string, session.error_index)
		}
	}

	// results
	return out, nil
}