NanoKVMGo.Metrics (nano_kvm_go v0.4.0)

Copy Markdown View Source

What the NanoKVM-Go is doing, sampled each second: the share of the CPU in use, the memory in use, the SoC temperature, the traffic of each network interface, the load average and the time since it started. The process keeps the last five minutes for graphs, and sends each sample to its subscribers.

NanoKVMGo.Metrics.current()
#=> %{
#=>   cpu: %{total: 12.5, cores: [10.0, 15.0]},
#=>   memory: %{used: 251_658_240, total: 510_390_272, percent: 49.3},
#=>   temperature: 71.3,
#=>   network: %{"wlan0" => %{down: 1200.0, up: 310.5}},
#=>   load: [0.08, 0.03, 0.01],
#=>   uptime: 33_432
#=> }

:ok = NanoKVMGo.Metrics.subscribe()

receive do
  {NanoKVMGo.Metrics, sample} -> sample
end

CPU and memory are in percent, the temperature in degrees Celsius (nil without the sensor), network traffic in bytes per second each way (down received, up sent, the loopback left out), the uptime in seconds. The first sample comes a second after the start: the CPU's share is measured between two readings.

Options

  • :name - the name to register, NanoKVMGo.Metrics by default
  • :interval - milliseconds between samples, 1000 by default
  • :history - how many samples to keep, 300 by default
  • :root - the root of the file system with procfs and sysfs, "/" by default
  • :clock - a function that gives the time in milliseconds, which the traffic is measured against, the monotonic time by default

Summary

Types

The samples kept, oldest first: CPU and memory in percent, temperatures, and the traffic of each interface. An interface that came later has fewer.

A message to subscribers: each sample.

One sample, see the module documentation.

Functions

Returns a specification to start this module under a supervisor.

The last sample, nil before the first.

The samples kept, oldest first.

Starts the process. See the module documentation for the options.

Sends each sample to the caller, as {NanoKVMGo.Metrics, sample}, until it unsubscribes or exits.

Stops the samples to the caller.

Types

history()

@type history() :: %{
  cpu: [float()],
  memory: [float()],
  temperature: [float() | nil],
  network: %{required(String.t()) => %{down: [float()], up: [float()]}}
}

The samples kept, oldest first: CPU and memory in percent, temperatures, and the traffic of each interface. An interface that came later has fewer.

message()

@type message() :: {NanoKVMGo.Metrics, sample()}

A message to subscribers: each sample.

option()

@type option() ::
  {:name, GenServer.name()}
  | {:interval, pos_integer()}
  | {:history, pos_integer()}
  | {:root, Path.t()}
  | {:clock, (-> integer())}

sample()

@type sample() :: %{
  cpu: %{total: float(), cores: [float()]},
  memory: %{used: non_neg_integer(), total: non_neg_integer(), percent: float()},
  temperature: float() | nil,
  network: %{required(String.t()) => %{down: float(), up: float()}},
  load: [float()],
  uptime: non_neg_integer() | nil
}

One sample, see the module documentation.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

current(server \\ __MODULE__)

@spec current(GenServer.server()) :: sample() | nil

The last sample, nil before the first.

history(server \\ __MODULE__)

@spec history(GenServer.server()) :: history()

The samples kept, oldest first.

start_link(opts \\ [])

@spec start_link([option()]) :: GenServer.on_start()

Starts the process. See the module documentation for the options.

subscribe(server \\ __MODULE__)

@spec subscribe(GenServer.server()) :: :ok

Sends each sample to the caller, as {NanoKVMGo.Metrics, sample}, until it unsubscribes or exits.

unsubscribe(server \\ __MODULE__)

@spec unsubscribe(GenServer.server()) :: :ok

Stops the samples to the caller.