-1
import csv
import re
from pysnmp.entity.rfc3413.oneliner import cmdgen
from concurrent.futures import ThreadPoolExecutor

# Define your SNMP oids and other variables here
# (Keep your OID definitions and descriptions here)

UPS_Mode = ".1.3.6.1.2.1.33.1.4.1.0"
Battery_Input_Voltage = ".1.3.6.1.2.1.33.1.3.3.1.3.1"
Battery_Output_Voltage = ".1.3.6.1.2.1.33.1.4.4.1.2.1"
Battery_Estimated_Mins_Remaining = ".1.3.6.1.2.1.33.1.2.3.0"
Battery_Estimated_Charge_Remaining = ".1.3.6.1.2.1.33.1.2.4.0"

oids = (
UPS_Mode,
Battery_Output_Voltage,
Battery_Input_Voltage,
Battery_Estimated_Mins_Remaining,
Battery_Estimated_Charge_Remaining,
)

oid_descriptions = {
UPS_Mode: "UPS Mode",
Battery_Input_Voltage: "Battery Input Voltage",
Battery_Output_Voltage: "Battery Output Voltage",
Battery_Estimated_Mins_Remaining: "Battery Estimated Minutes Remaining",
Battery_Estimated_Charge_Remaining: "Battery Estimated Charge Remaining",
}

# SNMP community string
snmp_ro_comm = 'public'
auth = cmdgen.CommunityData(snmp_ro_comm)
cmdGen = cmdgen.CommandGenerator()

# Read UPS IP addresses from CSV
def read_csv(file_path):
    with open(file_path) as fh:
      return re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', fh.read())

# Perform SNMP query for a host
def snmp_query(host):
   errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
      auth,
      cmdgen.UdpTransportTarget((host, 161)),
     # Add your OIDs here
oids = (
UPS_Mode,
Battery_Output_Voltage,
Battery_Input_Voltage,
Battery_Estimated_Mins_Remaining,
Battery_Estimated_Charge_Remaining,
),

    lookupMib=False,
)

oid_desc_values = {"Host": host}
for oid, val in varBinds:
    oid_str = oid.prettyPrint()
    val_str = val.prettyPrint()

    oid_desc = oid_descriptions.get(oid_str)

    if oid == UPS_Mode:
        mode = "Normal mode" if val_str == "3" else "Battery mode"
        oid_desc_values[oid_desc] = mode
    else:
        oid_desc_values[oid_desc] = val_str

# Process varBinds and populate oid_desc_values dictionary

return oid_desc_values

 # Main function
 def main():
   input_csv_file = 'C:/users/bel/UPS.csv'
   output_csv_file = 'C:/users/bel/UPS_output.csv'
   lst = read_csv(input_csv_file)

with open(output_csv_file, mode='w', newline='') as csv_file:
    fieldnames = ['Host'] + list(oid_descriptions.values())
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writeheader()

    with ThreadPoolExecutor(max_workers=15) as executor:
        results = list(executor.map(snmp_query, lst))
        csv_writer.writerows(results)

print(f"CSV file has been created at: {output_csv_file}")

if __name__ == '__main__':
   main()   

My script is only printing host column in the CSV file but not giving output for any other OID description. I want to store the respective OID value of the defined OIDs in the CSV file but it is not giving the expected output and am not able to understand what is wrong with the code, it is reading a CSV file to access the IP addresses of ups devices using an SNMP card.

NickD
  • 5,937
  • 1
  • 21
  • 38
  • What do you get when you print out the `fieldnames` variable? The behaviour you described sounds like it should be a problem with the fieldnames list. – Cmd858 Aug 28 '23 at 14:23
  • This code has obvious indentation errors. Please fix. – John Gordon Sep 01 '23 at 01:29

0 Answers0