当前位置: 首页>>代码示例>>Python>>正文


Python plugins.BackgroundTask类代码示例

本文整理汇总了Python中cherrypy.process.plugins.BackgroundTask的典型用法代码示例。如果您正苦于以下问题:Python BackgroundTask类的具体用法?Python BackgroundTask怎么用?Python BackgroundTask使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了BackgroundTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: start_updater

    def start_updater(self):
        def predicate(x):
            try:
                return x.updater
            except (TypeError, AttributeError):
                return False

        for plugin in pluginmanager.get_instances():
            try:
                for name, method in inspect.getmembers(plugin, predicate):
                    worker = BackgroundTask(method.interval, method)
                    self.worker.add(worker)
                    worker.start()
            except (TypeError, AttributeError, StopIteration):
                pass
开发者ID:ibutra,项目名称:SpyDashServer,代码行数:15,代码来源:server.py

示例2: __init__

 def __init__(self, **kargs):
     self.conn = kargs['conn']
     self.objstore = kargs['objstore']
     self.caps = CapabilitiesModel(**kargs)
     self.guests_stats_thread = BackgroundTask(GUESTS_STATS_INTERVAL,
                                               self._update_guests_stats)
     self.guests_stats_thread.start()
开发者ID:gouzongmei,项目名称:kimchi,代码行数:7,代码来源:vms.py

示例3: RequestLogger

class RequestLogger(object):
    def __init__(self):
        log = os.path.join(paths.state_dir, REQUEST_LOG_FILE)
        h = logging.handlers.WatchedFileHandler(log, 'a')
        h.setFormatter(logging.Formatter('%(message)s'))
        self.handler = h
        self.logger = logging.getLogger(WOK_REQUEST_LOGGER)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(self.handler)

        # start request log's downloadable temporary files removal task
        interval = LOG_DOWNLOAD_TIMEOUT * SECONDS_PER_HOUR
        self.clean_task = BackgroundTask(interval, self.cleanLogFiles)
        self.clean_task.start()

    def cleanLogFiles(self):
        globexpr = "%s/*.txt" % get_log_download_path()
        remove_old_files(globexpr, LOG_DOWNLOAD_TIMEOUT)
开发者ID:kimchi-project,项目名称:wok,代码行数:18,代码来源:reqlogger.py

示例4: __init__

    def __init__(self, **kargs):
        self.host_stats = defaultdict(list)
        gbconfig = config.get('gingerbase', {})
        self.statshistory_on = gbconfig.get('statshistory_on', True)

        # create thread to collect statistcs and cache values only if
        # statshistory_on is enabled in gingerbase.conf
        if self.statshistory_on:
            self.host_stats_thread = BackgroundTask(HOST_STATS_INTERVAL,
                                                    self.update_host_stats)
            self.host_stats_thread.start()
开发者ID:open-power-host-os,项目名称:gingerbase,代码行数:11,代码来源:host.py

示例5: RequestLogger

class RequestLogger(object):
    def __init__(self):
        log = os.path.join(config.get("logging", "log_dir"), REQUEST_LOG_FILE)
        h = logging.handlers.RotatingFileHandler(log, 'a',
                                                 maxBytes=MAX_FILE_SIZE,
                                                 backupCount=NUM_BACKUP_FILES)
        h.setFormatter(logging.Formatter('%(message)s'))
        self.handler = h
        self.logger = logging.getLogger(WOK_REQUEST_LOGGER)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(self.handler)

        # start request log's downloadable temporary files removal task
        interval = LOG_DOWNLOAD_TIMEOUT * SECONDS_PER_HOUR
        self.clean_task = BackgroundTask(interval, self.cleanLogFiles)
        self.clean_task.start()

    def cleanLogFiles(self):
        globexpr = "%s/*.txt" % get_log_download_path()
        remove_old_files(globexpr, LOG_DOWNLOAD_TIMEOUT)
开发者ID:harche,项目名称:wok,代码行数:20,代码来源:reqlogger.py

示例6: __init__

    def __init__(self):
        log = os.path.join(config.get("logging", "log_dir"), REQUEST_LOG_FILE)
        h = logging.handlers.WatchedFileHandler(log, 'a')
        h.setFormatter(logging.Formatter('%(message)s'))
        self.handler = h
        self.logger = logging.getLogger(WOK_REQUEST_LOGGER)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(self.handler)

        # start request log's downloadable temporary files removal task
        interval = LOG_DOWNLOAD_TIMEOUT * SECONDS_PER_HOUR
        self.clean_task = BackgroundTask(interval, self.cleanLogFiles)
        self.clean_task.start()
开发者ID:fr34k8,项目名称:wok,代码行数:13,代码来源:reqlogger.py

示例7: __init__

 def __init__(self, libvirt_uri=None, objstore_loc=None):
     self.libvirt_uri = libvirt_uri or 'qemu:///system'
     self.conn = LibvirtConnection(self.libvirt_uri)
     self.objstore = ObjectStore(objstore_loc)
     self.graphics_ports = {}
     self.next_taskid = 1
     self.stats = {}
     self.qemu_stream = False
     self.qemu_stream_dns = False
     self.libvirt_stream_protocols = []
     # Subscribe function to set host capabilities to be run when cherrypy
     # server is up
     # It is needed because some features tests depends on the server
     cherrypy.engine.subscribe('start', self._set_capabilities)
     self.statsThread = BackgroundTask(STATS_INTERVAL, self._update_stats)
     self.statsThread.start()
     self.distros = self._get_distros()
     if 'qemu:///' in self.libvirt_uri:
         self._default_pool_check()
         self._default_network_check()
开发者ID:zhoumeina,项目名称:kimchi,代码行数:20,代码来源:model.py

示例8: start_background_tasks

def start_background_tasks(config):
    global database_cleanup_task, monitoring_aggregation_task
    if config["http_server"]["stale_session_cleanup_enabled"]:
        database_cleanup_task = BackgroundTask(
            config["http_server"]["stale_session_cleanup_interval"], clear_stale_sessions(config["database"])
        )
        database_cleanup_task.start()
    else:
        cherrypy.log(
            "Warning, stale_session_cleanup_enabled is set to false.  The database may grow.", severity=logging.WARNING
        )

    if config["http_server"]["monitoring_aggregation_enabled"]:
        monitoring_aggregation_task = BackgroundTask(
            config["http_server"]["monitoring_aggregation_interval"], monitoring_aggregation
        )
        monitoring_aggregation_task.start()
    else:
        cherrypy.log(
            "Warning, monitoring_aggregation_enabled is set to false."
            + "  There will be no useful monitoring data generated",
            severity=logging.WARNING,
        )
开发者ID:cedexis,项目名称:idfetch,代码行数:23,代码来源:app.py

示例9: __init__

 def __init__(self, **kargs):
     self.host_stats = defaultdict(list)
     self.host_stats_thread = BackgroundTask(HOST_STATS_INTERVAL,
                                             self._update_host_stats)
     self.host_stats_thread.start()
开发者ID:popbjc,项目名称:kimchi,代码行数:5,代码来源:host.py

示例10: HostStatsModel

class HostStatsModel(object):
    __metaclass__ = Singleton

    def __init__(self, **kargs):
        self.host_stats = defaultdict(list)
        self.host_stats_thread = BackgroundTask(HOST_STATS_INTERVAL,
                                                self._update_host_stats)
        self.host_stats_thread.start()

    def lookup(self, *name):
        return {'cpu_utilization': self.host_stats['cpu_utilization'][-1],
                'memory': self.host_stats['memory'][-1],
                'disk_read_rate': self.host_stats['disk_read_rate'][-1],
                'disk_write_rate': self.host_stats['disk_write_rate'][-1],
                'net_recv_rate': self.host_stats['net_recv_rate'][-1],
                'net_sent_rate': self.host_stats['net_sent_rate'][-1]}

    def _update_host_stats(self):
        preTimeStamp = self.host_stats['timestamp']
        timestamp = time.time()
        # FIXME when we upgrade psutil, we can get uptime by psutil.uptime
        # we get uptime by float(open("/proc/uptime").readline().split()[0])
        # and calculate the first io_rate after the OS started.
        with open("/proc/uptime") as time_f:
            seconds = (timestamp - preTimeStamp if preTimeStamp else
                       float(time_f.readline().split()[0]))

        self.host_stats['timestamp'] = timestamp
        self._get_host_disk_io_rate(seconds)
        self._get_host_network_io_rate(seconds)

        self._get_percentage_host_cpu_usage()
        self._get_host_memory_stats()

        # store only 60 stats (1 min)
        for key, value in self.host_stats.iteritems():
            if isinstance(value, list):
                if len(value) == 60:
                    self.host_stats[key] = value[10:]

    def _get_percentage_host_cpu_usage(self):
        # This is cpu usage producer. This producer will calculate the usage
        # at an interval of HOST_STATS_INTERVAL.
        # The psutil.cpu_percent works as non blocking.
        # psutil.cpu_percent maintains a cpu time sample.
        # It will update the cpu time sample when it is called.
        # So only this producer can call psutil.cpu_percent in gingerbase.
        self.host_stats['cpu_utilization'].append(psutil.cpu_percent(None))

    def _get_host_memory_stats(self):
        virt_mem = psutil.virtual_memory()
        # available:
        #  the actual amount of available memory that can be given
        #  instantly to processes that request more memory in bytes; this
        #  is calculated by summing different memory values depending on
        #  the platform (e.g. free + buffers + cached on Linux)
        memory_stats = {'total': virt_mem.total,
                        'free': virt_mem.free,
                        'cached': virt_mem.cached,
                        'buffers': virt_mem.buffers,
                        'avail': virt_mem.available}
        self.host_stats['memory'].append(memory_stats)

    def _get_host_disk_io_rate(self, seconds):
        disk_read_bytes = self.host_stats['disk_read_bytes']
        disk_write_bytes = self.host_stats['disk_write_bytes']
        prev_read_bytes = disk_read_bytes[-1] if disk_read_bytes else 0
        prev_write_bytes = disk_write_bytes[-1] if disk_write_bytes else 0

        disk_io = psutil.disk_io_counters(False)
        read_bytes = disk_io.read_bytes
        write_bytes = disk_io.write_bytes

        rd_rate = int(float(read_bytes - prev_read_bytes) / seconds + 0.5)
        wr_rate = int(float(write_bytes - prev_write_bytes) / seconds + 0.5)

        self.host_stats['disk_read_rate'].append(rd_rate)
        self.host_stats['disk_write_rate'].append(wr_rate)
        self.host_stats['disk_read_bytes'].append(read_bytes)
        self.host_stats['disk_write_bytes'].append(write_bytes)

    def _get_host_network_io_rate(self, seconds):
        net_recv_bytes = self.host_stats['net_recv_bytes']
        net_sent_bytes = self.host_stats['net_sent_bytes']
        prev_recv_bytes = net_recv_bytes[-1] if net_recv_bytes else 0
        prev_sent_bytes = net_sent_bytes[-1] if net_sent_bytes else 0

        net_ios = None
        if hasattr(psutil, 'net_io_counters'):
            net_ios = psutil.net_io_counters(True)
        elif hasattr(psutil, 'network_io_counters'):
            net_ios = psutil.network_io_counters(True)

        recv_bytes = 0
        sent_bytes = 0
        for key in set(self.nics() +
                       self.wlans()) & set(net_ios.iterkeys()):
            recv_bytes = recv_bytes + net_ios[key].bytes_recv
            sent_bytes = sent_bytes + net_ios[key].bytes_sent

#.........这里部分代码省略.........
开发者ID:popbjc,项目名称:kimchi,代码行数:101,代码来源:host.py

示例11: VMsModel

class VMsModel(object):
    def __init__(self, **kargs):
        self.conn = kargs['conn']
        self.objstore = kargs['objstore']
        self.caps = CapabilitiesModel(**kargs)
        self.guests_stats_thread = BackgroundTask(GUESTS_STATS_INTERVAL,
                                                  self._update_guests_stats)
        self.guests_stats_thread.start()

    def _update_guests_stats(self):
        vm_list = self.get_list()

        for name in vm_list:
            try:
                dom = VMModel.get_vm(name, self.conn)

                vm_uuid = dom.UUIDString()
                info = dom.info()
                state = DOM_STATE_MAP[info[0]]

                if state != 'running':
                    stats[vm_uuid] = {}
                    continue

                if stats.get(vm_uuid, None) is None:
                    stats[vm_uuid] = {}

                timestamp = time.time()
                prevStats = stats.get(vm_uuid, {})
                seconds = timestamp - prevStats.get('timestamp', 0)
                stats[vm_uuid].update({'timestamp': timestamp})

                self._get_percentage_cpu_usage(vm_uuid, info, seconds)
                self._get_network_io_rate(vm_uuid, dom, seconds)
                self._get_disk_io_rate(vm_uuid, dom, seconds)
            except Exception as e:
                # VM might be deleted just after we get the list.
                # This is OK, just skip.
                kimchi_log.debug('Error processing VM stats: %s', e.message)
                continue

    def _get_percentage_cpu_usage(self, vm_uuid, info, seconds):
        prevCpuTime = stats[vm_uuid].get('cputime', 0)

        cpus = info[3]
        cpuTime = info[4] - prevCpuTime

        base = (((cpuTime) * 100.0) / (seconds * 1000.0 * 1000.0 * 1000.0))
        percentage = max(0.0, min(100.0, base / cpus))

        stats[vm_uuid].update({'cputime': info[4], 'cpu': percentage})

    def _get_network_io_rate(self, vm_uuid, dom, seconds):
        prevNetRxKB = stats[vm_uuid].get('netRxKB', 0)
        prevNetTxKB = stats[vm_uuid].get('netTxKB', 0)
        currentMaxNetRate = stats[vm_uuid].get('max_net_io', 100)

        rx_bytes = 0
        tx_bytes = 0

        tree = ElementTree.fromstring(dom.XMLDesc(0))
        for target in tree.findall('devices/interface/target'):
            dev = target.get('dev')
            io = dom.interfaceStats(dev)
            rx_bytes += io[0]
            tx_bytes += io[4]

        netRxKB = float(rx_bytes) / 1000
        netTxKB = float(tx_bytes) / 1000

        rx_stats = (netRxKB - prevNetRxKB) / seconds
        tx_stats = (netTxKB - prevNetTxKB) / seconds

        rate = rx_stats + tx_stats
        max_net_io = round(max(currentMaxNetRate, int(rate)), 1)

        stats[vm_uuid].update({'net_io': rate, 'max_net_io': max_net_io,
                               'netRxKB': netRxKB, 'netTxKB': netTxKB})

    def _get_disk_io_rate(self, vm_uuid, dom, seconds):
        prevDiskRdKB = stats[vm_uuid].get('diskRdKB', 0)
        prevDiskWrKB = stats[vm_uuid].get('diskWrKB', 0)
        currentMaxDiskRate = stats[vm_uuid].get('max_disk_io', 100)

        rd_bytes = 0
        wr_bytes = 0

        tree = ElementTree.fromstring(dom.XMLDesc(0))
        for target in tree.findall("devices/disk/target"):
            dev = target.get("dev")
            io = dom.blockStats(dev)
            rd_bytes += io[1]
            wr_bytes += io[3]

        diskRdKB = float(rd_bytes) / 1024
        diskWrKB = float(wr_bytes) / 1024

        rd_stats = (diskRdKB - prevDiskRdKB) / seconds
        wr_stats = (diskWrKB - prevDiskWrKB) / seconds

#.........这里部分代码省略.........
开发者ID:gouzongmei,项目名称:kimchi,代码行数:101,代码来源:vms.py

示例12: VMsModel

class VMsModel(object):
    def __init__(self, **kargs):
        self.conn = kargs['conn']
        self.objstore = kargs['objstore']
        self.caps = CapabilitiesModel()
        self.guests_stats_thread = BackgroundTask(GUESTS_STATS_INTERVAL,
                                                  self._update_guests_stats)
        self.guests_stats_thread.start()

    def _update_guests_stats(self):
        vm_list = self.get_list()

        for name in vm_list:
            dom = VMModel.get_vm(name, self.conn)
            vm_uuid = dom.UUIDString()
            info = dom.info()
            state = DOM_STATE_MAP[info[0]]

            if state != 'running':
                stats[vm_uuid] = {}
                continue

            if stats.get(vm_uuid, None) is None:
                stats[vm_uuid] = {}

            timestamp = time.time()
            prevStats = stats.get(vm_uuid, {})
            seconds = timestamp - prevStats.get('timestamp', 0)
            stats[vm_uuid].update({'timestamp': timestamp})

            self._get_percentage_cpu_usage(vm_uuid, info, seconds)
            self._get_network_io_rate(vm_uuid, dom, seconds)
            self._get_disk_io_rate(vm_uuid, dom, seconds)

    def _get_percentage_cpu_usage(self, vm_uuid, info, seconds):
        prevCpuTime = stats[vm_uuid].get('cputime', 0)

        cpus = info[3]
        cpuTime = info[4] - prevCpuTime

        base = (((cpuTime) * 100.0) / (seconds * 1000.0 * 1000.0 * 1000.0))
        percentage = max(0.0, min(100.0, base / cpus))

        stats[vm_uuid].update({'cputime': info[4], 'cpu': percentage})

    def _get_network_io_rate(self, vm_uuid, dom, seconds):
        prevNetRxKB = stats[vm_uuid].get('netRxKB', 0)
        prevNetTxKB = stats[vm_uuid].get('netTxKB', 0)
        currentMaxNetRate = stats[vm_uuid].get('max_net_io', 100)

        rx_bytes = 0
        tx_bytes = 0

        tree = ElementTree.fromstring(dom.XMLDesc(0))
        for target in tree.findall('devices/interface/target'):
            dev = target.get('dev')
            io = dom.interfaceStats(dev)
            rx_bytes += io[0]
            tx_bytes += io[4]

        netRxKB = float(rx_bytes) / 1000
        netTxKB = float(tx_bytes) / 1000

        rx_stats = (netRxKB - prevNetRxKB) / seconds
        tx_stats = (netTxKB - prevNetTxKB) / seconds

        rate = rx_stats + tx_stats
        max_net_io = round(max(currentMaxNetRate, int(rate)), 1)

        stats[vm_uuid].update({'net_io': rate, 'max_net_io': max_net_io,
                               'netRxKB': netRxKB, 'netTxKB': netTxKB})

    def _get_disk_io_rate(self, vm_uuid, dom, seconds):
        prevDiskRdKB = stats[vm_uuid].get('diskRdKB', 0)
        prevDiskWrKB = stats[vm_uuid].get('diskWrKB', 0)
        currentMaxDiskRate = stats[vm_uuid].get('max_disk_io', 100)

        rd_bytes = 0
        wr_bytes = 0

        tree = ElementTree.fromstring(dom.XMLDesc(0))
        for target in tree.findall("devices/disk/target"):
            dev = target.get("dev")
            io = dom.blockStats(dev)
            rd_bytes += io[1]
            wr_bytes += io[3]

        diskRdKB = float(rd_bytes) / 1024
        diskWrKB = float(wr_bytes) / 1024

        rd_stats = (diskRdKB - prevDiskRdKB) / seconds
        wr_stats = (diskWrKB - prevDiskWrKB) / seconds

        rate = rd_stats + wr_stats
        max_disk_io = round(max(currentMaxDiskRate, int(rate)), 1)

        stats[vm_uuid].update({'disk_io': rate,
                               'max_disk_io': max_disk_io,
                               'diskRdKB': diskRdKB,
                               'diskWrKB': diskWrKB})
#.........这里部分代码省略.........
开发者ID:jayzcode,项目名称:kimchi,代码行数:101,代码来源:vms.py

示例13: HostStatsModel

class HostStatsModel(object):
    __metaclass__ = Singleton

    def __init__(self, **kargs):
        self.host_stats = defaultdict(int)
        self.host_stats_thread = BackgroundTask(HOST_STATS_INTERVAL,
                                                self._update_host_stats)
        self.host_stats_thread.start()

    def lookup(self, *name):
        return {'cpu_utilization': self.host_stats['cpu_utilization'],
                'memory': self.host_stats.get('memory'),
                'disk_read_rate': self.host_stats['disk_read_rate'],
                'disk_write_rate': self.host_stats['disk_write_rate'],
                'net_recv_rate': self.host_stats['net_recv_rate'],
                'net_sent_rate': self.host_stats['net_sent_rate']}

    def _update_host_stats(self):
        preTimeStamp = self.host_stats['timestamp']
        timestamp = time.time()
        # FIXME when we upgrade psutil, we can get uptime by psutil.uptime
        # we get uptime by float(open("/proc/uptime").readline().split()[0])
        # and calculate the first io_rate after the OS started.
        seconds = (timestamp - preTimeStamp if preTimeStamp else
                   float(open("/proc/uptime").readline().split()[0]))

        self.host_stats['timestamp'] = timestamp
        self._get_host_disk_io_rate(seconds)
        self._get_host_network_io_rate(seconds)

        self._get_percentage_host_cpu_usage()
        self._get_host_memory_stats()

    def _get_percentage_host_cpu_usage(self):
        # This is cpu usage producer. This producer will calculate the usage
        # at an interval of HOST_STATS_INTERVAL.
        # The psutil.cpu_percent works as non blocking.
        # psutil.cpu_percent maintains a cpu time sample.
        # It will update the cpu time sample when it is called.
        # So only this producer can call psutil.cpu_percent in kimchi.
        self.host_stats['cpu_utilization'] = psutil.cpu_percent(None)

    def _get_host_memory_stats(self):
        virt_mem = psutil.virtual_memory()
        # available:
        #  the actual amount of available memory that can be given
        #  instantly to processes that request more memory in bytes; this
        #  is calculated by summing different memory values depending on
        #  the platform (e.g. free + buffers + cached on Linux)
        memory_stats = {'total': virt_mem.total,
                        'free': virt_mem.free,
                        'cached': virt_mem.cached,
                        'buffers': virt_mem.buffers,
                        'avail': virt_mem.available}
        self.host_stats['memory'] = memory_stats

    def _get_host_disk_io_rate(self, seconds):
        prev_read_bytes = self.host_stats['disk_read_bytes']
        prev_write_bytes = self.host_stats['disk_write_bytes']

        disk_io = psutil.disk_io_counters(False)
        read_bytes = disk_io.read_bytes
        write_bytes = disk_io.write_bytes

        rd_rate = int(float(read_bytes - prev_read_bytes) / seconds + 0.5)
        wr_rate = int(float(write_bytes - prev_write_bytes) / seconds + 0.5)

        self.host_stats.update({'disk_read_rate': rd_rate,
                                'disk_write_rate': wr_rate,
                                'disk_read_bytes': read_bytes,
                                'disk_write_bytes': write_bytes})

    def _get_host_network_io_rate(self, seconds):
        prev_recv_bytes = self.host_stats['net_recv_bytes']
        prev_sent_bytes = self.host_stats['net_sent_bytes']

        net_ios = psutil.network_io_counters(True)
        recv_bytes = 0
        sent_bytes = 0
        for key in set(netinfo.nics() +
                       netinfo.wlans()) & set(net_ios.iterkeys()):
            recv_bytes = recv_bytes + net_ios[key].bytes_recv
            sent_bytes = sent_bytes + net_ios[key].bytes_sent

        rx_rate = int(float(recv_bytes - prev_recv_bytes) / seconds + 0.5)
        tx_rate = int(float(sent_bytes - prev_sent_bytes) / seconds + 0.5)

        self.host_stats.update({'net_recv_rate': rx_rate,
                                'net_sent_rate': tx_rate,
                                'net_recv_bytes': recv_bytes,
                                'net_sent_bytes': sent_bytes})
开发者ID:amoalsale,项目名称:kimchi,代码行数:91,代码来源:host.py

示例14: Model

class Model(object):
    dom_state_map = {0: 'nostate',
                     1: 'running',
                     2: 'blocked',
                     3: 'paused',
                     4: 'shutdown',
                     5: 'shutoff',
                     6: 'crashed'}

    pool_state_map = {0: 'inactive',
                      1: 'initializing',
                      2: 'active',
                      3: 'degraded',
                      4: 'inaccessible'}

    volume_type_map = {0: 'file',
                       1: 'block',
                       2: 'directory',
                       3: 'network'}

    def __init__(self, libvirt_uri=None, objstore_loc=None):
        self.libvirt_uri = libvirt_uri or 'qemu:///system'
        self.conn = LibvirtConnection(self.libvirt_uri)
        self.objstore = ObjectStore(objstore_loc)
        self.graphics_ports = {}
        self.next_taskid = 1
        self.stats = {}
        self.qemu_stream = False
        self.qemu_stream_dns = False
        self.libvirt_stream_protocols = []
        # Subscribe function to set host capabilities to be run when cherrypy
        # server is up
        # It is needed because some features tests depends on the server
        cherrypy.engine.subscribe('start', self._set_capabilities)
        self.statsThread = BackgroundTask(STATS_INTERVAL, self._update_stats)
        self.statsThread.start()
        self.distros = self._get_distros()
        if 'qemu:///' in self.libvirt_uri:
            self._default_pool_check()
            self._default_network_check()

    def _default_network_check(self):
        conn = self.conn.get()
        xml = """
             <network>
              <name>default</name>
              <forward mode='nat'/>
              <bridge name='virbr0' stp='on' delay='0' />
              <ip address='192.168.122.1' netmask='255.255.255.0'>
                <dhcp>
                  <range start='192.168.122.2' end='192.168.122.254' />
                </dhcp>
              </ip>
            </network>
        """
        try:
            net = conn.networkLookupByName("default")
        except libvirt.libvirtError:
            try:
                net = conn.networkDefineXML(xml)
            except libvirt.libvirtError, e:
                cherrypy.log.error(
                    "Fatal: Cannot create default network because of %s, exit kimchid" % e.message,
                    severity=logging.ERROR)
                sys.exit(1)

        if net.isActive() == 0:
            try:
                net.create()
            except libvirt.libvirtError, e:
                cherrypy.log.error(
                    "Fatal: Cannot activate default network because of %s, exit kimchid" % e.message,
                    severity=logging.ERROR)
                sys.exit(1)
开发者ID:zhoumeina,项目名称:kimchi,代码行数:74,代码来源:model.py

示例15: HostStatsModel

class HostStatsModel(object):
    __metaclass__ = Singleton

    def __init__(self, **kargs):
        self.host_stats = defaultdict(list)
        self.host_stats_thread = BackgroundTask(HOST_STATS_INTERVAL, self._update_host_stats)
        self.host_stats_thread.start()

    def lookup(self, *name):
        return {
            "cpu_utilization": self.host_stats["cpu_utilization"][-1],
            "memory": self.host_stats["memory"][-1],
            "disk_read_rate": self.host_stats["disk_read_rate"][-1],
            "disk_write_rate": self.host_stats["disk_write_rate"][-1],
            "net_recv_rate": self.host_stats["net_recv_rate"][-1],
            "net_sent_rate": self.host_stats["net_sent_rate"][-1],
        }

    def _update_host_stats(self):
        preTimeStamp = self.host_stats["timestamp"]
        timestamp = time.time()
        # FIXME when we upgrade psutil, we can get uptime by psutil.uptime
        # we get uptime by float(open("/proc/uptime").readline().split()[0])
        # and calculate the first io_rate after the OS started.
        seconds = timestamp - preTimeStamp if preTimeStamp else float(open("/proc/uptime").readline().split()[0])

        self.host_stats["timestamp"] = timestamp
        self._get_host_disk_io_rate(seconds)
        self._get_host_network_io_rate(seconds)

        self._get_percentage_host_cpu_usage()
        self._get_host_memory_stats()

        # store only 60 stats (1 min)
        for key, value in self.host_stats.iteritems():
            if isinstance(value, list):
                if len(value) == 60:
                    self.host_stats[key] = value[10:]

    def _get_percentage_host_cpu_usage(self):
        # This is cpu usage producer. This producer will calculate the usage
        # at an interval of HOST_STATS_INTERVAL.
        # The psutil.cpu_percent works as non blocking.
        # psutil.cpu_percent maintains a cpu time sample.
        # It will update the cpu time sample when it is called.
        # So only this producer can call psutil.cpu_percent in kimchi.
        self.host_stats["cpu_utilization"].append(psutil.cpu_percent(None))

    def _get_host_memory_stats(self):
        virt_mem = psutil.virtual_memory()
        # available:
        #  the actual amount of available memory that can be given
        #  instantly to processes that request more memory in bytes; this
        #  is calculated by summing different memory values depending on
        #  the platform (e.g. free + buffers + cached on Linux)
        memory_stats = {
            "total": virt_mem.total,
            "free": virt_mem.free,
            "cached": virt_mem.cached,
            "buffers": virt_mem.buffers,
            "avail": virt_mem.available,
        }
        self.host_stats["memory"].append(memory_stats)

    def _get_host_disk_io_rate(self, seconds):
        disk_read_bytes = self.host_stats["disk_read_bytes"]
        disk_write_bytes = self.host_stats["disk_write_bytes"]
        prev_read_bytes = disk_read_bytes[-1] if disk_read_bytes else 0
        prev_write_bytes = disk_write_bytes[-1] if disk_write_bytes else 0

        disk_io = psutil.disk_io_counters(False)
        read_bytes = disk_io.read_bytes
        write_bytes = disk_io.write_bytes

        rd_rate = int(float(read_bytes - prev_read_bytes) / seconds + 0.5)
        wr_rate = int(float(write_bytes - prev_write_bytes) / seconds + 0.5)

        self.host_stats["disk_read_rate"].append(rd_rate)
        self.host_stats["disk_write_rate"].append(wr_rate)
        self.host_stats["disk_read_bytes"].append(read_bytes)
        self.host_stats["disk_write_bytes"].append(write_bytes)

    def _get_host_network_io_rate(self, seconds):
        net_recv_bytes = self.host_stats["net_recv_bytes"]
        net_sent_bytes = self.host_stats["net_sent_bytes"]
        prev_recv_bytes = net_recv_bytes[-1] if net_recv_bytes else 0
        prev_sent_bytes = net_sent_bytes[-1] if net_sent_bytes else 0

        net_ios = psutil.network_io_counters(True)
        recv_bytes = 0
        sent_bytes = 0
        for key in set(netinfo.nics() + netinfo.wlans()) & set(net_ios.iterkeys()):
            recv_bytes = recv_bytes + net_ios[key].bytes_recv
            sent_bytes = sent_bytes + net_ios[key].bytes_sent

        rx_rate = int(float(recv_bytes - prev_recv_bytes) / seconds + 0.5)
        tx_rate = int(float(sent_bytes - prev_sent_bytes) / seconds + 0.5)

        self.host_stats["net_recv_rate"].append(rx_rate)
        self.host_stats["net_sent_rate"].append(tx_rate)
#.........这里部分代码省略.........
开发者ID:k0da,项目名称:kimchi,代码行数:101,代码来源:host.py


注:本文中的cherrypy.process.plugins.BackgroundTask类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。