Metadata-Version: 2.4
Name: cmk-appliance-client
Version: 0.1.0.dev0
Summary: Client library and CLI for the REST API of the Checkmk appliance
Author: Checkmk GmbH
License-Expression: Apache-2.0
Project-URL: Homepage, https://checkmk.com
Project-URL: Documentation, https://download.checkmk.com/appliance-client/latest/
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: System Administrators
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Operating System :: OS Independent
Classifier: Topic :: System :: Systems Administration
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Checkmk appliance REST API client

Python library and command line client (`cmk-appliance`) for the REST API of
the Checkmk appliance. It runs on an administrator's workstation or automation
host and talks to one or many appliances over HTTP(S). It is not part of the
appliance firmware.

The REST API is experimental. Enable it on the "REST API (experimental)"
page of the web configuration of the appliance and create an API token
there.

The client only uses the Python standard library. It needs the Python version
of the appliance firmware or a newer one, currently Python 3.11 or newer.

The API reference of the library is published with each release:
https://download.checkmk.com/appliance-client/latest/

## Installation

```console
pip install cmk-appliance-client
```

The package contributes `cmk.appliance.client` to the `cmk` namespace
package, so it can be installed next to other `cmk` packages, e.g. into the
Python environment of a Checkmk site.

## Command line

```console
export CMK_APPLIANCE_URL=https://appliance.example.com
export CMK_APPLIANCE_TOKEN=cma_...

cmk-appliance info
cmk-appliance version install check-mk-cloud-2.4.0p1-1.7-x86_64.cma
cmk-appliance site create mysite 2.4.0p1.cee --admin-password-file admin.pw
cmk-appliance site edit mysite --livestatus-tcp-port 6557 --acknowledge-site-restart
cmk-appliance site update mysite 2.4.0p1.cee --dry-run
cmk-appliance site update mysite 2.4.0p1.cee --acknowledge-site-stop
cmk-appliance version cleanup
cmk-appliance site stop-all
cmk-appliance setting show
cmk-appliance setting timezone Europe/Berlin
cmk-appliance setting mail smtp.example.com --username mailer --password-file - --tls-port 587
```

The token can also be read from a file or from stdin (`--token-file -`). There is no option
taking the token itself, since command lines end up in shell histories and
process listings. The same goes for passwords, e.g. of a new site: They are
read from a file or asked for.

`-` reads a secret from stdin instead of a file, for one option per call: Stdin
has no end between two secrets. Process substitution passes any number of
secrets, each through a pipe of its own, without storing them in files:

```console
pass show appliance/token | cmk-appliance --token-file - site create mysite 2.4.0p1.cee \
    --admin-password-file <(pass show appliance/cmkadmin) \
    --site-user-password-file <(pass show appliance/mysite)
```

`--insecure` skips the TLS certificate check (e.g. while the appliance still
uses its self-signed certificate), `--ca-file` verifies against a specific CA.

Operations running as background job on the appliance (site update, firmware
update, reboot, cluster operations) are waited for, with the progress log
shown on stderr. `--no-wait` prints the job ID instead. `--json` prints
results as JSON. On a terminal, uploads show their progress and speed, all longer operations
the time waited and, for jobs, the latest state.

`cmk-appliance setting` changes the settings of the device settings page, one
at a time. The options not given stay as they are, e.g. `setting agent
--only-from 10.0.0.5` keeps the agent version. Mail and the management
interface are turned off with `--off`.

Where the web UI asks for confirmation, the API requires an explicit
acknowledgement. A refused request names the options to add, e.g.:

```console
$ cmk-appliance site update mysite 2.4.0p1.cee
ERROR: The operation requires explicit acknowledgement. [precondition_failed]
To proceed, add: --acknowledge-site-stop
```

On an interactive terminal, the output is colored, e.g. errors, states and
the options to add. Piped or redirected output stays plain, as does all output
when the environment variable `NO_COLOR` is set.

`cmk-appliance --help` and `cmk-appliance COMMAND --help` list all commands.

An unexpected error of the command line client - a bug of the client, not a
refused request - writes a crash report to the state directory of the user
(`$XDG_STATE_HOME/cmk-appliance/crashes`, by default
`~/.local/state/cmk-appliance/crashes`). It never contains the API token.
Nothing is sent automatically: `cmk-appliance crash list` lists the reports,
`cmk-appliance crash show ID` shows one as it would be sent and
`cmk-appliance crash submit ID --name NAME --mail MAIL` sends it to the crash
report server of Checkmk, with your contact information in case of questions. The error message names these commands.

## Library

```python
from cmk.appliance.client import ApiError, Client

client = Client("https://appliance.example.com", token="cma_...")

print(client.info().firmware_version)

version = client.install_version("check-mk-cloud-2.4.0p1-1.7-x86_64.cma")
plan = client.plan_site_update("mysite", version)
job_id = client.update_site("mysite", version, acknowledge_site_stop=plan.site_running)
client.wait_for_job(job_id)

try:
    client.remove_version("2.3.0p1.cee")
except ApiError as e:
    print(e.status, e.code, e.message, e.details)
```

- Every client method maps to one API endpoint and returns typed models
  (`cmk.appliance.client.models`). Long-running operations return a job ID;
  `wait_for_job()` polls it, `wait_for_restart()` follows a reboot or firmware
  update until the device answers again. Jobs are known on the node running
  them only: Address the nodes of a cluster, not the cluster IP address, to
  follow jobs moving it. Otherwise the waits raise `OtherDeviceError`.
- Refused requests raise `ApiError`. Its `code` is part of the API contract,
  `message` is not. `requires` lists missing acknowledgements, `job_id` the
  running job a request conflicts with.
- Connection problems and non-API answers (e.g. a web server error page while
  the appliance reboots) raise `TransportError`. All exceptions derive from
  `ClientError`.
- Responses are parsed tolerantly: Fields added by newer firmware are
  ignored, as the compatibility rules of the API require.
- The HTTP layer is pluggable (`transport=`, see `Transport`); the default
  uses `urllib`. Redirects are not followed, so the token is never sent to
  another address: Use the URL the appliance is reachable at (e.g. `https://`
  when it redirects HTTP to HTTPS).

Uploads of Checkmk packages and firmware images are streamed from disk;
`on_upload_progress` is called with the bytes sent and the total, e.g. to
show the progress. The appliance processes them synchronously and does not
answer other requests meanwhile. Like the firmware update request, which checks the staged image
first, they use `long_timeout` instead of `timeout`.

## Versioning

The client follows Semantic Versioning, independently of the appliance
firmware. While the REST API is experimental, the client stays at `0.x`: A
minor release may require changes to code using it, a patch release never
does. Pin the minor version, e.g. `cmk-appliance-client~=0.1.0`.

The public interface is what the API reference documents, plus the error
codes of `ApiError`. Everything else, including the output of the command
line client, may change in any release.

## Compatibility with the appliance firmware

Which appliance features the client can use does not depend on its version
but on the API revision it covers (`API_REVISION`, e.g. `1.0`). The appliance
names its own revision in every response. Firmware releases without API
changes keep it, new endpoints and request fields raise its minor.

- **Older firmware:** Everything the firmware knows works. The client only
  sends the request fields a call uses, so calls not using newer options
  work with older firmware as well. Using an endpoint or option the firmware
  does not know raises `UnsupportedFeatureError`, naming the API revision it
  needs and the one of the appliance.
- **Newer firmware:** Everything works. The client warns once with
  `OutdatedClientWarning` that an update gives access to the newer features.
- **Deprecated API version:** Once an API version has a stable successor,
  the appliance deprecates it. The client warns with `ApiDeprecatedWarning`,
  naming the planned removal and the successor. A removed version is refused
  with the error code `api_version_removed`.
- **Firmware without REST API:** Raises `ApiUnavailableError`.

`Client.api_revision` and `Client.api_status` hold the revision and status
(`experimental`, `stable`, `deprecated`) the appliance named last. The
warnings derive from `ClientWarning`, so they can be filtered with the
`warnings` module. The command line client prints them as `warning:` lines.

## Security

Use HTTPS: The API token grants full administrative access to the appliance.
The appliance serves its web interface via plain HTTP by default. Set its web
access mode to "HTTPS enforced" and use an `https://` URL; a bare host name
like `appliance.example.com` means HTTPS. With an `http://` URL, the client
warns once with `InsecureTransportWarning` before it sends the token
unencrypted. On a trusted network, filter it with the `warnings` module.

## Changes

The changes of each release are described as werks, published with the werks
of Checkmk: https://werks.checkmk.com/?product=cma_client

## License

Copyright (C) 2026 Checkmk GmbH. Licensed under the Apache License, Version
2.0, see `LICENSE`.

