From bc03274ca07b685182f2db3e083e706a320b9b8d Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Thu, 12 Sep 2019 11:03:16 +0200 Subject: [PATCH] add config to set hostname command --- netbox_agent/config.py | 2 ++ netbox_agent/server.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/netbox_agent/config.py b/netbox_agent/config.py index d6911bd..306391d 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -27,6 +27,8 @@ def get_config(): p.add_argument('--log_level', default='debug') p.add_argument('--netbox.url', help='Netbox URL') p.add_argument('--netbox.token', help='Netbox API Token') + p.add_argument('--hostname_cmd', default=None, + help="Command to output hostname, used as Device's name in netbox") p.add_argument('--datacenter_location.driver', help='Datacenter location driver, ie: cmd, file') p.add_argument('--datacenter_location.driver_file', diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 3de977b..9a32042 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -1,8 +1,9 @@ import logging from pprint import pprint import socket +import subprocess -from netbox_agent.config import netbox_instance as nb +from netbox_agent.config import netbox_instance as nb, config import netbox_agent.dmidecode as dmidecode from netbox_agent.location import Datacenter, Rack from netbox_agent.inventory import Inventory @@ -85,7 +86,9 @@ class ServerBase(): return self.system[0]['Serial Number'].strip() def get_hostname(self): - return '{}'.format(socket.gethostname()) + if config.hostname_cmd is None: + return '{}'.format(socket.gethostname()) + return subprocess.getoutput(config.hostname_cmd) def is_blade(self): raise NotImplementedError @@ -310,7 +313,7 @@ class ServerBase(): # check hostname if server.name != self.get_hostname(): update += 1 - server.hostname = self.get_hostname() + server.name = self.get_hostname() if config.update_all or config.update_location: ret, server = self.update_netbox_location(server) -- 2.47.0