| 
                         1. 源码文件 
为了方便自己的使用,我完成了该代码,由两部分构成: 
源码文件:CPU_usage.py 
执行文件:CPU_usage.sh 
(1)CPU_usage.py代码 
<pre class="has"> 
# -- coding:utf-8 -- 
from multiprocessing import cpu_count 
!/usr/bin/env python
import os,time 
last_worktime=0 
last_idletime=0 
def get_cpu(): 
global last_worktime,last_idletime 
f=open("/proc/stat","r") 
line="" 
while not "cpu " in line: line=f.readline() 
f.close() 
spl=line.split(" ") 
worktime=int(spl[2])+int(spl[3])+int(spl[4]) 
idletime=int(spl[5]) 
dworktime=(worktime-last_worktime) 
didletime=(idletime-last_idletime) 
rate=float(dworktime)/(didletime+dworktime) 
last_worktime=worktime 
last_idletime=idletime 
if(last_worktime==0): return 0 
return rate 
def get_mem_usage_percent(): 
try: 
f = open('/proc/meminfo','r') 
for line in f: 
if line.startswith('MemTotal:'): 
mem_total = int(line.split()[1]) 
elif line.startswith('MemFree:'): 
mem_free = int(line.split()[1]) 
elif line.startswith('Buffers:'): 
mem_buffer = int(line.split()[1]) 
elif line.startswith('Cached:'): 
mem_cache = int(line.split()[1]) 
elif line.startswith('SwapTotal:'): 
vmem_total = int(line.split()[1]) 
elif line.startswith('SwapFree:'): 
vmem_free = int(line.split()[1]) 
else: 
continue 
f.close() 
except: 
return None 
physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache),mem_total) 
virtual_percent = 0 
if vmem_total > 0: 
virtual_percent = usage_percent((vmem_total - vmem_free),vmem_total) 
return physical_percent,virtual_percent 
def usage_percent(use,total): 
try: 
ret = (float(use) / total) * 100 
except ZeroDivisionError: 
raise Exception("ERROR - zero division error") 
return ret 
if name == "main": 
cpucount = "内核数: %s" % cpu_count() + "  " 
statvfs = os.statvfs('/')
# total_disk_space = statvfs.f_frsize * statvfs.f_blocks
# free_disk_space = statvfs.f_frsize * statvfs.f_bfree
# disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space
# disk_usage = int(disk_usage)
# disk_tip = "硬盘空间使用率:" + str(disk_usage) + "%  "
mem_usage = get_mem_usage_percent()
mem_usage = int(mem_usage[0])
mem_tip = "物理内存使用率:" + str(mem_usage) + "%  "
cpu_usage = int(get_cpu() * 100)
cpu_tip = "CPU使用率:" + str(cpu_usage) + "%  "
load_average = os.getloadavg()
load_tip = "系统负载" + str(load_average)
print(cpucount + mem_tip + cpu_tip + load_tip)</code></pre> 
说明:参考了网上资源() 
(2)CPU_usage.sh代码: 
<pre class="has"> 
for k in $(seq 2 96) 
do 
ssh cluster@AiLab${k} "hostname && cd ~ $$ sh CPU_usage.sh" 
done 
说明:我的集群节点是2到92,把编号改为你自己的集群节点编号。 
2. 使用命令 
最开始我是用原始的“sh CPU_usage.sh”来执行命令,实在是不方便。 
于是研究了一下怎么把命令改为自定义命令。 
后来找到~/.bashrc,在里面添加了alias命令自定义名字,偶然看到网上另一个教程(),改变了注意。 
资源里说: 
“bashrc 中有一句话 
# You may want to put all your additions into a separate file like 
# ~/.bash_aliases,instead of adding them here directly. 
就是说可以另外新建一个文件用于专门存放自己的alias信息” 
 
于是,可以这样来做: 
<pre class="has"> 
$ cd ~ 
$ vi .bash_aliases 
在文件中输入自己想设置的命令: 
<pre class="has"> 
alias usageinfo='cd ~ && sh CPU_info.sh' 
然后:wq保存退出,用下面命令激活: 
<pre class="has"> 
$ source .bashrc 
接下来,在任何目录下输入”usageinfo“,都可以方便地查看集群节点信息了。 
by CS青雀 
2018年7月26日                         (编辑:莱芜站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |