Beispiel #1
0
def insert_audit():
    """<add a docstring when method is fully understood>"""
    scriptname = "Audit"

    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                               passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME,
                               port=PROV_DB_PORT)
        cursor = conn.cursor()
    except:
        err_msg = "Audit: Connection failed to Provenance database."
        track_history_exceptions(err_msg)
        notify_support(err_msg, scriptname)


    try:
        cursor.execute(AUDIT_SELECT % ('N'))
        results = cursor.fetchall()

        # inconsistent indent characters makes this even _harder_ to follow
        # THOU SHALL NOT HAVE 180+ line methods with nesting this DEEP!
        if len(results) != 0:

            for row in results:
                # Python has a built-in `id`, so use _id to avoid redefining.
                _id = int(row[0])
                uuid = int(row[1])
                service_id = int(row[2])
                category_id = int(row[3])
                event_id = int(row[4])
                username = str(row[5])
                proxy_username = str(row[6])
                event_data = str(row[7])
                request_ipaddress = str(row[8])
                created_date = int(row[9])

                all_data = "{Audit row : " + str(_id) + "}"
                # no proxy_username AND no event_data
                if proxy_username is None and event_data is None:
                    insert_status = cursor.execute(QUERY_NO_PROXY_DATA %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                request_ipaddress,
                                                created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = ("Audit: QUERY_NO_PROXY_DATA query failed" +
                                    all_data)
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                # no proxy_username case (inside the 'if len > 0')
                elif proxy_username != None:
                    insert_status = cursor.execute(QUERY_PROXY %
                                            (uuid, event_id, category_id,
                                            service_id, username,
                                            proxy_username, request_ipaddress,
                                            created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        " query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = "Audit: QUERY_PROXY query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                # no event_data case (inside the 'if len > 0')
                elif event_data != None:
                    insert_status = cursor.execute(QUERY_DATA %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                event_data, request_ipaddress,
                                                created_date))

                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)

                    else:
                        err_msg = "Audit: QUERY_DATA query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                else:
                # final else block
                    insert_status = cursor.execute(QUERY_ALL %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                proxy_username, event_data,
                                                request_ipaddress,
                                                created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = "Audit: QUERY_ALL query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)

        # outside the if block ...
        cursor.close()

    except Exception, e:

        err_msg = "AUDIT EXCEPTION: " + str(e) + ": " + all_data
        log_exception(err_msg)
        failed_inserts_audit(all_data)
        notify_support(err_msg, scriptname)
        cursor.close()
def insert_history():
    """Handles inserting history records for provenance objects."""
    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                                passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME)
        cursor = conn.cursor()
    except:
        err_msg = "Connection failed to Provenance database."
        track_history_exceptions(err_msg)
        notify_support(err_msg, SCRIPTNAME)

    try:

        cursor.execute(PARENT_HIST_SELECT_QUERY % ('Y', 'N'))
        p_results = cursor.fetchall()

        if len(p_results) != 0:

            for parent in p_results:

                p_id = int(parent[0])
                p_history_code = str(parent[1])
                p_uuid = int(parent[2])
                p_service_id = int(parent[3])
                p_category_id = int(parent[4])
                p_event_id = int(parent[5])
                p_username = str(parent[6])
                p_createdate = int(parent[7])

                proc_return = cursor.execute(PROV_HIST_SELECT_ID % (p_uuid,
                                            p_event_id, p_category_id,
                                            p_service_id, p_username,
                                            p_createdate))
                p_id_results = cursor.fetchall()

                # parent ID results contain only 1 result record
                if len(p_id_results) == 1:
                    _process_single_parent_results(cursor, p_id,
                                                    p_history_code,
                                                    p_id_results)

                # parent ID results greater than 1 result record
                elif len(p_id_results) > 1:
                    err_msg = ("Multiple entries found for parent row. " +
                                "[parent_provenance_id]:" + "[" + str(p_id) +
                                "]")
                    track_history_exceptions(err_msg)
                    notify_support(err_msg, SCRIPTNAME)

                else:
                    err_msg = ("No entry found for parent row, but history " +
                                "tracking flag enabled. [parent_provenance_id,"
                                + "]:" + "[" + str(p_id) + "]")
                    track_history_exceptions(err_msg)
                    notify_support(err_msg, SCRIPTNAME)
        # finish up and close our cursor
        cursor.close()

    except Exception, exc:
        err_msg = ("Exception occured after establishing DB connection: " +
                    str(exc))
        track_history_exceptions(err_msg)
        notify_support(err_msg, SCRIPTNAME)
        cursor.close()
Beispiel #3
0
def insert_history():
    """Handles inserting history records for provenance objects."""
    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                               passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME,
                               port=PROV_DB_PORT)
        cursor = conn.cursor()
    except:
        err_msg = "Connection failed to Provenance database."
        track_history_exceptions(err_msg)
        notify_support(err_msg, SCRIPTNAME)

    try:

        cursor.execute(PARENT_HIST_SELECT_QUERY % ('Y', 'N'))
        p_results = cursor.fetchall()

        if len(p_results) != 0:

            for parent in p_results:

                p_id = int(parent[0])
                p_history_code = str(parent[1])
                p_uuid = int(parent[2])
                p_service_id = int(parent[3])
                p_category_id = int(parent[4])
                p_event_id = int(parent[5])
                p_username = str(parent[6])
                p_createdate = int(parent[7])

                proc_return = cursor.execute(PROV_HIST_SELECT_ID % (p_uuid,
                                            p_event_id, p_category_id,
                                            p_service_id, p_username,
                                            p_createdate))
                p_id_results = cursor.fetchall()

                # parent ID results contain only 1 result record
                if len(p_id_results) == 1:
                    _process_single_parent_results(cursor, p_id,
                                                    p_history_code,
                                                    p_id_results)

                # parent ID results greater than 1 result record
                elif len(p_id_results) > 1:
                    err_msg = ("Multiple entries found for parent row. " +
                                "[parent_provenance_id]:" + "[" + str(p_id) +
                                "]")
                    track_history_exceptions(err_msg)
                    notify_support(err_msg, SCRIPTNAME)

                else:
                    err_msg = ("No entry found for parent row, but history " +
                                "tracking flag enabled. [parent_provenance_id,"
                                + "]:" + "[" + str(p_id) + "]")
                    track_history_exceptions(err_msg)
                    notify_support(err_msg, SCRIPTNAME)
        # finish up and close our cursor
        cursor.close()

    except Exception, exc:
        err_msg = ("Exception occured after establishing DB connection: " +
                    str(exc))
        track_history_exceptions(err_msg)
        notify_support(err_msg, SCRIPTNAME)
        cursor.close()