local_periodic_tasks.py 3.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

import logging
import requests
from time import time

from django.conf import settings
from manager.mancelery import celery

Kálmán Viktor committed
25
from vm.tasks.vm_tasks import check_queue
26
from vm.models import Node, InstanceTemplate
Kálmán Viktor committed
27
from storage.models import DataStore
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
from monitor.client import Client

logger = logging.getLogger(__name__)


@celery.task(ignore_result=True)
def measure_response_time():
    r = requests.get(settings.DJANGO_URL, verify=False)
    total_miliseconds = (
        r.elapsed.seconds * 10**6 +
        r.elapsed.microseconds) / 1000

    Client().send([
        "%(name)s %(val)d %(time)s" % {
            'name': "portal.response_time",
            'val': total_miliseconds,
            'time': time(),
        }
    ])
Kálmán Viktor committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


@celery.task(ignore_result=True)
def check_celery_queues():
    graphite_string = lambda component, hostname, celery, is_alive, time: (
        "%s.%s.celery-queues.%s %d %s" % (
            component, hostname, celery, 1 if is_alive else 0, time)
    )

    metrics = []
    for n in Node.objects.all():  # disabled, offline nodes?
        for s in ["fast", "slow"]:
            is_queue_alive = check_queue(n.host.hostname, "vm", s)

            metrics.append(graphite_string("circle", n.host.hostname,
                                           "vm-" + s, is_queue_alive, time()))
63 64 65 66 67 68 69 70
        is_net_queue_alive = check_queue(n.host.hostname, "net", "fast")
        metrics.append(graphite_string("circle", n.host.hostname,
                                       "net-fast", is_net_queue_alive, time()))

        is_agent_queue_alive = check_queue(n.host.hostname, "agent")
        metrics.append(graphite_string("circle", n.host.hostname, "agent",
                                       is_agent_queue_alive, time()))

Kálmán Viktor committed
71 72 73 74 75
    for ds in DataStore.objects.all():
        for s in ["fast", "slow"]:
            is_queue_alive = check_queue(ds.hostname, "vm", s)

            metrics.append(graphite_string("storage", ds.hostname,
76 77
                                           "storage-" + s, is_queue_alive,
                                           time()))
Kálmán Viktor committed
78 79

    Client().send(metrics)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98


@celery.task(ignore_result=True)
def instance_per_template():
    graphite_string = lambda pk, state, val, time: (
        "template.%d.instances.%s %d %s" % (
            pk, state, val, time)
    )

    metrics = []
    for t in InstanceTemplate.objects.all():
        base = t.instance_set.filter(destroyed_at=None)
        running = base.filter(status="RUNNING").count()
        not_running = base.exclude(status="RUNNING").count()
        metrics.append(graphite_string(t.pk, "running", running, time()))
        metrics.append(graphite_string(t.pk, "not_running", not_running,
                                       time()))

    Client().send(metrics)