Metadata-Version: 2.4
Name: nz-sdk
Version: 0.2.0
Summary: Wire-compatible native Python SDK and drop-in CLI agent for Nezha Monitoring (nezhahq/nezha)
Author: kaecho
License-Expression: MIT
Project-URL: Homepage, https://github.com/kaecho/nz-sdk
Project-URL: Repository, https://github.com/kaecho/nz-sdk
Project-URL: Issues, https://github.com/kaecho/nz-sdk/issues
Project-URL: Documentation, https://github.com/kaecho/nz-sdk#readme
Keywords: nezha,nezha-agent,monitoring,grpc,telemetry,sdk
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
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: Topic :: System :: Monitoring
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: grpcio>=1.85.4
Requires-Dist: protobuf>=7.35.1
Requires-Dist: psutil>=5.9.0
Requires-Dist: PyYAML>=6.0
Requires-Dist: httpx>=0.27.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.24.0; extra == "dev"
Requires-Dist: ruff>=0.6.0; extra == "dev"
Requires-Dist: mypy>=1.10.0; extra == "dev"
Requires-Dist: build>=1.2.0; extra == "dev"
Requires-Dist: twine>=5.0.0; extra == "dev"
Requires-Dist: grpcio-tools>=1.60.0; extra == "dev"
Requires-Dist: cryptography>=42.0.0; extra == "dev"
Requires-Dist: types-PyYAML>=6.0.12; extra == "dev"
Requires-Dist: types-psutil>=5.9.5; extra == "dev"

# nz-sdk (Python)

Wire-compatible native Python SDK and drop-in CLI implementation of [`nezhahq/agent`](https://github.com/nezhahq/agent) for [Nezha Monitoring](https://github.com/nezhahq/nezha).

- **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 `TaskType`s**: 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

```bash
pip install nz-sdk
```

## Quickstart (Async SDK)

```python
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)

```python
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`:

```python
from nz_sdk import AgentConfig, SyncNezhaAgent

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

## Custom Collector & Task Hooks

```python
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

```bash
# 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).
