Skip to main content

nz-sdk (Python)

Wire-compatible native Python SDK and drop-in CLI implementation of nezhahq/agent for Nezha Monitoring.

  • Full gRPC protocol parity across all 6 proto.NezhaService RPCs (ReportSystemInfo2, ReportSystemInfo, ReportSystemState, RequestTask, IOStream, ReportGeoIP)
  • All 20 TaskTypes: HTTP/ICMP/TCP probes, shell commands, remote config reload & server transfer, MCP RPCs (Exec, FsList, FsRead, FsWrite, FsDelete, FsTransfer), and IOStream sub-protocols (TerminalGRPC, NAT, File Manager v1+v2)
  • Async-first SDK (asyncio + grpc.aio) with a thread-backed synchronous wrapper (SyncNezhaAgent), custom collector hooks, task handler overrides, and lifecycle callbacks
  • Drop-in CLI (nz-agent / python -m nz_sdk) compatible with config.yml and NZ_* environment variables

Installation

pip install nz-sdk

Quickstart (Async SDK)

import asyncio
from nz_sdk import AgentConfig, NezhaAgent


async def main() -> None:
    config = AgentConfig(
        server="dashboard.example.com:443",
        client_secret="your-agent-secret",
        tls=True,
    )
    async with NezhaAgent(
        config,
        on_connected=lambda boot_time: print(f"Connected! Dashboard boot: {boot_time}"),
        on_state_reported=lambda state: print(f"CPU: {state.cpu:.1f}%"),
    ) as agent:
        await asyncio.sleep(60)


asyncio.run(main())

Quickstart (Synchronous / Blocking)

from nz_sdk import AgentConfig, NezhaAgent

config = AgentConfig.from_yaml("config.yml")
agent = NezhaAgent(config)
agent.run_sync()

Or run in a background thread via SyncNezhaAgent:

from nz_sdk import AgentConfig, SyncNezhaAgent

with SyncNezhaAgent(AgentConfig.from_env()) as agent:
    ...

Custom Collector & Task Hooks

from nz_sdk import (
    AgentConfig,
    HostInfo,
    HostState,
    NezhaAgent,
    PsutilCollector,
    TaskRequest,
    TaskResponse,
    TaskType,
)


class CustomCollector(PsutilCollector):
    async def collect_host(self, config: AgentConfig) -> HostInfo:
        info = await super().collect_host(config)
        info.platform = "custom-embedded-linux"
        return info


async def handle_upgrade(task: TaskRequest) -> TaskResponse:
    return TaskResponse(id=task.id, type=task.type, successful=True, data="handled")


agent = NezhaAgent(
    AgentConfig(server="127.0.0.1:8008", client_secret="secret"),
    collector=CustomCollector(),
    task_handlers={TaskType.UPGRADE: handle_upgrade},
)

CLI Usage

# Load from config.yml (with NZ_* env var overrides)
nz-agent -c /etc/nezha/config.yml

# Or configure purely via flags / environment variables
nz-agent --server dashboard.example.com:443 --secret YOUR_SECRET --tls

# Equivalent module invocation
python -m nz_sdk --server 127.0.0.1:8008 --secret YOUR_SECRET

License

MIT License. See the repository root LICENSE for details and upstream attributions (nezhahq/agent WTFPL v2, Telegraf GPU parsers MIT).

Metadata

Release files for nz-sdk 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nz-sdk 0.1.0
File Size Uploaded Provenance Actions
nz_sdk-0.1.0.tar.gz 77.2 kB Without provenance Details for nz_sdk-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nz-sdk 0.1.0
File Interpreter ABI Platform Actions
nz_sdk-0.1.0-py3-none-any.whl Python 3 none any Details for nz_sdk-0.1.0-py3-none-any.whl

Total release size: 150.2 kB

Release files / nz_sdk-0.1.0.tar.gz

Download URL nz_sdk-0.1.0.tar.gz
Size 77.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fa3e2f3a5078c0f41456a196336fffb8e98bec6eab7cd389b34371dd6881980a
BLAKE2b-256 checksum
How to use checksums
f1ef6a79c25867ef0d417b0fa76cc54895fc1d11c6e40e86ddcfdc211a349436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / nz_sdk-0.1.0-py3-none-any.whl

Download URL nz_sdk-0.1.0-py3-none-any.whl
Size 73.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67e3d9d1bace2381cca01a2f0010774059a03b45880da8bedd88bdebb98bcb80
BLAKE2b-256 checksum
How to use checksums
73121fda2cca73856ec434154d85a35f831931a5ba912a9933e91858b2832a1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for nz-sdk 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page