2022-02-25 18:43:09 +01:00
|
|
|
import re
|
2024-10-21 12:55:54 +02:00
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
from shutil import which
|
|
|
|
|
|
|
|
from slugify import slugify
|
|
|
|
|
|
|
|
from netbox_agent.config import netbox_instance as nb
|
2020-07-01 18:54:58 +02:00
|
|
|
|
2019-08-26 16:54:48 +02:00
|
|
|
|
|
|
|
def is_tool(name):
|
2024-10-21 12:55:54 +02:00
|
|
|
"""Check whether `name` is on PATH and marked as executable."""
|
2019-08-26 16:54:48 +02:00
|
|
|
return which(name) is not None
|
2019-09-05 15:13:36 +02:00
|
|
|
|
|
|
|
|
2020-07-01 18:54:58 +02:00
|
|
|
def get_device_role(role):
|
2024-10-21 12:55:54 +02:00
|
|
|
device_role = nb.dcim.device_roles.get(name=role)
|
2020-07-01 18:54:58 +02:00
|
|
|
if device_role is None:
|
|
|
|
raise Exception('DeviceRole "{}" does not exist, please create it'.format(role))
|
|
|
|
return device_role
|
|
|
|
|
|
|
|
|
|
|
|
def get_device_type(type):
|
2024-10-21 12:55:54 +02:00
|
|
|
device_type = nb.dcim.device_types.get(model=type)
|
2020-07-01 18:54:58 +02:00
|
|
|
if device_type is None:
|
|
|
|
raise Exception('DeviceType "{}" does not exist, please create it'.format(type))
|
|
|
|
return device_type
|
|
|
|
|
|
|
|
|
2022-03-31 20:58:23 +02:00
|
|
|
def get_device_platform(device_platform):
|
|
|
|
if device_platform is None:
|
2022-03-31 17:08:16 +02:00
|
|
|
try:
|
2022-07-18 22:06:33 +02:00
|
|
|
# Python 3.8+ moved linux_distribution() to distro
|
|
|
|
try:
|
|
|
|
import distro
|
2024-10-21 12:55:54 +02:00
|
|
|
|
2022-07-18 22:06:33 +02:00
|
|
|
linux_distribution = " ".join(distro.linux_distribution())
|
|
|
|
except ImportError:
|
|
|
|
import platform
|
2024-10-21 12:55:54 +02:00
|
|
|
|
2022-07-18 22:06:33 +02:00
|
|
|
linux_distribution = " ".join(platform.linux_distribution())
|
2022-03-31 11:14:26 +02:00
|
|
|
|
2022-03-31 17:08:16 +02:00
|
|
|
if not linux_distribution:
|
|
|
|
return None
|
2022-07-18 22:06:33 +02:00
|
|
|
except (ModuleNotFoundError, NameError, AttributeError):
|
2022-03-31 17:08:16 +02:00
|
|
|
return None
|
|
|
|
else:
|
2022-03-31 20:58:23 +02:00
|
|
|
linux_distribution = device_platform
|
2022-03-31 17:08:16 +02:00
|
|
|
|
|
|
|
device_platform = nb.dcim.platforms.get(name=linux_distribution)
|
2022-03-30 11:57:31 +02:00
|
|
|
if device_platform is None:
|
|
|
|
device_platform = nb.dcim.platforms.create(
|
2022-03-31 17:08:16 +02:00
|
|
|
name=linux_distribution, slug=slugify(linux_distribution)
|
2022-03-30 11:57:31 +02:00
|
|
|
)
|
|
|
|
return device_platform
|
|
|
|
|
2024-10-21 12:55:54 +02:00
|
|
|
|
2019-09-05 15:13:36 +02:00
|
|
|
def get_vendor(name):
|
|
|
|
vendors = {
|
2024-10-21 12:55:54 +02:00
|
|
|
"PERC": "Dell",
|
|
|
|
"SANDISK": "SanDisk",
|
|
|
|
"DELL": "Dell",
|
|
|
|
"ST": "Seagate",
|
|
|
|
"CRUCIAL": "Crucial",
|
|
|
|
"MICRON": "Micron",
|
|
|
|
"INTEL": "Intel",
|
|
|
|
"SAMSUNG": "Samsung",
|
|
|
|
"EH0": "HP",
|
|
|
|
"HGST": "HGST",
|
|
|
|
"HUH": "HGST",
|
|
|
|
"MB": "Toshiba",
|
|
|
|
"MC": "Toshiba",
|
|
|
|
"MD": "Toshiba",
|
|
|
|
"MG": "Toshiba",
|
|
|
|
"WD": "WDC",
|
2020-02-02 20:08:56 +01:00
|
|
|
}
|
2019-09-05 15:13:36 +02:00
|
|
|
for key, value in vendors.items():
|
|
|
|
if name.upper().startswith(key):
|
|
|
|
return value
|
|
|
|
return name
|
2020-04-19 12:28:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_hostname(config):
|
|
|
|
if config.hostname_cmd is None:
|
2024-10-21 12:55:54 +02:00
|
|
|
return "{}".format(socket.gethostname())
|
2020-04-19 12:28:49 +02:00
|
|
|
return subprocess.getoutput(config.hostname_cmd)
|
2020-07-01 18:54:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_netbox_tags(tags):
|
2021-05-11 20:42:13 +02:00
|
|
|
ret = []
|
2020-07-01 18:54:58 +02:00
|
|
|
for tag in tags:
|
2024-10-21 12:55:54 +02:00
|
|
|
nb_tag = nb.extras.tags.get(name=tag)
|
2020-07-01 18:54:58 +02:00
|
|
|
if not nb_tag:
|
|
|
|
nb_tag = nb.extras.tags.create(
|
|
|
|
name=tag,
|
2020-12-15 12:47:47 +01:00
|
|
|
slug=slugify(tag),
|
2020-07-01 18:54:58 +02:00
|
|
|
)
|
2021-05-11 20:42:13 +02:00
|
|
|
ret.append(nb_tag)
|
|
|
|
return ret
|
2022-02-25 18:43:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_mount_points():
|
|
|
|
mount_points = {}
|
2024-10-21 12:55:54 +02:00
|
|
|
output = subprocess.getoutput("mount")
|
2022-02-25 18:43:09 +01:00
|
|
|
for r in output.split("\n"):
|
|
|
|
if not r.startswith("/dev/"):
|
|
|
|
continue
|
|
|
|
mount_info = r.split()
|
|
|
|
device = mount_info[0]
|
2024-10-21 12:55:54 +02:00
|
|
|
device = re.sub(r"\d+$", "", device)
|
2022-02-25 18:43:09 +01:00
|
|
|
mp = mount_info[2]
|
|
|
|
mount_points.setdefault(device, []).append(mp)
|
|
|
|
return mount_points
|