Extract and parse LXD instance information.

List LXD instances.

$ lxc list
+---------+---------+---------------------+------+-----------+-----------+
|  NAME   |  STATE  |        IPV4         | IPV6 |   TYPE    | SNAPSHOTS |
+---------+---------+---------------------+------+-----------+-----------+
| adguard | RUNNING | 10.114.53.3 (eth0)  |      | CONTAINER | 0         |
+---------+---------+---------------------+------+-----------+-----------+
| named   | RUNNING | 10.114.53.4 (eth0)  |      | CONTAINER | 0         |
+---------+---------+---------------------+------+-----------+-----------+
| unbound | RUNNING | 10.114.53.12 (eth0) |      | CONTAINER | 0         |
+---------+---------+---------------------+------+-----------+-----------+

Display instance information.

$ lxc info named
Name: named
Status: RUNNING
Type: container
Architecture: aarch64
PID: 1191
Created: 2021/11/23 23:14 CET
Last Used: 2022/02/28 23:13 CET

Resources:
  Processes: 23
  CPU usage:
    CPU usage (in seconds): 447
  Memory usage:
    Memory (current): 51.76MiB
    Memory (peak): 109.51MiB
  Network usage:
    eth0:
      Type: broadcast
      State: UP
      Host interface: vethb07caec0
      MAC address: 00:16:3e:d8:bf:6e
      MTU: 1500
      Bytes received: 570.24kB
      Bytes sent: 470.82kB
      Packets received: 4369
      Packets sent: 3723
      IP addresses:
        inet:  10.114.53.4/28 (global)
        inet6: fe80::216:3eff:fed8:bf6e/64 (link)
    lo:
      Type: loopback
      State: UP
      MTU: 65536
      Bytes received: 378B
      Bytes sent: 378B
      Packets received: 8
      Packets sent: 8
      IP addresses:
        inet:  127.0.0.1/8 (local)
        inet6: ::1/128 (local)

Display instance details using JSON format.

$ lxc list --format json | \
  jq '.[] | select(.name=="named")'
{
  "architecture": "aarch64",
  "config": {
    "image.architecture": "arm64",
    "image.description": "Debian bullseye arm64 (20211123_05:24)",
    "image.os": "Debian",
    "image.release": "bullseye",
    "image.serial": "20211123_05:24",
    "image.type": "squashfs",
    "image.variant": "default",
    "volatile.base_image": "c998286a496ed66c99eb4023844a8c16017a560d15708c3b4491f79635bccd30",
    "volatile.eth0.host_name": "vethb07caec0",
    "volatile.eth0.hwaddr": "00:16:3e:d8:bf:6e",
    "volatile.idmap.base": "0",
    "volatile.idmap.current": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.idmap.next": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.last_state.idmap": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.last_state.power": "RUNNING",
    "volatile.uuid": "d997c332-b2f6-4a6e-8bba-e9d2b4b0bacd"
  },
  "devices": {},
  "ephemeral": false,
  "profiles": [
    "default"
  ],
  "stateful": false,
  "description": "",
  "created_at": "2021-11-23T22:14:57.334549691Z",
  "expanded_config": {
    "image.architecture": "arm64",
    "image.description": "Debian bullseye arm64 (20211123_05:24)",
    "image.os": "Debian",
    "image.release": "bullseye",
    "image.serial": "20211123_05:24",
    "image.type": "squashfs",
    "image.variant": "default",
    "volatile.base_image": "c998286a496ed66c99eb4023844a8c16017a560d15708c3b4491f79635bccd30",
    "volatile.eth0.host_name": "vethb07caec0",
    "volatile.eth0.hwaddr": "00:16:3e:d8:bf:6e",
    "volatile.idmap.base": "0",
    "volatile.idmap.current": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.idmap.next": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.last_state.idmap": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":1000000000}]",
    "volatile.last_state.power": "RUNNING",
    "volatile.uuid": "d997c332-b2f6-4a6e-8bba-e9d2b4b0bacd"
  },
  "expanded_devices": {
    "eth0": {
      "name": "eth0",
      "network": "lxdbr0",
      "type": "nic"
    },
    "root": {
      "path": "/",
      "pool": "default",
      "type": "disk"
    }
  },
  "name": "named",
  "status": "Running",
  "status_code": 103,
  "last_used_at": "2022-02-28T22:13:52.01463686Z",
  "location": "none",
  "type": "container",
  "project": "default",
  "backups": null,
  "state": {
    "status": "Running",
    "status_code": 103,
    "disk": {},
    "memory": {
      "usage": 54431744,
      "usage_peak": 114831360,
      "swap_usage": 0,
      "swap_usage_peak": 0
    },
    "network": {
      "eth0": {
        "addresses": [
          {
            "family": "inet",
            "address": "10.114.53.4",
            "netmask": "28",
            "scope": "global"
          },
          {
            "family": "inet6",
            "address": "fe80::216:3eff:fed8:bf6e",
            "netmask": "64",
            "scope": "link"
          }
        ],
        "counters": {
          "bytes_received": 570238,
          "bytes_sent": 470817,
          "packets_received": 4369,
          "packets_sent": 3723,
          "errors_received": 0,
          "errors_sent": 0,
          "packets_dropped_outbound": 0,
          "packets_dropped_inbound": 0
        },
        "hwaddr": "00:16:3e:d8:bf:6e",
        "host_name": "vethb07caec0",
        "mtu": 1500,
        "state": "up",
        "type": "broadcast"
      },
      "lo": {
        "addresses": [
          {
            "family": "inet",
            "address": "127.0.0.1",
            "netmask": "8",
            "scope": "local"
          },
          {
            "family": "inet6",
            "address": "::1",
            "netmask": "128",
            "scope": "local"
          }
        ],
        "counters": {
          "bytes_received": 378,
          "bytes_sent": 378,
          "packets_received": 8,
          "packets_sent": 8,
          "errors_received": 0,
          "errors_sent": 0,
          "packets_dropped_outbound": 0,
          "packets_dropped_inbound": 0
        },
        "hwaddr": "",
        "host_name": "",
        "mtu": 65536,
        "state": "up",
        "type": "loopback"
      }
    },
    "pid": 1191,
    "processes": 23,
    "cpu": {
      "usage": 447145798939
    }
  },
  "snapshots": null
}

Extract basic instance information.

$ lxc list --format json | \
  jq -r '.[] | "Name:\t\(.name)\nStatus:\t\(.state.status)\nType:\t\(.type)\nArch:\t\(.architecture)\n"'
Name:	adguard
Status:	Running
Type:	container
Arch:	aarch64

Name:	named
Status:	Running
Type:	container
Arch:	aarch64

Name:	unbound
Status:	Running
Type:	container
Arch:	aarch64

Extract memory usage.

$ lxc list --format json | \
  jq -r '.[] | "The \(.type) \(.name) is currently using \(.state.memory.usage/pow(1024;2)|floor) MiB of memory (up to \(.state.memory.usage_peak/pow(1024;2)|floor) MiB)"'
The container adguard is currently using 256 MiB of memory (up to 346 MiB)
The container named is currently using 51 MiB of memory (up to 109 MiB)
The container unbound is currently using 82 MiB of memory (up to 97 MiB)

Extract network information.

$ lxc list --format json | \
  jq -r --arg int "eth0" '.[] | "\(.name) - \($int)\n Address: \(.state.network[$int].addresses[] |select(.family=="inet") | .address)\n  Netmask: \(.state.network[$int].addresses[] |select(.family=="inet") | .netmask)\n  Bytes sent: \(.state.network[$int].counters.bytes_sent)\n  Bytes received: \(.state.network[$int].counters.bytes_received)\n"'
adguard - eth0
 Address: 10.114.53.3
  Netmask: 28
  Bytes sent: 96454860
  Bytes received: 136850048

named - eth0
 Address: 10.114.53.4
  Netmask: 28
  Bytes sent: 471692
  Bytes received: 571015

unbound - eth0
 Address: 10.114.53.12
  Netmask: 28
  Bytes sent: 20706861
  Bytes received: 39336137

Extract number of processes running inside specific instance.

$ lxc list --format json | \
  jq -r --arg name "adguard" '.[] | select(.name == $name) | "There are \(.state.processes) processes running in \($name) \(.type)\n"'
There are 21 processes running in adguard container