summaryrefslogtreecommitdiff
path: root/sublab_project/sublab_monitor/tasks.py
blob: 33dcad6292f35f7aed9d7fc28546669606162ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Celery tasks.
"""
from datetime import timedelta
from celery.task import PeriodicTask
import subprocess

import sublab_monitor

class NetworkStatus(PeriodicTask):
    """Periodic task for getting sublab network status
    """
    hosts = {
            'taifun': '172.22.83.5',
            'trieste': '172.22.80.4',
            'nautilus': '172.22.80.7',
    }
    run_every = timedelta(minutes=4)
    ignore_result = True

    def __init__(self, *args, **kwargs):
        PeriodicTask.__init__(self, *args, **kwargs)


    @staticmethod
    def host_alive(host):
        rc = subprocess.call(['ping', '-c', '2', '-W', '1', host])

        if rc == 0:
            return True
        else:
            return False

    def run(self, **kwargs):
        """Ping all the hosts.
        """
        self.logger = self.get_logger(**kwargs)

        results = {}
        for host, target in self.hosts.items():
            results[host] = self.host_alive(target)

        storage = sublab_monitor.Storage('network_status')
        for host, status in results.items():
            storage.set(host, status)

        return repr(results)