add config to set hostname command

This commit is contained in:
Solvik Blum 2019-09-12 11:03:16 +02:00
parent 9f7f29c190
commit bc03274ca0
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
2 changed files with 8 additions and 3 deletions

View file

@ -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',

View file

@ -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):
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)