Exemplo n.º 1
0
def get_accounts():
    """
    RETURNS ALL SL ACCOUNTS (ACCOUNTS, KEYS AND DESCRIPTION)
    """
    # Get accounts to be procesed
    url = "https://dstnotes.lexington.ibm.com/DST/servers.nsf/CGI_SLAPIConfig?OpenAgent&login"
    request = urllib2.Request(url)
    base64string = base64.encodestring("%s:%s" % ("*****@*****.**", "sadb_uat")).replace("\n", "")
    request.add_header("Authorization", "Basic %s" % base64string)
    try:
        result = urllib2.urlopen(request)
    except Exception as e:
        send_mail("", "NOTES AGENT ERROR: Could not be possible to retrieve SL accounts and keys information")
        log.error("NOTES AGENT ERROR: Could not be possible to retrieve SL accounts and keys information")
        print "NOTES AGENT ERROR: Could not be possible to retrieve SL accounts and keys"
        sys.exit(234)
    sl_accounts = result.read().split("\n")
    # with open("accounts.txt", "r") as myfile:
    # data = myfile.read()
    # accounts = data.split('\n')
    account_data = []
    for sl_acc in sl_accounts:
        if not sl_acc:
            continue
        account_data.append(sl_acc.split("/"))
    return account_data
Exemplo n.º 2
0
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
Exemplo n.º 3
0
 else:
     instance_id = False
 # IP discover options --
 scan_type = params[2]
 # ----------------------
 total_accounts = len(accounts)
 log.info("total_accounts %d" % total_accounts)
 startTime = datetime.now()
 # CSV writer
 writer, csv_log_file = set_csv_writer()
 # KVM discovery ---------------------------------------------------
 try:
     kvm_discovery(accounts, params[1], writer, scan_type)
 except Exception as e:
     send_mail("", "Discovery error: Unhandled Exception (%s)" % e)
     log.error("Discovery error: %s" % e)
     print "Discovery error: Unhandled Exception (%s)" % e
     sys.exit(444)
 # -----------------------------------------------------------------
 csv_log_file.close()
 exec_time = str(datetime.now() - startTime)
 log.info("Baremetals scanned %d" % bm_scanned)
 log.info("total_guests %d" % total_guests)
 log.info("END OF EXECUTION %s" % exec_time)
 print "\n--------------------------------------------"
 print "# Baremetals found: %d" % bm_scanned
 print "# END OF EXECUTION %s" % exec_time
 print "--------------------------------------------"
 if not output:
     sys.stdout.close()
     sys.stdout = default_std_out