Compare commits
	
		
			8 Commits
		
	
	
		
			fix-snmp-e
			...
			ac476df7ce
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						ac476df7ce
	
				 | 
					
					
						|||
| 
						
						
							
						
						f48a9d1417
	
				 | 
					
					
						|||
| 
						
						
							
						
						b7331faba0
	
				 | 
					
					
						|||
| 
						
						
							
						
						3875f4501f
	
				 | 
					
					
						|||
| 
						
						
							
						
						7fec5c5049
	
				 | 
					
					
						|||
| 
						
						
							
						
						2309ef1deb
	
				 | 
					
					
						|||
| 
						
						
							
						
						75f8c6a637
	
				 | 
					
					
						|||
| 
						
						
							
						
						0d54632c52
	
				 | 
					
					
						
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -172,3 +172,4 @@ cython_debug/
 | 
			
		||||
#.idea/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
WARP.md
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										77
									
								
								GEMINI.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								GEMINI.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
# Fichier d'aide pour Gemini
 | 
			
		||||
 | 
			
		||||
Ce document fournit un résumé concis du projet `snmp2mqtt` pour aider au développement et à la maintenance assistés par l'IA.
 | 
			
		||||
 | 
			
		||||
## 1. Objectif du Projet
 | 
			
		||||
 | 
			
		||||
Le projet `snmp2mqtt` est une passerelle écrite en Python qui a pour but de :
 | 
			
		||||
1.  **Interroger** des équipements réseau (routeurs, switchs, etc.) via le protocole **SNMP**.
 | 
			
		||||
2.  **Récupérer** des métriques spécifiques (trafic, statut des ports, etc.) définies par des OIDs.
 | 
			
		||||
3.  **Publier** ces données sur un broker **MQTT**.
 | 
			
		||||
4.  **S'intégrer automatiquement** avec **Home Assistant** grâce au mécanisme de "MQTT Discovery", permettant de créer des capteurs sans configuration manuelle côté Home Assistant.
 | 
			
		||||
 | 
			
		||||
## 2. Architecture et Technologies
 | 
			
		||||
 | 
			
		||||
-   **Langage** : Python 3
 | 
			
		||||
-   **Dépendances principales** (`requirements.txt`) :
 | 
			
		||||
    -   `pysnmp>=7.0.0` : Pour la communication SNMP asynchrone.
 | 
			
		||||
    -   `paho-mqtt>=2.0.0` : Pour la communication avec le broker MQTT.
 | 
			
		||||
    -   `PyYAML>=6.0.0` : Pour le parsing du fichier de configuration.
 | 
			
		||||
-   **Configuration** : Un unique fichier `config.yaml` centralise tous les paramètres (MQTT, appareils, OIDs).
 | 
			
		||||
-   **Exécution** : Le script utilise le multi-threading. Un thread est démarré pour chaque appareil défini dans la configuration, ce qui permet une surveillance parallèle et isolée.
 | 
			
		||||
 | 
			
		||||
## 3. Structure du Code (`snmp2mqtt.py`)
 | 
			
		||||
 | 
			
		||||
Le script principal est organisé de la manière suivante :
 | 
			
		||||
 | 
			
		||||
1.  **`main()`** : Point d'entrée. Il parse les arguments (`--config`), charge la configuration et appelle `process_devices()`.
 | 
			
		||||
2.  **`process_devices(config)`** :
 | 
			
		||||
    -   Orchestre le lancement des threads.
 | 
			
		||||
    -   Crée et démarre une instance de `DeviceMonitorThread` pour chaque appareil.
 | 
			
		||||
    -   Gère l'arrêt propre (`graceful shutdown`) en attendant que tous les threads se terminent.
 | 
			
		||||
3.  **`DeviceMonitorThread(threading.Thread)`** :
 | 
			
		||||
    -   Classe qui encapsule la logique de surveillance pour un seul appareil.
 | 
			
		||||
    -   `run()` : méthode principale du thread.
 | 
			
		||||
        -   Établit la connexion MQTT.
 | 
			
		||||
        -   Publie la configuration de découverte automatique pour Home Assistant (une seule fois au démarrage).
 | 
			
		||||
        -   Entre dans une boucle infinie qui :
 | 
			
		||||
            -   Appelle `get_snmp()` pour récupérer les données.
 | 
			
		||||
            -   Publie l'état des capteurs et le statut de disponibilité (`online`/`offline`) sur MQTT.
 | 
			
		||||
            -   Attend un intervalle (`sleep_interval`) avant la prochaine interrogation.
 | 
			
		||||
4.  **`get_snmp(req)`** :
 | 
			
		||||
    -   Fonction `async` qui utilise `pysnmp` pour exécuter les requêtes `GET` SNMP pour tous les OIDs d'un appareil.
 | 
			
		||||
    -   Retourne un dictionnaire contenant les valeurs formatées.
 | 
			
		||||
5.  **Fonctions de configuration et MQTT** :
 | 
			
		||||
    -   `load_config()` : Charge et valide le fichier `config.yaml`.
 | 
			
		||||
    -   `connect_mqtt()` : Initialise le client MQTT.
 | 
			
		||||
    -   `publish()` : Wrapper pour publier les messages MQTT.
 | 
			
		||||
    -   `publish_ha_autodiscovery_config()` : Construit et publie les messages de configuration pour Home Assistant MQTT Discovery.
 | 
			
		||||
 | 
			
		||||
## 4. Flux de Données
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
[Appareil SNMP] <--- (Requête SNMP GET) --- [snmp2mqtt.py / Thread]
 | 
			
		||||
       |
 | 
			
		||||
       | (Réponse SNMP)
 | 
			
		||||
       v
 | 
			
		||||
[snmp2mqtt.py / Thread] --- (Publication MQTT) ---> [Broker MQTT]
 | 
			
		||||
                                                          |
 | 
			
		||||
                                                          | (MQTT Discovery & State)
 | 
			
		||||
                                                          v
 | 
			
		||||
                                                  [Home Assistant]
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## 5. Comment développer
 | 
			
		||||
 | 
			
		||||
-   **Environnement** :
 | 
			
		||||
    1.  Créer un environnement virtuel : `python3 -m venv .venv`
 | 
			
		||||
    2.  Activer l'environnement : `source .venv/bin/activate`
 | 
			
		||||
    3.  Installer les dépendances : `pip install -r requirements.txt`
 | 
			
		||||
-   **Configuration** :
 | 
			
		||||
    -   Copier et modifier `config.yaml` pour pointer vers un broker MQTT de test et un appareil SNMP accessible.
 | 
			
		||||
-   **Lancement** :
 | 
			
		||||
    -   `python snmp2mqtt.py --config config.yaml`
 | 
			
		||||
-   **Points clés à modifier** :
 | 
			
		||||
    -   Pour ajouter une nouvelle fonctionnalité à un capteur Home Assistant, modifier `create_ha_sensor_config()`.
 | 
			
		||||
    -   Pour changer la logique de récupération SNMP, modifier `get_snmp()`.
 | 
			
		||||
    -   Pour ajouter de nouveaux paramètres de configuration, mettre à jour `load_config()` pour la validation.
 | 
			
		||||
							
								
								
									
										24
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								README.md
									
									
									
									
									
								
							@@ -15,7 +15,7 @@ Passerelle SNMP vers MQTT pour l'intégration Home Assistant. Ce script Python s
 | 
			
		||||
 | 
			
		||||
### 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
 | 
			
		||||
- **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
 | 
			
		||||
@@ -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
 | 
			
		||||
- 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
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
@@ -361,6 +371,16 @@ devices:
 | 
			
		||||
   - Vérifiez que MQTT Discovery est activé dans Home Assistant
 | 
			
		||||
   - 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
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
@@ -438,7 +458,7 @@ Chaque thread est clairement identifié dans les logs :
 | 
			
		||||
 | 
			
		||||
## Logs et debugging
 | 
			
		||||
 | 
			
		||||
Le script utilise le module `logging` de Python avec le niveau DEBUG par défaut. Les logs incluent :
 | 
			
		||||
Le script utilise le module `logging` de Python avec le niveau INFO par défaut. Les logs incluent :
 | 
			
		||||
 | 
			
		||||
- Chargement de la configuration
 | 
			
		||||
- Connexions MQTT
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										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
 | 
			
		||||
							
								
								
									
										60
									
								
								config.yaml
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								config.yaml
									
									
									
									
									
								
							@@ -3,10 +3,10 @@
 | 
			
		||||
 | 
			
		||||
# MQTT Broker Configuration
 | 
			
		||||
mqtt:
 | 
			
		||||
  broker: "192.168.10.202"
 | 
			
		||||
  broker: "IP or FQDN"
 | 
			
		||||
  port: 1883
 | 
			
		||||
  user: "snmp2mqtt"
 | 
			
		||||
  password: "snmp_2_MQTT"
 | 
			
		||||
  user: "USER"
 | 
			
		||||
  password: "PASSWORD"
 | 
			
		||||
 | 
			
		||||
# Optional: Sleep interval between SNMP polls (default: 2 seconds)
 | 
			
		||||
sleep_interval: 2
 | 
			
		||||
@@ -16,72 +16,30 @@ sleep_interval: 2
 | 
			
		||||
devices:
 | 
			
		||||
  # Device name (used for MQTT topics and Home Assistant device identification)
 | 
			
		||||
  mikrotik_hex:
 | 
			
		||||
    ip: "192.168.10.2"
 | 
			
		||||
    ip: "IP"
 | 
			
		||||
    snmp_community: "public"
 | 
			
		||||
    oids:
 | 
			
		||||
      # Starlink VPN interface (interface index 12)
 | 
			
		||||
      - name: "stln_vpn_in"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.10.12"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "stlon_vpn_out"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.16.12"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "stln_vpn_status"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.8.12"
 | 
			
		||||
        type: "bool"
 | 
			
		||||
        HA_device_class: "connectivity"
 | 
			
		||||
        HA_platform: "binary_sensor"
 | 
			
		||||
      
 | 
			
		||||
      # LAN Bridge interface (interface index 6)
 | 
			
		||||
      - name: "lan_bridge_in"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.10.6"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "lan_bridge_out"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.16.6"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "lan_bridge_status"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.8.6"
 | 
			
		||||
        type: "bool"
 | 
			
		||||
        HA_device_class: "connectivity"
 | 
			
		||||
        HA_platform: "binary_sensor"
 | 
			
		||||
      
 | 
			
		||||
      # Starlink interface (interface index 1)
 | 
			
		||||
      - name: "starlink_in"
 | 
			
		||||
      # example interface index 1
 | 
			
		||||
      - name: "if1_in"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.10.1"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "starlink_out"
 | 
			
		||||
      - name: "if1_out"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.16.1"
 | 
			
		||||
        type: "int"
 | 
			
		||||
        HA_device_class: "data_size"
 | 
			
		||||
        HA_platform: "sensor"
 | 
			
		||||
        HA_unit: "bit"
 | 
			
		||||
      
 | 
			
		||||
      - name: "starlink_status"
 | 
			
		||||
      - name: "if1_status"
 | 
			
		||||
        oid: ".1.3.6.1.2.1.2.2.1.8.1"
 | 
			
		||||
        type: "bool"
 | 
			
		||||
        HA_device_class: "connectivity"
 | 
			
		||||
        HA_platform: "binary_sensor"
 | 
			
		||||
 | 
			
		||||
      
 | 
			
		||||
# OID Configuration Reference:
 | 
			
		||||
# - name: Unique identifier for this metric (used in MQTT topics and Home Assistant)
 | 
			
		||||
# - oid: SNMP Object Identifier
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,8 @@
 | 
			
		||||
pysnmp>=7.0.0
 | 
			
		||||
 | 
			
		||||
# 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
 | 
			
		||||
PyYAML>=6.0.0
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							@@ -19,7 +19,7 @@ import time
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(
 | 
			
		||||
        format='(%(levelname)s) [%(threadName)s] %(message)s',
 | 
			
		||||
        level=logging.DEBUG
 | 
			
		||||
        level=logging.INFO
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
# Global shutdown flag
 | 
			
		||||
@@ -179,12 +179,14 @@ def load_config(config_path):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def connect_mqtt(mqtt_config):
 | 
			
		||||
    def on_connect(client, userdata, flags, rc):
 | 
			
		||||
        if rc == 0:
 | 
			
		||||
            print("Connected to MQTT Broker!")
 | 
			
		||||
    def on_connect(client, userdata, connect_flags, reason_code, properties):
 | 
			
		||||
        if reason_code == 0:
 | 
			
		||||
            logging.info("Connected to MQTT Broker!")
 | 
			
		||||
        else:
 | 
			
		||||
            print("Failed to connect, return code {rc}")
 | 
			
		||||
    client = mqtt_client.Client()
 | 
			
		||||
            logging.error(f"Failed to connect to MQTT Broker, reason code: {reason_code}")
 | 
			
		||||
    
 | 
			
		||||
    # 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.on_connect = on_connect
 | 
			
		||||
    client.connect(mqtt_config['broker'], mqtt_config['port'])
 | 
			
		||||
@@ -284,6 +286,9 @@ def create_ha_sensor_config(req, oid):
 | 
			
		||||
    # Add device class if specified
 | 
			
		||||
    if 'HA_device_class' in oid:
 | 
			
		||||
        config['device_class'] = oid['HA_device_class']
 | 
			
		||||
        # Add state_class for total_increasing counters like data size
 | 
			
		||||
        if oid['HA_device_class'] == 'data_size':
 | 
			
		||||
            config['state_class'] = 'total_increasing'
 | 
			
		||||
    
 | 
			
		||||
    # Add unit of measurement if specified
 | 
			
		||||
    if 'HA_unit' in oid:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user