Linux服务器CPU、内存、磁盘空间、负载情况查看python脚本

释放双眼,带上耳机,听听看~!

[本文出自天外归云的博客园]

网上搜,东拼西凑,组装了一个可以查Linux服务器CPU使用率、内存使用率、磁盘空间占用率、负载情况的python脚本。

脚本内容如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
1# -*- coding:utf-8 -*- -
2import os, time
3
4last_worktime=0
5last_idletime=0
6
7def get_cpu():
8        global last_worktime, last_idletime
9        f=open("/proc/stat","r")
10        line=""
11        while not "cpu " in line: line=f.readline()
12        f.close()
13        spl=line.split(" ")
14        worktime=int(spl[2])+int(spl[3])+int(spl[4])
15        idletime=int(spl[5])
16        dworktime=(worktime-last_worktime)
17        didletime=(idletime-last_idletime)
18        rate=float(dworktime)/(didletime+dworktime)
19        last_worktime=worktime
20        last_idletime=idletime
21        if(last_worktime==0): return 0
22        return rate
23
24def get_mem_usage_percent():
25    try:
26        f = open('/proc/meminfo', 'r')
27        for line in f:
28            if line.startswith('MemTotal:'):
29                mem_total = int(line.split()[1])
30            elif line.startswith('MemFree:'):
31                mem_free = int(line.split()[1])
32            elif line.startswith('Buffers:'):
33                mem_buffer = int(line.split()[1])
34            elif line.startswith('Cached:'):
35                mem_cache = int(line.split()[1])
36            elif line.startswith('SwapTotal:'):
37                vmem_total = int(line.split()[1])
38            elif line.startswith('SwapFree:'):
39                vmem_free = int(line.split()[1])
40            else:
41                continue
42        f.close()
43    except:
44        return None
45    physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total)
46    virtual_percent = 0
47    if vmem_total > 0:
48        virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total)
49    return physical_percent, virtual_percent
50
51def usage_percent(use, total):
52    try:
53        ret = (float(use) / total) * 100
54    except ZeroDivisionError:
55        raise Exception("ERROR - zero division error")
56    return ret
57
58statvfs = os.statvfs('/')
59
60total_disk_space = statvfs.f_frsize * statvfs.f_blocks
61free_disk_space = statvfs.f_frsize * statvfs.f_bfree
62disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space
63disk_usage = int(disk_usage)
64disk_tip = "硬盘空间使用率(最大100%):"+str(disk_usage)+"%"
65print(disk_tip)
66
67mem_usage = get_mem_usage_percent()
68mem_usage = int(mem_usage[0])
69mem_tip = "物理内存使用率(最大100%):"+str(mem_usage)+"%"
70print(mem_tip)
71
72cpu_usage = int(get_cpu()*100)
73cpu_tip = "CPU使用率(最大100%):"+str(cpu_usage)+"%"
74print(cpu_tip)
75
76load_average = os.getloadavg()
77load_tip = "系统负载(三个数值中有一个超过3就是高):"+str(load_average)
78print(load_tip)
79

在Linux服务器上touch一个py文件,把以上内容粘贴进去并保存。运行python脚本,效果如下:

Linux服务器CPU、内存、磁盘空间、负载情况查看python脚本

一目了然。以后再出现访问后台接口502、无返回的情况,在后台服务器执行一下脚本,看看是不是这方面引起的问题,是不是内存占用过高,是不是磁盘满了等等。方便后台服务器环境问题的定位,以便联系相关的开发或运维来协助解决问题。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ explicit关键字

2022-1-11 12:36:11

病毒疫情

福建省新增新型冠状病毒感染的肺炎疫情情况

2020-1-29 16:05:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索