def decommissioned_lookup(guests, bm_id): """ PROCEDURE TO CHECK FOR DECOMMISSIONED SERVERS """ decommissioned_servers = check_decommissioned(guests, bm_id) if decommissioned_servers: log.info("Decommissioned servers: %s " % decommissioned_servers)
def run(self, options): test_name = inspect.stack()[1][3] log.info("Starting system test '%s' against server %s..." % (test_name, CONFIG['server'])) cookie = str(uuid.uuid4()) options.test_name = test_name options.system_test_cookie = cookie fetch_testrun_id_thread = threading.Thread( target=self._fetch_testrun_ids, args=(test_name, options.server, cookie, self.testrun_ids, log)) fetch_testrun_id_thread.start() parameters = _parameter_validator({}, options.__dict__) result = ots_trigger(parameters) # Wait for the thread to get testrun id fetch_testrun_id_thread.join() self.testrun_ids.sort() self.result = result return self
def kvm_discovery(dst_sl_accounts, inst_id, rep_writer, scan_type): """ Main method to collect kvm hypervisors. """ global bm_scanned global total_guests for account in dst_sl_accounts: if inst_id: hw_records = get_hardware(account[0], account[1]) if hw_records: search = [bm for bm in hw_records if int(inst_id) == bm["id"]] if search: hw_records = search else: continue else: continue else: hw_records = get_hardware(account[0], account[1]) print "\n" print 34 * "-" print "# PROCESSING ACCOUNT: %s #" % account[0] print 34 * "-" if hw_records: print 63 * "-" print "# COLLECTING BARE METAL INFO. (%d baremetals)" % len(hw_records) print 63 * "-" log.info("PROCESSING ACCOUNT: %s , Baremetals:%d)" % (account[0], len(hw_records))) else: print 63 * "-" print "# COLLECTING BARE METAL INFO." print 63 * "-" print " * Baremetals not found" log.info("PROCESSING ACCOUNT: %s ,Baremetals: False)" % account[0]) continue # Collect firewalls information fw_records = get_firewalls(account[0], account[1])[1:] print 63 * "-" print "# GET CONNECTION PARAMS." print 63 * "-" # TODO Para cada hardware threads para esse procedimento for hw in hw_records: if "primaryBackendIpAddress" not in hw: continue connections = [get_connection_params(hw, fw_records)] if inst_id: if connections[0]["bm_id"] != int(inst_id): continue bm_scanned += 1 create_db_guest_file(account, connections[0]["bm_id"]) # ------------------# # Connections tests # # ------------------# print "\n * Baremetal: %s" % hw["fullyQualifiedDomainName"] print " - Connection info:" print " - hw_id: %s" % connections[0]["bm_id"] print " - hw_ip: %s" % connections[0]["bm_ip"] print " - fw_ip: %s" % connections[0]["fw_ip"] for conn in connections: # Firewall sem ip if not conn["fw_ip"]: conn["fw_conn"] = False conn["bm_conn"] = False conn["fw_conn_message"] = "Firewall ip not available" conn["bm_conn_message"] = "Firewall ip not available" print " - Firewall connectivity: %s " % conn["fw_conn"] print " - Error: %s" % conn["fw_conn_message"] log.error( "ACCOUNT: %s, HW_ID: %s, FW_CNX: %s, FW_CNX_MESSAGE: %s" % (account[0], hw["id"], conn["fw_conn"], str(conn["fw_conn_message"]).strip()) ) rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), ] ) continue # --------------------------------------------------------------# # Firewall Connection test # # --------------------------------------------------------------# if type(conn["fw_ip"]) != bool: for ip in conn["fw_ip"]: result = test_firewall_connection(ip) if result[0] == 0: conn["fw_conn"] = True conn["fw_conn_message"] = result[1] conn["fw_ip"] = ip break else: conn["fw_conn"] = False conn["fw_conn_message"] = result[1] conn["bm_conn"] = False conn["bm_conn_message"] = "Firewall not available" continue print " - Firewall connectivity: %s " % conn["fw_conn"] # --------------------------------------------------------------# # BareMetal Connection test # # --------------------------------------------------------------# if conn["fw_conn"]: cnx_method = "jump_host" result = test_baremetal_connections(conn) if result[0] == 0: # Connection successful conn["bm_conn"] = True conn["bm_conn_message"] = "Baremetal %s is alive" % (result[1]) user = result[2] print " - Baremetal connectivity: %s" % conn["bm_conn"] print " - Jump_host connection OK" sync_result = send_collectors(conn["fw_ip"], conn["bm_ip"], user, cnx_method) if sync_result[0] != 0: # debug print sync_result[1] conn["rsync"] = False print " - ERROR: Rsync failed" conn["bm_conn_message"] = "ERROR: Rsync failed" log.error("ACCOUNT: %s, HW_ID: %s, ERROR: RSYNC FAILED" % (account[0], hw["id"])) rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), conn["bm_ip"], conn["bm_conn"], str(conn["bm_conn_message"]).strip(), ] ) continue else: conn["rsync"] = True hyp_data = execute_collector(conn["fw_ip"], conn["bm_ip"], result[2], cnx_method, scan_type) else: conn["bm_conn"] = False conn["bm_conn_message"] = "Error: %s" % (result[1]) print " - Baremetal connectivity: %s" % conn["bm_conn"] print " - %s" % conn["bm_conn_message"] log.error( "ACCOUNT: %s, HW_ID: %s, FW_CNX: %s, BM_CNX: %s, FW_CNX_MESSAGE: %s, BM_CNX_MESSAGE: %s" % ( account[0], hw["id"], conn["fw_conn"], conn["bm_conn"], conn["fw_conn_message"], conn["bm_conn_message"], ) ) rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), conn["bm_ip"], conn["bm_conn"], str(conn["bm_conn_message"]).strip(), ] ) continue else: print " - Error: %s" % conn["fw_conn_message"] log.error( "ACCOUNT: %s, HW_ID: %s, FW_CNX: %s, FW_CNX_MESSAGE: %s" % (account[0], hw["id"], conn["fw_conn"], str(conn["fw_conn_message"]).strip()) ) rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), ] ) continue # Data collected. if "hyp_data" in locals(): if hyp_data: if "INTERNAL ERROR" in hyp_data: print " - Error: %s" % hyp_data["INTERNAL ERROR"] log.error("Error: %s" % hyp_data["INTERNAL ERROR"]) elif "error" in hyp_data["hypervisor"]["node_info"]: print " - Failed to connect to the hypervisor" log.error( "ACCOUNT: %s,HW_ID: %s, FQDN: %s, BM_IP: %s, ERROR: Failed to connect to the hypervisor" % (account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["bm_ip"]) ) elif not hyp_data["hypervisor"]["guests"]: guest = False print " - KVM Guests not found" log.info("Guests not found: %s" % hyp_data["hypervisor"]["guests"]) rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), conn["bm_ip"], conn["bm_conn"], str(conn["bm_conn_message"]).strip(), guest, ] ) continue elif not hyp_data["hypervisor"]["node_info"]["new_guests"] and hyp_data["hypervisor"]["guests"]: guest = True decommissioned_lookup(hyp_data["hypervisor"]["guests"], connections[0]["bm_id"]) log.info("Guests already inserted at the DB: %s" % hyp_data["hypervisor"]["guests"]) print " - Guests already inserted at the DB." rep_writer.writerow( [ account[0], hw["id"], hw["fullyQualifiedDomainName"], conn["networkVlans"], conn["fw_ip"], conn["fw_conn"], str(conn["fw_conn_message"]).strip(), conn["bm_ip"], conn["bm_conn"], str(conn["bm_conn_message"]).strip(), guest, ] ) continue elif hyp_data["hypervisor"]["node_info"]["new_guests"]: # New guests ----------------------------------------------------------- print " - New guests found: %s" % hyp_data["hypervisor"]["guests"] log.info("New guests found: %s" % hyp_data["hypervisor"]["guests"]) # ---------------------------------------------------------------------- guest = True total_guests += len(hyp_data["hypervisor"]["guests"]) if "networks_scaneed" in hyp_data["hypervisor"]["node_info"]: # Networks Scanned ------------------------------------------------- print " - NETWORKS FOUND:" for ntw in hyp_data["hypervisor"]["node_info"]["networks_scaneed"]: print " - %s" % ntw # ------------------------------------------------------------------ decommissioned_lookup(hyp_data["hypervisor"]["guests"], connections[0]["bm_id"]) print " - Inserting DB" insert_vms_data(hyp_data, hw) elif scan_type == "simple_scan" or scan_type == "complete_scan": guest = True total_guests += len(hyp_data["hypervisor"]["guests"]) # Networks Scanned ------------------------------------------------- print " - NETWORKS FOUND:" for ntw in hyp_data["hypervisor"]["node_info"]["networks_scaneed"]: print " - %s" % ntw # ------------------------------------------------------------------ decommissioned_lookup(hyp_data["hypervisor"]["guests"], connections[0]["bm_id"]) print " - Inserting DB" insert_vms_data(hyp_data, hw) if "search" in locals(): return 0
if __name__ == "__main__": """ MAIN """ # os.system('./start_ssh_agent.sh') # os.system('source $HOME/.keychain/${HOSTNAME}-sh') curr_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory cleanup(curr_dir) params = main() output = params[3] script_starded = datetime.now().strftime("%d/%m/%Y %H:%M:%S") log_folder = "%s/logs" % curr_dir if not os.path.exists(log_folder): os.makedirs(log_folder) log.info("SCRIPT STARTED %s" % script_starded) if not output: print "---------------------------------------" print "# SCRIPT STARTED at %s" % str(script_starded) print "---------------------------------------" default_std_out = sys.stdout sys.stdout = open("%s/logs/output.log" % curr_dir, "w") else: print "---------------------------------------" print "# SCRIPT STARTED at %s" % str(script_starded) print "---------------------------------------" accounts = get_accounts() if params[0] != "all" and params[0]: accounts = [acc for acc in accounts if acc[0] == params[0]] if not params[0]: