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)
  • Stealth Transport (Default-On): RFC 8484 binary DNS-over-HTTPS (doh=True with IP-based resolvers) and Hidden SNI (hide_sni=True omitting TLS ClientHello extension 0x0000, optional decoy sni, full CA + RFC 6125 real-host verification, and opt-in sni_fallback)
  • 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.2.1

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.2.1
File Size Uploaded Provenance Actions
nz_sdk-0.2.1.tar.gz 96.6 kB Without provenance Details for nz_sdk-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 181.6 kB

Release files / nz_sdk-0.2.1.tar.gz

Download URL nz_sdk-0.2.1.tar.gz
Size 96.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4706ce7469ed421a11090259fdfb800460e89c231362978c5b21a91d95e8a4ef
BLAKE2b-256 checksum
How to use checksums
ae12131fdd5801cccd678d645d31da882655582b3f6dad043b6d3e528463ce83
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.2.1-py3-none-any.whl

Download URL nz_sdk-0.2.1-py3-none-any.whl
Size 85.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c50767c574e7ebe79dcb33bf484a1782d7e796880305fcfd1a2d9d9ca3886e8
BLAKE2b-256 checksum
How to use checksums
467538e60f3d26ab9d65b6b0f8a07ac8b8276471c6705099362363ae022ffbff
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.2.1

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