Ejemplo n.º 1
0
    def ajax_search(self, query, refresh=False):
        if not re.match(r'^[a-z0-9-\*\? ]+$', query): abort(403)

        if refresh:
            self.nexus_init()
            all_hubs = nexus.list_hub_ids()
        else:
            c = sqlite3.connect(self.db_path).cursor()
            c.execute('SELECT hub_id FROM Cache')
            all_hubs = [row[0] for row in c.fetchall()]

        hubs = set()
        for keyword in query.split(' '):
            keyword = '*' + keyword.strip('*') + '*'
            hubs.update(fnmatch.filter(all_hubs, keyword))

        if len(hubs) == 1:
            return """
            <script>window.location.href = "{}";</script>
            """.format(url_for('serve_status', hub_id=list(hubs)[0]))

        results = []
        oldest_cache = arrow.utcnow()
        for hub_id in hubs:
            if refresh:
                data = fetch_data(hub_id, self.config)
                res = assess_data(data, self.config)

                results.append((res.hub_id, res.hub_health, res.error
                                or res.summary))

                continue

            c.execute('SELECT * FROM Cache WHERE hub_id=?', [hub_id])
            for hub_id, hub_health, summary, cache_time in c.fetchall():
                cache_time = arrow.get(cache_time, 'YYYY-MM-DD HH:mm:ss')
                oldest_cache = min(cache_time, oldest_cache)

                hub_health = {
                    'RED': nexus.RED,
                    'YELLOW': nexus.YELLOW,
                    'GREEN': nexus.GREEN,
                    'FAIL': nexus.RED
                }[hub_health]

                results.append((hub_id, hub_health, summary))

        if refresh or not results: oldest_cache = None
        else: oldest_cache = oldest_cache.humanize()
        # else: oldest_cache = '{} ({})'.format(
        #     oldest_cache.humanize(),
        #     oldest_cache.format('YYYY-MM-DD HH:mm:ss ZZ')
        # )

        return render_template('ajax_search.html',
                               results=results,
                               nexus=nexus,
                               oldest_cache=oldest_cache,
                               query=query)
Ejemplo n.º 2
0
    def ajax_status(self, hub_id):
        if not re.match(r'^[a-z0-9-]+$', hub_id): abort(403)

        self.nexus_init()

        data = fetch_data(hub_id, self.config)
        result = assess_data(data, self.config)

        return render_template('ajax_status.html', h=result, nexus=nexus)
Ejemplo n.º 3
0
    def ajax_search(self, query, refresh=False):
        if not re.match(r'^[a-z0-9-\*\? ]+$', query): abort(403)

        if refresh:
            self.nexus_init()
            all_nodes = nexus.list_node_ids()
        else:
            c = sqlite3.connect(self.db_path).cursor()
            c.execute('SELECT node_id FROM Cache')
            all_nodes = [row[0] for row in c.fetchall()]

        nodes = set()
        for keyword in query.split(' '):
            keyword = '*' + keyword.strip('*') + '*'
            nodes.update(fnmatch.filter(all_nodes, keyword))

        if len(nodes) == 1:
            return """
            <script>window.location.href = "{}";</script>
            """.format(url_for('serve_status', node_id=list(nodes)[0]))

        results = []
        oldest_cache = arrow.utcnow()
        for node_id in nodes:
            if refresh:
                data = fetch_data(node_id, self.config)
                res = assess_data(data, self.config)

                results.append((
                    res.node_id,
                    res.node_health,
                    res.error or res.summary
                ))

                continue

            c.execute('SELECT * FROM Cache WHERE node_id=?', [node_id])
            for node_id, node_health, summary, cache_time in c.fetchall():
                cache_time = arrow.get(cache_time, 'YYYY-MM-DD HH:mm:ss')
                oldest_cache = min(cache_time, oldest_cache)

                node_health = {
                    'RED': nexus.RED, 'YELLOW': nexus.YELLOW,
                    'GREEN': nexus.GREEN, 'FAIL': nexus.RED
                }[node_health]

                results.append((node_id, node_health, summary))

        if refresh or not results: oldest_cache = None
        else: oldest_cache = oldest_cache.humanize()
        # else: oldest_cache = '{} ({})'.format(
        #     oldest_cache.humanize(),
        #     oldest_cache.format('YYYY-MM-DD HH:mm:ss ZZ')
        # )

        return render_template('ajax_search.html', results=results,
            nexus=nexus, oldest_cache=oldest_cache, query=query)
Ejemplo n.º 4
0
    def ajax_status(self, node_id):
        if not re.match(r'^[a-z0-9-]+$', node_id): abort(403)

        self.nexus_init()

        data = fetch_data(node_id, self.config)
        result = assess_data(data, self.config)

        return render_template('ajax_status.html', h=result, nexus=nexus)
Ejemplo n.º 5
0
    def ajax_search(self, query, refresh=False):
        if not re.match(r"^[a-z0-9-\*\? ]+$", query):
            abort(403)

        if refresh:
            self.nexus_init()
            all_hubs = nexus.list_hub_ids()
        else:
            c = sqlite3.connect(self.db_path).cursor()
            c.execute("SELECT hub_id FROM Cache")
            all_hubs = [row[0] for row in c.fetchall()]

        hubs = set()
        for keyword in query.split(" "):
            keyword = "*" + keyword.strip("*") + "*"
            hubs.update(fnmatch.filter(all_hubs, keyword))

        if len(hubs) == 1:
            return """
            <script>window.location.href = "{}";</script>
            """.format(
                url_for("serve_status", hub_id=list(hubs)[0])
            )

        results = []
        oldest_cache = arrow.utcnow()
        for hub_id in hubs:
            if refresh:
                data = fetch_data(hub_id, self.config)
                res = assess_data(data, self.config)

                results.append((res.hub_id, res.hub_health, res.error or res.summary))

                continue

            c.execute("SELECT * FROM Cache WHERE hub_id=?", [hub_id])
            for hub_id, hub_health, summary, cache_time in c.fetchall():
                cache_time = arrow.get(cache_time, "YYYY-MM-DD HH:mm:ss")
                oldest_cache = min(cache_time, oldest_cache)

                hub_health = {"RED": nexus.RED, "YELLOW": nexus.YELLOW, "GREEN": nexus.GREEN, "FAIL": nexus.RED}[
                    hub_health
                ]

                results.append((hub_id, hub_health, summary))

        if refresh or not results:
            oldest_cache = None
        else:
            oldest_cache = oldest_cache.humanize()
        # else: oldest_cache = '{} ({})'.format(
        #     oldest_cache.humanize(),
        #     oldest_cache.format('YYYY-MM-DD HH:mm:ss ZZ')
        # )

        return render_template("ajax_search.html", results=results, nexus=nexus, oldest_cache=oldest_cache, query=query)
Ejemplo n.º 6
0
    def ajax_status(self, hub_id):
        if not re.match(r"^[a-z0-9-]+$", hub_id):
            abort(403)

        self.nexus_init()

        data = fetch_data(hub_id, self.config)
        result = assess_data(data, self.config)

        return render_template("ajax_status.html", h=result, nexus=nexus)
Ejemplo n.º 7
0
    def ajax_all(self):
        self.nexus_init()

        hubs_list = get_tunnel_connections(self.config["tunnel_info"])

        results = []
        for hub_id in hubs_list:
            app.logger.info(hub_id)

            data = fetch_data(hub_id, self.config)
            res = assess_data(data, self.config)

            if res.error:
                color = nexus.RED
            elif res.hub_health != nexus.GREEN:
                color = nexus.YELLOW
            else:
                color = nexus.GREEN

            results.append((res.hub_id, color, res.error or res.summary))

        return render_template("ajax_search.html", results=results, nexus=nexus)
Ejemplo n.º 8
0
    def ajax_all(self):
        self.nexus_init()

        nodes_list = get_bridge_connections(self.config['bridge_host'])

        results = []
        for node_id in nodes_list:
            app.logger.info(node_id)

            data = fetch_data(node_id, self.config)
            res = assess_data(data, self.config)

            if res.error:
                color = nexus.RED
            elif res.node_health != nexus.GREEN:
                color = nexus.YELLOW
            else:
                color = nexus.GREEN

            results.append((res.node_id, color, res.error or res.summary))

        return render_template('ajax_search.html', results=results, nexus=nexus)
Ejemplo n.º 9
0
    def ajax_all(self):
        self.nexus_init()

        hubs_list = get_tunnel_connections(self.config['tunnel_info'])

        results = []
        for hub_id in hubs_list:
            app.logger.info(hub_id)

            data = fetch_data(hub_id, self.config)
            res = assess_data(data, self.config)

            if res.error:
                color = nexus.RED
            elif res.hub_health != nexus.GREEN:
                color = nexus.YELLOW
            else:
                color = nexus.GREEN

            results.append((res.hub_id, color, res.error or res.summary))

        return render_template('ajax_search.html',
                               results=results,
                               nexus=nexus)
Ejemplo n.º 10
0
    def run(self):
        for _ in range(5):
            try:
                nexus.init(self.config)
            except paramiko.SSHException:
                continue
            break
        else:
            logging.error('Could not log in')
            exit(1)

        self.log.debug('Logged in.')

        all_nodes = nexus.list_node_ids()
        for node_id in all_nodes:
            data = fetch_data(node_id, self.config)
            res = assess_data(data, self.config)

            node_health = {
                nexus.RED: 'RED', nexus.YELLOW: 'YELLOW', nexus.GREEN: 'GREEN'
            }[res.node_health]

            if res.error: node_health = 'FAIL'

            c = self.db.cursor()
            c.execute('SELECT * FROM Cache WHERE node_id=?', [res.node_id])
            for _, old_health, old_summary, old_time in c.fetchall():
                if old_health == node_health: break

                a = arrow.get(old_time, 'YYYY-MM-DD HH:mm:ss')

                info = {
                    'node_id': res.node_id,
                    'node_health': node_health,
                    'summary': res.error or res.summary,
                    'time': arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ'),
                    'human_old_time': a.humanize(),
                    'old_time': a.format('YYYY-MM-DD HH:mm:ss ZZ'),
                    'old_health': old_health,
                    'old_summary': old_summary,
                    'config': data.config,
                }

                if node_health == 'FAIL':
                    self.log.info("{node_id} failed: {summary} "
                        "[was: {old_health} - {old_time}]".format(**info))

                    self._send_mail(info)
                    break

                # If the status went to GREEN or got worse (* -> R || G -> Y)
                if node_health == 'GREEN' or node_health == 'RED' or (
                    old_health == 'GREEN' and node_health == 'YELLOW'):
                    self.log.info("{node_id} is {node_health}: {summary} "
                        "[was: {old_health} - {old_time}]".format(**info))

                    self._send_mail(info)

            self.log.debug("{} is {}: {}".format(
                res.node_id,
                node_health,
                res.error or res.summary
            ))

            c.execute("""INSERT OR REPLACE INTO Cache
                (node_id, node_health, summary, time)
                VALUES (?, ?, ?, datetime('now'));""", (
                    res.node_id,
                    node_health,
                    res.error or res.summary
            ))
            self.db.commit()
Ejemplo n.º 11
0
    def run(self):
        for _ in range(5):
            try:
                nexus.init(self.config)
            except paramiko.SSHException:
                continue
            break
        else:
            logging.error('Could not log in')
            exit(1)

        self.log.debug('Logged in.')

        all_hubs = nexus.list_hub_ids()
        for hub_id in all_hubs:
            data = fetch_data(hub_id, self.config)
            res = assess_data(data, self.config)

            hub_health = {
                nexus.RED: 'RED',
                nexus.YELLOW: 'YELLOW',
                nexus.GREEN: 'GREEN'
            }[res.hub_health]

            if res.error: hub_health = 'FAIL'

            c = self.db.cursor()
            c.execute('SELECT * FROM Cache WHERE hub_id=?', [res.hub_id])
            for _, old_health, old_summary, old_time in c.fetchall():
                if old_health == hub_health: break

                a = arrow.get(old_time, 'YYYY-MM-DD HH:mm:ss')

                info = {
                    'hub_id': res.hub_id,
                    'hub_health': hub_health,
                    'summary': res.error or res.summary,
                    'time': arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ'),
                    'human_old_time': a.humanize(),
                    'old_time': a.format('YYYY-MM-DD HH:mm:ss ZZ'),
                    'old_health': old_health,
                    'old_summary': old_summary,
                    'config': data.config,
                }

                if hub_health == 'FAIL':
                    self.log.info(
                        "{hub_id} failed: {summary} "
                        "[was: {old_health} - {old_time}]".format(**info))

                    self._send_mail(info)
                    break

                # If the status went to GREEN or got worse (* -> R || G -> Y)
                if hub_health == 'GREEN' or hub_health == 'RED' or (
                        old_health == 'GREEN' and hub_health == 'YELLOW'):
                    self.log.info(
                        "{hub_id} is {hub_health}: {summary} "
                        "[was: {old_health} - {old_time}]".format(**info))

                    self._send_mail(info)

            self.log.debug("{} is {}: {}".format(res.hub_id, hub_health,
                                                 res.error or res.summary))

            c.execute(
                """INSERT OR REPLACE INTO Cache
                (hub_id, hub_health, summary, time)
                VALUES (?, ?, ?, datetime('now'));""",
                (res.hub_id, hub_health, res.error or res.summary))
            self.db.commit()
Ejemplo n.º 12
0
DIR = os.path.dirname(os.path.realpath(__file__))

with open(os.path.join(DIR, 'config.json')) as f:
    config = json.load(f)

for _ in range(5):
    try:
        init(config)
    except paramiko.SSHException:
        continue
    break
else:
    exit(1)

hubs_list = get_tunnel_connections(config['tunnel_info'])

results = []
for hub_id in hubs_list:
    data = fetch_data(hub_id, config)
    res = assess_data(data, config)

    if res.error:
        color = 'RED'
    elif res.hub_health != GREEN:
        color = 'YELLOW'
    else:
        color = 'GREEN'

    print('[{}] {}... {}'.format(color, res.hub_id, res.error or res.summary))