示例#1
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        Monitor.poll(self)

        # create new, empty event
        #
        current = {
            # the UTC timestamp when measurement was taken
            u'timestamp': utcnow(),

            # the effective last period in secods
            u'last_period': self._last_period,
        }

        # FIXME: add ratio of ram usage

        with open("/proc/meminfo") as f:
            res = f.read()
            new = res.split()
            new_clean = [x.replace(":", "") for x in new if x != 'kB']
            for i in range(0, len(new_clean), 2):
                k = u'{}'.format(new_clean[i])
                current[k] = int(new_clean[i + 1])

        self._last_value = current

        return succeed(self._last_value)
示例#2
0
    def __init__(self, config=None):
        Monitor.__init__(self, config)

        self._cpu_last = None

        self._processors = {}
        self._sockets = []
        self._physid_to_id = {}
        self._id_to_physid = {}

        sockets = {}

        with open('/proc/cpuinfo', 'r') as fd:

            processor_id = None
            physical_socket_id = None
            physical_core_id = None

            for line in fd.readlines():

                line = line.strip()

                if line == "":
                    self._processors[processor_id] = (physical_socket_id,
                                                      physical_core_id)
                    if physical_socket_id not in sockets:
                        sockets[physical_socket_id] = []
                    sockets[physical_socket_id].append(physical_core_id)
                else:
                    key, value = line.split(':')
                    key = key.strip()
                    value = value.strip()

                    if key == "processor":
                        processor_id = int(value)

                    elif key == "physical id":
                        physical_socket_id = int(value)

                    elif key == "core id":
                        physical_core_id = int(value)

        i = 0
        for pi in sorted(sockets.keys()):
            cores = []
            j = 0
            for pj in sorted(sockets[i]):
                cores.append(pj)
                self._physid_to_id[(pi, pj)] = (i, j)
                self._id_to_physid[(i, j)] = (pi, pj)
                j += 1
            self._sockets.append((pi, cores))
            i += 1
示例#3
0
    def __init__(self, config=None):
        Monitor.__init__(self, config)

        # Map of process types.
        self._ptypes = self._config.get(u'process_types', {})

        # Process type to map otherwise unmapped processes to.
        self._ptype_unassigned = self._config.get(u'process_type_unassigned',
                                                  u'unassigned')

        # Filter processes by these types.
        filter_ptypes = self._config.get(u'filter_process_types', None)
        self._filter_ptypes = set(filter_ptypes) if filter_ptypes else None
示例#4
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        hdata = Monitor.poll(self)

        start = perf_counter_ns()

        devices = {}
        usage = {}
        counters = psutil.disk_io_counters(True)
        for dev in psutil.disk_partitions():
            if dev.device.startswith('/dev/loop'):
                continue
            key = dev.device.split('/')[-1]
            if key not in devices:
                if key in counters:
                    devices[key] = dict(dev._asdict(),
                                        **counters[key]._asdict())
                else:
                    devices[key] = dev._asdict()
                usage[key] = psutil.disk_usage(dev.mountpoint)._asdict()

        hdata['devices'] = devices
        hdata['usage'] = usage

        hdata[u'elapsed'] = perf_counter_ns() - start

        self._last_value = hdata

        return hdata
示例#5
0
文件: _disk.py 项目: yankos/crossbar
    def __init__(self, config=None):
        Monitor.__init__(self, config)

        self._storage = self._config.get(u'storage', [])

        # flat list of block devices
        #
        self._devices = []
        for subsystem in self._storage:
            for device, _ in subsystem['devices']:
                self._devices.append(device)

        # map indexed by device holding last raw (cumulative) values
        self._last = {}

        # map indexed by device holding last values (since previous sample)
        self._values = {}

        for device in self._devices:
            self._last[device] = None
            self._values[device] = None
示例#6
0
    def poll(self):
        hdata = Monitor.poll(self)

        start = perf_counter_ns()

        hdata['cpu'] = psutil.cpu_times_percent()._asdict()
        hdata['memory'] = psutil.virtual_memory()._asdict()
        hdata['loadavg'] = os.getloadavg()

        # uptime, as all durations, is in ns
        hdata['uptime'] = int(hdata['timestamp'] - psutil.boot_time() * 10**10)

        hdata[u'elapsed'] = perf_counter_ns() - start

        self._last_value = hdata

        return hdata
示例#7
0
    def poll(self, sensors=[]):
        """
        Measure current stats value and return new stats.
        """
        hdata = Monitor.poll(self)

        start = perf_counter_ns()

        hdata['io_counters'] = self._process.io_counters()._asdict()
        hdata['cpu_times'] = self._process.cpu_times()._asdict()
        a = round(((self._process.cpu_times().user + self._process.cpu_times().system) * 100) /
                  (time.time() - self._process.create_time()), 3)
        hdata['percent'] = a

        for sensor in sensors:
            hdata[sensor.ID] = sensor._elapsed

        hdata[u'elapsed'] = perf_counter_ns() - start

        self._last_value = hdata

        return hdata
示例#8
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        hdata = Monitor.poll(self)

        start = perf_counter_ns()

        counters = {}
        io_counters = psutil.net_io_counters(True)
        for dev in io_counters:
            if dev.startswith('virbr') or dev.startswith(
                    'lo') or dev.startswith('docker'):
                continue
            counters[dev] = io_counters[dev]._asdict()

        hdata['net_io_counters'] = counters
        hdata['net_if_addrs'] = psutil.net_if_addrs()

        hdata[u'elapsed'] = perf_counter_ns() - start

        self._last_value = hdata

        return hdata
示例#9
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        hdata = Monitor.poll(self)

        start = perf_counter_ns()

        battery = None
        bat = psutil.sensors_battery()
        if bat:
            battery = bat._asdict()

        fans = []
        for key, val in (psutil.sensors_fans() or {}).items():
            for item in val:
                item = item._asdict()
                item['device'] = key
                fans.append(item)

        temperatures = []
        for key, val in (psutil.sensors_temperatures() or {}).items():
            for item in val:
                item = item._asdict()
                item['device'] = key
                temperatures.append(item)

        hdata['battery'] = battery
        hdata['fans'] = fans
        hdata['temperatures'] = temperatures

        hdata[u'elapsed'] = perf_counter_ns() - start

        self._last_value = hdata

        return hdata
示例#10
0
 def __init__(self, config=None):
     Monitor.__init__(self, config)
     self._process = psutil.Process()
示例#11
0
 def poll(self):
     """
     Measure current stats value and return new stats.
     """
     Monitor.poll(self)
     return deferToThread(self._poll)
示例#12
0
文件: _disk.py 项目: yankos/crossbar
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        Monitor.poll(self)

        # create new, empty event
        #
        current = {
            # the UTC timestamp when measurement was taken
            u'timestamp': utcnow(),

            # the effective last period in secods
            u'last_period': self._last_period,

            # storage subsystem measurements
            u'subsystems': []
        }

        # normalize with effective period
        diff = self._last_period or 1.

        # get IO stats per device from procfs (/sys/block/<device>/stat)
        # see: https://www.kernel.org/doc/Documentation/block/stat.txt
        #
        for device in self._devices:
            with open('/sys/block/{}/stat'.format(device)) as fd:
                res = fd.read()

                new = [int(s.strip()) for s in res.split()]

                if not self._last[device]:
                    self._last[device] = new

                last = self._last[device]

                self._values[device] = {
                    u'read_ios': int((new[0] - last[0]) / diff),
                    u'read_merges': int((new[1] - last[1]) / diff),
                    u'read_bytes': int(512 * (new[2] - last[2]) / diff),
                    u'read_ticks': int((new[3] - last[3]) / diff),
                    u'write_ios': int((new[4] - last[4]) / diff),
                    u'write_merges': int((new[5] - last[5]) / diff),
                    u'write_bytes': int(512 * (new[6] - last[6]) / diff),
                    u'write_ticks': int((new[7] - last[7]) / diff),
                    u'in_flight': new[8],
                    u'io_ticks': int((new[9] - last[9]) / diff),
                    u'time_in_queue': int((new[10] - last[10]) / diff)
                }

                self._last[device] = new

        # transform raw measurements into target event structure
        #
        for subsys in self._storage:
            subsystem = {u'id': subsys[u'id'], u'devices': []}

            for device_id, device_label in subsys[u'devices']:
                values = self._values[device_id]
                device = {
                    u'id': device_id,
                    u'type': device_label,
                    u'read_ios': values['read_ios'],
                    u'read_bytes': values['read_bytes'],
                    u'read_ms': values['read_ticks'],
                    u'write_ios': values['write_ios'],
                    u'write_bytes': values['write_bytes'],
                    u'write_ms': values['write_ticks'],
                    u'in_flight': values['in_flight'],
                    u'active_ms': values['io_ticks'],
                    u'wait_ms': values['time_in_queue'],
                }
                subsystem[u'devices'].append(device)

            current[u'subsystems'].append(subsystem)

        self._last_value = current

        return self._last_value
示例#13
0
 def __init__(self, config=None):
     Monitor.__init__(self, config)
示例#14
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        Monitor.poll(self)

        # create new, empty event
        #
        current = {
            # the UTC timestamp when measurement was taken
            u'timestamp': utcnow(),

            # the effective last period in secods
            u'last_period': self._last_period,

            # readings per CPU socket
            u'sockets': []
        }

        # fill in per-socket/per-core structures
        #
        for i in range(len(self._sockets)):
            socket = {
                # sequentially numbered socket ID
                u'id': i,

                # physical socket ID
                u'physical_id': self._sockets[i][0],

                # CPU socket temperature
                u'temperature': None,

                # CPU cores on this socket
                u'cores': []
            }
            for j in range(len(self._sockets[i][1])):
                core = {
                    # sequentially numbered core ID
                    u'id': j,

                    # physical core ID on this socket
                    u'physical_id': self._sockets[i][1][j],

                    # CPU core load
                    u'user': None,
                    u'system': None,
                    u'nice': None,
                    u'idle': None,
                    u'iowait': None,
                    u'irq': None,
                    u'softirq': None,
                    u'steal': None,
                    u'total': None,

                    # CPU core frequency
                    u'frequency': None,

                    # CPU core temperature
                    u'temperature': None
                }
                socket[u'cores'].append(core)
            current[u'sockets'].append(socket)

        # get current CPU load (via psutil)
        #
        cpu_now = psutil.cpu_times(percpu=True)

        if not self._cpu_last:
            self._cpu_last = cpu_now
        else:
            for i in range(len(cpu_now)):

                socket_id, core_id = self._physid_to_id[self._processors[i]]

                core = current[u'sockets'][socket_id][u'cores'][core_id]

                digits = 8

                # CPU core load stats
                core[u'user'] = round(cpu_now[i].user - self._cpu_last[i].user,
                                      digits)
                core[u'system'] = round(
                    cpu_now[i].system - self._cpu_last[i].system, digits)
                core[u'nice'] = round(cpu_now[i].nice - self._cpu_last[i].nice,
                                      digits)
                core[u'idle'] = round(cpu_now[i].idle - self._cpu_last[i].idle,
                                      digits)
                core[u'iowait'] = round(
                    cpu_now[i].iowait - self._cpu_last[i].iowait, digits)
                core[u'irq'] = round(cpu_now[i].irq - self._cpu_last[i].irq,
                                     digits)
                core[u'softirq'] = round(
                    cpu_now[i].softirq - self._cpu_last[i].softirq, digits)
                core[u'steal'] = round(
                    cpu_now[i].steal - self._cpu_last[i].steal, digits)

                # total CPU core load (= user + nice + system)
                core[u'total'] = round(
                    (cpu_now[i].user + cpu_now[i].nice + cpu_now[i].system) -
                    (self._cpu_last[i].user + self._cpu_last[i].nice +
                     self._cpu_last[i].system), digits)

                # normalize with effective period
                diff = self._last_period or 1.
                for k in core:
                    if (k != 'id' and k != 'physical_id' and k != 'frequency'
                            and k != 'temperature'):
                        core[k] = round(core[k] / diff, digits)

            self._cpu_last = cpu_now

        # get current CPU frequency (from procfs)
        #
        with open('/proc/cpuinfo', 'r') as fd:

            physical_socket_id = None
            physical_core_id = None
            frequency = None

            for line in fd.readlines():

                line = line.strip()

                if line == "":
                    socket_id, core_id = self._physid_to_id[(
                        physical_socket_id, physical_core_id)]
                    core = current[u'sockets'][socket_id][u'cores'][core_id]
                    core[u'frequency'] = frequency
                else:
                    key, value = line.split(':')
                    key = key.strip()
                    value = value.strip()

                    if key == "physical id":
                        physical_socket_id = int(value)

                    elif key == "core id":
                        physical_core_id = int(value)

                    elif key == "cpu MHz":
                        frequency = float(value)

        # get current CPU temperature (via /usr/bin/sensors)
        #
        res = yield getProcessOutput('/usr/bin/sensors')

        physical_socket_id = None
        physical_core_id = None
        # socket_temperature = None
        core_temperature = -1

        for line in res.splitlines():

            line = line.strip()

            if line == "":
                pass
            else:
                if line.startswith("Physical"):
                    key, value = line.split(':')
                    match = self._PATH_SENSORS_PHYS_SOCKET_ID.match(key)
                    if match:
                        physical_socket_id = int(match.groups()[0])
                        value = value.strip()
                        # socket_temperature = float(value[1:5])

                elif line.startswith("Core"):
                    key, value = line.split(':')
                    match = self._PATH_SENSORS_PHYS_CORE_ID.match(key)
                    if match:
                        physical_core_id = int(match.groups()[0])
                        value = value.strip()
                        core_temperature = float(value[1:5])

                        socket_id, core_id = self._physid_to_id[(
                            physical_socket_id, physical_core_id)]

                        core = current[u'sockets'][socket_id][u'cores'][
                            core_id]
                        core[u'temperature'] = core_temperature

        self._last_value = current

        returnValue(self._last_value)