Skip to main content

Kubernetes manifest deploy, readiness monitoring, threaded exporter integration, and cleanup tooling.

Reason this release was yanked:

deprecated

Project description

swchmonclient

A Python library for deploying the monitoring stack and consuming metric events over STOMP in the Swarmchestrate project.

Install

pip install swchmonclient
uv add swchmonclient

Overview

  • Metrics defined in the SAT are monitored by EMS.
  • Composite metrics and SLOs are consumed from the central EPM.
  • Raw metrics can be consumed from the local EPA, an explicit list of nodes, or all nodes discovered from Kubernetes.
  • The library buffers metric values until query_metric_values(...) or query_metric_values_raw(...) is called.
  • Returned samples are consumed from the in-memory buffers.

Quick Example

from swchmonclient import (
    deploy_monitoring,
    query_metric_values,
    query_metric_values_raw,
    subscribe_metric,
    subscribe_metric_raw,
    undeploy_monitoring,
    unsubscribe_metric,
)

deploy_monitoring("./k3s.yaml", "tosca_metrics_ze.yaml", "http://optimusdb.example/swarmkb")
subscribe_metric("/topic/mysample_metric")
recent_values = query_metric_values("/topic/mysample_metric", seconds=60)
subscribe_metric_raw("/topic/myraw_metric", ["10.0.0.1", "10.0.0.2"])
subscribe_metric_raw("/topic/myraw_metric", "all")
subscribe_metric_raw("/topic/myraw_metric", "local")
raw_values = query_metric_values_raw("/topic/myraw_metric", seconds=60)
unsubscribe_metric("/topic/mysample_metric")
unsubscribe_metric("/topic/myraw_metric")
undeploy_monitoring(
    "./k3s.yaml",
    "tosca_metrics_ze.yaml",
    "http://optimusdb.example/swarmkb",
    namespace="default",
)

Raw Metric Subscriptions

subscribe_metric_raw(metric, node) supports three selector modes:

  • ["10.0.0.1", "10.0.0.2"] starts one raw listener per explicit node/IP
  • "all" resolves all Kubernetes VM private IPs internally
  • "local" resolves the current machine's private IP and starts one raw listener for it

Raw subscriptions connect directly to the resolved node IPs. Read buffered raw samples with:

raw_values = query_metric_values_raw("cpu_util_instance", seconds=60)

The returned structure is grouped by node/IP:

{
    "10.0.0.1": [
        {"timestamp": 1716712345.12, "value": 42.0},
    ],
}

Each raw metric on each node/IP keeps up to 1000 cached samples, dropping the oldest entries first when the buffer fills.

If you subscribe multiple raw metrics for the same node/IP, the library reuses the same raw listener thread for that node and dynamically subscribes the additional metric topics on that connection.

API Reference

deploy_monitoring(kubeconfig_path: str | None, sat_file: str, optimusdb_url: str, logger: logging.Logger | None = None) -> int

Deploys the standard monitoring stack manifests.

If ./manifests/emsconfig.yaml or ./manifests/ems+netdata-k3s_parametric.yaml is missing locally, the library downloads it from the v0.1.0 release assets before deployment. When a local copy already exists, it validates the content against the release asset, logs whether it matches, and replaces the file if it differs.

Parameter Required Type Description
kubeconfig_path No str | None Path to the kubeconfig file. If omitted, the default kubeconfig / in-cluster fallback is used.
sat_file Yes str SAT file path injected into the templated manifest.
optimusdb_url Yes str OptimusDB URL injected into the templated manifest.
logger No logging.Logger | None Custom logger. If omitted, stdout logging is configured automatically.

Output: process-style exit code: 0 on success, 1 if one or more deploy steps fail.

undeploy_monitoring(kubeconfig_path: str | None, sat_file: str, optimusdb_url: str, namespace: str | None = None, logger: logging.Logger | None = None) -> int

Undeploys the standard monitoring stack manifests and the related cleanup resources.

Like deployment, undeploy ensures the two required monitoring manifests are available locally, logs whether existing local copies match the published release assets, and refreshes differing files from the release.

Parameter Required Type Description
kubeconfig_path No str | None Path to the kubeconfig file. If omitted, the default kubeconfig / in-cluster fallback is used.
sat_file Yes str SAT file path used to render the templated manifest before undeploy.
optimusdb_url Yes str OptimusDB URL used to render the templated manifest before undeploy.
namespace No str | None Namespace override for deleting namespaced resources. If omitted, manifest/default namespaces are used.
logger No logging.Logger | None Custom logger. If omitted, stdout logging is configured automatically.

Output: process-style exit code: 0 on success, 1 if one or more undeploy steps fail.

subscribe_metric(metric: str) -> str

Starts or reuses the shared standard metric listener for the requested metric topic.

Parameter Required Type Description
metric Yes str Metric name or full topic destination. Plain names are normalized to /topic/<metric>.

Output: str thread name of the shared listener, currently metric-listener.

subscribe_metric_raw(metric: str, node: list[str] | str, cache_size: int | None = None) -> dict[str, str]

Starts raw metric listeners that connect directly to node IPs.

Parameter Required Type Description
metric Yes str Metric name or full topic destination. Plain names are normalized to /topic/<metric>.
node Yes list[str] | str Raw node selector. Use an explicit node/IP list, "all" for all Kubernetes VM private IPs, or "local" for the current machine's private IP.
cache_size No int | None Per raw metric per node sample buffer size. If omitted, the default value 1000 is used.

Output: dict[str, str] mapping each resolved node/IP to the listener thread name started for it.

Notes:

  • Starts one raw listener thread per resolved node/IP and reuses it for additional raw metrics on that same node/IP.
  • Raw subscriptions connect directly to each resolved node/IP instead of STOMP_HOST.
  • Mixing subscribe_metric(...) and subscribe_metric_raw(...) for the same metric is rejected.
  • For node="all", Kubernetes config is resolved automatically from the default kubeconfig, KUBECONFIG, common local files such as ./k3s.yaml, or in-cluster config.

query_metric_values(metric: str, seconds: int) -> list[Any]

Returns buffered metric values received within the last seconds seconds and consumes those returned samples.

Parameter Required Type Description
metric Yes str Metric name or full topic destination.
seconds Yes int Time window to read from. Must be non-negative.
[42.0, 41.7]

query_metric_values_raw(metric: str, seconds: int) -> dict[str, list[dict[str, Any]]]

Returns buffered raw metric values received within the last seconds seconds and consumes those returned samples.

Parameter Required Type Description
metric Yes str Metric name or full topic destination.
seconds Yes int Time window to read from. Must be non-negative.

Output:

{
    "10.0.0.1": [
        {"timestamp": 1716712345.12, "value": 42.0},
    ],
}

unsubscribe_metric(metric: str, nodes: list[str] | None = None) -> None

Stops metric listeners or removes node-specific subscriptions, depending on the subscription mode.

Parameter Required Type Description
metric Yes str Metric name or full topic destination.
nodes No list[str] | None For raw subscriptions, stops only the listed node/IP listeners. For standard subscriptions, blocks those nodes from future samples. If omitted, removes the full subscription.

Output: no return value.

Behavior summary:

  • Standard metric + no nodes: stop the shared listener when the last standard metric is removed.
  • Standard metric + nodes: keep the listener running, but ignore future samples from those nodes.
  • Raw metric + no nodes: stop all raw listeners for that metric.
  • Raw metric + nodes: stop only the listed raw node/IP listeners and remove their cached data buckets.

Examples

Runnable examples are available under examples/:

  • examples/deploy_example.py
  • examples/undeploy_example.py
  • examples/subscribe_cpu_util_instance_example.py
  • examples/subscribe_cpu_util_instance_raw_example.py

Simple Snippets

deploy_monitoring

from swchmonclient import deploy_monitoring

exit_code = deploy_monitoring("./k3s.yaml", "tosca_metrics_ze.yaml", "http://optimusdb.example/swarmkb")

undeploy_monitoring

from swchmonclient import undeploy_monitoring

exit_code = undeploy_monitoring(
    "./k3s.yaml",
    "tosca_metrics_ze.yaml",
    "http://optimusdb.example/swarmkb",
    namespace="default",
)

subscribe_metric

from swchmonclient import subscribe_metric

thread_name = subscribe_metric("cpu_util_instance")
print(thread_name)

subscribe_metric_raw

from swchmonclient import subscribe_metric_raw

threads = subscribe_metric_raw("cpu_util_instance", "local")
# or: subscribe_metric_raw("cpu_util_instance", "local", cache_size=500)
# or: subscribe_metric_raw("cpu_util_instance", "all")
# or: subscribe_metric_raw("cpu_util_instance", ["10.0.0.1", "10.0.0.2"])
print(threads)

query_metric_values

from swchmonclient import query_metric_values

standard_values = query_metric_values("cpu_util_instance", seconds=60)

query_metric_values_raw

from swchmonclient import query_metric_values_raw

raw_values = query_metric_values_raw("cpu_util_instance", seconds=60)

unsubscribe_metric

from swchmonclient import unsubscribe_metric

unsubscribe_metric("cpu_util_instance")
# or: unsubscribe_metric("cpu_util_instance", nodes=["10.0.0.1"])

Development

uv sync
uv build

Switching between in-cluster and local development

The STOMP listener reads STOMP_HOST and STOMP_PORT from .env, so you can switch environments without changing code.

Use the bundled profiles:

./scripts/use-cluster-env.sh
./scripts/use-dev-env.sh
  • use-cluster-env.sh writes .env with emsserver-ems-server:61610
  • use-dev-env.sh writes .env with 127.0.0.1:61610

When running outside the cluster, start a port-forward to the ClusterIP service:

./scripts/use-dev-env.sh
./scripts/port-forward-emsserver.sh

If the service is not in default, pass the namespace explicitly:

./scripts/port-forward-emsserver.sh monitoring

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swchmonclient-0.1.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swchmonclient-0.1.0-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file swchmonclient-0.1.0.tar.gz.

File metadata

  • Download URL: swchmonclient-0.1.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for swchmonclient-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ba82221a142a25f23e5bac542238d71a9a3c6ba26e0b98ba29c7a22ed7ad5376
MD5 df7a5369b905b3a8462d0760d7d4a780
BLAKE2b-256 0e10663bfb6d6cb02efe734505908f8d720aecff748e356a3f6ae61a70be6250

See more details on using hashes here.

File details

Details for the file swchmonclient-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: swchmonclient-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for swchmonclient-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7f9adb27e42f7630006f68ce027e549265b5aa4f37880e4843e646ac0d5d804
MD5 d789d0dfa6df3db144c324aeea24a40a
BLAKE2b-256 94e53e030d278544ece6ec905c0bf531e1112afa7c599b44aa1c65fa5009b50c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page