disk space calculation using /sys/block/ #15
No reviewers
Labels
No Label
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: Sysdeploy/idlers-agent#15
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "shailaja/idlers-agent:disk/space"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
c0d919ce2c
to217e433f76
@ -37,0 +41,4 @@
size = int(f.read().strip())
disk += size
except Exception as e:
logging.error(f"Failed to read disk size for {device}: {e}")
We shouldn't log it, just skip it, or we should first test if the file exists or is readable to filter the obvious exceptions.
1e586266f7
to5b61f38715
@ -36,3 +30,1 @@
disk = sum(int(x.split()[9]) for x in diskstats.split('\n') if x) * 512 // 10**9
logging.info(f"RAM: {ram}MB, Disk: {disk}GB")
return ram, disk
def get_ram_and_disk(self):
Indentation error
@ -39,0 +46,4 @@
pass # Skip the device if any exception occurs
disk = disk * 512 // (1024**3) # convert to GB
logging.info(f"RAM: {ram}MB, Disk: {disk}GB")
Well, this
logging.info
we could keep but I would prefer if we got rid of f-strings due to #9aa577a0d02
to1021fdb0e1
@ -39,0 +35,4 @@
# Disk space information
disk = 0
for device in os.listdir('/sys/block'):
Maybe we should fallback to
/proc/diskstats
if/sys/block
is not available, @PeterSurda ?We shouldn't use
/proc/diskstats
at all because it doesn't contain the information we need. We could use/proc/partitions
if/sys/block
isn't available. But I don't think I have such a system. The wiki page for sysfs says it's available since kernel 2.5. I got rid of my last machine running 2.4 around 2014-2015.a502498394
tofe6dbebf57
@ -37,0 +37,4 @@
disk = 0
for device in os.listdir('/sys/block'):
size_path = f'/sys/block/{device}/size'
if os.path.exists(size_path) and os.access(size_path, os.R_OK):
We also need to check for
/sys/block/*/device
as I explained in the chat. The logic is supposed to be like this.The code as it is now doesn't filter virtual devices.
Also please avoid f-strings due to #9.
Also please watching out for needing to rebase PRs.
fe6dbebf57
toddbf2143d0
e545bcefa4
tof051a6e29d
f051a6e29d
to54d0b0decd
I'm merging, please address my comments in the next PR.
@ -95,3 +95,4 @@
# RAM information
with open('/proc/meminfo', 'r') as f:
meminfo = f.read()
ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024
This can be rounded up, because on systems which have 1GB of RAM, it will result in 0GB (because it subtracts the amount allocated for the kernel).
@ -102,0 +101,4 @@
for device in os.listdir('/sys/block'):
device_path = '/sys/block/{}/device'.format(device)
size_path = '/sys/block/{}/size'.format(device)
if os.path.islink(device_path):
There is a trick to make the code more readable. Use a negative condition and
continue
. This will reduce indentation.e.g.