Compare commits
	
		
			11 Commits
		
	
	
		
			96b0cd9b99
			...
			update-pah
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						2309ef1deb
	
				 | 
					
					
						|||
| 
						
						
							
						
						75f8c6a637
	
				 | 
					
					
						|||
| 
						
						
							
						
						0d54632c52
	
				 | 
					
					
						|||
| 
						
						
							
						
						5b670c4708
	
				 | 
					
					
						|||
| 
						
						
							
						
						85a14f4fa0
	
				 | 
					
					
						|||
| 
						
						
							
						
						e4e79a34a9
	
				 | 
					
					
						|||
| 
						
						
							
						
						ffd86281ef
	
				 | 
					
					
						|||
| 
						
						
							
						
						e45c3c1f18
	
				 | 
					
					
						|||
| 
						
						
							
						
						7199432169
	
				 | 
					
					
						|||
| 
						
						
							
						
						4e2dec2441
	
				 | 
					
					
						|||
| 
						
						
							
						
						083cfcce1d
	
				 | 
					
					
						
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -172,3 +172,4 @@ cython_debug/
 | 
				
			|||||||
#.idea/
 | 
					#.idea/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WARP.md
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										213
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										213
									
								
								README.md
									
									
									
									
									
								
							@@ -15,7 +15,7 @@ Passerelle SNMP vers MQTT pour l'intégration Home Assistant. Ce script Python s
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
### Composants principaux
 | 
					### Composants principaux
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- **Client SNMP** : Utilise `pysnmp.hlapi.asyncio.slim` pour la récupération asynchrone des données SNMP
 | 
					- **Client SNMP** : Utilise `pysnmp.hlapi.asyncio` (version 7.x) pour la récupération asynchrone des données SNMP avec `get_cmd`, `SnmpEngine` et `UdpTransportTarget`
 | 
				
			||||||
- **Publisher MQTT** : Utilise `paho.mqtt.client` pour publier les données vers un broker MQTT
 | 
					- **Publisher MQTT** : Utilise `paho.mqtt.client` pour publier les données vers un broker MQTT
 | 
				
			||||||
- **Intégration Home Assistant** : Génère la configuration de découverte automatique compatible avec Home Assistant MQTT Discovery
 | 
					- **Intégration Home Assistant** : Génère la configuration de découverte automatique compatible avec Home Assistant MQTT Discovery
 | 
				
			||||||
- **Traitement des données** : Convertit les valeurs des OID SNMP vers les types appropriés (int, bool) pour les capteurs Home Assistant
 | 
					- **Traitement des données** : Convertit les valeurs des OID SNMP vers les types appropriés (int, bool) pour les capteurs Home Assistant
 | 
				
			||||||
@@ -28,6 +28,16 @@ Passerelle SNMP vers MQTT pour l'intégration Home Assistant. Ce script Python s
 | 
				
			|||||||
- Accès réseau aux équipements SNMP à surveiller
 | 
					- Accès réseau aux équipements SNMP à surveiller
 | 
				
			||||||
- Broker MQTT accessible
 | 
					- Broker MQTT accessible
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Dépendances principales
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- **pysnmp >= 7.0.0** : Bibliothèque SNMP avec nouvelle API asynchrone
 | 
				
			||||||
 | 
					- **paho-mqtt >= 2.0.0** : Client MQTT pour la communication avec le broker
 | 
				
			||||||
 | 
					- **PyYAML >= 6.0.0** : Parsing des fichiers de configuration YAML
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					⚠️ **Notes importantes sur les versions** :
 | 
				
			||||||
 | 
					- **pysnmp 7.x** : Changements d'API incompatibles avec les versions 6.x et antérieures. L'ancienne classe `Slim` a été supprimée au profit de `get_cmd()` avec des objets `SnmpEngine`, `UdpTransportTarget`, etc.
 | 
				
			||||||
 | 
					- **paho-mqtt 2.x** : Nouvelle API de callbacks (VERSION2) qui remplace l'ancienne API deprecated (VERSION1). Les signatures des callbacks ont changé.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Configuration de l'environnement
 | 
					### Configuration de l'environnement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
@@ -156,21 +166,136 @@ python snmp2mqtt.py -c config.yaml
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Intégration Home Assistant
 | 
					## Intégration Home Assistant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### MQTT Discovery (Découverte automatique)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Le script implémente le protocole **MQTT Discovery** de Home Assistant pour une intégration transparente et automatique. Aucune configuration manuelle n'est nécessaire dans Home Assistant.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Fonctionnement de l'autodécouverte
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. **Au démarrage** : Publication des configurations de découverte
 | 
				
			||||||
 | 
					2. **Pendant l'exécution** : Mise à jour continue des états des capteurs
 | 
				
			||||||
 | 
					3. **Surveillance** : Gestion des statuts de disponibilité (online/offline)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Topics MQTT générés
 | 
					### Topics MQTT générés
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Le script génère automatiquement les topics MQTT suivants :
 | 
					#### Topics de découverte (Discovery)
 | 
				
			||||||
 | 
					Chaque capteur génère un topic de configuration individuel :
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					homeassistant/{platform}/{node_id}/{object_id}/config
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- **Configuration** : `homeassistant/device/{device_id}/config`
 | 
					**Exemples** :
 | 
				
			||||||
- **État** : `SNMP/{device_name}/state`
 | 
					```bash
 | 
				
			||||||
 | 
					# Capteur de trafic réseau
 | 
				
			||||||
 | 
					homeassistant/sensor/mikrotik_hex/mikrotik_hex_starlink_in/config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Découverte automatique
 | 
					# Statut de connectivité  
 | 
				
			||||||
 | 
					homeassistant/binary_sensor/mikrotik_hex/mikrotik_hex_starlink_status/config
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Les équipements et capteurs sont automatiquement découverts dans Home Assistant via MQTT Discovery. Chaque équipement apparaîtra avec :
 | 
					#### Topics de données
 | 
				
			||||||
 | 
					- **État** : `SNMP/{device_name}/state` - Données JSON des capteurs
 | 
				
			||||||
 | 
					- **Disponibilité** : `SNMP/{device_name}/availability` - Statut online/offline
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Un identifiant unique basé sur le nom et l'IP
 | 
					### Configuration automatique des capteurs
 | 
				
			||||||
- Des capteurs groupés par équipement
 | 
					
 | 
				
			||||||
- Des icônes et unités appropriées selon la classe d'équipement
 | 
					Chaque capteur est configuré avec :
 | 
				
			||||||
- Des modèles de valeurs JSON pour extraire les données
 | 
					
 | 
				
			||||||
 | 
					```json
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  "name": "mikrotik_hex starlink_in",
 | 
				
			||||||
 | 
					  "unique_id": "mikrotik_hex_192_168_10_2_starlink_in",
 | 
				
			||||||
 | 
					  "state_topic": "SNMP/mikrotik_hex/state",
 | 
				
			||||||
 | 
					  "value_template": "{{ value_json.starlink_in }}",
 | 
				
			||||||
 | 
					  "device_class": "data_size",
 | 
				
			||||||
 | 
					  "unit_of_measurement": "bit",
 | 
				
			||||||
 | 
					  "icon": "mdi:network",
 | 
				
			||||||
 | 
					  "device": {
 | 
				
			||||||
 | 
					    "identifiers": ["snmp2mqtt_mikrotik_hex_192_168_10_2"],
 | 
				
			||||||
 | 
					    "name": "mikrotik_hex",
 | 
				
			||||||
 | 
					    "model": "SNMP Device",
 | 
				
			||||||
 | 
					    "manufacturer": "Network Equipment"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "availability": {
 | 
				
			||||||
 | 
					    "topic": "SNMP/mikrotik_hex/availability",
 | 
				
			||||||
 | 
					    "payload_available": "online",
 | 
				
			||||||
 | 
					    "payload_not_available": "offline"
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Découverte automatique dans Home Assistant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Regroupement par équipement
 | 
				
			||||||
 | 
					Tous les capteurs d'un même équipement sont automatiquement regroupés sous une seule carte d'équipement :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- **Identifiant unique** basé sur `device_name` + `ip`
 | 
				
			||||||
 | 
					- **Nom d'affichage** basé sur le nom de l'équipement
 | 
				
			||||||
 | 
					- **Métadonnées** : modèle, fabricant, version du logiciel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Types de capteurs supportés
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| Type HA | Platform | Description | Icône |
 | 
				
			||||||
 | 
					|---------|----------|-------------|-------|
 | 
				
			||||||
 | 
					| `sensor` | `sensor` | Valeurs numériques (trafic, CPU, etc.) | Selon device_class |
 | 
				
			||||||
 | 
					| `binary_sensor` | `binary_sensor` | États binaires (online/offline, actif/inactif) | mdi:network-outline |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Classes d'équipements et icônes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| Device Class | Utilisation | Icône Auto | Unité Suggérée |
 | 
				
			||||||
 | 
					|--------------|-------------|------------|----------------|
 | 
				
			||||||
 | 
					| `data_size` | Trafic réseau, volumes de données | mdi:network | bit, byte, MB, GB |
 | 
				
			||||||
 | 
					| `connectivity` | Statut des interfaces, connexions | mdi:network-outline | - |
 | 
				
			||||||
 | 
					| `power_factor` | Pourcentages (CPU, mémoire) | mdi:gauge | % |
 | 
				
			||||||
 | 
					| `temperature` | Températures d'équipements | mdi:thermometer | °C, °F |
 | 
				
			||||||
 | 
					| `signal_strength` | Qualité des signaux | mdi:signal | dBm, % |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Surveillance de disponibilité
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Statuts de disponibilité
 | 
				
			||||||
 | 
					- **Online** : Équipement accessible et données mises à jour
 | 
				
			||||||
 | 
					- **Offline** : Équipement inaccessible ou erreurs SNMP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Mécanisme de heartbeat
 | 
				
			||||||
 | 
					- Mise à jour du statut à chaque cycle de surveillance
 | 
				
			||||||
 | 
					- Marquage offline automatique en cas d'erreur
 | 
				
			||||||
 | 
					- Statut offline lors de l'arrêt du script
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Persistance et redémarrages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Configuration Discovery retenue
 | 
				
			||||||
 | 
					- **Flag retain=true** sur les topics de configuration
 | 
				
			||||||
 | 
					- **Redécouverte automatique** après redémarrage de Home Assistant
 | 
				
			||||||
 | 
					- **Pas de perte de configuration** lors des redémarrages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Données d'état temps réel
 | 
				
			||||||
 | 
					- **Flag retain=false** sur les données d'état  
 | 
				
			||||||
 | 
					- **Données fraîches uniquement** après redémarrage
 | 
				
			||||||
 | 
					- **Historique préservé** par Home Assistant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Intégration dans l'interface Home Assistant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Après démarrage du script, vous verrez automatiquement :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. **Page Équipements** : Nouveaux équipements SNMP avec leurs capteurs
 | 
				
			||||||
 | 
					2. **États et Historiques** : Données temps réel et graphiques
 | 
				
			||||||
 | 
					3. **Cartes automatiques** : Ajout facile aux tableaux de bord
 | 
				
			||||||
 | 
					4. **Notifications** : Alertes sur les changements d'état
 | 
				
			||||||
 | 
					5. **Automations** : Utilisation des capteurs dans les règles
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Exemple d'équipement découvert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					📱 mikrotik_hex (SNMP Device)
 | 
				
			||||||
 | 
					├── 📊 mikrotik_hex starlink_in (123.45 MB)
 | 
				
			||||||
 | 
					├── 📊 mikrotik_hex starlink_out (67.89 MB)  
 | 
				
			||||||
 | 
					├── 🔌 mikrotik_hex starlink_status (Online)
 | 
				
			||||||
 | 
					├── 📊 mikrotik_hex lan_bridge_in (234.56 MB)
 | 
				
			||||||
 | 
					├── 📊 mikrotik_hex lan_bridge_out (78.90 MB)
 | 
				
			||||||
 | 
					└── 🔌 mikrotik_hex lan_bridge_status (Online)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Statut: Online | Dernière mise à jour: il y a 2 secondes
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Exemple de configuration complète
 | 
					## Exemple de configuration complète
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -246,6 +371,16 @@ devices:
 | 
				
			|||||||
   - Vérifiez que MQTT Discovery est activé dans Home Assistant
 | 
					   - Vérifiez que MQTT Discovery est activé dans Home Assistant
 | 
				
			||||||
   - Surveillez les logs MQTT avec `mosquitto_sub`
 | 
					   - Surveillez les logs MQTT avec `mosquitto_sub`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					5. **Erreurs liées à PySNMP**
 | 
				
			||||||
 | 
					   - **"ModuleNotFoundError: No module named 'pysnmp.hlapi.asyncio.slim'"** : Vous utilisez une version pysnmp 6.x. Mettez à jour vers >= 7.0.0
 | 
				
			||||||
 | 
					   - **"Please call .create() to construct UdpTransportTarget object"** : Erreur corrigée dans cette version, utilisez `pip install -r requirements.txt`
 | 
				
			||||||
 | 
					   - **Erreurs d'importation SNMP** : Assurez-vous d'avoir pysnmp 7.x avec `pip show pysnmp`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					6. **Erreurs liées à Paho MQTT**
 | 
				
			||||||
 | 
					   - **"DeprecationWarning: Callback API version 1 is deprecated"** : Vous utilisez une version paho-mqtt < 2.0. Mettez à jour vers >= 2.0.0
 | 
				
			||||||
 | 
					   - **Erreurs de callback MQTT** : La nouvelle API VERSION2 change la signature des callbacks (ex: `on_connect` reçoit maintenant 5 paramètres)
 | 
				
			||||||
 | 
					   - **Vérification version** : `pip show paho-mqtt` pour confirmer la version installée
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Commandes de test utiles
 | 
					### Commandes de test utiles
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
@@ -263,7 +398,63 @@ ping 192.168.10.202
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Support multi-équipements
 | 
					## Support multi-équipements
 | 
				
			||||||
 | 
					
 | 
				
			||||||
**Note** : Le support complet multi-équipements nécessite une implémentation avec threading/multiprocessing. Actuellement, le script traite le premier équipement de la liste et affiche un avertissement pour les autres.
 | 
					Le script supporte nativement la surveillance simultanée de plusieurs équipements grâce à une architecture **multi-threading** :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Fonctionnement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- **Thread indépendant** pour chaque équipement configuré
 | 
				
			||||||
 | 
					- **Surveillance parallèle** : tous les équipements sont surveillés simultanément
 | 
				
			||||||
 | 
					- **Isolation des erreurs** : la défaillance d'un équipement n'affecte pas les autres
 | 
				
			||||||
 | 
					- **Clients MQTT séparés** : chaque thread utilise son propre client MQTT
 | 
				
			||||||
 | 
					- **Arrêt gracieux** : tous les threads s'arrêtent proprement sur signal
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Avantages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- ⚡ **Performance optimale** : pas de blocage entre équipements
 | 
				
			||||||
 | 
					- 🔄 **Traitement parallèle** : requêtes SNMP simultanées
 | 
				
			||||||
 | 
					- 🛡️ **Robustesse** : isolation des défaillances
 | 
				
			||||||
 | 
					- 📊 **Scalabilité** : facilement extensible à des dizaines d'équipements
 | 
				
			||||||
 | 
					- 🔧 **Maintenance** : logs clairement identifiés par équipement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Configuration multi-équipements
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```yaml
 | 
				
			||||||
 | 
					devices:
 | 
				
			||||||
 | 
					  routeur_principal:
 | 
				
			||||||
 | 
					    ip: "192.168.10.1"
 | 
				
			||||||
 | 
					    snmp_community: "public"
 | 
				
			||||||
 | 
					    oids:
 | 
				
			||||||
 | 
					      # ... configuration OID ...
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  switch_bureau:
 | 
				
			||||||
 | 
					    ip: "192.168.10.5"
 | 
				
			||||||
 | 
					    snmp_community: "public" 
 | 
				
			||||||
 | 
					    oids:
 | 
				
			||||||
 | 
					      # ... configuration OID ...
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  point_acces_wifi:
 | 
				
			||||||
 | 
					    ip: "192.168.10.10"
 | 
				
			||||||
 | 
					    snmp_community: "private"
 | 
				
			||||||
 | 
					    oids:
 | 
				
			||||||
 | 
					      # ... configuration OID ...
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Logs multi-threading
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Chaque thread est clairement identifié dans les logs :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					(INFO) [Device-routeur_principal] Starting monitoring thread
 | 
				
			||||||
 | 
					(INFO) [Device-switch_bureau] MQTT client connected
 | 
				
			||||||
 | 
					(DEBUG) [Device-point_acces_wifi] Published state to SNMP/point_acces_wifi/state
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Gestion des ressources
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- **Clients MQTT uniques** : ID client basé sur le nom de l'équipement
 | 
				
			||||||
 | 
					- **Topics séparés** : chaque équipement a ses propres topics MQTT
 | 
				
			||||||
 | 
					- **Discovery HA indépendante** : configuration Home Assistant par équipement
 | 
				
			||||||
 | 
					- **Disponibilité individuelle** : statut online/offline par équipement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Logs et debugging
 | 
					## Logs et debugging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										112
									
								
								WARP.md
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								WARP.md
									
									
									
									
									
								
							@@ -1,112 +0,0 @@
 | 
				
			|||||||
# WARP.md
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
This file provides guidance to WARP (warp.dev) when working with code in this repository.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Project Overview
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
snmp2mqtt is a Python script that bridges SNMP network device monitoring with MQTT messaging for Home Assistant integration. It specifically monitors a MikroTik router (Hex) and publishes network interface statistics and status information to MQTT topics for Home Assistant discovery.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Architecture
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Core Components
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
- **SNMP Client**: Uses `pysnmp.hlapi.asyncio.slim` for asynchronous SNMP data retrieval from network devices
 | 
					 | 
				
			||||||
- **MQTT Publisher**: Uses `paho.mqtt.client` to publish data to an MQTT broker
 | 
					 | 
				
			||||||
- **Home Assistant Integration**: Generates device discovery configuration compatible with Home Assistant MQTT Discovery
 | 
					 | 
				
			||||||
- **Data Processing**: Converts SNMP OID values to appropriate data types (int, bool) for Home Assistant sensors
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Key Functions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
- `get_snmp(req)`: Asynchronously retrieves SNMP data from configured OIDs
 | 
					 | 
				
			||||||
- `connect_mqtt(mqtt_config)`: Establishes MQTT broker connection
 | 
					 | 
				
			||||||
- `publish(topic, client, data, retain, qos)`: Publishes JSON data to MQTT topics
 | 
					 | 
				
			||||||
- `ha_create_config(req)`: Generates Home Assistant device discovery configuration
 | 
					 | 
				
			||||||
- `send_to_mqtt()`: Main loop that continuously publishes config and state data
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Configuration Structure
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
The script uses two main configuration dictionaries:
 | 
					 | 
				
			||||||
- `req`: Defines the target device, SNMP community, and monitored OIDs with Home Assistant metadata
 | 
					 | 
				
			||||||
- `mqtt_config`: MQTT broker connection parameters
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Common Development Commands
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Environment Setup
 | 
					 | 
				
			||||||
```bash
 | 
					 | 
				
			||||||
# Activate virtual environment
 | 
					 | 
				
			||||||
source bin/activate
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Install dependencies (if needed)
 | 
					 | 
				
			||||||
pip install pysnmp paho-mqtt
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Check installed packages
 | 
					 | 
				
			||||||
pip list
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Running the Application
 | 
					 | 
				
			||||||
```bash
 | 
					 | 
				
			||||||
# Run the main script
 | 
					 | 
				
			||||||
python snmp2mqtt.py
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Run with Python 3 explicitly
 | 
					 | 
				
			||||||
python3 snmp2mqtt.py
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Run from virtual environment
 | 
					 | 
				
			||||||
./bin/python snmp2mqtt.py
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Development and Testing
 | 
					 | 
				
			||||||
```bash
 | 
					 | 
				
			||||||
# Test SNMP connectivity to device
 | 
					 | 
				
			||||||
# (Manual SNMP walk example)
 | 
					 | 
				
			||||||
snmpwalk -v2c -c public 192.168.10.2 1.3.6.1.2.1.2.2.1.10
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Monitor MQTT messages (if mosquitto-clients available)
 | 
					 | 
				
			||||||
mosquitto_sub -h 192.168.10.202 -u snmp2mqtt -P 'snmp_2_MQTT' -t 'homeassistant/device/+/config'
 | 
					 | 
				
			||||||
mosquitto_sub -h 192.168.10.202 -u snmp2mqtt -P 'snmp_2_MQTT' -t 'SNMP/+/state'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Check network connectivity
 | 
					 | 
				
			||||||
ping 192.168.10.2
 | 
					 | 
				
			||||||
ping 192.168.10.202
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Configuration Notes
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Device Configuration
 | 
					 | 
				
			||||||
- Hardcoded to monitor MikroTik Hex router at IP `192.168.10.2`
 | 
					 | 
				
			||||||
- SNMP community: `public`
 | 
					 | 
				
			||||||
- Monitors interfaces: Starlink (index 1), LAN bridge (index 6), VPN (index 12)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### MQTT Configuration  
 | 
					 | 
				
			||||||
- Broker: `192.168.10.202:1883`
 | 
					 | 
				
			||||||
- Credentials: `snmp2mqtt` / `snmp_2_MQTT`
 | 
					 | 
				
			||||||
- Config topic: `homeassistant/device/{device_id}/config`
 | 
					 | 
				
			||||||
- State topic: `SNMP/{device_name}/state`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Monitored Metrics
 | 
					 | 
				
			||||||
For each interface:
 | 
					 | 
				
			||||||
- **Incoming bytes** (`oid: .1.3.6.1.2.1.2.2.1.10.X`) - Published as data_size sensor
 | 
					 | 
				
			||||||
- **Outgoing bytes** (`oid: .1.3.6.1.2.1.2.2.1.16.X`) - Published as data_size sensor  
 | 
					 | 
				
			||||||
- **Interface status** (`oid: .1.3.6.1.2.1.2.2.1.8.X`) - Published as connectivity binary_sensor
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Customization Points
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Adding New Devices
 | 
					 | 
				
			||||||
1. Create new `req` configuration dictionary with device details
 | 
					 | 
				
			||||||
2. Update `mqtt_config` if different broker needed
 | 
					 | 
				
			||||||
3. Configure appropriate SNMP OIDs for the device type
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Adding New OIDs
 | 
					 | 
				
			||||||
Each OID entry requires:
 | 
					 | 
				
			||||||
- `name`: Unique identifier for Home Assistant
 | 
					 | 
				
			||||||
- `oid`: SNMP Object Identifier
 | 
					 | 
				
			||||||
- `type`: Python type conversion (int, bool)
 | 
					 | 
				
			||||||
- `HA_platform`: Home Assistant platform (sensor, binary_sensor)
 | 
					 | 
				
			||||||
- `HA_device_class`: Device class for proper Home Assistant categorization
 | 
					 | 
				
			||||||
- `HA_unit`: (optional) Unit of measurement
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
### Home Assistant Integration
 | 
					 | 
				
			||||||
The script automatically creates Home Assistant MQTT Discovery configuration with:
 | 
					 | 
				
			||||||
- Device identification and grouping
 | 
					 | 
				
			||||||
- Sensor types and units appropriate for network monitoring
 | 
					 | 
				
			||||||
- Value templates for JSON data extraction
 | 
					 | 
				
			||||||
							
								
								
									
										12
									
								
								config.yaml
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								config.yaml
									
									
									
									
									
								
							@@ -82,18 +82,6 @@ devices:
 | 
				
			|||||||
        HA_device_class: "connectivity"
 | 
					        HA_device_class: "connectivity"
 | 
				
			||||||
        HA_platform: "binary_sensor"
 | 
					        HA_platform: "binary_sensor"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Example of how to add another device:
 | 
					 | 
				
			||||||
#  another_device:
 | 
					 | 
				
			||||||
#    ip: "192.168.10.3"
 | 
					 | 
				
			||||||
#    snmp_community: "public"
 | 
					 | 
				
			||||||
#    oids:
 | 
					 | 
				
			||||||
#      - name: "cpu_usage"
 | 
					 | 
				
			||||||
#        oid: ".1.3.6.1.4.1.14988.1.1.3.14.0"  # MikroTik CPU usage
 | 
					 | 
				
			||||||
#        type: "int"
 | 
					 | 
				
			||||||
#        HA_device_class: "power_factor"
 | 
					 | 
				
			||||||
#        HA_platform: "sensor"
 | 
					 | 
				
			||||||
#        HA_unit: "%"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# OID Configuration Reference:
 | 
					# OID Configuration Reference:
 | 
				
			||||||
# - name: Unique identifier for this metric (used in MQTT topics and Home Assistant)
 | 
					# - name: Unique identifier for this metric (used in MQTT topics and Home Assistant)
 | 
				
			||||||
# - oid: SNMP Object Identifier
 | 
					# - oid: SNMP Object Identifier
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,12 @@
 | 
				
			|||||||
# Install with: pip install -r requirements.txt
 | 
					# Install with: pip install -r requirements.txt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# SNMP library for asynchronous SNMP operations
 | 
					# SNMP library for asynchronous SNMP operations
 | 
				
			||||||
pysnmp>=6.0.0
 | 
					# Note: pysnmp 7.x uses a new API structure (no more Slim class)
 | 
				
			||||||
 | 
					pysnmp>=7.0.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# MQTT client library for connecting to MQTT brokers
 | 
					# MQTT client library for connecting to MQTT brokers
 | 
				
			||||||
paho-mqtt>=1.6.0
 | 
					# Note: paho-mqtt 2.x uses a new callback API (VERSION2) instead of the deprecated VERSION1
 | 
				
			||||||
 | 
					paho-mqtt>=2.0.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# YAML configuration file parsing
 | 
					# YAML configuration file parsing
 | 
				
			||||||
PyYAML>=6.0.0
 | 
					PyYAML>=6.0.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										351
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							
							
						
						
									
										351
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							@@ -1,7 +1,9 @@
 | 
				
			|||||||
#!/bin/env python3
 | 
					#!/bin/env python3
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from pysnmp.hlapi.asyncio.slim import Slim
 | 
					from pysnmp.hlapi.asyncio import (
 | 
				
			||||||
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
 | 
					    get_cmd, CommunityData, UdpTransportTarget, ContextData, 
 | 
				
			||||||
 | 
					    SnmpEngine, ObjectIdentity, ObjectType
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import random
 | 
					import random
 | 
				
			||||||
from paho.mqtt import client as mqtt_client
 | 
					from paho.mqtt import client as mqtt_client
 | 
				
			||||||
@@ -11,12 +13,99 @@ import yaml
 | 
				
			|||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import threading
 | 
				
			||||||
 | 
					import signal
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
logging.basicConfig(
 | 
					logging.basicConfig(
 | 
				
			||||||
        format='(%(levelname)s) %(message)s',
 | 
					        format='(%(levelname)s) [%(threadName)s] %(message)s',
 | 
				
			||||||
        level=logging.DEBUG
 | 
					        level=logging.DEBUG
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Global shutdown flag
 | 
				
			||||||
 | 
					shutdown_event = threading.Event()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class DeviceMonitorThread(threading.Thread):
 | 
				
			||||||
 | 
					    """Thread class for monitoring a single device"""
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def __init__(self, device_name, device_config, mqtt_config, sleep_interval=2):
 | 
				
			||||||
 | 
					        super().__init__(name=f"Device-{device_name}")
 | 
				
			||||||
 | 
					        self.device_name = device_name
 | 
				
			||||||
 | 
					        self.device_config = device_config
 | 
				
			||||||
 | 
					        self.mqtt_config = mqtt_config.copy()
 | 
				
			||||||
 | 
					        self.sleep_interval = sleep_interval
 | 
				
			||||||
 | 
					        self.daemon = True  # Dies when main thread dies
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        # Create unique client ID for this device
 | 
				
			||||||
 | 
					        self.mqtt_config['client_id'] = f"snmp-mqtt-{device_name}-{random.randint(0, 1000)}"
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        # Create device request object
 | 
				
			||||||
 | 
					        self.req = {
 | 
				
			||||||
 | 
					            "device_name": device_name,
 | 
				
			||||||
 | 
					            "ip": device_config["ip"],
 | 
				
			||||||
 | 
					            "snmp_community": device_config["snmp_community"],
 | 
				
			||||||
 | 
					            "oids": device_config["oids"]
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def run(self):
 | 
				
			||||||
 | 
					        """Main thread execution"""
 | 
				
			||||||
 | 
					        logging.info(f"Starting monitoring thread for device: {self.device_name} ({self.device_config['ip']})")
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            # Setup MQTT connection
 | 
				
			||||||
 | 
					            client = connect_mqtt(self.mqtt_config)
 | 
				
			||||||
 | 
					            client.loop_start()
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            state_topic = f"SNMP/{self.device_name}/state"
 | 
				
			||||||
 | 
					            availability_topic = f"SNMP/{self.device_name}/availability"
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            logging.info(f"[{self.device_name}] MQTT client connected")
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            # Publish Home Assistant autodiscovery configuration (only once on startup)
 | 
				
			||||||
 | 
					            publish_ha_autodiscovery_config(client, self.req)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            # Mark device as available
 | 
				
			||||||
 | 
					            publish(availability_topic, client, "online", True, 1)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            logging.info(f"[{self.device_name}] Starting monitoring loop")
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            # Main monitoring loop
 | 
				
			||||||
 | 
					            while not shutdown_event.is_set():
 | 
				
			||||||
 | 
					                try:
 | 
				
			||||||
 | 
					                    # Get SNMP data and publish state
 | 
				
			||||||
 | 
					                    state = asyncio.run(get_snmp(self.req))
 | 
				
			||||||
 | 
					                    publish(state_topic, client, state, False, 0)
 | 
				
			||||||
 | 
					                    logging.debug(f"[{self.device_name}] Published state to {state_topic}: {json.dumps(state)}")
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    # Update availability (heartbeat)
 | 
				
			||||||
 | 
					                    publish(availability_topic, client, "online", False, 1)
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                except Exception as e:
 | 
				
			||||||
 | 
					                    logging.error(f"[{self.device_name}] Error in monitoring loop: {e}")
 | 
				
			||||||
 | 
					                    # Mark as offline on error
 | 
				
			||||||
 | 
					                    publish(availability_topic, client, "offline", False, 1)
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                # Wait for next iteration or shutdown signal
 | 
				
			||||||
 | 
					                shutdown_event.wait(timeout=self.sleep_interval)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            # Cleanup - mark device as offline
 | 
				
			||||||
 | 
					            publish(availability_topic, client, "offline", True, 1)
 | 
				
			||||||
 | 
					            client.loop_stop()
 | 
				
			||||||
 | 
					            client.disconnect()
 | 
				
			||||||
 | 
					            logging.info(f"[{self.device_name}] Monitoring thread stopped gracefully")
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            logging.error(f"[{self.device_name}] Fatal error in monitoring thread: {e}")
 | 
				
			||||||
 | 
					            # Try to mark as offline on fatal error
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                publish(availability_topic, client, "offline", True, 1)
 | 
				
			||||||
 | 
					                client.disconnect()
 | 
				
			||||||
 | 
					            except:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        logging.info(f"[{self.device_name}] Thread {self.name} finished")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def parse_arguments():
 | 
					def parse_arguments():
 | 
				
			||||||
    """Parse command line arguments"""
 | 
					    """Parse command line arguments"""
 | 
				
			||||||
@@ -90,12 +179,14 @@ def load_config(config_path):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def connect_mqtt(mqtt_config):
 | 
					def connect_mqtt(mqtt_config):
 | 
				
			||||||
    def on_connect(client, userdata, flags, rc):
 | 
					    def on_connect(client, userdata, connect_flags, reason_code, properties):
 | 
				
			||||||
        if rc == 0:
 | 
					        if reason_code == 0:
 | 
				
			||||||
            print("Connected to MQTT Broker!")
 | 
					            logging.info("Connected to MQTT Broker!")
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            print("Failed to connect, return code {rc}")
 | 
					            logging.error(f"Failed to connect to MQTT Broker, reason code: {reason_code}")
 | 
				
			||||||
    client = mqtt_client.Client()
 | 
					    
 | 
				
			||||||
 | 
					    # Use the new callback API version 2
 | 
				
			||||||
 | 
					    client = mqtt_client.Client(callback_api_version=mqtt_client.CallbackAPIVersion.VERSION2)
 | 
				
			||||||
    client.username_pw_set(mqtt_config["user"], mqtt_config["password"])
 | 
					    client.username_pw_set(mqtt_config["user"], mqtt_config["password"])
 | 
				
			||||||
    client.on_connect = on_connect
 | 
					    client.on_connect = on_connect
 | 
				
			||||||
    client.connect(mqtt_config['broker'], mqtt_config['port'])
 | 
					    client.connect(mqtt_config['broker'], mqtt_config['port'])
 | 
				
			||||||
@@ -103,7 +194,11 @@ def connect_mqtt(mqtt_config):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def publish(topic, client, data, retain, qos):
 | 
					def publish(topic, client, data, retain, qos):
 | 
				
			||||||
 | 
					    if isinstance(data, str):
 | 
				
			||||||
 | 
					        msg = data
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
        msg = json.dumps(data)
 | 
					        msg = json.dumps(data)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    result = client.publish(topic=topic, payload=msg, qos=qos, retain=bool(retain))
 | 
					    result = client.publish(topic=topic, payload=msg, qos=qos, retain=bool(retain))
 | 
				
			||||||
    status = result[0]
 | 
					    status = result[0]
 | 
				
			||||||
    if status == 0:
 | 
					    if status == 0:
 | 
				
			||||||
@@ -113,25 +208,34 @@ def publish(topic, client, data, retain, qos):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def get_snmp(req):
 | 
					async def get_snmp(req):
 | 
				
			||||||
 | 
					    """Asynchronously retrieve SNMP data from device using new pysnmp API"""
 | 
				
			||||||
    data = {}
 | 
					    data = {}
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Create SNMP engine and transport target
 | 
				
			||||||
 | 
					    snmpEngine = SnmpEngine()
 | 
				
			||||||
 | 
					    authData = CommunityData(req["snmp_community"])
 | 
				
			||||||
 | 
					    transportTarget = await UdpTransportTarget.create((req["ip"], 161))
 | 
				
			||||||
 | 
					    contextData = ContextData()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    for oid in req["oids"]:
 | 
					    for oid in req["oids"]:
 | 
				
			||||||
        with Slim(1) as slim:
 | 
					        try:
 | 
				
			||||||
            errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
 | 
					            # Perform async SNMP GET operation
 | 
				
			||||||
                req["snmp_community"],
 | 
					            errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
 | 
				
			||||||
                req["ip"],
 | 
					                snmpEngine,
 | 
				
			||||||
                161,
 | 
					                authData,
 | 
				
			||||||
                ObjectType(ObjectIdentity(oid["oid"])),
 | 
					                transportTarget,
 | 
				
			||||||
 | 
					                contextData,
 | 
				
			||||||
 | 
					                ObjectType(ObjectIdentity(oid["oid"]))
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if errorIndication:
 | 
					            if errorIndication:
 | 
				
			||||||
                logging.error(errorIndication)
 | 
					                logging.error(f"{req['device_name']} SNMP error indication: {errorIndication}")
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
            elif errorStatus:
 | 
					            elif errorStatus:
 | 
				
			||||||
                logging.error(
 | 
					                logging.error(
 | 
				
			||||||
                    "{} at {}".format(
 | 
					                    f"{req['device_name']} SNMP error status: {errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1][0] or '?'}"
 | 
				
			||||||
                        errorStatus.prettyPrint(),
 | 
					 | 
				
			||||||
                        errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
 | 
					 | 
				
			||||||
                    )
 | 
					 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                for varBind in varBinds:
 | 
					                for varBind in varBinds:
 | 
				
			||||||
                    logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}")
 | 
					                    logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}")
 | 
				
			||||||
@@ -142,101 +246,152 @@ async def get_snmp(req):
 | 
				
			|||||||
                            data.update({oid["name"]: "OFF"})
 | 
					                            data.update({oid["name"]: "OFF"})
 | 
				
			||||||
                    else:
 | 
					                    else:
 | 
				
			||||||
                        data.update({oid["name"]: oid["type"](varBind[1])})
 | 
					                        data.update({oid["name"]: oid["type"](varBind[1])})
 | 
				
			||||||
    logging.debug(f"JSON : {json.dumps(data)}")
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            logging.error(f"{req['device_name']} Exception getting OID {oid['oid']}: {e}")
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    logging.debug(f"{req['device_name']} JSON : {json.dumps(data)}")
 | 
				
			||||||
    return data
 | 
					    return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def ha_create_config(req):
 | 
					def create_ha_device_info(req):
 | 
				
			||||||
    ha_config = {}
 | 
					    """Create device information for Home Assistant MQTT Discovery"""
 | 
				
			||||||
    device = {
 | 
					    return {
 | 
				
			||||||
            "ids": f"{req['device_name']}_{req['ip']}".replace(".", "_"),
 | 
					        "identifiers": [f"snmp2mqtt_{req['device_name']}_{req['ip']}".replace(".", "_")],
 | 
				
			||||||
        "name": req['device_name'],
 | 
					        "name": req['device_name'],
 | 
				
			||||||
 | 
					        "model": "SNMP Device",
 | 
				
			||||||
 | 
					        "manufacturer": "Network Equipment",
 | 
				
			||||||
 | 
					        "via_device": "snmp2mqtt"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    origin = {
 | 
					
 | 
				
			||||||
            "name": "snmp2mqtt"
 | 
					
 | 
				
			||||||
 | 
					def create_ha_sensor_config(req, oid):
 | 
				
			||||||
 | 
					    """Create Home Assistant MQTT Discovery configuration for a single sensor"""
 | 
				
			||||||
 | 
					    device_info = create_ha_device_info(req)
 | 
				
			||||||
 | 
					    sensor_id = f"{req['device_name']}_{req['ip']}_{oid['name']}".replace(".", "_")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    config = {
 | 
				
			||||||
 | 
					        "name": f"{req['device_name']} {oid['name']}",
 | 
				
			||||||
 | 
					        "unique_id": sensor_id,
 | 
				
			||||||
 | 
					        "state_topic": f"SNMP/{req['device_name']}/state",
 | 
				
			||||||
 | 
					        "value_template": f"{{{{ value_json.{oid['name']} }}}}",
 | 
				
			||||||
 | 
					        "device": device_info,
 | 
				
			||||||
 | 
					        "origin": {
 | 
				
			||||||
 | 
					            "name": "snmp2mqtt",
 | 
				
			||||||
 | 
					            "sw_version": "1.0.0",
 | 
				
			||||||
 | 
					            "support_url": "https://git.antoineve.me/AntoineVe/snmp2mqtt"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    ha_config.update({"dev": device, "o": origin})
 | 
					    }
 | 
				
			||||||
    ha_config.update({"state_topic": f"SNMP/{req['device_name']}/state"})
 | 
					    
 | 
				
			||||||
    ha_config.update({"qos": 2})
 | 
					    # Add device class if specified
 | 
				
			||||||
    cmps = {}
 | 
					    if 'HA_device_class' in oid:
 | 
				
			||||||
 | 
					        config['device_class'] = oid['HA_device_class']
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Add unit of measurement if specified
 | 
				
			||||||
 | 
					    if 'HA_unit' in oid:
 | 
				
			||||||
 | 
					        config['unit_of_measurement'] = oid['HA_unit']
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Add icon based on device class
 | 
				
			||||||
 | 
					    icon_mapping = {
 | 
				
			||||||
 | 
					        'data_size': 'mdi:network',
 | 
				
			||||||
 | 
					        'connectivity': 'mdi:network-outline',
 | 
				
			||||||
 | 
					        'power_factor': 'mdi:gauge',
 | 
				
			||||||
 | 
					        'temperature': 'mdi:thermometer',
 | 
				
			||||||
 | 
					        'signal_strength': 'mdi:signal'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if 'HA_device_class' in oid and oid['HA_device_class'] in icon_mapping:
 | 
				
			||||||
 | 
					        config['icon'] = icon_mapping[oid['HA_device_class']]
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Add availability topic
 | 
				
			||||||
 | 
					    config['availability'] = {
 | 
				
			||||||
 | 
					        "topic": f"SNMP/{req['device_name']}/availability",
 | 
				
			||||||
 | 
					        "payload_available": "online",
 | 
				
			||||||
 | 
					        "payload_not_available": "offline"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_ha_discovery_topic(req, oid):
 | 
				
			||||||
 | 
					    """Get the correct Home Assistant MQTT Discovery topic for a sensor"""
 | 
				
			||||||
 | 
					    platform = oid['HA_platform']  # 'sensor' or 'binary_sensor'
 | 
				
			||||||
 | 
					    node_id = req['device_name']
 | 
				
			||||||
 | 
					    object_id = f"{req['device_name']}_{oid['name']}".replace(".", "_")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Format: homeassistant/<platform>/<node_id>/<object_id>/config
 | 
				
			||||||
 | 
					    return f"homeassistant/{platform}/{node_id}/{object_id}/config"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def publish_ha_autodiscovery_config(client, req):
 | 
				
			||||||
 | 
					    """Publish Home Assistant MQTT Discovery configuration for all sensors of a device"""
 | 
				
			||||||
 | 
					    logging.info(f"[{req['device_name']}] Publishing Home Assistant autodiscovery configuration")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Publish availability as online
 | 
				
			||||||
 | 
					    availability_topic = f"SNMP/{req['device_name']}/availability"
 | 
				
			||||||
 | 
					    publish(availability_topic, client, "online", True, 1)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Publish discovery configuration for each OID/sensor
 | 
				
			||||||
    for oid in req['oids']:
 | 
					    for oid in req['oids']:
 | 
				
			||||||
        cmps.update(
 | 
					        config = create_ha_sensor_config(req, oid)
 | 
				
			||||||
                {
 | 
					        topic = get_ha_discovery_topic(req, oid)
 | 
				
			||||||
                    f"{req['device_name']}_{req['ip']}_{oid['name']}".replace(".", "_"):
 | 
					        
 | 
				
			||||||
                    {
 | 
					        # Publish with retain=True so HA discovers it after restarts
 | 
				
			||||||
                        "p": oid['HA_platform'],
 | 
					        publish(topic, client, config, True, 1)
 | 
				
			||||||
                        "device_class": oid['HA_device_class'],
 | 
					        logging.info(f"[{req['device_name']}] Published discovery config for {oid['name']} to {topic}")
 | 
				
			||||||
                        "value_template": f"{{{{ value_json.{oid['name']}}}}}",
 | 
					        
 | 
				
			||||||
                        "unique_id": f"{req['device_name']}_{req['ip']}_{oid['name']}".replace(".", "_"),
 | 
					        # Small delay to avoid overwhelming the broker
 | 
				
			||||||
                        "name": oid['name']
 | 
					        time.sleep(0.1)
 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                    })
 | 
					 | 
				
			||||||
        if "HA_unit" in oid.keys():
 | 
					 | 
				
			||||||
            cmps.update(
 | 
					 | 
				
			||||||
                 {f"{req['device_name']}_{req['ip']}_{oid['name']}".replace(".", "_"):
 | 
					 | 
				
			||||||
                    {"unit_of_measurement": oid['HA_unit']}})
 | 
					 | 
				
			||||||
    ha_config.update({"cmps": cmps})
 | 
					 | 
				
			||||||
    logging.debug(f"config : {json.dumps(ha_config)}")
 | 
					 | 
				
			||||||
    return ha_config
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_to_mqtt(device_name, device_config, mqtt_config, sleep_interval=2):
 | 
					def signal_handler(signum, frame):
 | 
				
			||||||
    """Send SNMP data to MQTT for a single device"""
 | 
					    """Handle shutdown signals gracefully"""
 | 
				
			||||||
    # Create device request object
 | 
					    logging.info(f"Received signal {signum}, initiating graceful shutdown...")
 | 
				
			||||||
    req = {
 | 
					    shutdown_event.set()
 | 
				
			||||||
        "device_name": device_name,
 | 
					 | 
				
			||||||
        "ip": device_config["ip"],
 | 
					 | 
				
			||||||
        "snmp_community": device_config["snmp_community"],
 | 
					 | 
				
			||||||
        "oids": device_config["oids"]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    config = ha_create_config(req)
 | 
					 | 
				
			||||||
    client = connect_mqtt(mqtt_config)
 | 
					 | 
				
			||||||
    client.loop_start()
 | 
					 | 
				
			||||||
    config_topic = f"homeassistant/device/{config['dev']['ids']}/config"
 | 
					 | 
				
			||||||
    state_topic = config['state_topic']
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    while True:
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            publish(config_topic, client, config, True, 0)
 | 
					 | 
				
			||||||
            logging.info(f"{config_topic} -> {config}")
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logging.error(f"Error publishing config for {device_name}: {e}")
 | 
					 | 
				
			||||||
            pass
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            state = asyncio.run(get_snmp(req))
 | 
					 | 
				
			||||||
            publish(state_topic, client, state, False, 0)
 | 
					 | 
				
			||||||
            logging.info(f"{state_topic} -> {state}")
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logging.error(f"Error getting SNMP data for {device_name}: {e}")
 | 
					 | 
				
			||||||
            pass
 | 
					 | 
				
			||||||
        sleep(sleep_interval)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def process_devices(config):
 | 
					def process_devices(config):
 | 
				
			||||||
    """Process multiple devices from configuration"""
 | 
					    """Process multiple devices using threading"""
 | 
				
			||||||
    mqtt_config = config['mqtt'].copy()
 | 
					    mqtt_config = config['mqtt'].copy()
 | 
				
			||||||
    mqtt_config['client_id'] = f"snmp-mqtt-{random.randint(0, 1000)}"
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    # Get sleep interval from config or use default
 | 
					 | 
				
			||||||
    sleep_interval = config.get('sleep_interval', 2)
 | 
					    sleep_interval = config.get('sleep_interval', 2)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if len(config['devices']) == 1:
 | 
					    # Setup signal handlers for graceful shutdown
 | 
				
			||||||
        # Single device mode - run directly
 | 
					    signal.signal(signal.SIGINT, signal_handler)
 | 
				
			||||||
        device_name = list(config['devices'].keys())[0]
 | 
					    signal.signal(signal.SIGTERM, signal_handler)
 | 
				
			||||||
        device_config = config['devices'][device_name]
 | 
					 | 
				
			||||||
        logging.info(f"Starting monitoring for single device: {device_name}")
 | 
					 | 
				
			||||||
        send_to_mqtt(device_name, device_config, mqtt_config, sleep_interval)
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        # Multiple devices mode - would need threading/multiprocessing
 | 
					 | 
				
			||||||
        # For now, let's process the first device and warn about others
 | 
					 | 
				
			||||||
        logging.warning(f"Multiple devices detected ({len(config['devices'])}), but only processing the first one")
 | 
					 | 
				
			||||||
        logging.warning("Multi-device support will require threading implementation")
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        device_name = list(config['devices'].keys())[0]
 | 
					    device_threads = []
 | 
				
			||||||
        device_config = config['devices'][device_name]
 | 
					    
 | 
				
			||||||
        logging.info(f"Starting monitoring for device: {device_name}")
 | 
					    try:
 | 
				
			||||||
        send_to_mqtt(device_name, device_config, mqtt_config, sleep_interval)
 | 
					        logging.info(f"Starting monitoring for {len(config['devices'])} device(s)")
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        # Create and start a thread for each device
 | 
				
			||||||
 | 
					        for device_name, device_config in config['devices'].items():
 | 
				
			||||||
 | 
					            thread = DeviceMonitorThread(
 | 
				
			||||||
 | 
					                device_name=device_name,
 | 
				
			||||||
 | 
					                device_config=device_config,
 | 
				
			||||||
 | 
					                mqtt_config=mqtt_config,
 | 
				
			||||||
 | 
					                sleep_interval=sleep_interval
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            device_threads.append(thread)
 | 
				
			||||||
 | 
					            thread.start()
 | 
				
			||||||
 | 
					            logging.info(f"Started thread for device: {device_name}")
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        # Wait for all threads to complete or shutdown signal
 | 
				
			||||||
 | 
					        while any(thread.is_alive() for thread in device_threads) and not shutdown_event.is_set():
 | 
				
			||||||
 | 
					            time.sleep(0.5)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					        logging.error(f"Error in process_devices: {e}")
 | 
				
			||||||
 | 
					        shutdown_event.set()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Wait for all threads to finish
 | 
				
			||||||
 | 
					    logging.info("Waiting for all monitoring threads to finish...")
 | 
				
			||||||
 | 
					    for thread in device_threads:
 | 
				
			||||||
 | 
					        if thread.is_alive():
 | 
				
			||||||
 | 
					            thread.join(timeout=5.0)  # Wait max 5 seconds per thread
 | 
				
			||||||
 | 
					            if thread.is_alive():
 | 
				
			||||||
 | 
					                logging.warning(f"Thread {thread.name} did not stop gracefully")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    logging.info("All monitoring threads have finished")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user