From a98144d77c822ba50c9cfc27c3463117f680bf04 Mon Sep 17 00:00:00 2001 From: Anael Beutot Date: Thu, 3 May 2012 17:33:31 +0200 Subject: [PATCH] Rewrite host with new tag and plugin system. --- ccnode/host/__init__.py | 26 ++++++++++++++++++-------- ccnode/host/tags.py | 6 ++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ccnode/host/__init__.py b/ccnode/host/__init__.py index f73d354..8a60046 100644 --- a/ccnode/host/__init__.py +++ b/ccnode/host/__init__.py @@ -1,9 +1,10 @@ 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. diff --git a/ccnode/host/tags.py b/ccnode/host/tags.py index 61c3a89..19e28f3 100644 --- a/ccnode/host/tags.py +++ b/ccnode/host/tags.py @@ -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 -- GitLab