local_periodic_tasks.py 4.23 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 firewall.tasks.remote_tasks import check_queue as check_queue_fw
27
from vm.models import Node, InstanceTemplate
28
from firewall.models import Firewall
Kálmán Viktor committed
29
from storage.models import DataStore
30 31 32 33 34 35 36
from monitor.client import Client

logger = logging.getLogger(__name__)


@celery.task(ignore_result=True)
def measure_response_time():
37 38 39 40 41
    try:
        r = requests.get(settings.DJANGO_URL, verify=False,
                         timeout=0.5)
    except requests.exceptions.Timeout:
        return
42 43 44 45 46 47 48 49 50 51 52
    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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68


@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()))
69 70 71 72 73 74 75 76
        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
77 78 79 80 81
    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,
82 83
                                           "storage-" + s, is_queue_alive,
                                           time()))
Kálmán Viktor committed
84

85 86 87 88 89
    for fw in Firewall.objects.all():
        is_queue_alive = check_queue_fw(fw.name, "firewall", None)
        metrics.append(graphite_string(
            "firewall", fw.name, "firewall", is_queue_alive, time()))

Kálmán Viktor committed
90
    Client().send(metrics)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109


@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)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125


@celery.task(ignore_result=True)
def allocated_memory():
    graphite_string = lambda hostname, val, time: (
        "circle.%s.memory.allocated %d %s" % (
            hostname, val, time)
    )

    metrics = []
    for n in Node.objects.all():
        print n.allocated_ram
        metrics.append(graphite_string(
            n.host.hostname, n.allocated_ram, time()))

    Client().send(metrics)