Commit a98144d7 authored by Anael Beutot's avatar Anael Beutot
Browse files

Rewrite host with new tag and plugin system.

parent 41356d0b
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
import logging
import os.path
from itertools import chain, imap
from subprocess import Popen, PIPE, STDOUT

from ccnode.node import DefaultHandler
from ccnode.tags import Tag, tag_inspector
from ccnode.plugins import Base as BasePlugin
from ccnode.host import tags


@@ -25,18 +26,27 @@ def disk_tag_value(disk_name):
    return size


class Handler(DefaultHandler):
class Handler(BasePlugin):
    """Handler for host role."""
    def __init__(self, *args, **kwargs):
        DefaultHandler.__init__(self, *args, **kwargs)
        BasePlugin.__init__(self, *args, **kwargs)

        for t in tag_inspector(tags):
            self.tags[t.name] = t
        # add plugin tags
        self.tag_db['__main__'].update(dict(
            (t.name, t) for t in tag_inspector(tags),
        ))

        # disk related tags
        for d in self.tags['disk'].value.split():
            t = Tag('disk%s_size' % d, disk_tag_value(d), 60)
            self.tags[t.name] = t
        self.tag_db['__main__'].update(dict((t.name, t) for t in imap(
            lambda d: Tag('disk%s_size' % d, disk_tag_value(d), 60),
            self.tag_db['__main__']['disk']._calculate_value().split(),
        )))

        # rpc handler
        self.rpc_handler.update(dict(
            execute_command=self.execute_command,
            node_shutdown=self.node_shutdown,
        ))

    def execute_command(self, command):
        """Execute an arbitrary shell command on the host.
+6 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ def h():
    """Hostname tag."""
    return getfqdn()
h.ttl = 3600 * 24  # one day
h.refresh = 5


# CPU related tags
@@ -42,6 +43,7 @@ def cpuuse():
    """CPU usage in percentage."""
    return u'%.1f' % psutil.cpu_percent()
cpuuse.ttl = 10
cpuuse.refresh = 2


# memory related tags
@@ -55,12 +57,14 @@ def memfree():
    """Available physical memory on system."""
    return unicode(psutil.avail_phymem())
memfree.ttl = 5
memfree.refresh = 10


def memused():
    """Used physical memory on system."""
    return unicode(psutil.used_phymem())
memused.ttl = 5
memused.refresh = 10


# disks related tags
@@ -126,6 +130,7 @@ def uptime():
    """Uptime of the system in seconds."""
    return open('/proc/uptime').read().split()[0].split(u'.')[0] or None
uptime.ttl = 5
uptime.refresh = 5


def load():
@@ -138,3 +143,4 @@ def load():
        pass
    return load_
load.ttl = 5
load.refresh = 5