+++ /dev/null
-#!/usr/bin/python3
-###############################################################################
-# #
-# Pakfire - The IPFire package management system #
-# Copyright (C) 2011 Pakfire development team #
-# #
-# This program is free software: you can redistribute it and/or modify #
-# it under the terms of the GNU General Public License as published by #
-# the Free Software Foundation, either version 3 of the License, or #
-# (at your option) any later version. #
-# #
-# This program is distributed in the hope that it will be useful, #
-# but WITHOUT ANY WARRANTY; without even the implied warranty of #
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
-# GNU General Public License for more details. #
-# #
-# You should have received a copy of the GNU General Public License #
-# along with this program. If not, see <http://www.gnu.org/licenses/>. #
-# #
-###############################################################################
-
-import multiprocessing
-import os
-import socket
-
-from . import _pakfire
-from .i18n import _
-
-class System(object):
- """
- Class that grants access to several information about
- the system this software is running on.
- """
- @property
- def hostname(self):
- hn = socket.gethostname()
-
- # If a host has got no domain part, we add one.
- if not "." in hn:
- hn = "%s.localdomain" % hn
-
- return hn
-
- @property
- def cpu_count(self):
- """
- Count the number of CPU cores.
- """
- return multiprocessing.cpu_count()
-
- def parse_cpuinfo(self):
- ret = {}
-
- with open("/proc/cpuinfo") as f:
- for line in f.readlines():
- try:
- # Split the lines by colons.
- a, b = line.split(":")
-
- # Strip whitespace.
- a = a.strip()
- b = b.strip()
-
- ret[a] = b
- except:
- pass
-
- return ret
-
- @property
- def cpu_model(self):
- cpuinfo = self.parse_cpuinfo()
-
- ret = cpuinfo.get("model name", None)
-
- # Some ARM platforms do not provide "model name", so we
- # try an other way.
- if ret is None:
- try:
- ret = "%(Hardware)s - %(Processor)s" % cpuinfo
- except KeyError:
- pass
-
- # Remove too many spaces.
- if ret:
- ret = " ".join(ret.split())
-
- return ret or _("Could not be determined")
-
- @property
- def cpu_bogomips(self):
- return _pakfire.performance_index()
-
- def get_loadavg(self):
- return os.getloadavg()
-
- @property
- def loadavg1(self):
- return self.get_loadavg()[0]
-
- @property
- def loadavg5(self):
- return self.get_loadavg()[1]
-
- @property
- def loadavg15(self):
- return self.get_loadavg()[2]
-
- def has_overload(self):
- """
- Checks, if the load average is not too high.
-
- On this is to be decided if a new job is taken.
- """
- # If there are more than 2 processes in the process queue per CPU
- # core we will assume that the system has heavy load and to not request
- # a new job.
- return self.loadavg5 >= self.cpu_count * 2
-
- def parse_meminfo(self):
- ret = {}
-
- with open("/proc/meminfo") as f:
- for line in f.readlines():
- try:
- a, b, c = line.split()
-
- a = a.strip()
- a = a.replace(":", "")
- b = int(b)
-
- ret[a] = b * 1024
- except:
- pass
-
- return ret
-
- @property
- def memory_total(self):
- meminfo = self.parse_meminfo()
-
- return meminfo.get("MemTotal", None)
-
- # For compatibility
- memory = memory_total
-
- @property
- def memory_free(self):
- meminfo = self.parse_meminfo()
-
- free = meminfo.get("MemFree", None)
- if free:
- buffers = meminfo.get("Buffers")
- cached = meminfo.get("Cached")
-
- return free + buffers + cached
-
- @property
- def swap_total(self):
- meminfo = self.parse_meminfo()
-
- return meminfo.get("SwapTotal", None)
-
- @property
- def swap_free(self):
- meminfo = self.parse_meminfo()
-
- return meminfo.get("SwapFree", None)
-
-
-# Create an instance of this class to only keep it once in memory.
-system = System()
-
-if __name__ == "__main__":
- print("Hostname", system.hostname)
- print("Arch", system.arch)
-
- print("CPU Model", system.cpu_model)
- print("CPU count", system.cpu_count)
- print("Memory", system.memory)