Use shell scripts to monitor CPU utilization in Linux.

Source: Internet
Author: User

1. Monitor CPU information with Vmstat tools.

Vmstat is a Linux system monitoring tool that uses the Vmstat command to get information about processes, memory, memory paging, blocking IO, traps, and CPU activity.

R: The number of processes in the running queue; B: The number of processes waiting for IO.

SWPD: Used virtual memory size (k); Free: idle memory size; buff: buffer size used; cache: Cached size used.

Si: The size of memory written from the swap area per second (kb/s); so: the size of the swap partition written from memory per second.

BI: The number of blocks read per second, and the number of blocks written by Bo per second.

In: Interrupts per second, including clock interrupts, CS: Number of context switches per second.

US (User Time): Client process execution consumes CPU times, sy (System time): Process execution consumes CPU, ID: Idle time (including IO wait time), WA: Wait io time.

The code is as follows:

#!/bin/bash
#
cpu_us=$ ( Vmstat | awk ' {print $13} ' | Sed-n ' $p ')
cpu_sy=$ (Vmstat | awk ' {PRINT $14} ' | Sed-n ' $p ')
cpu_id=$ (Vmstat | awk ' {PRINT $ $} ' | Sed-n ' $p ')
cpu_wa=$ (Vmstat | awk ' {PRINT $16} ' | Sed-n ' $p ')
cpu_st=$ (Vmstat | awk ' {PRINT $17} ' | Sed-n ' $p ')

echo-e "cpuinfo:\nus=$ cpu_us\nsy= $CPU _sy\nid= $CPU _id\nwa= $CPU _wa\nst= $CPU _st "

2, based on/proc/stat computing CPU utilization to monitor the alarm and extract the highest CPU occupied the top ten processes.

/proc/stat:

This file contains information about all CPU activity, and all values in the file are accumulated from the start of the system to the current moment. You can use the information to calculate the CPU utilization.

Each line of each parameter means (in the first act example, in jiffies,1jiffies=0.01 seconds):

User (214): Accumulated from the start of the system to the current moment, the CPU time of the users, does not contain the Nice value is negative process.

Nice (304): Accumulates from the start of the system to the current moment.

System (1906): The CPU time occupied by a process that has a negative value from the beginning of the boot to the current time.

Idle (1296541): Accumulate from the start of the system to the current time, in addition to the hard disk IO wait time other than waiting time.

Iowait (1620): The hard disk IO wait time is accumulated from the start of the system to the current time.

IRQ (5): Accumulates from the start of the system to the current moment, hard interrupt time.

SOFTIRQ (457): Accumulated from the start of the system to the current moment, soft interrupt time.

CPU Time =USER+NICE+SYSTEM+IDLE+IOWAIT+IRQ+SOFTIRQ.

CPU utilization = (IDLE2-IDLE1)/(CPU2-CPU1) *100.

PS aux:

Show other user-initiated processes (a)

View your own processes in the system (x)

User and startup time to start this process (U)

The code is as follows:

#!/bin/bash
#
cpu1= ' cat/proc/stat | grep ' CPU ' | awk ' {print $ ' "$ $" "$4" "$ $" "$6" "$7" "$8} "
Sleep 5
cpu2= ' cat/proc/stat | grep ' CPU ' | awk ' {print $ ' "$ $" "$4" "$ $" "$6" "$7" "$8} "
idle1= ' echo $CPU 1 | awk ' {print $4} '
idle2= ' echo $CPU 2 | awk ' {print $4} '
cpu1_total= ' echo $CPU 1 | awk ' {print $1+$2+$3+$4+$5+$6+$7} '
cpu2_total= ' echo $CPU 2 | awk ' {print $1+$2+$3+$4+$5+$6+$7} '
idle= ' echo ' $IDLE 2-$IDLE 1 "| BC '
cpu_total= ' echo ' $CPU 2_total-$CPU 1_total "| BC '
rate= ' echo ' scale=4, ($CPU _total-$IDLE)/$CPU _total*100 "| BC | awk ' {printf '%.2f ', ' $ '
echo "cpu_rate:${rate}%"
cpu_rate= ' echo $RATE | cut-d.-f1 '
if [$CPU _rate-ge]
Then echo "CPU Warn"
PS aux | grep-v USER | sort-rn-k3 | head
else echo "It's OK"
fi

Use shell scripts to monitor CPU utilization in Linux.

Related Article

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.