2 Commits

Author SHA1 Message Date
f44df8b1fe Fix memory_used OID parsing for Mikrotik devices
Handle OctetString values returned as bytes by converting them to int
using little-endian byte order (4 bytes) for Mikrotik devices.
2026-06-09 22:48:27 +02:00
8e6d193d4c feat: Ajoute les services systemd pour snmp2mqtt 2026-06-09 16:52:58 +02:00
4 changed files with 94 additions and 2 deletions

39
SYSTEMD.md Normal file
View File

@@ -0,0 +1,39 @@
# Service systemd pour snmp2mqtt
## Installation
Copiez le fichier dans `/etc/systemd/system/` :
```bash
sudo cp snmp2mqtt.service /etc/systemd/system/
sudo cp snmp-discover.service /etc/systemd/system/
```
Créez un utilisateur dédié :
```bash
sudo useradd -r -s /bin/false snmp2mqtt
sudo chown -R snmp2mqtt:snmp2mqtt /home/snmp2mqtt/snmp2mqtt
```
Rechargez systemd et activez le service :
```bash
sudo systemctl daemon-reload
sudo systemctl enable snmp2mqtt
sudo systemctl start snmp2mqtt
```
## Journalisation
```bash
journalctl -u snmp2mqtt -f
```
## Pour snmp-discover
Le service `snmp-discover.service` est de type `oneshot` et ne doit pas être activé automatiquement. Il est destiné à être lancé manuellement pour configurer les appareils :
```bash
sudo systemctl start snmp-discover
```

15
snmp-discover.service Normal file
View File

@@ -0,0 +1,15 @@
[Unit]
Description=SNMP Discovery Tool for snmp2mqtt
Documentation=https://git.antoineve.me/AntoineVe/snmp2mqtt
[Service]
Type=oneshot
User=snmp2mqtt
Group=snmp2mqtt
WorkingDirectory=/home/snmp2mqtt/snmp2mqtt
ExecStart=/home/snmp2mqtt/snmp2mqtt/.venv/bin/python /home/snmp2mqtt/snmp2mqtt/snmp-discover.py --config /home/snmp2mqtt/snmp2mqtt/config.yaml
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target

View File

@@ -278,10 +278,29 @@ async def get_snmp(req):
continue
else:
for varBind in varBinds:
logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}")
raw_value = varBind[1]
# Handle different SNMP value types
if oid['type'] == int and hasattr(raw_value, '__bytes__'):
# Convert OctetString/bytes to int
# For Mikrotik devices, memory values might be in the first 4 bytes
raw_bytes = bytes(raw_value)
# Try to interpret as integer, handling different byte orders
try:
if len(raw_bytes) >= 4:
# Try little-endian first (common for Mikrotik)
value = int.from_bytes(raw_bytes[:4], byteorder='little', signed=False)
else:
value = int.from_bytes(raw_bytes, byteorder='big', signed=False)
except (ValueError, TypeError):
value = raw_value
else:
value = raw_value
logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](value)}")
# Cast to the right type
value = oid['type'](varBind[1])
value = oid['type'](value)
# Apply operation if defined
if 'operation' in oid:

19
snmp2mqtt.service Normal file
View File

@@ -0,0 +1,19 @@
[Unit]
Description=SNMP to MQTT bridge for Home Assistant
Documentation=https://git.antoineve.me/AntoineVe/snmp2mqtt
After=network.target
Wants=network.target
[Service]
Type=simple
User=snmp2mqtt
Group=snmp2mqtt
WorkingDirectory=/home/snmp2mqtt/snmp2mqtt
ExecStart=/home/snmp2mqtt/snmp2mqtt/.venv/bin/python /home/snmp2mqtt/snmp2mqtt/snmp2mqtt.py --config /home/snmp2mqtt/snmp2mqtt/config.yaml
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target