below is my python script, polling interface traffic, I have used snmp_bulkwalk as getting OID incrementing error, first attempt to script for cacti, this python script, script is having execute permission to www-data and in /var/www/html/cacti/scripts/ folder, not having much knowledge on index and query parameters, using python3.7, cacti version is 1.2.25
Code: Select all
#!/home/hostaddition/bin/python
import sys
from easysnmp import Session
# Define the base OID for the MIB
base_oid = '.1.3.6.1.4.1.8255.1.2.1.2.37.1.1'
# Define the ifType dictionary
iftype = {
"0": "ifOther",
"1": "e1",
"2": "e3",
"3": "sONET",
"4": "iBC",
"5": "vOICE",
"6": "eTHERNET",
"7": "aTM"
}
def fetch_data(host, oid):
session = Session(hostname=host, community='private', version=2)
try:
result = session.bulkwalk(oid)
return result
except Exception as e:
print(f"Error fetching data for OID {oid}: {e}")
return []
def main():
if len(sys.argv) != 3:
print("Usage: script.py <hostname>")
sys.exit(1)
host = sys.argv[1]
cmd = sys.argv[2]
# Define the MIB object identifiers
ifOperStatus_oid = base_oid + '.3'
ifType_oid = base_oid + '.4'
objectName_oid = base_oid + '.1001'
tejasStoctetsRx_oid = base_oid + '.480'
tejasStoctetsTx_oid = base_oid + '.482'
# Fetch data for the defined OIDs
ifOperStatus_data = fetch_data(host, ifOperStatus_oid)
ifType_data = fetch_data(host, ifType_oid)
objectName_data = fetch_data(host, objectName_oid)
tejasStoctetsRx_data = fetch_data(host, tejasStoctetsRx_oid)
tejasStoctetsTx_data = fetch_data(host, tejasStoctetsTx_oid)
# Process the retrieved data and print as a comma-separated string
for index, item in enumerate(objectName_data):
objectName = objectName_data[index].value
ifOperStatus = ifOperStatus_data[index].value
ifType = ifType_data[index].value
tejasStoctetsRx = tejasStoctetsRx_data[index].value
tejasStoctetsTx = tejasStoctetsTx_data[index].value
# Use the ifType dictionary to get the corresponding value
ifType_str = iftype.get(ifType, "Unknown")
output_string = f"objectName:{objectName},ifOperStatus:{ifOperStatus},ifType:{ifType_str},tejasStoctetsRx:{tejasStoctetsRx},tejasStoctetsTx:{tejasStoctetsTx}"
print(output_string)
if __name__ == '__main__':
main()
here is output of python script, here index and query are just parameter
here is my xml file in /var/www/html/cacti/resource/snmp_queries/folderxxxx:/var/www/html/cacti/scripts# ./olt_interface_traffic.py xxx_N_TJ8P_xxxT-1 index
objectName:Port_Eth-1000-1-1-1,ifOperStatus:2,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-10000-1-1-2,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:73856,tejasStoctetsTx:147712
objectName:Port_Eth-1000-1-1-3,ifOperStatus:2,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-10000-1-1-4,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:73856,tejasStoctetsTx:73856
objectName:Port_Eth-1000-1-1-5,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:23317,tejasStoctetsTx:31760
objectName:Port_Eth-1000-1-1-6,ifOperStatus:2,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-201,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-202,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-203,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-204,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-205,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-206,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-207,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-2500-1-1-208,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:0,tejasStoctetsTx:0
objectName:Port_Eth-100-1-1-3595,ifOperStatus:1,ifType:eTHERNET,tejasStoctetsRx:176121,tejasStoctetsTx:176595
Code: Select all
<interface>
<name>Get OLT Interface Traffic</name>
<script_path>/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py</script_path>
<arg_prepend>|host_hostname| |</arg_prepend>
<arg_index>index</arg_index>
<arg_query>query</arg_query>
<arg_get>walk</arg_get>
<arg_num_indexes>num_indexes</arg_num_indexes>
<output_delimeter>:</output_delimeter>
<index_order>objectName</index_order>
<index_order_type>numeric</index_order_type>
<index_title_format>|chosen_order_field|</index_title_format>
<fields>
<objectName>
<name>CPU Interface Name</name>
<direction>input</direction>
<query_name>index</query_name>
</objectName>
<ifOperStatus>
<name>Operationaal Status</name>
<direction>input</direction>
<query_name>query</query_name>
</ifOperStatus>
<ifType>
<name>Interface Type</name>
<direction>input</direction>
<query_name>query</query_name>
</ifType>
<tejasStoctetsRx>
<name>Received Packets</name>
<direction>output</direction>
<query_name>tejasStoctetsRx</query_name>
</tejasStoctetsRx>
<tejasStoctetsTx>
<name>Transmitted Packets</name>
<direction>output</direction>
<query_name>tejasStoctetsTx</query_name>
</tejasStoctetsTx>
</fields>
</interface>
Data Query Debug Information
Total: 0.000000, Delta: 0.000000, Found data query XML file at '/var/www/html/cacti/resource/snmp_queries/olt_interface_traffic.xml'
Total: 0.000000, Delta: 0.000000, Running Data Query [25].
Total: 0.000000, Delta: 0.000000, Found Type = '4' [Script Query].
Total: 0.000000, Delta: 0.000000, XML file parsed ok.
Total: 0.220000, Delta: 0.220000, Executing script for num of indexes '/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py 'MUM_N_TJ16P_APOLT-1' '|' 'num_indexes''
Total: 0.220000, Delta: 0.000000, Found number of indexes: Usage: script.py <hostname>
Total: 0.450000, Delta: 0.220000, Executing script for list of indexes '/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py 'MUM_N_TJ16P_APOLT-1' '|' 'index'', Index Count: 1
Click to show Data Query output for 'index'
Found index: Usage: script.py <hostname>
Click to show Data Query output for field 'objectName'
Executing script query '/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py 'MUM_N_TJ16P_APOLT-1' '|' 'query' 'index''
Found item [objectName=' script.py <hostname>'] index: Usage
Click to show Data Query output for field 'ifOperStatus'
Executing script query '/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py 'MUM_N_TJ16P_APOLT-1' '|' 'query' 'query''
Found item [ifOperStatus=' script.py <hostname>'] index: Usage
Click to show Data Query output for field 'ifType'
Executing script query '/home/hostaddition/bin/python /var/www/html/cacti/scripts/olt_interface_traffic.py 'MUM_N_TJ16P_APOLT-1' '|' 'query' 'query''
Found item [ifType=' script.py <hostname>'] index: Usage
Total: 1.100000, Delta: 0.660000, Checking for Sort Field change. No changes detected.
Total: 1.100000, Delta: 0.000000, Update Data Query Sort Cache complete
Total: 1.100000, Delta: 0.000000, Index Association with Local Data complete
Total: 1.150000, Delta: 0.040000, No Index Changes Detected, Skipping Re-Index and Poller Cache Re-population
Total: 1.150000, Delta: 0.000000, Automation Executing for Data Query complete
Total: 1.150000, Delta: 0.000000, Plugin hooks complete