Example #1
0
// newUri creates a session from a parsed uri.
func newUri(uri string, version SnmpVersion, parsed_uri *_Ctype_GURI) (session *_Ctype_GNetSnmp, err error) {
	var gerror *C.GError
	session = C.gnet_snmp_new_uri(parsed_uri, &gerror)

	// error handling
	if gerror != nil {
		err_string := C.GoString((*_Ctype_char)(gerror.message))
		C.g_clear_error(&gerror)
		return session, fmt.Errorf("%s: %s", libname(), err_string)
	}
	if session == nil {
		return session, fmt.Errorf("%s: unable to create session", libname())
	}
	session.version = (_Ctype_GNetSnmpVersion)(version)

	// results
	return session, nil
}
Example #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
}
Example #3
0
// newUri creates a session from a parsed uri.
func newUri(params *QueryParams, parsed_uri *_Ctype_GURI) (session *_Ctype_GNetSnmp, err error) {

	var gerror *C.GError
	session = C.gnet_snmp_new_uri(parsed_uri, &gerror)

	// error handling
	if gerror != nil {
		err_string := C.GoString((*_Ctype_char)(gerror.message))
		C.g_clear_error(&gerror)
		return session, fmt.Errorf("%s: newUri(): %s", libname(), err_string)
	}
	if session == nil {
		return session, fmt.Errorf("%s: newUri(): unable to create session", libname())
	}

	if params.Version == GNET_SNMP_V1 { // default in library is v2c
		C.gnet_snmp_set_version(session, 0)
	}
	C.gnet_snmp_set_timeout(session, (_Ctype_guint)(params.Timeout))
	C.gnet_snmp_set_retries(session, (_Ctype_guint)(params.Timeout))

	return session, nil
}