Skip to content

Commit 28f8edd

Browse files
authored
Merge pull request #357 from ribetm/vm_disks
Set total disk space on VMs
2 parents 8c2b2ba + 59eccdb commit 28f8edd

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
cluster = self.get_netbox_cluster(config.virtual.cluster_name)
90101

@@ -97,6 +108,7 @@ def netbox_create_or_update(self, config):
97108
platform=self.device_platform.id,
98109
vcpus=vcpus,
99110
memory=memory,
111+
disk=disk,
100112
tenant=tenant.id if tenant else None,
101113
tags=[{"name": x} for x in self.tags],
102114
)
@@ -112,6 +124,9 @@ def netbox_create_or_update(self, config):
112124
if vm.memory != memory:
113125
vm.memory = memory
114126
updated += 1
127+
if vm.disk != disk:
128+
vm.disk = disk
129+
updated += 1
115130

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

0 commit comments

Comments
 (0)