def identify_device_RID_from_int_ip(ip): """ Author - Jonathan Steward Function - finds the device_id based on interface ip Inputs - String - Ip address of interface returns - int - ID of related device """ command = """ SELECT device_id from `FYP Data`.interfaces WHERE ip_address = '{}';""".format(ip) result = get_data(command) print "device_id for {} ip is {}".format(ip, result) if not result: print_error("something wrong getting device information") return (False, "") if len(result) > 1: print_error("more than one device record, can't carry out automation") # email admin return (False, "") result = result[0][0] print "device id is {}".format(result) command = """ SELECT ip from `FYP Data`.device_table WHERE device_id = '{}';""".format(result) result = get_data(command) print "device RID is {}".format(result[0][0]) return result[0][0]
def match_db_to_device(device_interfaces, device_id): """ Author - Jonathan Steward Function - Take the list of interfaces and match up to the database interface and link the database information with the local interface Inputs - device_interfaces - list - list of interfaceobjects from SNMP gets device_id - returns - device_interfaces - list - list of interface objects from snmp gets with database info """ command = "SELECT * FROM `FYP Data`.interfaces where device_id = {}".format(device_id) # data will be ID/device_id/name/description/ip_address/state/lastupdate/traffic_counter/speed db_interfaces = get_data(command) for i in range(len(device_interfaces)): for db_int in db_interfaces: if device_interfaces[i].value == db_int[2]: name = str(device_interfaces[i].value) oid_index = int(device_interfaces[i].oid_index) device_interfaces[i] = {"name": name, "oid_index": oid_index} device_interfaces[i]["db_id"] = db_int[0] device_interfaces[i]["previous_update"] = db_int[6] device_interfaces[i]["previous_counter"] = db_int[7] device_interfaces[i]["speed"] = db_int[8] break return device_interfaces
def check_for_peers(): """ Author - Jonathan Steward Function - Gets data from the database about the different peers checks the state of the peers and calls the relevant functions Inputs - n/a returns - int - configures - Number of peers configured used for logging int - checks - Number of peers checked used for logging int - fails - Number of peer actions that failed used for logging """ command = "SELECT * FROM `FYP Data`.BGP_peers" data = get_data(command) # data = peerId/device_id/remote_asn/loopback_source/remote_ip/source_interface/approved/approver/configured non_approved = [] configures = [] checks = [] fails = [] for peer in data: if peer[6] == 0: # Need to approve print_error("peer id {} for device {} and remote ip {} needs to be approved".format( peer[0], peer[1], peer[4])) non_approved.append(peer[0]) elif peer[8] == 0: # Need to configure set_peer_status(peer[0], 1) configures.append(peer[0]) state, peer_object, device = configure_peer_workflow(peer) if state: set_peer_status(peer[0], 2) state = check_peer_status( peer_object["device_ip"], peer_object["remote_ip"], device) if state: print_error("Peer is established") set_peer_status(peer[0], 3) else: fails.append(peer[0]) elif peer[8] == 1: # Started to configured but didn't work print_error("Peer id {} was attempted to be configured previously but there was a problem".format(peer[0])) # EMAIL NETWORK ENGINEER OR SOMETHING SIMILAR elif peer[8] == 2: # Configured but wasn't in established state checks.append(peer[0]) print_error("Checking peer: {}".format(peer)) state = check_peer_workflow(peer) if state: print_error("Peer is established") set_peer_status(peer[0], 3) else: print_error("Peer still not established") fails.append(peer[0]) else: # Peer fully configured pass # Setting peer status to in progress return configures, checks, fails, non_approved
def main(): command = "SELECT * FROM `FYP Data`.device_table;" devices = get_data(command) for device in devices: (device_id, device_ip, vendor, community, username, passwd, enpass, config_lock, lock_reason, asn) = device device_interfaces = Grab_device_interfaces_snmp(device_ip, community) if not device_interfaces: continue device_interfaces = match_db_to_device(device_interfaces, device_id) device_interfaces = poll_traffic(device_interfaces, device_ip, community)
def close_event(device_int): """ Author - Jonathan Steward Function - closes event that was created once automation has been triggered Inputs - device_int - object - combined database and snmp gathered information returns - n/a """ command = """ SELECT * FROM `FYP Data`.interface_events where interface_id = {} and state ='active' and issue = 'out utilization'""".format(device_int["db_id"]) event = get_data(command) command = """ UPDATE `FYP Data`.`interface_events` SET `state` = 'resolved' WHERE event_id = {} ;""".format(event[0][0]) set_data_mysql(command) update_interface_and_history(device_int)
def Device_interfaces_update(): """ Author - Jonathan Steward Function - Workflow function Inputs - n/a returns - int - updates - the number of interface records updated int - adds - The number of interface records added """ command = "SELECT * FROM `FYP Data`.device_table;" devices = get_data(command) updates = 0 adds = 0 unreachable = 0 for device in devices: updates, adds, unreachable = process_interfaces( device, updates, adds, unreachable) return updates, adds, unreachable """
def find_device_ip(device_id): """ Author - Jonathan Steward Function - find the RID of the device required along with some other details Inputs - int - device_id - device_id of the request device returns - string - RID of device string - Vendor of device int - ASN of device """ command = """ SELECT ip, vendor, asn from `FYP Data`.device_table WHERE device_id = {} """.format(device_id) data = get_data(command) if len(data) > 1: print_error("There multipul devices listed under device_id {} somehow".format(device_id)) return "", "", "" if len(data) == 0: print_error("No Data was found for device_id {}".format(device_id)) return "", "", "" print data[0] return data[0][0], data[0][1], data[0][2]
def check_for_event(device_int, device_ip): """ Author - Jonathan Steward Function - checks for an existing event and if one exists if its an old event or not Inputs - Global - timeout_minuets - defined at the top to identify how many minuets old an event needs to be before closing it. device_int - object - combined database and snmp gathered information device_ip - string - ip address of the host for this event returns - """ command = """ SELECT * FROM `FYP Data`.interface_events where `interface_id` = '{}' and `state` = 'active' and `issue` = 'out utilization'""".format(device_int["db_id"]) events = get_data(command) if events: time_now = datetime.now() time_diff = (time_now - events[0][4]).seconds timeout_minuets = 5 if time_diff / 60 > timeout_minuets: print_error("closing old event older than {} minuets".format(timeout_minuets)) command = """ UPDATE `FYP Data`.`interface_events` SET `state` = 'resolved' WHERE event_id = {} ;""".format(events[0][0]) set_data_mysql(command) else: print "event for {} on {} already exists will not act".format(device_int["name"], device_ip) return False command = """ INSERT INTO `FYP Data`.`interface_events` (`interface_id`, `state`, `issue`) VALUES ('{}', 'active', 'out utilization');""".format(device_int["db_id"]) set_data_mysql(command) return True
def process_interfaces(device, updates, adds, unreachable): """ Author - Jonathan Steward Function - Function to carry out the logic to detect new or update interfaces and add to DB Inputs - Device - touple - one data record from the database containing device details updates - int - Number of updates so far adds - int - Number of added interfaces so far returns - int - updates - the number of interface records updated int - adds - The number of interface records added """ print "grabbing details for:\n{}".format(device) (device_id, device_ip, vendor, community, username, passwd, enpass, config_lock, lock_reason, asn) = device device_interfaces = grab_device_interfaces(device_ip, community) if not device_interfaces: unreachable += 1 return updates, adds, unreachable command = "SELECT * FROM `FYP Data`.interfaces where device_id = {}".format( device_id) db_interfaces = get_data(command) # db_interfaces will be ID/device_id/name/description/ip_address/state/lastupdate/traffic_counter/speed for ifindex, device_int in device_interfaces.items(): # Checking new device interface state = "new" for interface in db_interfaces: # finding matching database interface if device_int["name"] == interface[2]: # Check state if device_int["description"] != interface[3]: state = "update" updates += 1 command = update_command(device_int, interface) print "need to update record" break if device_int["speed"] != interface[8]: state = "update" updates += 1 command = update_command(device_int, interface) print "need to update record" break if device_int["ip"] != interface[4]: state = "update" updates += 1 command = update_command(device_int, interface) print "need to update record" break if device_int["state"] != interface[5]: state = "update" updates += 1 command = update_command(device_int, interface) print "need to update record" break state = "good" # print "interface details the same as the database." break else: continue # itterated through all db interfaces if state == "new": adds += 1 print "A new interface was detected: {}".format(device_int["name"]) command = add_command(device_int, device_id) return updates, adds, unreachable
def db_read(attempt): get_data("SELECT * FROM `FYP Data`.device_table;") logging.info("done read number: {}".format(attempt)) print "done read number: {}".format(attempt)