~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/kylin-assistant-daemon/src/appcollections/monitorball/monitor_ball.py

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- coding: utf-8 -*-
 
3
### BEGIN LICENSE
 
4
 
 
5
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
 
6
# This program is free software: you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License version 3, as published
 
8
# by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
13
# PURPOSE.  See the GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
### END LICENSE
 
18
 
 
19
import psutil
 
20
import time
 
21
import subprocess
 
22
 
 
23
class MonitorBall:
 
24
 
 
25
    # clean up memory
 
26
    def cleanup_memory(self):
 
27
        '''force changed to disk, update the super block'''
 
28
        pone = subprocess.Popen(["sync"], shell=True)
 
29
        pone.wait()
 
30
 
 
31
        '''drop pagecache, dentries and inodes to free the memory'''
 
32
        ptwo = subprocess.Popen(["echo 3 > /proc/sys/vm/drop_caches"], shell=True)
 
33
        ptwo.wait()
 
34
 
 
35
    # get cpu percent
 
36
    def get_cpu_percent(self, percpu = False):
 
37
        return psutil.cpu_percent(0.1, percpu)
 
38
 
 
39
    # get total memory + cache
 
40
    def get_total_cmemory(self, symbol = "m"):
 
41
        mem = psutil.phymem_usage()
 
42
        value = mem.total
 
43
        return self.bytes2human(value, symbol)
 
44
 
 
45
    # get used memory + cache
 
46
    def get_used_cmemory(self, symbol = "m"):
 
47
        mem = psutil.phymem_usage()
 
48
        value = mem.used
 
49
        return self.bytes2human(value, symbol)
 
50
 
 
51
    # get free memory + cache
 
52
    def get_free_cmemory(self, symbol = "m"):
 
53
        mem = psutil.phymem_usage()
 
54
        value = mem.free
 
55
        return self.bytes2human(value, symbol)
 
56
        
 
57
    # get total memory
 
58
    def get_total_memory(self, symbol = "m"):
 
59
        baseData = psutil.virtual_memory()
 
60
        value = baseData.total
 
61
        return self.bytes2human(value, symbol)
 
62
 
 
63
    # get used memory
 
64
    def get_used_memory(self, symbol = "m"):
 
65
        baseData = psutil.virtual_memory()
 
66
        value = baseData.total - baseData.available
 
67
        return self.bytes2human(value, symbol)
 
68
 
 
69
    # get free memory
 
70
    def get_free_memory(self, symbol = "m"):
 
71
        baseData = psutil.virtual_memory()
 
72
        value = baseData.available
 
73
        return self.bytes2human(value, symbol)
 
74
        
 
75
    # get memory used percent
 
76
    def get_memory_percent(self):
 
77
        baseData = psutil.virtual_memory()
 
78
        return baseData.percent
 
79
 
 
80
    # get network total flow, return (up, down)
 
81
    def get_network_flow_total(self, symbol = "k"):
 
82
        network = psutil.network_io_counters()
 
83
        sent = network.bytes_sent
 
84
        recv = network.bytes_recv
 
85
 
 
86
        if(symbol == "b"):
 
87
            return (sent, recv)
 
88
        elif(symbol == "k"):
 
89
            k_up = float(sent) / 1024
 
90
            ups = str(k_up)
 
91
            ups = ups[0:ups.find(".") + 2]
 
92
 
 
93
            k_down = float(recv) / 1024
 
94
            downs = str(k_down)
 
95
            downs = downs[0:downs.find(".") + 2]
 
96
 
 
97
            return (ups, downs)
 
98
        else:
 
99
            return None
 
100
 
 
101
    # byte to human by symbol
 
102
    def bytes2human(self, value, symbol):
 
103
        if symbol == "k":
 
104
            value = value / 1024
 
105
        elif symbol == "m":
 
106
            value = value / 1024 / 1024
 
107
        elif symbol == "g":
 
108
            valuef = float(value) / 1024 / 1024 / 1024
 
109
            values = str(valuef)
 
110
            value = values[0:values.find(".") + 2]
 
111
        else:
 
112
            pass
 
113
        return str(value)
 
114
 
 
115
if __name__ == "__main__":
 
116
    mmm = MonitorBall()
 
117
    #   print mmm.get_network_flow_total()
 
118
    #   print mmm.get_network_flow_total("b")
 
119
    #   mmm.cleanup_memory()
 
120
    #   print mmm.get_cpu_percent()
 
121
    #   print mmm.get_cpu_percent(True)
 
122
    #   print mmm.get_cpu_percent()
 
123
    #   print mmm.get_free_memory("m")
 
124
    #   print mmm.get_used_memory("g")
 
125
    #   print mmm.get_total_memory("g")
 
126
    #   mmm.ttestt()
 
127
    print(mmm.get_memory_percent())
 
128
    print(mmm.get_total_memory())
 
129
    print(mmm.get_used_memory())
 
130
    print(mmm.get_free_memory())