Esempio n. 1
0
def _poll_hosts_threaded():
    log.debug('Starting host polling')
    s = time.perf_counter()

    with app.app_context():
        pool = ThreadPool(config['Max_Threads'])
        threads = []

        for host in json.loads(get_all_hosts()):
            threads.append(pool.apply_async(_poll_host_task, (host['id'], )))

        pool.close()
        pool.join()

        for thread in threads:
            host_info, new_poll_history, host_alert = thread.get()
            host = Hosts.query.filter_by(id=int(host_info['id'])).first()
            host.previous_status = host_info['previous_status']
            host.status = host_info['status']
            host.last_poll = host_info['last_poll']
            db.session.add(new_poll_history)
            if host_alert:
                db.session.add(host_alert)
        db.session.commit()

    log.debug("Host polling finished executing in {} seconds.".format(
        time.perf_counter() - s))
Esempio n. 2
0
def _poll_host_task(host_id):
    with app.app_context():
        host_info = json.loads(get_host(int(host_id)))
        status, poll_time, hostname = poll_host(host_info['ip_address'])
        host_alert = None

        # Update host status
        host_info['previous_status'] = host_info['status']
        host_info['status'] = status
        host_info['last_poll'] = poll_time

        # Add poll history for host
        if hostname:
            host_info['hostname'] = hostname

        new_poll_history = PollHistory(host_id=host_info['id'],
                                       poll_time=poll_time,
                                       poll_status=status)

        log.info('{} - {}'.format(host_info['alerts_enabled'],
                                  type(host_info['alerts_enabled'])))
        if host_info[
                'alerts_enabled'] and host_info['previous_status'] != status:
            # Create alert if status changed
            host_alert = HostAlerts(host_id=host_info['id'],
                                    hostname=host_info['hostname'],
                                    ip_address=host_info['ip_address'],
                                    host_status=host_info['status'],
                                    poll_time=host_info['last_poll'])

    return host_info, new_poll_history, host_alert
Esempio n. 3
0
def _get_alert_status_message(alert):
    with app.app_context():
        host = Hosts.query.filter_by(id=alert.host_id).first()
        message = '<b>{} [{}]</b>: Status changed from <b>"{}"</b> to <b>"{}"</b> at {}<br><br>'.format(
            host.hostname,
            host.ip_address,
            host.previous_status,
            host.status,
            host.last_poll
        )

        return message
Esempio n. 4
0
def _poll_history_cleanup_task():
    log.debug('Starting poll history cleanup')
    s = time.perf_counter()

    with app.app_context():
        retention_days = json.loads(
            get_polling_config())['history_truncate_days']
        current_date = date.today()

        # Delete poll history where date_created < today - retention_days
        PollHistory.query.filter(PollHistory.date_created < (
            current_date - timedelta(days=retention_days))).delete()

        db.session.commit()

    log.debug("Poll history cleanup finished executing in {} seconds.".format(
        time.perf_counter() - s))
Esempio n. 5
0
def _host_status_alerts_threaded():
    with app.app_context():
        alerts_enabled = json.loads(get_alerts_enabled())['alerts_enabled']
        smtp_configured = json.loads(get_smtp_configured())['smtp_configured']
        alerts = HostAlerts.query.filter_by(alert_cleared=False).all()

        for alert in alerts:
            alert.alert_cleared = True

        if smtp_configured and alerts_enabled:
            pool = ThreadPool(config['Max_Threads'])
            threads = []

            message = ''
            for alert in alerts:
                threads.append(
                    pool.apply_async(_get_alert_status_message, (alert,))
                )

            pool.close()
            pool.join()

            for thread in threads:
                message += thread.get()

            if message:
                recipients = ';'.join(x['email'] for x in Schemas.users(many=True).dump(Users.query.filter_by(alerts_enabled=True)))
                try:
                    send_smtp_message(
                        recipient=recipients,
                        subject='IPMON - Host Status Change Alert',
                        message=message
                    )
                except Exception as exc:
                    log.error('Failed to send host status change alert email: {}'.format(exc))

        db.session.commit()