{ "cells": [ { "cell_type": "markdown", "id": "592b8135-c06b-41b7-895e-9dd70787f6ac", "metadata": {}, "source": [ "# Use Glances API in your Python code" ] }, { "cell_type": "markdown", "id": "e5ec86ae-ce2b-452f-b715-54e746026a96", "metadata": {}, "source": [ "## Init the Glances API" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba9b3546-65a0-4eec-942b-1855ff5c5d32", "metadata": {}, "outputs": [], "source": [ "from glances import api" ] }, { "cell_type": "code", "execution_count": 3, "id": "e81ad928-3b61-4654-8589-13cb29e7f292", "metadata": {}, "outputs": [], "source": [ "gl = api.GlancesAPI()" ] }, { "cell_type": "markdown", "id": "6ec912a3-0875-4cdb-8539-e84ffb27768a", "metadata": {}, "source": [ "## Get plugins list" ] }, { "cell_type": "code", "execution_count": 5, "id": "1ce57a13-a90d-4d65-b4a4-2bc45112697e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['alert',\n", " 'ports',\n", " 'diskio',\n", " 'containers',\n", " 'processcount',\n", " 'programlist',\n", " 'gpu',\n", " 'percpu',\n", " 'vms',\n", " 'system',\n", " 'network',\n", " 'cpu',\n", " 'amps',\n", " 'processlist',\n", " 'load',\n", " 'sensors',\n", " 'uptime',\n", " 'now',\n", " 'connections',\n", " 'fs',\n", " 'wifi',\n", " 'ip',\n", " 'help',\n", " 'version',\n", " 'psutilversion',\n", " 'core',\n", " 'mem',\n", " 'folders',\n", " 'quicklook',\n", " 'memswap',\n", " 'raid']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.plugins()" ] }, { "cell_type": "markdown", "id": "d5be2964-7a28-4b93-9dd0-1481afd2ee50", "metadata": {}, "source": [ "## Get CPU stats" ] }, { "cell_type": "code", "execution_count": 7, "id": "0d1636d2-3f3e-44d4-bb67-45487384f79f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'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}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.cpu" ] }, { "cell_type": "code", "execution_count": 8, "id": "99681a33-045e-43bf-927d-88b15872fad0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.cpu.get('total')" ] }, { "cell_type": "markdown", "id": "07e30de4-8f2a-4110-9c43-2a87d91dbf24", "metadata": {}, "source": [ "## Get MEMORY stats" ] }, { "cell_type": "code", "execution_count": 10, "id": "33502d93-acf9-49c5-8bcd-0a0404b47829", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'total': 16422858752, 'available': 6726169136, 'percent': 59.0, 'used': 9696689616, 'free': 541847552, 'active': 8672595968, 'inactive': 5456875520, 'buffers': 354791424, 'cached': 6520318384, 'shared': 729960448}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.mem" ] }, { "cell_type": "code", "execution_count": 11, "id": "404cd8d6-ac38-4830-8ead-4b747e0ca7b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6779998768" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.mem.get('available')" ] }, { "cell_type": "markdown", "id": "74e27e9f-3240-4827-a754-3538b7d68119", "metadata": {}, "source": [ "Display it in a user friendly way:" ] }, { "cell_type": "code", "execution_count": 13, "id": "fa83b40a-51e8-45fa-b478-d0fcc9de4639", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'6.28G'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.auto_unit(gl.mem.get('available'))" ] }, { "cell_type": "markdown", "id": "bfaf5b94-7c9c-4fdc-8a91-71f543cafa4b", "metadata": {}, "source": [ "## Get NETWORK stats" ] }, { "cell_type": "code", "execution_count": 16, "id": "a0ab2ce7-e9bd-4a60-9b90-095a9023dac7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'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}}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.network" ] }, { "cell_type": "markdown", "id": "b65f7280-d9f0-4719-9e10-8b78dc414bae", "metadata": {}, "source": [ "Get the list of networks interfaces:" ] }, { "cell_type": "code", "execution_count": 18, "id": "1a55d32a-bd7d-4dfa-b239-8875c01f205e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['wlp0s20f3']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.network.keys()" ] }, { "cell_type": "markdown", "id": "8c7e0215-e96a-4f7e-a187-9b7bee1abcf9", "metadata": {}, "source": [ "Get stats for a specific network interface:" ] }, { "cell_type": "code", "execution_count": 20, "id": "9aacfb32-c0e3-4fc7-b1d2-d216e46088cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'bytes_sent': 118799,\n", " 'bytes_recv': 275052,\n", " 'speed': 0,\n", " 'key': 'interface_name',\n", " 'interface_name': 'wlp0s20f3',\n", " 'alias': 'WIFI',\n", " 'bytes_all': 393851,\n", " 'time_since_update': 46.24822926521301,\n", " 'bytes_recv_gauge': 1108795793,\n", " 'bytes_recv_rate_per_sec': 5947.0,\n", " 'bytes_sent_gauge': 21268464,\n", " 'bytes_sent_rate_per_sec': 2568.0,\n", " 'bytes_all_gauge': 1130064257,\n", " 'bytes_all_rate_per_sec': 8516.0}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.network.get('wlp0s20f3')" ] }, { "cell_type": "code", "execution_count": 21, "id": "4f5ae513-6022-4a52-8d6c-e8b62afacc24", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5105.0" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.network.get('wlp0s20f3').get('bytes_recv_rate_per_sec')" ] }, { "cell_type": "markdown", "id": "8b0bdbf4-e386-44aa-9585-1d042f0ded5d", "metadata": {}, "source": [ "## Additional information" ] }, { "cell_type": "markdown", "id": "5c52a0c7-06fb-432a-bdb7-9921f432d5a6", "metadata": {}, "source": [ "Example for the LOAD plugin." ] }, { "cell_type": "code", "execution_count": 29, "id": "99303a2b-52a3-440f-a896-ad4951a9de34", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'min1': 1.01123046875, 'min5': 0.83447265625, 'min15': 0.76171875, 'cpucore': 16}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.load" ] }, { "cell_type": "markdown", "id": "7a560824-2787-4436-b39b-63de0c455536", "metadata": {}, "source": [ "Get the limit configured in the glances.conf:" ] }, { "cell_type": "code", "execution_count": 34, "id": "cbbc6a81-623f-4eff-9d08-e6a8b5981660", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'min1': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 1 minute.',\n", " 'unit': 'float'},\n", " 'min5': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 5 minutes.',\n", " 'unit': 'float'},\n", " 'min15': {'description': 'Average sum of the number of processes waiting in the run-queue plus the number currently executing over 15 minutes.',\n", " 'unit': 'float'},\n", " 'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'}}" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.load.fields_description" ] }, { "cell_type": "markdown", "id": "2bd51d13-77e3-48f0-aa53-af86df6425f8", "metadata": {}, "source": [ "Get field description and unit:" ] }, { "cell_type": "code", "execution_count": 30, "id": "8682edcf-a8b9-424c-976f-2a301a05be6a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history_size': 1200.0,\n", " 'load_disable': ['False'],\n", " 'load_careful': 0.7,\n", " 'load_warning': 1.0,\n", " 'load_critical': 5.0}" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.load.get_limits()" ] }, { "cell_type": "raw", "id": "3c671ff8-3a0c-48d3-8247-6081c69c19a9", "metadata": {}, "source": [ "Get current stats views regarding limits:" ] }, { "cell_type": "code", "execution_count": 33, "id": "45e03e9b-233c-4359-bcbc-7d2f06aca1c6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'min1': {'decoration': 'DEFAULT',\n", " 'optional': False,\n", " 'additional': False,\n", " 'splittable': False,\n", " 'hidden': False},\n", " 'min5': {'decoration': 'OK',\n", " 'optional': False,\n", " 'additional': False,\n", " 'splittable': False,\n", " 'hidden': False},\n", " 'min15': {'decoration': 'OK_LOG',\n", " 'optional': False,\n", " 'additional': False,\n", " 'splittable': False,\n", " 'hidden': False},\n", " 'cpucore': {'decoration': 'DEFAULT',\n", " 'optional': False,\n", " 'additional': False,\n", " 'splittable': False,\n", " 'hidden': False}}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gl.load.get_views()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.0" } }, "nbformat": 4, "nbformat_minor": 5 }