In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to get Linux system CPU, memory, network usage through SSH under python windows. Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.
To do a program, you need to use the CPU of the system, memory, network usage. So write one yourself.
Calculate CPU utilization.
To read cpu usage, first understand the contents of the/proc/stat file. Here is an example of the/proc/stat file:
cpu, cpu0, cpu1…Each row of numbers has the same meaning, from left to right, it means user, nice, system, idle, iowait, irq, softirq respectively. For example, ubuntu 12.04 will output 10 columns of data, centos 6.4 will output 9 columns of data, the meaning of the latter columns is not clear, each system is output at least 7 columns. The specific meaning of each column is as follows:
user: CPU time in user mode (excluding CPU time taken by processes with negative nice values)
nice: CPU time taken by processes with a negative nice value
system: CPU time occupied by kernel state
idle: CPU idle time
iowait: Time to wait for IO
irq: hard interrupt time in the system
softirq: soft interrupt time in the system
The units for each of these values are jiffies, a global variable in the kernel that records the number of beats since system startup, which is treated as a unit of time here.
The intr row contains terminal information since system startup. The first column indicates the total number of interrupts, and each subsequent number corresponds to the number of times a certain type of interrupt has occurred.
The ctxt line contains the number of cpu context switches
The btime line contains the running time of the system in seconds
The processes/total_forks line contains the number of tasks created since system startup
The procs_running line contains the number of tasks currently running
The procs_blocked line contains the number of tasks currently blocked
Because it doesn't take much to calculate cpu utilization, the screenshot doesn't include all of the contents of the/proc/stat file.
After knowing the contents of the/proc file, you can calculate the CPU utilization rate. The specific method is: first read the file contents at t1 to obtain the CPU operation at this time, then wait for a period of time to read the file contents again at t2 to obtain the CPU operation, and then calculate the CPU utilization rate according to the data at the two times in the following way: 100 - (idle2 - idle1)*100/(total2 - total1), where total = user + system + nice + idle + iowait + irq + softirq. Python code is implemented as follows:
#-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port): sshClient = paramiko.SSHClient() sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) sshClient.connect(host,port,userName,password) command = r"cat /proc/stat" stdin,stdout,stderr = sshClient.exec_command(command) outs = stdout.readlines() sshClient.close() return outsdef getCpuInfo(outs): for line in outs: line = line.lstrip() counters = line.split() if len(counters)
< 5: continue if counters[0].startswith('cpu'): break total = 0 for i in range(1, len(counters)): total = total + long(counters[i]) idle = long(counters[4]) return {'total':total, 'idle':idle} def calcCpuUsage(counters1, counters2): idle = counters2['idle'] - counters1['idle'] total = counters2['total'] - counters1['total'] return 100 - (idle*100/total) if __name__ == '__main__': while True: SSHOutput = getSSHOutput("192.168.144.128","root","******",22) counters1 = getCpuInfo(SSHOutput) time.sleep(1) SSHOutput = getSSHOutput("192.168.144.128","root","******",22) counters2 = getCpuInfo(SSHOutput) print (calcCpuUsage(counters1, counters2))二、计算内存的利用率 计算内存的利用率需要读取的是/proc/meminfo文件,该文件的结构比较清晰。 需要知道的是内存的使用总量为used = total - free - buffers - cached,python代码实现如下: #-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port): sshClient = paramiko.SSHClient() sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) sshClient.connect(host,port,userName,password) command = r"cat /proc/meminfo" stdin,stdout,stderr = sshClient.exec_command(command) outs = stdout.readlines() sshClient.close() return outs def getMemInfo(outs): res = {'total':0, 'free':0, 'buffers':0, 'cached':0} index = 0 for line in outs: if(index == 4): break line = line.lstrip() memItem = line.lower().split() if memItem[0] == 'memtotal:': res['total'] = long(memItem[1]) index = index + 1 continue elif memItem[0] == 'memfree:': res['memfree'] = long(memItem[1]) index = index + 1 continue elif memItem[0] == 'buffers:': res['buffers'] = long(memItem[1]) index = index + 1 continue elif memItem[0] == 'cached:': res['cached'] = long(memItem[1]) index = index + 1 continue return res def calcMemUsage(counters): used = counters['total'] - counters['free'] - counters['buffers'] - counters['cached'] total = counters['total'] return used*100/total if __name__ == '__main__': while True: SSHOutput = getSSHOutput("192.168.144.128","root","******",22) counters = getMemInfo(SSHOutput) time.sleep(1) print (calcMemUsage(counters)) 三、获取网络的使用情况 获取网络使用情况需要读取的是/proc/net/dev文件,如下图所示,里边的内容也很清晰,不做过多的介绍,直接上python代码,取的是eth0的发送和收取的总字节数:#-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port): sshClient = paramiko.SSHClient() sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) sshClient.connect(host,port,userName,password) command = r"cat /proc/net/dev" stdin,stdout,stderr = sshClient.exec_command(command) outs = stdout.readlines() sshClient.close() return outsdef getNetInfo(dev,outs): res = {'total':0, 'in':0, 'out':0} for line in outs: if line.lstrip().startswith(dev): line = line.replace(':', ' ') items = line.split() res['in'] = long(items[1]) res['out'] = long(items[len(items)/2 + 1]) res['total'] = res['in'] + res['out'] return resif __name__ == '__main__': SSHOutput = getSSHOutput("192.168.144.128","root","******",22) print getNetInfo ('eth0 ',SSHOutput) After reading the above content, do you have any further understanding of how to obtain Linux system CPU, memory and network usage through SSH under Python Windows? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.