I used Mininet to create a topology which contains of a floodlight controller, OVS switches and hosts. I generated a traffic and tried to collect data using python and jupyter notebook, but it returns zero packet counts.
Following are my code:
from pysnmp.hlapi import *
import csv
import time
import os
# Add your MIB directory to the search path
mib_path = '/usr/share/snmp/mibs/'
mib_builder = builder.MibBuilder()
mib_builder.addMibSources(builder.DirMibSource(mib_path))
# Set the MIB directory path
mib_dir = '/usr/share/snmp/mibs'
# Update the MIB search path
if 'MIBDIRS' in os.environ:
os.environ['MIBDIRS'] = f"{mib_dir}:{os.environ['MIBDIRS']}"
else:
os.environ['MIBDIRS'] = mib_dir
# Function to retrieve bandwidth usage and packet counts from a switch
def get_traffic_data(switch_ip, community_string):
# SNMP OIDs for bandwidth usage and packet counts
bandwidth_oid = ObjectIdentity('IF-MIB', 'ifInOctets', 1)
packet_count_oid = ObjectIdentity('IF-MIB', 'ifInUcastPkts', 1)
def get_traffic_data(switch_ip, community_string, bandwidth_oid, packet_count_oid):
# SNMP GET operation to retrieve data
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community_string),
UdpTransportTarget((switch_ip, 161)),
ContextData(),
ObjectType(bandwidth_oid),
ObjectType(packet_count_oid),
lookupMib=False) # Disable MIB lookup
)
if errorIndication:
print(f"Error retrieving data: {errorIndication}")
return None, None
elif errorStatus:
print(f"Error retrieving data: {errorStatus.prettyPrint()}")
return None, None
else:
# Extract bandwidth usage and packet counts from SNMP response
bandwidth_usage = int(varBinds[0][1]) if varBinds[0][1] else 0
packet_count = int(varBinds[1][1]) if varBinds[1][1] else 0
return bandwidth_usage, packet_count
def collect_data_regularly(switch_ip, community_string, interval, num_iterations, bandwidth_oid, packet_count_oid):
data = []
for i in range(num_iterations):
# Get traffic data from the switch
bandwidth_usage, packet_count = get_traffic_data(switch_ip, community_string, bandwidth_oid, packet_count_oid)
if bandwidth_usage is not None and packet_count is not None:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
data.append((timestamp, bandwidth_usage, packet_count))
print(f"Iteration {i+1}: Bandwidth Usage = {bandwidth_usage}, Packet Count = {packet_count}")
else:
print("Error retrieving data")
time.sleep(interval)
return data
# Function to write data to CSV file
def write_to_csv(data, filename):
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Timestamp', 'Bandwidth Usage', 'Packet Count'])
for timestamp, bandwidth_usage, packet_count in data:
writer.writerow([timestamp, bandwidth_usage, packet_count])
if __name__ == "__main__":
switch_ip = '127.0.0.1'
community_string = 'public'
interval = 10 # Data collection interval in seconds (e.g., collect data every minute)
num_iterations = 10 # Number of iterations
# Define SNMP OIDs for bandwidth usage and packet counts
bandwidth_oid = ObjectIdentity('IF-MIB', 'ifInOctets', 1)
packet_count_oid = ObjectIdentity('IF-MIB', 'ifInUcastPkts', 1)
# Collect data at regular intervals
collected_data = collect_data_regularly(switch_ip, community_string, interval, num_iterations, bandwidth_oid, packet_count_oid)
# Write collected data to CSV file
write_to_csv(collected_data, 'traffic_data.csv')
I don’t know what the issue is.