- Python 96.7%
- Shell 3%
- Go 0.3%
|
|
||
|---|---|---|
| .forgejo | ||
| .github | ||
| debian | ||
| docs | ||
| examples | ||
| proto | ||
| rpm | ||
| scripts | ||
| src/tetragon_client | ||
| tests | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| CONTRIBUTING.md | ||
| Dockerfile.debbuild | ||
| Dockerfile.rpmbuild | ||
| LICENSE | ||
| poetry.lock | ||
| pyproject.toml | ||
| README.md | ||
| release.sh | ||
| RELEASING.md | ||
| tests.sh | ||
| TETRAGON_VERSION | ||
tetragon-client
A typed Python client for Cilium Tetragon, the eBPF security observability and runtime enforcement tool. It works with Tetragon on Kubernetes or, as here, on a plain Docker host.
- The whole gRPC API. Stream events live; load, list, configure and delete tracing policies; check health; change log levels; dump the process cache; tune export rotation. Sync and asyncio clients.
- Export files without gaps.
ExportFileReaderreads Tetragon's JSON export (--export-filename), follows its rotation (including gzipped backups) and resumes from a checkpoint after a restart or crash, so your program misses nothing while it's down. - One event type. Both sources give you the same
GetEventsResponseprotobuf messages (or plain dicts), so your handling code doesn't care where events came from. - Typed and packaged. Ships type stubs (
py.typed), is checked withmypy --strict, and is available from PyPI and as Debian, Ubuntu and Fedora packages.
It's a general-purpose library: anything that wants Tetragon events (a SIEM forwarder, an alerting script, a compliance tool's ingester) can use it.
Install
From PyPI with pip or pipx:
pip install tetragon-client
On Debian 12/13 and Ubuntu 24.04 / 26.04, from the mig5 apt repository:
sudo mkdir -p /usr/share/keyrings
curl -fsSL https://mig5.net/static/mig5.asc | sudo gpg --dearmor -o /usr/share/keyrings/mig5.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mig5.gpg] https://apt.mig5.net $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mig5.list
sudo apt update
sudo apt install python3-tetragon-client
On Fedora 43 / 44, from the mig5 rpm repository:
sudo rpm --import https://mig5.net/static/mig5.asc
sudo tee /etc/yum.repos.d/mig5.repo > /dev/null << 'EOF'
[mig5]
name=mig5 Repository
baseurl=https://rpm.mig5.net/$releasever/rpm/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mig5.net/static/mig5.asc
EOF
sudo dnf install python3-tetragon-client
The release key's fingerprint is 54A9 1143 AE0A B4F7 743B 01FE 888E D1B4 23A3 BC99.
PyPI releases come with detached signatures and a signed SHA256SUMS.
Quick start
Live events over gRPC
from tetragon_client import TetragonClient, event_filter, event_kind
with TetragonClient() as client: # unix:///var/run/tetragon/tetragon.sock
print("Tetragon", client.version())
for event in client.events(allow=[event_filter(kinds=["process_exec"])]):
proc = event.process_exec.process
print(proc.binary, proc.arguments)
The stream reconnects by itself when Tetragon restarts. Events emitted while it's disconnected are not replayed; read the export file when every event matters.
Every event, from the export file
from tetragon_client import ExportFileReader, FileCheckpoint
reader = ExportFileReader(
"/var/log/tetragon/tetragon.log",
FileCheckpoint("/var/lib/my-ingester/tetragon.pos"),
)
with reader:
for event in reader: # follows rotation; resumes where it stopped
handle(event)
Delivery is at-least-once: after a crash, the last few events may be delivered again. (Under extreme load Tetragon can drop events before they reach the file at all; see export files.)
To store each event exactly once, keep the position in your own database and commit it with the events (examples/sqlite_checkpoint.py).
Managing policies
with TetragonClient() as client:
for policy in client.list_tracing_policies():
print(policy.name, policy.state, policy.mode)
client.configure_tracing_policy("file-monitoring-filtered", mode="monitor")
From the shell
tetragon-events grpc --kind process_exec
tetragon-events file /var/log/tetragon/tetragon.log --checkpoint ~/.tetragon.pos
tetragon-events version
Each event is printed as one line of JSON, in the same shape as
tetra getevents -o json.
Running Tetragon for this library
With Tetragon in Docker, the client needs the gRPC socket and, for
ExportFileReader, the export directory mounted on the host:
docker run --name tetragon --rm --pull always \
--pid=host --cgroupns=host --privileged \
-v /sys/kernel/btf/vmlinux:/var/lib/tetragon/btf \
-v /etc/tetragon/tetragon.tp.d:/etc/tetragon/tetragon.tp.d:ro \
-v /var/run/tetragon:/var/run/tetragon \
-v /var/log/tetragon:/var/log/tetragon \
quay.io/cilium/tetragon:v1.8.0 \
--export-filename /var/log/tetragon/tetragon.log \
--export-file-max-size-mb 100 --export-file-max-backups 20 --export-file-compress
See docs/deployment.md for permissions, retention and security.
Documentation
- Deployment: Tetragon flags, socket and file access, retention, security.
- gRPC client: events and filters, reconnection, every API method, asyncio, TLS.
- Export files: rotation, checkpoints, delivery guarantees, custom stores.
- Writing an ingester: choosing a source, error handling, deduplication.
- Command line:
tetragon-events. - Protobuf compatibility: supported versions, distro packages, updating Tetragon's API.
- Examples: streaming, enforcement alerts, checkpointed ingestion, exactly-once storage, policy management.
- Contributing and releasing.
Compatibility
| Python | 3.11 to 3.14 |
| Tetragon API | generated from v1.8.0 (TETRAGON_VERSION); newer servers work, and unknown fields and event kinds are tolerated |
| PyPI wheel | grpcio ≥ 1.71.2, protobuf ≥ 5.29 and < 8 |
| Debian / Ubuntu / Fedora packages | the distribution's own python3-grpcio and python3-protobuf |
Licence
Apache-2.0. The vendored Tetragon API definitions in proto/ and the code
generated from them are Apache-2.0, © Authors of Hubble/Tetragon.