示例#1
0
def write_hosts_to_db(host_list: List):
    session = session_factory()

    for host in host_list:
        new_host = Host(host=host[0], host_name=host[1])
        session.add(new_host)

    session.commit()
    session.close()

    return None
示例#2
0
def compare_ping_results(batch: int) -> Dict:
    batch_prev = batch - 1

    session = session_factory()

    # get the results from the previous batch and store in a dict
    results_prev = session.query(PingLog).filter_by(batch=batch_prev).all()

    dict_prev = {"up": [], "down": []}

    for row in results_prev:
        if row.status == True:
            dict_prev["up"].append((row.host, row.host_name))
        else:
            dict_prev["down"].append((row.host, row.host_name))

    # get the results of the current batch and store them in a dictionary
    results_cur = session.query(PingLog).filter_by(batch=batch).all()

    dict_cur = {"up": [], "down": []}

    for row in results_cur:
        if row.status == True:
            dict_cur["up"].append((row.host, row.host_name))
        else:
            dict_cur["down"].append((row.host, row.host_name))

    # create a dict of changes
    changes_dict = {
        "now_up": [],
        "now_down": [],
        "no_change": [],
        "new_host": []
    }

    for item in dict_cur["up"]:
        if item in dict_prev["up"]:
            changes_dict["no_change"].append(item)
        elif item in dict_prev["down"]:
            changes_dict["now_up"].append(item)
        else:
            changes_dict["new_host"].append(item)

    for item in dict_cur["down"]:
        if item in dict_prev["up"]:
            changes_dict["now_up"].append(item)
        elif item in dict_prev["down"]:
            changes_dict["no_change"].append(item)
        else:
            changes_dict["new_host"].append(item)

    return changes_dict
示例#3
0
def remove_host(id: int):
    session = session_factory()

    host = get_host_by_id(id)
    # check if user id exists
    if not host:
        print(f'Host ID {id} not found in db. Please try again.')
        return True

    session.delete(host)
    session.commit()
    session.close()

    return True
示例#4
0
def remove_user(id: int):
    session = session_factory()

    user = get_user_by_id(id)
    # check if user id exists
    if not user:
        print(f'User ID {id} not found in db. Please try again.')
        return True

    session.delete(user)
    session.commit()
    session.close()

    return True
示例#5
0
def get_batch_num() -> int:
    session = session_factory()

    # check if there are any existing batches
    if session.query(PingLog).all():
        # get a list of all the hosts in the db
        batch_num = session.query(PingLog.batch).order_by(
            PingLog.id.desc()).first()
        batch_num = batch_num.batch
        session.close()
    else:
        batch_num = 0

    return batch_num
示例#6
0
def add_host(host: List) -> bool:
    session = session_factory()

    # check for duplicate email
    if get_host_by_address(host[0]):
        print(f'This host already exists')
        return True

    new_host = Host(host=host[0], host_name=host[1])
    session.add(new_host)

    session.commit()
    session.close()

    sys.exit(f"New host {host[1]} created.")
示例#7
0
def add_user(user: List) -> bool:
    session = session_factory()

    # check for duplicate email
    if get_user_by_email(user[2]):
        print(f'This email already exists')
        return True

    new_user = User(f_name=user[0], l_name=user[1], email=user[2])
    session.add(new_user)

    session.commit()
    session.close()

    return True
示例#8
0
def get_host_list() -> List:
    session = session_factory()

    # get a list of all the hosts in the db
    hosts = session.query(Host).all()

    # create a list of hosts to ping
    host_list = []
    for host in hosts:
        host_list.append((host.host, host.host_name))

    # clear the db session
    session.close()

    return host_list
示例#9
0
def ping_the_hosts(host_list: List, response_wait_time: int) -> Tuple[List, int]:
    for host in host_list:
        # ping each host with one packet and wait up to 250 milliseconds for a response
        print(platform.system())
        if platform.system() == 'Windows':
            response = os.system(f'ping -W {response_wait_time} -n 1 {host[0]}')
        else:
            response = os.system(f'ping -W {response_wait_time} -c 1 {host[0]}')

        if response == 0:
            report.append([f'{host[0]} which is *{host[1]}* is UP!', 1])
        else:
            report.append([f'{host[0]} which is *{host[1]}* is DOWN!', 0])

        results.append((host[0], host[1], response))

    # write results to the db
    session = session_factory()

    # get previous batch number
    batch_num = get_batch_num()
    # increment batch number
    batch_num = batch_num + 1

    for result in results:
        if result[2] == 0:
            status = 1
        else:
            status = 0
        log_entry = PingLog(batch=batch_num, host=result[0], host_name=result[1], status=status)
        session.add(log_entry)

    session.commit()
    session.close()

    return report, batch_num
示例#10
0
def get_all_hosts() -> List[Host]:
    session = session_factory()
    hosts = session.query(Host).all()
    session.close()

    return hosts
示例#11
0
def get_host_by_id(id: int) -> Optional[Host]:
    session = session_factory()
    host = session.query(Host).filter_by(id=id).first()
    session.close()
    return host
示例#12
0
def get_host_by_address(address: str) -> Optional[Host]:
    session = session_factory()
    host = session.query(Host).filter_by(host=address).first()
    session.close()
    return host
示例#13
0
def get_user_by_id(id: int) -> Optional[User]:
    session = session_factory()
    user = session.query(User).filter_by(id=id).first()
    session.close()
    return user
示例#14
0
def list_users() -> List[User]:
    session = session_factory()
    user_list = session.query(User).order_by(User.id.desc()).all()
    session.close()
    return user_list
示例#15
0
def get_user_by_email(email: str) -> Optional[User]:
    session = session_factory()
    user = session.query(User).filter_by(email=email).first()
    session.close()
    return user