Added condition to check if file exists and readable and skips it if it is not readable

This commit is contained in:
Shailaja Kumari 2024-06-25 16:42:20 +05:30
parent 217e433f76
commit 1e586266f7
Signed by: shailaja
GPG Key ID: 2B9455CAFBC4D75A

View File

@ -27,7 +27,7 @@ class ServerData:
self.public_ip = self.get_public_ip() self.public_ip = self.get_public_ip()
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
def get_ram_and_disk(self): def get_ram_and_disk(self):
# RAM information # RAM information
with open('/proc/meminfo', 'r') as f: with open('/proc/meminfo', 'r') as f:
meminfo = f.read() meminfo = f.read()
@ -36,12 +36,14 @@ class ServerData:
# Disk space information # Disk space information
disk = 0 disk = 0
for device in os.listdir('/sys/block'): 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):
try: try:
with open(f'/sys/block/{device}/size', 'r') as f: with open(size_path, 'r') as f:
size = int(f.read().strip()) size = int(f.read().strip())
disk += size disk += size
except Exception as e: except Exception:
logging.error(f"Failed to read disk size for {device}: {e}") pass # Skip the device if any exception occurs
disk = disk * 512 // (1024**3) # convert to GB disk = disk * 512 // (1024**3) # convert to GB
logging.info(f"RAM: {ram}MB, Disk: {disk}GB") logging.info(f"RAM: {ram}MB, Disk: {disk}GB")