From c9d1c11c16ef02fd04a6b8408c7bbd32bdab5bf7 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 17 Jan 2019 21:42:08 +0800 Subject: [PATCH] . --- chapter2/mysite/myapp/utils.py | 49 ++++++++++--------- .../{settings.py => settings.py.template} | 0 2 files changed, 26 insertions(+), 23 deletions(-) rename chapter2/mysite/mysite/{settings.py => settings.py.template} (100%) diff --git a/chapter2/mysite/myapp/utils.py b/chapter2/mysite/myapp/utils.py index 60d2c6b..4b98ee0 100644 --- a/chapter2/mysite/myapp/utils.py +++ b/chapter2/mysite/myapp/utils.py @@ -4,35 +4,38 @@ import subprocess import re def get_execute_out(cmd): - return subprocess.getoutput(cmd) + return subprocess.getoutput(cmd) def get_platform_info(): - if platform.system()=='Linux': - ls = get_execute_out('top -bn 1 -i -c') - infos = ls.split('\n') + if platform.system()=='Linux': + ls = get_execute_out('top -bn 1 -i -c') + infos = ls.split('\n') + + info = {} - info = {} + m = re.match(r'top -.+?up\s+(.+?),', infos[0]) + if m: + info["up_time"] = m.group(1) - m = re.match(r'top -.+?up\s+(.+?),', infos[0]) - if m: - info["up_time"] = m.group(1) + m = re.match(r'Tasks:\s+(.+?)\s+total', infos[1]) + if m: + info["tasks"] = m.group(1) - m = re.match(r'Tasks:\s+(.+?)\s+total', infos[1]) - if m: - info["tasks"] = m.group(1) + m = re.match(r'%Cpu\(s\):\s+(.+?)\s+us,\s+(.+?)\s+sy,.+?ni,\s+(.+?)\s+id,', infos[2]) + if m: + info["cpu_us"] = m.group(1) + info["cpu_sy"] = m.group(2) + info["cpu_id"] = m.group(3) - m = re.match(r'%Cpu\(s\):\s+(.+?)\s+us,\s+(.+?)\s+sy,.+?ni,\s+(.+?)\s+id,', infos[2]) - if m: - info["cpu_us"] = m.group(1) - info["cpu_sy"] = m.group(2) - info["cpu_id"] = m.group(3) + m = re.match(r'KiB Mem : (\d+)\s+total,\s+(\d+)\s+free,\s+(\d+)\s+used,', infos[3]) + if m: + info["mem_total"] = m.group(1) + info["mem_free"] = m.group(2) + info["mem_used"] = m.group(3) - m = re.match(r'KiB Mem : (\d+)\s+total,\s+(\d+)\s+free,\s+(\d+)\s+used,', infos[3]) - if m: - info["mem_total"] = m.group(1) - info["mem_free"] = m.group(2) - info["mem_used"] = m.group(3) + return info - return info + return {'up_time': '20:52', 'tasks': '127', 'cpu_us': '1.6', 'cpu_sy': '1.6', 'cpu_id': '96.9', 'mem_total': '12134268', 'mem_free': '5648868', 'mem_used': '4537556'} - return {'up_time': '20:52', 'tasks': '127', 'cpu_us': '1.6', 'cpu_sy': '1.6', 'cpu_id': '96.9', 'mem_total': '12134268', 'mem_free': '5648868', 'mem_used': '4537556'} \ No newline at end of file +if __name__ == "__main__": + print(get_platform_info()) \ No newline at end of file diff --git a/chapter2/mysite/mysite/settings.py b/chapter2/mysite/mysite/settings.py.template similarity index 100% rename from chapter2/mysite/mysite/settings.py rename to chapter2/mysite/mysite/settings.py.template