How to calculate the CPU usage using python and bash

Source: Internet
Author: User
This article mainly introduces python and bash methods for calculating CPU utilization. It involves Python's reading skills for system hardware information and has some reference value, for more information about how to calculate the CPU usage in python and bash, see the following example. Share it with you for your reference. The details are as follows:

At the beginning, a bash implementation was written;
Because I have been learning python recently, I tried to implement it again using python;
Support the python2 environment;
I would like to thank you for your suggestions on any improvement;

The Python code is as follows:

#!/usr/bin/python# -*- coding:utf8 -*-__author__ = 'chenwx'def cpu_rate():  import time  def cpu_r():    f = open("/proc/stat","r")    for f_line in f:      break    f.close()    f_line = f_line.split(" ")    f_line_a=[]    for i in f_line:      if i.isdigit():        i=int(i)        f_line_a.append(i)    total = sum(f_line_a)    idle = f_line_a[3]    return total,idle  total_a,idle_a=cpu_r()  time.sleep(2)  total_b,idle_b=cpu_r()  sys_idle = idle_b - idle_a  sys_total = total_b - total_a  sys_us = sys_total - sys_idle  cpu_a = (float(sys_us)/sys_total)*100  return cpu_a# print cpu_rate()

Bash implementation:

#! /Bin/bash # I think there should be some way to simplify the calculation array; # I didn't expect it at the moment. Please give me some suggestions. Thank you; cpu_a = ('grep' cpu '/proc/stat ') total_a =$ ($ {cpu_a [1]} + $ {cpu_a [2]} + $ {cpu_a [3]} + $ {cpu_a [4]} + $ {cpu_a [5] }+ {cpu_a [6] }+ {cpu_a [7] }+ {cpu_a [8] }+ {cpu_a [9]}) idle_a =$ {cpu_a [4]} sleep 5cpu_ B = ('grep' cpu '/proc/stat ') total_ B = $ ($ {cpu_ B [1]} + $ {cpu_ B [2]} + $ {cpu_ B [3]} + $ {cpu_ B [4]} + $ {cpu_ B [5]} + $ {cpu_ B [6]} + $ {cpu_ B [7]} + $ {cpu_ B [8]} + $ {cpu_ B [9]}) idle_ B =$ {cpu_ B [4]} sys_idle =$ ($ idle_ B-$ idle_a) sys_total =$ ($ total_ B-$ total_a )) sys_us = $ ($ sys_total-$ sys_idle) echo "scale = 2; $ sys_us/$ sys_total * 100" | bc

How to traverse the array:

# I found a solution for Array calculation, but I still feel that the for Loop Calculation method is complicated. # I wonder if there is any method for calculating all values in the logarithm group together. cpu_rate_a () {cpu_a = ('grep' cpu '/proc/stat') for I in $ {cpu_a [@]: 1} do total_a =$ ($ total_a + $ I )) doneidle_a =$ {cpu_a [4]} sleep 5cpu_ B = ('grep' cpu '/proc/stat') for I in $ {cpu_ B [@]: 1} do total_ B =$ ($ total_ B + $ I) doneidle_ B =$ {cpu_ B [4]} sys_idle =$ ($ idle_ B-$ idle_a )) sys_total = $ ($ total_ B-$ total_a) sys_us = $ ($ sys_total-$ sys_idle) local_cpu_rate = $ (echo "scale = 2; $ sys_us/$ sys_total * 100 "| bc )}

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.