Commit 64cf304c authored by Morten Brekkevold's avatar Morten Brekkevold

Use management profiles on NAV 5.0 and newer.

Because:

- NAV 5.0 drops SNMP attributes as part of the Netbox model, introducing
  the concept of management profiles. Kindnavsync needs to be able to
  work with both pre-5.0 and post 5.0-versions of NAV for now.

Support for older NAV versions can be dropped later.
parent 02ea3080
......@@ -5,6 +5,7 @@ Best-effort synkronisering mellom autoritative utstyrsdata i Kind og
NAVs liste over hva som skal overvåkes.
"""
import copy
import functools
import os
import socket
from collections import defaultdict
......@@ -26,6 +27,15 @@ NAV_API_URL = "http://localhost/api/1"
DEFAULT_SNMP_COMMUNITY = 'public'
DEFAULT_SNMP_VERSION = '2'
DEFAULT_MANAGEMENT_PROFILE = {
"name": "Uninett standard SNMP profile",
"protocol": "SNMP",
"configuration": {
"community": DEFAULT_SNMP_COMMUNITY,
"version": int(DEFAULT_SNMP_VERSION),
"write": False,
},
}
CATEGORY_MAP = {
'lag 3 svitsj': 'GSW',
......@@ -406,17 +416,27 @@ def insert_one_into_nav(nav_api, kind_device):
There is no management or matching of serial numbers here - we rely on NAV
to collect serial numbers via SNMP.
:type nav_api: kindnavsync.navapi.NAVAPI
:type kind_device: KindDevice
"""
netbox = NAVBox(
sysname=kind_device.name, ip=kind_device.loopback,
read_only=DEFAULT_SNMP_COMMUNITY,
snmp_version=DEFAULT_SNMP_VERSION,
roomid=kind_device.room,
organizationid=DEFAULT_ORGANIZATION,
categoryid=CATEGORY_MAP.get(kind_device.type, DEFAULT_CATEGORY),
)
nav_version = nav_api.get_version()
if nav_version[0] >= 5:
# NAV >= 5 wants management profiles
profile = find_or_create_default_management_profile(nav_api)
netbox["profiles"] = [profile["id"]]
else:
# Old NAV versions needs SNMP attrs directly on netbox objects
netbox["read_only"] = DEFAULT_SNMP_COMMUNITY
netbox["snmp_version"] = DEFAULT_SNMP_VERSION
try:
nav_api.get_room(kind_device.room)
......@@ -443,6 +463,29 @@ def insert_one_into_nav(nav_api, kind_device):
raise
@functools.lru_cache(1)
def find_or_create_default_management_profile(nav_api):
"""
:type nav_api: kindnavsync.navapi.NAVAPI
"""
profiles = nav_api.list_management_profiles()
candidates = [p for p in profiles if _is_candidate_profile(p)]
if candidates:
return candidates[0]
else:
response = nav_api.post_management_profile(DEFAULT_MANAGEMENT_PROFILE)
return response.body
def _is_candidate_profile(profile):
config = profile.get("configuration", {})
return (
profile.get("protocol") == "SNMP"
and config.get("version") == int(DEFAULT_SNMP_VERSION)
and config.get("community") == DEFAULT_SNMP_COMMUNITY
)
def update_existing_in_nav(nav_api, kind_devices):
LOG.debug("Sammenligner eksisterende utstyr i NAV med utstyr fra Kind")
for device in kind_devices:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment