From ee830ec5574a546f03915974ec6bcf6d34b4e246 Mon Sep 17 00:00:00 2001 From: Radek Date: Thu, 6 Mar 2025 13:00:02 +0000 Subject: [PATCH] add env config --- .env | 2 ++ Dockerfile | 6 +++--- custom_snmp_script.py | 6 +++++- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..f632375 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +SNMP_COMMUNITY=public +CUSTOM_OID=.1.3.6.1.4.1.38446.1.2.4.1.9.1 diff --git a/Dockerfile b/Dockerfile index 7902db4..0613b75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 # 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 161/udp -# Start snmpd -CMD ["snmpd", "-f"] +# Start snmpd with environment variables +CMD ["sh", "-c", "snmpd -f -c /etc/snmp/snmpd.conf -Lf /dev/null -p /var/run/snmpd.pid -a -x $SNMP_COMMUNITY"] diff --git a/custom_snmp_script.py b/custom_snmp_script.py index 1ee2738..6fd0612 100644 --- a/custom_snmp_script.py +++ b/custom_snmp_script.py @@ -1,7 +1,11 @@ #!/usr/bin/env python3 +import os import random import time +# Read the OID from the environment variable +oid = os.getenv('CUSTOM_OID', '.1.3.6.1.4.1.38446.1.2.4.1.9.1') + # Initial value value = 1385062 @@ -10,7 +14,7 @@ while True: value += random.randint(0, 6) # Output the value in the required format - print(f"INTEGER: {value}") + print(f"{oid} = INTEGER: {value}") # Wait for 4 minutes time.sleep(240)