Linux-函數的使用-編寫監控腳本

Linux-函數的使用-編寫監控腳本

  • 前言
  • 一、監控cpu
  • 二、采集內存的使用信息
  • 三、采集磁盤和分區的使用信息
  • 四、顯示進程的信息


前言

編寫監控腳本實現以下功能
監控cpu,內存,磁盤,進程等信息,每隔5分鐘記錄這些信息到日志文件里performance_usage.log


一、監控cpu

1.編寫一個消耗cpu的腳本test1.sh

[root@hz shell]# vim test1.sh
#!/bin/bash
# 消耗cpu資源
i=1
while :
do((i++))echo $i
done

2.執行腳本
[root@hz shell]# bash test1.sh

3.另一個終端編寫監控腳本采集cpu信息

小數運算使用bc計算器

[root@hz shell]# echo “10.4 + 45.7”|bc
56.1

[root@hz shell]# vim monitor.sh 
#!/bin/bash
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "use process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}
# 調用cpu_info函數
cpu_info[root@hz shell]# bash monitor.sh 
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
[root@hz shell]# cat /var/log/performance_usage.log 
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1

二、采集內存的使用信息

[root@hz shell]# free -m
total used free shared buff/cache available
Mem: 3627 504 2985 9 370 3123
Swap: 2047 0 2047
[root@hz shell]# cat /proc/meminfo
MemTotal: 3714640 kB
MemFree: 3057220 kB
MemAvailable: 3198012 kB
Buffers: 2708 kB
Cached: 325128 kB

total 表示總的內存
free 表示重來沒有使用的
used 表示使用的內存

buffer 寫數據的時候的緩存 --》算使用的空間,但是操作系統會回收里面的空間,這樣就可以釋放部分空間
cache 讀數據的時候的緩存
shared 共享內存消耗的空間 --》使用的空間
available 表示整個系統里還可以使用的內存 available=free+buffer/cache里釋放的緩存空間
used=buffer/cache里還在使用的空間+shared+進程真正消耗的空間

[root@hz shell]# mv monitor.sh  monitor_cpu_mem.sh 
[root@hz shell]# vim monitor_cpu_mem.sh 
#!/bin/bashget_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info[root@hz shell]# bash monitor_cpu_mem.sh 
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem:  buff_cache: 327 shared: 9[root@hz shell]# cat /var/log/performance_usage.log 
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
20250628221600 user process: 0.0 system process: 3.1 idle: 96.9 used: 3.1
20250628221600 total_mem: 3627 M available_mem: 2819 free_mem: 2806 used_mem:  buff_cache: 236 shared: 8
20250628221823 user process: 28.1 system process: 46.9 idle: 3.1 used: 96.9
20250628221823 total_mem: 3627 M available_mem: 2816 free_mem: 2802 used_mem:  buff_cache: 236 shared: 9
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem:  buff_cache: 327 shared: 9

三、采集磁盤和分區的使用信息

[root@hz shell]# mv monitor_cpu_mem.sh  monitor_cpu_mem_disk.sh 
[root@hz shell]# cat monitor_cpu_mem_disk.sh 
#!/bin/bash
get_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 采集磁盤和分區的使用信息
disk_info(){# 總共有多少磁盤和磁盤的大小disk_total=$(lsblk|grep "^sd"|awk '{print $1,$4}')# 分區的信息df -Th|egrep "/$|/boot$"|awk '{print $3,$4,$5,$6,$7}'|while read size used free used_percent part_name
doecho "$get_time disk size: $size used: $used free: $free used_percent: $used_persent part_name: $part_name disk_total: $disk_total"|tee -a /var/log/performance_usage.log
done
}
# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info# 調用disk_info函數
disk_info[root@hz shell]# bash monitor_cpu_mem_disk.sh 
20250628225929 user process: 29.0 system process: 51.6 idle: 0.0 used: 100.0
20250628225929 total_mem: 3627 M available_mem: 2803 free_mem: 2699 used_mem:  buff_cache: 328 shared: 9
20250628225929 disk size: 17G used: 6.3G free: 11G used_percent:  part_name: / disk_total: sda 20G
20250628225929 disk size: 960M used: 225M free: 736M used_percent:  part_name: /boot disk_total: sda 20G[root@hz shell]# cat /var/log/performance_usage.log 
use process: 26.5 system process: 50.0 idle: 2.9 used: 97.1
20250628221600 user process: 0.0 system process: 3.1 idle: 96.9 used: 3.1
20250628221600 total_mem: 3627 M available_mem: 2819 free_mem: 2806 used_mem:  buff_cache: 236 shared: 8
20250628221823 user process: 28.1 system process: 46.9 idle: 3.1 used: 96.9
20250628221823 total_mem: 3627 M available_mem: 2816 free_mem: 2802 used_mem:  buff_cache: 236 shared: 9
20250628222334 user process: 32.3 system process: 48.4 idle: 0.0 used: 100.0
20250628222334 total_mem: 3627 M available_mem: 2811 free_mem: 2708 used_mem:  buff_cache: 327 shared: 9
20250628225806 user process: 28.1 system process: 53.1 idle: 0.0 used: 100.0
20250628225806 total_mem: 3627 M available_mem: 2803 free_mem: 2700 used_mem:  buff_cache: 328 shared: 9
20250628225806 disk size: 17G used: 6.3G free: 11G used_percent:  part_name: / disk_total: sda 20G
20250628225806 disk size: 960M used: 225M free: 736M used_percent:  part_name: /boot disk_total: sda 20G
20250628225929 user process: 29.0 system process: 51.6 idle: 0.0 used: 100.0
20250628225929 total_mem: 3627 M available_mem: 2803 free_mem: 2699 used_mem:  buff_cache: 328 shared: 9
20250628225929 disk size: 17G used: 6.3G free: 11G used_percent:  part_name: / disk_total: sda 20G
20250628225929 disk size: 960M used: 225M free: 736M used_percent:  part_name: /boot disk_total: sda 20G

四、顯示進程的信息

需要顯示cpu使用率最高的前5個進程
需要顯示內存使用率最高的前5個進程

進程的總信息,有多少個進程,有多少個進程在running
Tasks: 162 total, 1 running, 161 sleeping, 0 stopped, 0 zombie

ps aux|sort -k3 -nr|head -5
ps aux|sort -k4 -nr|head -5

ps aux --sort=-%cpu |head -n 6|tail -n 5
ps aux --sort=-%mem |head -n 6|tail -n 5

+ 表示升序
- 表示降序
%cpu 表示字段名

[root@hz shell]# vim monitor_cpu_mem_disk_process.sh 
[root@hz shell]# cat monitor_cpu_mem_disk_process.sh 
#!/bin/bashget_time=$(date +%Y%m%d%H%M%S)
# 采集cpu信息
cpu_info() {top -bn 1|awk '/^%Cpu/{print $2,$4,$8}'|while read us sy idledo# 得到使用的cpu比率used_cpu=$( echo "scale=2;100-$idle"|bc )# 采集信息存放到日志文件,同時輸出屏幕echo "$get_time user process: $us system process: $sy idle: $idle used: $used_cpu"|tee -a /var/log/performance_usage.logdone
}# 采集內存信息
mem_info() {free -m|awk '/^Mem/{print $2,$3,$4,$5,$6,$7}'|while read total used free shared buff_cache availabledoecho "$get_time total_mem: $total M available_mem: $available free_mem: $free used_mem: $used_mem buff_cache: $buff_cache shared: $shared"|tee -a /var/log/performance_usage.log done
}
# 采集磁盤和分區的使用信息
disk_info(){# 總共有多少磁盤和磁盤的大小disk_total=$(lsblk|grep "^sd"|awk '{print $1,$4}')# 分區的信息df -Th|egrep "/$|/boot$"|awk '{print $3,$4,$5,$6,$7}'|while read size used free used_percent part_name
doecho "$get_time disk size: $size used: $used free: $free used_percent: $used_persent part_name: $part_name disk_total: $disk_total"|tee -a /var/log/performance_usage.log
done
}# 采集進程的信息
process_info(){# 顯示cpu使用率最高的前5個進程#cpu_top_5=$(ps aux --sort=-%cpu |head -n 6|tail - n 5)ps aux --sort=-%cpu |head -n 6|tail -n 5|awk '{print "pid:"$2,"cpu_percent:"$3,"program_name:"$11}'|tee -a /var/log/performance_usage.log #cpu_top_5=$(ps aux|sort -k3 -nr|head -5)# 顯示內存使用率最高的前5個進程#mem_top_5=$(ps aux --sort=-%mem |head -n 6|tail - n 5)ps aux --sort=-%mem |head -n 6|tail -n 5|awk '{print "pid:"$2,"mem_percent:"$4,"program_name:"$11}'|tee -a /var/log/performance_usage.log 
}# 顯示進程的總信息,有多少進程,有多少個進程在runningtop -bn 1|grep "^Task"|awk '{print "total_processes:" $2,"running_processes:"$4}'|tee -a /var/log/performance_usage.log# 調用cpu_info函數
cpu_info# 調用mem_info函數
mem_info# 調用disk_info函數
disk_info# 調用process_info函數
process_info[root@hz shell]# bash monitor_cpu_mem_disk_process.sh 
total_processes:165 running_processes:3
20250706222631 user process: 37.5 system process: 46.9 idle: 3.1 used: 96.9
20250706222631 total_mem: 3627 M available_mem: 2753 free_mem: 2612 used_mem:  buff_cache: 371 shared: 9
20250706222631 disk size: 17G used: 6.3G free: 11G used_percent:  part_name: / disk_total: sda 20G
20250706222631 disk size: 960M used: 225M free: 736M used_percent:  part_name: /boot disk_total: sda 20G
pid:22277 cpu_percent:75.9 program_name:bash
pid:20538 cpu_percent:1.2 program_name:sshd:
pid:900 cpu_percent:0.6 program_name:/usr/sbin/mysqld
pid:718 cpu_percent:0.1 program_name:/usr/bin/vmtoolsd
pid:21504 cpu_percent:0.1 program_name:[kworker/0:1-events]
pid:900 mem_percent:11.2 program_name:/usr/sbin/mysqld
pid:709 mem_percent:0.5 program_name:/usr/sbin/NetworkManager
pid:1425 mem_percent:0.5 program_name:/usr/bin/python3
pid:1 mem_percent:0.3 program_name:/usr/lib/systemd/systemd
pid:1433 mem_percent:0.3 program_name:sshd:

授予可執行權限

[root@hz shell]# chmod +x monitor_cpu_mem_disk_process.sh

制定計劃任務,執行腳本

[root@hz shell]# crontab -e
*/5 * * * * /shell/monitor_cpu_mem_disk_process.sh

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/94189.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/94189.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/94189.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Authelia:開源雙因素認證與單點登錄解決方案

項目標題與描述 Authelia是一個開源的認證和授權服務器,專注于為應用程序提供雙因素認證(2FA)和單點登錄(SSO)功能。通過Web門戶,Authelia能夠作為身份和訪問管理(IAM)系統&#xff…

Apache Ozone 介紹與部署使用(最新版2.0.0)

目錄 一、軟件介紹 二、軟件架構 Ozone Manager(OM) Storage Container Manager(SCM) Containers Datanodes Storage Containers Recon Recon 和 Ozone Manager Recon 和 Storage Container Manager 三、安裝部署 準備…

Review --- Linux

Review — Linux Linux 是一種開源的類 Unix 操作系統內核,廣泛應用于服務器、嵌入式設備和個人計算機中。其核心特點是開源、穩定、安全和高度的可定制性。對于大學畢業生而言,掌握 Linux 的基本操作和原理是進入 IT 行業的重要技能之一。 Linux 的基本…

【msyql 】占用硬盤太大 ,那些文件可以清理

從目錄內容來看,這臺 MySQL 服務器上主要是 xxl-job 調度平臺的數據庫。占用空間最大的是:24G xxl_job_log.ibd這個文件是 xxl-job 的任務執行日志表,隨著時間推移,日志量會非常大。可以清理的文件和方法1. 清理 xxl_job_log 表數…

58 C++ 現代C++編程藝術7-模板友元

C 現代C編程藝術7-模板友元 文章目錄C 現代C編程藝術7-模板友元一、基礎應用場景 🧩1. 模板類聲明友元函數2. 普通類聲明模板函數為友元二、模板類互訪場景 ??1. 同類模板互訪(一對一)2. 異類模板互訪(多對多)三、高…

Undertow —— JBOSS 的社區版,redhat 下場維護的開源項目,頂頂好用的 Java web server

Undertow JBoss Community Undertow Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO. Undertow 是一個用 Java 編寫的靈活高性能 Web 服務器,提供基于 NIO 的阻塞和非阻塞 API。…

【AI智能體】Dify 搭建業務單據差異核對助手實戰詳解

目錄 一、前言 二、Dify介紹 2.1 Dify 是什么 2.2 Dify 核心特性 2.2.1 Dify特點 2.2.2 Dify 多模型支持 2.2.3 Dify 適應場景 2.2.4 基于Dify 搭建發票識別應用優勢 三、Dify 搭建業務單據核對助手實戰過程 3.1 前置準備 3.1.1 安裝必要的插件 3.2 完整操作步驟 3…

Centos編譯安裝Python3.10

gcc編譯源碼包 下載python源碼包并解壓 wget https://www.python.org/ftp/python/3.10.18/Python-3.10.18.tgz tar -xf Python-3.10.18.tgz cd Python-3.10.18系統編譯依賴環境安裝 sudo yum install zlib-devel ncurses-devel gdbm-devel nss-devel openssl-devel readline-de…

Maya 3D建模 導入參考圖、鎖定參考圖

1 導入參考圖切換到 前視圖 或者 側視圖 導入 (根據參考圖片類別去選擇)方法1:視圖--圖像平面--導入圖像方法2:直接點 圖像平面 備注:誤操作導致看不到 解決辦法:顯示--視口 找對應的2 鎖定參考圖目的&…

基于單片機智能加濕器/空氣加濕器

傳送門 👉👉👉👉其他作品題目速選一覽表 👉👉👉👉其他作品題目功能速覽 概述 基于單片機的智能加濕器通過集成溫濕度傳感器、控制模塊和霧化裝置,實現環境濕度的自…

SNDR:高精度ADC系統的綜合性能標尺

SNDR:高精度ADC系統的綜合性能標尺 一、SNDR的本質定義與理論基礎 信噪失真比(Signal-to-Noise-and-Distortion Ratio) 是評估ADC系統綜合性能的核心指標,定義為信號功率與噪聲及失真功率之和的比值: SNDRdB10log?10(PsignalPnoisePdistorti…

2025年滲透測試面試題總結-31(題目+回答)

安全領域各種資源,學習文檔,以及工具分享、前沿信息分享、POC、EXP分享。不定期分享各種好玩的項目及好用的工具,歡迎關注。 目錄 一、代碼審計核心思路(261) 二、MySQL Getshell前提(262) …

[創業之路-560]:機械、電氣、自控、電子、軟件、信息、通信、大數據、人工智能,上述技術演進過程

上述關鍵詞反映的技術演進過程可梳理為一條從機械執行到智能決策的遞進式發展主線,各技術領域在不同階段相互滲透、共同推動機器人技術從功能替代向認知革命躍遷。以下是具體演進邏輯與趨勢分析:一、技術演進的三階段遞進機械主導階段(工業革…

芋道前端項目部署后刷新 404 的解決辦法(Nginx 配置教程)

很多同學在把 芋道前端項目 部署到服務器后,會遇到一個奇怪的問題: 👉 項目首頁能正常訪問,但一旦在瀏覽器里手動刷新某個頁面,就會報 404 Not Found 錯誤。 這到底是為什么呢?又該怎么解決呢?下…

更適合后端寶寶的前端三件套之HTML

文章目錄📕1. HTML基礎??1.1 什么是HTML??1.2 認識HTML標簽??1.3 HTML文件基本結構??1.4 標簽層次結構📕2. HTML常見標簽??2.1 標題標簽??2.2 段落標簽??2.3 換行標簽??2.4 圖片標簽??2.5 超鏈接標簽??2.6 表格標簽📕3. …

【JVM內存結構系列】四、不同垃圾回收器與堆內存的適配關系:從分代GC到Region GC

在JVM內存體系中,堆內存的“分代結構”與“對象流轉規則”是通用基礎,但垃圾回收器(GC)是決定堆內存實際表現的核心變量——不同GC為實現“低延遲”“高吞吐量”等目標,會對堆的劃分方式、對象管理邏輯、參數配置規則進…

Zemax光學設計輸出3D

輸出立體數據文件(IGES/STEP/SAT/STL 格式)的參數設置界面,各參數含義如下:1. 起始面/終止面:設定要輸出立體數據對應的光學表面范圍,從第 0 個表面到第 9 個表面 ,限定參與輸出的光學結構表面區…

模塊測試與低功耗模式全攻略

一、模塊測試流程在測試一個模塊時,建議遵循以下步驟:基本測試:測試該模塊的寄存器讀寫功能是否正常。可以向每個寄存器寫入 0x5A5A 和 0xA5A5,這兩種模式可以覆蓋對寄存器寫入 0 和 1 的情況。進階測試:在基本測試通過…

機器學習實驗三、使用決策樹算法預測泰坦尼克號幸存者

實驗目的1. 掌握特征工程,會進行特征提取與特征選擇,會進行缺失值填充。2. 建立決策樹模型,解決實際問題。3. 會對模型進行調試,能夠繪制并保存決策樹。實驗環境Python 3.7.0,Sklearn ,PyCharm實驗原理1、特…

從全棧開發到微服務架構:一次真實的Java面試實錄

從全棧開發到微服務架構:一次真實的Java面試實錄 面試官與應聘者介紹 面試官:李明,某互聯網大廠技術負責人,擅長Java后端、微服務及云原生架構。 應聘者:張偉,28歲,碩士學歷,擁有5年…