Compare commits
6 Commits
c06e385bc9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cae6eb95eb | ||
|
|
65014dabf4 | ||
|
|
8b5c2f0ca2 | ||
|
|
7fc263cf3d | ||
|
|
e419b16639 | ||
|
|
ee830ec557 |
5
.env
Normal file
5
.env
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
SNMP_COMMUNITY=public
|
||||||
|
CUSTOM_OID=.1.3.6.1.4.1.38446.1.2.4.1.9.1
|
||||||
|
RANGE_MIN=0
|
||||||
|
RANGE_MAX=6
|
||||||
|
SNMP_PORT=161
|
||||||
@@ -14,10 +14,10 @@ COPY custom_snmp_script.py /usr/local/bin/custom_snmp_script.py
|
|||||||
RUN chmod +x /usr/local/bin/custom_snmp_script.py
|
RUN chmod +x /usr/local/bin/custom_snmp_script.py
|
||||||
|
|
||||||
# Configure snmpd to use the custom script
|
# Configure snmpd to use the custom script
|
||||||
RUN echo "extend .1.3.6.1.4.1.38446.1.2.4.1.9.1 /usr/local/bin/custom_snmp_script.py" >> /etc/snmp/snmpd.conf
|
RUN echo "extend customOID /usr/local/bin/custom_snmp_script.py" >> /etc/snmp/snmpd.conf
|
||||||
|
|
||||||
# Expose the SNMP port
|
# Expose the SNMP port using an environment variable
|
||||||
EXPOSE 161/udp
|
EXPOSE 161/udp
|
||||||
|
|
||||||
# Start snmpd
|
# Start snmpd with environment variables
|
||||||
CMD ["snmpd", "-f"]
|
CMD ["sh", "-c", "snmpd -f -c /etc/snmp/snmpd.conf -Lf /dev/null -p /var/run/snmpd.pid -a -x $SNMP_COMMUNITY -p $SNMP_PORT"]
|
||||||
|
|||||||
43
README.md
43
README.md
@@ -1,12 +1,13 @@
|
|||||||
# SNMPD Custom OID Docker Container
|
# SNMPD Custom OID Docker Container
|
||||||
|
|
||||||
This Docker container runs `snmpd` with a custom script that provides a specific OID (`.1.3.6.1.4.1.38446.1.2.4.1.9.1`). The value of this OID increases randomly by a small amount every 4 minutes.
|
This Docker container runs `snmpd` with a custom script that provides a specific OID. The value of this OID increases randomly by a small amount every 4 minutes. The SNMP community string, OID, increment range, and port can be configured using a `.env` file.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Lightweight Docker container based on Alpine Linux.
|
- Lightweight Docker container based on Alpine Linux.
|
||||||
- Custom Python script to generate and update the OID value.
|
- Custom Python script to generate and update the OID value.
|
||||||
- Configurable SNMP daemon (`snmpd`) to use the custom script.
|
- Configurable SNMP daemon (`snmpd`) to use the custom script.
|
||||||
|
- Dynamic configuration using a `.env` file with Docker environment variables.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
@@ -16,12 +17,24 @@ This Docker container runs `snmpd` with a custom script that provides a specific
|
|||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- `snmpd`: The SNMP daemon.
|
- `snmpd`: The SNMP daemon.
|
||||||
- Python 3: Used to write the custom script.
|
- `Python 3`: Used to write the custom script.
|
||||||
- `random`: Python library for generating random numbers.
|
- `random`: Python library for generating random numbers (included in Python's standard library).
|
||||||
|
|
||||||
## How to Use
|
## How to Use
|
||||||
|
|
||||||
### Step 1: Build the Docker Image
|
### Step 1: Create a `.env` File
|
||||||
|
|
||||||
|
Create a `.env` file in the same directory as your `Dockerfile` with the following content:
|
||||||
|
|
||||||
|
```
|
||||||
|
SNMP_COMMUNITY=public
|
||||||
|
CUSTOM_OID=.1.3.6.1.4.1.38446.1.2.4.1.9.1
|
||||||
|
RANGE_MIN=0
|
||||||
|
RANGE_MAX=6
|
||||||
|
SNMP_PORT=161
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Build the Docker Image
|
||||||
|
|
||||||
Navigate to the directory containing the `Dockerfile` and `custom_snmp_script.py`, then run:
|
Navigate to the directory containing the `Dockerfile` and `custom_snmp_script.py`, then run:
|
||||||
|
|
||||||
@@ -29,15 +42,17 @@ Navigate to the directory containing the `Dockerfile` and `custom_snmp_script.py
|
|||||||
docker build -t snmpd-custom .
|
docker build -t snmpd-custom .
|
||||||
```
|
```
|
||||||
|
|
||||||
### Step 2: Run the Docker Container
|
### Step 3: Run the Docker Container
|
||||||
|
|
||||||
Start the Docker container with the following command:
|
Start the Docker container with the following command, using the `--env-file` option to pass the environment variables:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d -p 161:161/udp --name snmpd-custom-container snmpd-custom
|
docker run -d -p ${SNMP_PORT}:${SNMP_PORT}/udp --name snmpd-custom-container --env-file .env snmpd-custom
|
||||||
```
|
```
|
||||||
|
|
||||||
### Step 3: Verify the Setup
|
Replace `${SNMP_PORT}` with the actual port number you want to use, or use the value from your `.env` file.
|
||||||
|
|
||||||
|
### Step 4: Verify the Setup
|
||||||
|
|
||||||
You can verify that the container is running and the SNMP daemon is providing the custom OID by using an SNMP client to query the OID:
|
You can verify that the container is running and the SNMP daemon is providing the custom OID by using an SNMP client to query the OID:
|
||||||
|
|
||||||
@@ -50,15 +65,21 @@ You should see the value incrementing randomly every 4 minutes.
|
|||||||
## Customization
|
## Customization
|
||||||
|
|
||||||
- **Initial Value**: You can change the initial value in the `custom_snmp_script.py` file.
|
- **Initial Value**: You can change the initial value in the `custom_snmp_script.py` file.
|
||||||
- **Increment Range**: Modify the range in the script to change how much the value increments.
|
- **Increment Range**: Modify the `RANGE_MIN` and `RANGE_MAX` in the `.env` file to change how much the value increments.
|
||||||
- **Update Interval**: Adjust the sleep interval in the script to change the frequency of updates.
|
- **Update Interval**: Adjust the sleep interval in the script to change the frequency of updates.
|
||||||
|
- **Community String and OID**: Modify the `.env` file to change the SNMP community string and OID.
|
||||||
|
- **SNMP Port**: Modify the `SNMP_PORT` in the `.env` file to change the port on which `snmpd` listens.
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- Ensure that the SNMP community string (`public` in the example) matches your configuration.
|
- Ensure that the SNMP community string and port in the `.env` file match your configuration.
|
||||||
- The container exposes port 161 for SNMP communication.
|
- The container exposes the specified SNMP port for communication.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under the MIT License.
|
This project is licensed under the MIT License.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Feel free to contribute or modify the setup to fit your specific needs!
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
# Read the OID and range from environment variables
|
||||||
|
oid = os.getenv('CUSTOM_OID', '.1.3.6.1.4.1.38446.1.2.4.1.9.1')
|
||||||
|
range_min = int(os.getenv('RANGE_MIN', 0))
|
||||||
|
range_max = int(os.getenv('RANGE_MAX', 6))
|
||||||
|
|
||||||
# Initial value
|
# Initial value
|
||||||
value = 1385062
|
value = 1385062
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Increment the value by a random amount between 0 and 6
|
# Increment the value by a random amount within the specified range
|
||||||
value += random.randint(0, 6)
|
value += random.randint(range_min, range_max)
|
||||||
|
|
||||||
# Output the value in the required format
|
# Output the value in the required format
|
||||||
print(f"INTEGER: {value}")
|
print(f"{oid} = INTEGER: {value}")
|
||||||
|
|
||||||
# Wait for 4 minutes
|
# Wait for 4 minutes
|
||||||
time.sleep(240)
|
time.sleep(240)
|
||||||
|
|||||||
Reference in New Issue
Block a user