Files
glances/glances.ipynb
T
2025-11-21 18:36:50 +01:00

12 KiB

Use Glances API in your Python code

Init the Glances API

In [2]:
from glances import api
In [3]:
gl = api.GlancesAPI()

Get plugins list

In [5]:
gl.plugins()
Out [5]:
['alert',
 'ports',
 'diskio',
 'containers',
 'processcount',
 'programlist',
 'gpu',
 'percpu',
 'vms',
 'system',
 'network',
 'cpu',
 'amps',
 'processlist',
 'load',
 'sensors',
 'uptime',
 'now',
 'connections',
 'fs',
 'wifi',
 'ip',
 'help',
 'version',
 'psutilversion',
 'core',
 'mem',
 'folders',
 'quicklook',
 'memswap',
 'raid']

Get CPU stats

In [7]:
gl.cpu
Out [7]:
{'total': 3.8, 'user': 3.0, 'nice': 0.0, 'system': 0.8, 'idle': 96.1, 'iowait': 0.1, 'irq': 0.0, 'steal': 0.0, 'guest': 0.0, 'ctx_switches': 0, 'interrupts': 0, 'soft_interrupts': 0, 'syscalls': 0, 'cpucore': 16, 'time_since_update': 141.46278643608093, 'ctx_switches_gauge': 12830371, 'ctx_switches_rate_per_sec': 0, 'interrupts_gauge': 9800040, 'interrupts_rate_per_sec': 0, 'soft_interrupts_gauge': 3875931, 'soft_interrupts_rate_per_sec': 0, 'syscalls_gauge': 0, 'syscalls_rate_per_sec': 0}
In [8]:
gl.cpu.get('total')
Out [8]:
3.1

Get MEMORY stats

In [10]:
gl.mem
Out [10]:
{'total': 16422858752, 'available': 6726169136, 'percent': 59.0, 'used': 9696689616, 'free': 541847552, 'active': 8672595968, 'inactive': 5456875520, 'buffers': 354791424, 'cached': 6520318384, 'shared': 729960448}
In [11]:
gl.mem.get('available')
Out [11]:
6779998768

Display it in a user friendly way:

In [13]:
gl.auto_unit(gl.mem.get('available'))
Out [13]:
'6.28G'

Get NETWORK stats

In [16]:
gl.network
Out [16]:
{'wlp0s20f3': {'bytes_sent': 1130903, 'bytes_recv': 2213272, 'speed': 0, 'key': 'interface_name', 'interface_name': 'wlp0s20f3', 'alias': 'WIFI', 'bytes_all': 3344175, 'time_since_update': 354.35748958587646, 'bytes_recv_gauge': 1108380679, 'bytes_recv_rate_per_sec': 6245.0, 'bytes_sent_gauge': 21062113, 'bytes_sent_rate_per_sec': 3191.0, 'bytes_all_gauge': 1129442792, 'bytes_all_rate_per_sec': 9437.0}}

Get the list of networks interfaces:

In [18]:
gl.network.keys()
Out [18]:
['wlp0s20f3']

Get stats for a specific network interface:

In [20]:
gl.network.get('wlp0s20f3')
Out [20]:
{'bytes_sent': 118799,
 'bytes_recv': 275052,
 'speed': 0,
 'key': 'interface_name',
 'interface_name': 'wlp0s20f3',
 'alias': 'WIFI',
 'bytes_all': 393851,
 'time_since_update': 46.24822926521301,
 'bytes_recv_gauge': 1108795793,
 'bytes_recv_rate_per_sec': 5947.0,
 'bytes_sent_gauge': 21268464,
 'bytes_sent_rate_per_sec': 2568.0,
 'bytes_all_gauge': 1130064257,
 'bytes_all_rate_per_sec': 8516.0}
In [21]:
gl.network.get('wlp0s20f3').get('bytes_recv_rate_per_sec')
Out [21]:
5105.0

Additional information

Example for the LOAD plugin.

In [29]:
gl.load
Out [29]:
{'min1': 1.01123046875, 'min5': 0.83447265625, 'min15': 0.76171875, 'cpucore': 16}

Get the limit configured in the glances.conf:

In [34]:
gl.load.fields_description
Out [34]:
{'min1': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 1 minute.',
  'unit': 'float'},
 'min5': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 5 minutes.',
  'unit': 'float'},
 'min15': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 15 minutes.',
  'unit': 'float'},
 'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'}}

Get field description and unit:

In [30]:
gl.load.get_limits()
Out [30]:
{'history_size': 1200.0,
 'load_disable': ['False'],
 'load_careful': 0.7,
 'load_warning': 1.0,
 'load_critical': 5.0}
Cell:
[Cell type raw - unsupported, skipped]
In [33]:
gl.load.get_views()
Out [33]:
{'min1': {'decoration': 'DEFAULT',
  'optional': False,
  'additional': False,
  'splittable': False,
  'hidden': False},
 'min5': {'decoration': 'OK',
  'optional': False,
  'additional': False,
  'splittable': False,
  'hidden': False},
 'min15': {'decoration': 'OK_LOG',
  'optional': False,
  'additional': False,
  'splittable': False,
  'hidden': False},
 'cpucore': {'decoration': 'DEFAULT',
  'optional': False,
  'additional': False,
  'splittable': False,
  'hidden': False}}