Skip to content

Commit 59eccdb

Browse files
committed
Set total disk space on VMs
1 parent 3833e38 commit 59eccdb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

netbox_agent/virtualmachine.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import os
3+
import subprocess
24

35
import netbox_agent.dmidecode as dmidecode
46
from netbox_agent.config import config
@@ -47,6 +49,14 @@ def get_memory(self):
4749
def get_vcpus(self):
4850
return os.cpu_count()
4951

52+
def get_disk(self):
53+
disk_space = 0
54+
disk_data = subprocess.getoutput("lshw -json -c disk")
55+
for disk in json.loads(disk_data):
56+
size = int(disk.get("size", 0)) / 1073741824
57+
disk_space += size
58+
return round(disk_space, 1)
59+
5060
def get_netbox_vm(self):
5161
hostname = get_hostname(config)
5262
vm = nb.virtualization.virtual_machines.get(name=hostname)
@@ -85,6 +95,7 @@ def netbox_create_or_update(self, config):
8595

8696
vcpus = self.get_vcpus()
8797
memory = self.get_memory()
98+
disk = self.get_disk()
8899
tenant = self.get_netbox_tenant()
89100
if not vm:
90101
logging.debug("Creating Virtual machine..")
@@ -96,6 +107,7 @@ def netbox_create_or_update(self, config):
96107
platform=self.device_platform.id,
97108
vcpus=vcpus,
98109
memory=memory,
110+
disk=disk,
99111
tenant=tenant.id if tenant else None,
100112
tags=[{"name": x} for x in self.tags],
101113
)
@@ -111,6 +123,9 @@ def netbox_create_or_update(self, config):
111123
if vm.memory != memory:
112124
vm.memory = memory
113125
updated += 1
126+
if vm.disk != disk:
127+
vm.disk = disk
128+
updated += 1
114129

115130
vm_tags = sorted(set([x.name for x in vm.tags]))
116131
tags = sorted(set(self.tags))

0 commit comments

Comments
 (0)