NanoLink Python SDK
Python SDK for NanoLink monitoring system.
Installation
pip install kkape-nanolink
Or install from source:
cd sdk/python
pip install -e .
Quick Start
import asyncio
import os
import secrets
from nanolink import NanoLinkServer, ServerConfig, Metrics, AgentConnection
from nanolink.connection import ValidationResult
async def main():
expected_token = os.environ["NANOLINK_AGENT_TOKEN"]
def validate_token(token: str) -> ValidationResult:
if secrets.compare_digest(expected_token, token):
return ValidationResult(valid=True, permission_level=0)
return ValidationResult(valid=False, error_message="Invalid token")
# Authentication is required by default.
server = NanoLinkServer(ServerConfig(
grpc_port=39100,
token_validator=validate_token,
))
# Handle agent connections
@server.on_agent_connect
async def handle_connect(agent: AgentConnection):
print(f"Agent connected: {agent.hostname}")
print(f" OS: {agent.os}/{agent.arch}")
print(f" Version: {agent.version}")
# Handle agent disconnections
@server.on_agent_disconnect
async def handle_disconnect(agent: AgentConnection):
print(f"Agent disconnected: {agent.hostname}")
# Handle metrics
@server.on_metrics
async def handle_metrics(metrics: Metrics):
if metrics.cpu:
print(f"CPU: {metrics.cpu.usage_percent:.1f}%")
if metrics.cpu.model:
print(f" Model: {metrics.cpu.model}")
if metrics.memory:
print(f"Memory: {metrics.memory.usage_percent:.1f}%")
for gpu in metrics.gpus:
print(f"GPU: {gpu.name} - {gpu.usage_percent:.1f}%")
# Start server
await server.run_forever()
if __name__ == "__main__":
asyncio.run(main())
Token Validation
from nanolink import NanoLinkServer, ServerConfig, ValidationResult
def my_token_validator(token: str) -> ValidationResult:
if token == "admin-token":
return ValidationResult(valid=True, permission_level=3)
elif token == "read-token":
return ValidationResult(valid=True, permission_level=0)
else:
return ValidationResult(valid=False, error_message="Invalid token")
config = ServerConfig(
grpc_port=39100,
token_validator=my_token_validator,
)
server = NanoLinkServer(config)
Sending Commands
from nanolink import Command
# Get agent
agent = server.get_agent_by_hostname("my-server")
# List processes
result = await agent.send_command(Command.process_list())
print(result.output)
# Restart a service
result = await agent.send_command(Command.service_restart("nginx"))
if result.success:
print("Service restarted successfully")
else:
print(f"Error: {result.error}")
# Execute shell command (requires SuperToken)
result = await agent.send_command(
Command.shell_execute("df -h", super_token="your-super-token")
)
Permission Levels
| Level | Name | Allowed Operations |
|---|---|---|
| 0 | READ_ONLY | View metrics, process list, logs |
| 1 | BASIC_WRITE | Download files, clear temp files |
| 2 | SERVICE_CONTROL | Restart services, Docker containers, kill processes |
| 3 | SYSTEM_ADMIN | System reboot, execute shell commands |
TLS Configuration
config = ServerConfig(
port=9100,
tls_cert_path="/path/to/cert.pem",
tls_key_path="/path/to/key.pem",
)
server = NanoLinkServer(config)
Development
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run linting
ruff check .
black --check .
mypy .
License
Apache 2.0 License - see LICENSE for details.
Metadata
Release files for kkape-nanolink 0.4.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kkape_nanolink-0.4.9.tar.gz | 39.3 kB | Without provenance | Details for kkape_nanolink-0.4.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kkape_nanolink-0.4.9-py3-none-any.whl | Python 3 | none | any | Details for kkape_nanolink-0.4.9-py3-none-any.whl |
Total release size: 82.6 kB
Release files / kkape_nanolink-0.4.9.tar.gz
| Download URL | kkape_nanolink-0.4.9.tar.gz |
|---|---|
| Size | 39.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86afed6b7af2f675ad2e3ab58f82608ba47de12c66038e9b2226a827655f2081
|
|
BLAKE2b-256 checksum How to use checksums |
1e3407d5f3e9b99afad087e1d98d4e3df7c27682da26c837a79ea46876f30e7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / kkape_nanolink-0.4.9-py3-none-any.whl
| Download URL | kkape_nanolink-0.4.9-py3-none-any.whl |
|---|---|
| Size | 43.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1423d3e02a0cac84ab8338e21c6d5a57d00523f5f934d96977a2c82ef3518683
|
|
BLAKE2b-256 checksum How to use checksums |
d438f741cbf2a4661f70b045f76dfd6d650c7449d692bf9a44f3eccaa2c65fde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for kkape-nanolink 0.4.9
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.