Add memory info for Podman

This commit is contained in:
nicolargo 2023-01-21 15:40:48 +01:00
parent 216b78936a
commit 5dd133af7d
3 changed files with 50 additions and 3 deletions

View File

@ -20,6 +20,7 @@ import types
import subprocess
import os
from datetime import datetime
import re
from glances.logger import logger
@ -355,3 +356,33 @@ def urlopen_auth(url, username, password):
headers={'Authorization': 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode()).decode()},
)
)
def string_value_to_float(s):
"""Convert a string with a value and an unit to a float.
Example:
'12.5 MB' -> 12500000.0
'32.5 GB' -> 32500000000.0
Args:
s (string): Input string with value and unit
Output:
float: The value in float
"""
convert_dict = {
None: 1,
'B': 1,
'KB': 1000,
'MB': 1000000,
'GB': 1000000000,
'TB': 1000000000000,
'PB': 1000000000000000,
}
unpack_string = [float(i[0]) if i[1] == '' else i[1].upper() for i in re.findall(r'([\d.]+)|([^\d.]+)', s.replace(' ', ''))]
if len(unpack_string) == 2:
value, unit = unpack_string
elif len(unpack_string) == 1:
value = unpack_string[0]
unit = None
else:
return None
return value * convert_dict[unit]

View File

@ -14,7 +14,7 @@ import threading
import time
from copy import deepcopy
from glances.compat import iterkeys, itervalues, nativestr, pretty_date
from glances.compat import iterkeys, itervalues, nativestr, pretty_date, string_value_to_float
from glances.logger import logger
from glances.plugins.glances_plugin import GlancesPlugin
from glances.processes import sort_stats as sort_stats_processes, glances_processes
@ -449,7 +449,10 @@ class Plugin(GlancesPlugin):
# MEMORY
# @TODO
# Should convert 'MemUsage': '352.3kB / 7.836GB' to bytes...
container_stats['memory'] = {}
container_stats['memory'] = {
'usage': string_value_to_float(podman_stats[container_stats['IdShort']]['MemUsage'].split(' / ')[0]),
'limit': string_value_to_float(podman_stats[container_stats['IdShort']]['MemUsage'].split(' / ')[1]),
}
container_stats['memory_percent'] = float(podman_stats[container_stats['IdShort']]['Mem'][:-1])
# Is it possible ?
container_stats['io'] = {}

View File

@ -10,6 +10,10 @@
"""Glances unitary tests suite."""
#
# ./venv/bin/python unitest.py
#
import time
import unittest
@ -25,7 +29,7 @@ from glances.thresholds import GlancesThresholdCritical
from glances.thresholds import GlancesThresholds
from glances.plugins.glances_plugin import GlancesPlugin
from glances.programs import processes_to_programs
from glances.compat import subsample, range
from glances.compat import subsample, range, string_value_to_float
from glances.secure import secure_popen
from glances.compat import PY3
@ -284,6 +288,15 @@ class TestGlances(unittest.TestCase):
# Check if number of processes in the list equal counter
# self.assertEqual(total, len(stats_grab))
def test_018_string_value_to_float(self):
"""Check string_value_to_float function"""
print('INFO: [TEST_018] Check string_value_to_float function')
self.assertEqual(string_value_to_float('32kB'), 32000.0)
self.assertEqual(string_value_to_float('32 KB'), 32000.0)
self.assertEqual(string_value_to_float('15.5MB'), 15500000.0)
self.assertEqual(string_value_to_float('25.9'), 25.9)
self.assertEqual(string_value_to_float('12'), 12)
def test_094_thresholds(self):
"""Test thresholds classes"""
print('INFO: [TEST_094] Thresholds')