I am using cacti 1.2.25 on ubuntu 18.04 with Mariadb 10.4 and PHP 7.2
My python script is polling Rx/Tx octets from host and working from command prompt and cacti log also showing output
#for index_value,arr_index_value in enumerate(arr_index,start=1):
for index_value, arr_index_value in enumerate(arr_index[:min_length], start=1):
#print(f"{arr_index[i]}{xml_delimiter}{arr[i]}"
#print(f"{index_value}{xml_delimiter}{arr[index_value]}")
print(f"{index_value}{xml_delimiter}{arr[index_value-1]}")
elif cmd == "get" and query_field and query_index:
Perform SNMP walk on the specified query_field OID
result = reindex(session.bulkwalk(oids["objectName"]))
if result:
position = int(query_index) - 1
if 0 <= position < len(result):
value = result[position]
parts = value.split("Port_Eth-")
if len(parts) == 2:
modifiedValue = parts[1].replace("-", ".")
full_oid = f"{oids[query_field]}.{modifiedValue}"
#print(full_oid)
get_result = session.get(full_oid)
if get_result:
print(f"{query_field}:{get_result.value}")
else:
print("SNMP get operation failed")
else:
print(f"Invalid format in the SNMP walk result for position {position}")
else:
print(f"Invalid query_index: {query_index}. It does not correspond to a valid position in the SNMP walk result.")
else:
print("SNMP walk did not return an array.")
else:
print("Invalid use of script query, required parameters:")
print("")
`
I am using cacti 1.2.25 on ubuntu 18.04 with Mariadb 10.4 and PHP 7.2 My python script is polling Rx/Tx octets from host and working from command prompt and cacti log also showing output
here is logs from cacti.log
2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[xxx_WTC_S_8P-01] HT[1] DS[xxx_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] Graphs[xxx_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] TT[305.33] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '2', output: tejasStoctetsTx:72369859
2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '2'' 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[xxx_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] Graphs[xxx_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] TT[288.09] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '2', output: tejasStoctetsRx:1337737592 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '2'' 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[xxx_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] Graphs[xxx_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] TT[283.51] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '1', output: tejasStoctetsTx:20485 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '1'' 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[xxx_WTC_S_8P-01] HT[1] DS[xxx_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] Graphs[xxx_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] TT[280.06] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '1', output: tejasStoctetsRx:28363 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'xxx_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '1''
here is my python3 script, having permission -rwxr-xr-x 1 www-data www-data 2100 Oct 29 10:32 olt_interface_traffic.py
`#!/home/hostaddition/bin/python
import sys import easysnmp
Check if the script is run from the command line
if not sys.argv[0] or len(sys.argv) < 14: print("This script is only meant to run at the command line.") sys.exit(1)
Define all OIDs we need for further processing
oids = { "index": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.7", "ifOperStatus": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.3", "ifType": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.4", "objectName": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.1001", "lCTName": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.1002", "tejasStoctetsRx": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.480", "tejasStoctetsTx": ".1.3.6.1.4.1.8255.1.2.1.2.37.1.1.482", }
xml_delimiter = "!"
All required input parameters
hostname = sys.argv[1] snmp_community = sys.argv[2] snmp_version = int(sys.argv[3]) snmp_port = int(sys.argv[4]) snmp_timeout = int(sys.argv[5]) max_oids = int(sys.argv[6])
Required for SNMP V3
snmp_auth_username = sys.argv[7] snmp_auth_password = sys.argv[8] snmp_auth_protocol = sys.argv[9] snmp_priv_passphrase = sys.argv[10] snmp_priv_protocol = sys.argv[11] snmp_context = sys.argv[12] cmd = sys.argv[13]
if len(sys.argv) > 14: query_field = sys.argv[14] else: query_field = None
if len(sys.argv) > 15: query_index = sys.argv[15] else: query_index = None
Get the number of SNMP retries from global settings
snmp_retries = 1 # You may change this as needed
Initialize the SNMP session
session = easysnmp.Session( hostname=hostname, community=snmp_community, version=snmp_version, timeout=2, # Convert seconds to microseconds retries=snmp_retries, )
Function to reindex the SNMP response
def reindex(arr): return [entry.value for entry in arr]
Perform SNMP operations based on the command
if cmd == "index":
Retrieve all indices from the target
elif cmd == "query" and query_field: arr_index = reindex(session.bulkwalk(oids["index"])) arr = reindex(session.bulkwalk(oids[query_field])) min_length = min(len(arr_index), len(arr))
for i in range(len(arr_index)):
elif cmd == "get" and query_field and query_index:
Perform SNMP walk on the specified query_field OID
else: print("Invalid use of script query, required parameters:") print(" ")
`
2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[MUM_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] Graphs[MUM_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] TT[305.33] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '2', output: tejasStoctetsTx:72369859 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '2'' 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[MUM_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] Graphs[MUM_WTC_S_8P-01 - Port_Eth-10000-1-1-2 - ETH-1-1-2 - Interface Traffic] TT[288.09] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '2', output: tejasStoctetsRx:1337737592 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '2'' 2023-11-04 14:35:03 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[MUM_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] Graphs[MUM_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] TT[283.51] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '1', output: tejasStoctetsTx:20485 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsTx' '1'' 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] Device[MUM_WTC_S_8P-01] HT[1] DS[MUM_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] Graphs[MUM_WTC_S_8P-01 - Port_Eth-1000-1-1-1 - ETH-1-1-1 - Interface Traffic] TT[280.06] SCRIPT: /home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '1', output: tejasStoctetsRx:28363 2023-11-04 14:35:02 - SPINE: Poller[Main Poller] PID[26777] PT[139688959690496] The executable is '/home/hostaddition/bin/python' in '/home/hostaddition/bin/python /var/www/html/cacti/scripts/query_interface_traffic.py 'MUM_WTC_S_8P-01' 'private' '2' '161' '2' '50' '""' '""' '""' '""' '""' '""' 'get' 'tejasStoctetsRx' '1''
here is script_query xml file `
`