Example #1
0
class Windows:
    """
    Klasa ta zawiera szereg metod informujących o obciążeniu systemu.
    """

    def __init__(self):
        """
        Konstruktor klasy Windows.
        """
        self.data = SystemInfo()

    def cpu_usage(self):
        """
        Zwraca informacje na temat obciążenia CPU.
        """
        cpu = {}

        cpu["system"] = str(WMI().Win32_Processor()[0].LoadPercentage)

        return {"CPU" : cpu}

    def disk_space(self):
        """
        Zwraca informacje na temat dysku twardego.
        """
        myWMI = WMI()

        diskList = []

        for disk in myWMI.Win32_LogicalDisk(DriveType=3):
            d = {}

            d["name"]  = str(disk.Caption)
            d["total"] = str(disk.Size)
            d["free"]  = str(disk.FreeSpace)
            d["used"]  = str(int(disk.Size) - int(disk.FreeSpace))

            diskList.append(d)

        return {"Hard drives" : diskList}

    def get_data(self):
        """
        Zwraca wszystkie informacje.
        """
        self.data.set_ram(self.ram_usage())
        self.data.set_cpu(self.cpu_usage())
        self.data.set_disk(self.disk_space())

        return self.data

    def metrics(self):
        """
        Zwraca metryki.
        """
        return {"hostname" : getenv("COMPUTERNAME"), "cpu" : "1", "ram" : "1", "hdd" : "1"}

    def ram_usage(self):
        """
        Zwraca informacje na temat całkowitej, użytej oraz wolnej ilości pamięci RAM.
        """
        myWMI = WMI()

        ram = {}

        ram["total"] = str(myWMI.Win32_ComputerSystem()[0].TotalPhysicalMemory)
        ram["free"]  = str(myWMI.Win32_OperatingSystem()[0].FreePhysicalMemory) * 1024
        ram["used"]  = str(int(myWMI.Win32_ComputerSystem()[0].TotalPhysicalMemory) - int(myWMI.Win32_OperatingSystem()[0].FreePhysicalMemory) * 1024)

        return {"RAM" : ram}
Example #2
0
 def __init__(self):
     """
     Konstruktor klasy Windows.
     """
     self.data = SystemInfo()
Example #3
0
    def __init__(self):
        """
        Konstruktor klasy Linux.
        """

        self.data = SystemInfo()
Example #4
0
class Linux:
    """
    Klasa ta zawiera szereg metod informujących o obciażeniu systemu.
    """

    def __init__(self):
        """
        Konstruktor klasy Linux.
        """

        self.data = SystemInfo()

    def ram_usage(self):
        """
        Zwraca informacje na temat całkowitej, użytej oraz wolnej ilości pamięci RAM.
        """

        output = getoutput("free").split()

        ram = {}

        ram["total"] = str(output[7])
        ram["used"] = str(output[8])
        ram["free"] = str(output[9])

        return {"RAM" : ram}

    def cpu_usage(self, n = 2, delay = 0.2):
        """
        Zwraca informacje na temat obciażenia CPU.\n

        n - liczba iteracji pomijanych zanim obciażenie CPU zostanie obliczone\n
        delay - opoźnienie pomiędzy iteracjami\n
        """

        if not isinstance(n, int) or not isinstance(delay, float):
            raise TypeError

        if n < 2:
            n = 2

        if delay < 0.0 :
            delay = 0.2

        output = getoutput("top -b -d " + str(delay) + " -n " + str(n))

        idx = -1
        for i in range(output.count("Cpu")) :
            idx = output.find("Cpu", idx + 1)

        output = output[idx:].split()

        cpu = {}
        cpu["user"] = output[1].split("%")[0]
        cpu["system"] = output[2].split("%")[0]

        return {"CPU" : cpu}

    def disk_space(self):
        """
        Zwraca informacje na temat dysku twardego.\n
        """

        output = getoutput("df -l").split("\n")

        disk = []
        for s in output[1:] :
            d = {}
            val = s.split()
            d["name"] = val[0]
            d["total"] = str(val[1])
            d["used"] = str(val[2])
            d["free"] = str(val[3])
            disk.append(d)

        return {"Hard drives" : disk}

    def metrics(self):
        """
        Zwraca metryki.\n
        """
        return {"hostname" : getoutput("hostname"), "cpu" : "1", "ram" : "1", "hdd" : "1"}

    def get_data(self):
        """
        Zwraca wszystkie informacje.\n
        """

        self.data.set_ram(self.ram_usage())
        self.data.set_cpu(self.cpu_usage())
        self.data.set_disk(self.disk_space())

        return self.data
Example #5
0
 def __init__(self):
     self.data = SystemInfo()