示例#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 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
示例#3
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
示例#4
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
示例#5
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
示例#6
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
示例#7
0
 def poll(self):
     """
     Measure current stats value and return new stats.
     """
     Monitor.poll(self)
     return deferToThread(self._poll)
示例#8
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
示例#9
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)