使用shell指令碼監控Linux主機

2021-09-25 23:31:52 字數 1624 閱讀 7578

[root@localhost ~]# cat sysmon.sh

#!/bin/bash

dug=(df

−h∣g

rep"

/(df -h | grep "/

(df−h∣

grep

"/" | awk 『』 | awk -f% '' at position 2: 1}̲') cug=(expr 100 - $(mpstat | tail -1 | awk 『』 | awk -f. '' at position 2: 1}̲')) mug=(expr $(free | grep 「mem:」 | awk 『』) * 100 / $(free | grep 「mem:」 | awk 『』))

alog="/tmp/alert.txt"

email=「root」 #郵件傳送目的位址變數

if [ dug

−gt90

]the

nech

o"磁碟

佔用率:

dug -gt 90 ] then echo " 磁碟佔用率:

dug−gt

90]t

hene

cho"

磁碟占用

率:dug % " >> $alog

fiif [ $cug -gt 80 ]

then

echo " cpu使用率: $cug % " >> $alog

fiif [ mug

−gt90

]the

nech

o"記憶體

使用率:

mug -gt 90 ] then echo " 記憶體使用率:

mug−gt

90]t

hene

cho"

記憶體使用

率:mug % " >> $alog

fiif [ -f $alog ]

then

cat $alog | mail -s 「host alert」 $email #有關傳送郵件的配置

rm -rf $alog

rm -rf $alog

fi[root@localhost ~]# systemctl status crond #查詢crond服務是否為啟動狀態

● crond.service - command scheduler

loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

active: active (running) since 四 2019-05-23 05:03:09 cst; 1h 24min ago

main pid: 1339 (crond)

tasks: 1

cgroup: /system.slice/crond.service

└─1339 /usr/sbin/crond -n

…[root@localhost ~]# crontab -e #建立新的任務計畫

*/30 * * * * /root/sysmon.sh

[root@localhost ~]# crontab -l #檢視任務計畫

*/30 * * * * /root/sysmon.sh

Mono使用C 執行shell指令碼 Linux

process process new process process.startinfo.filename bash process.startinfo.createnowindow false 獲取或設定指示是否在新視窗中啟動該程序的值 不想彈出powershell視窗看執行過程的話,就 tru...

使用 shell 指令碼監控 Linux 主機

編寫以下指令碼命令 root localhost sh vim sysmon.sh bin bash 提取效能監控指標 磁碟占用 cpu使用 記憶體使用 dug df h grep awk awk f cug expr 100 mpstat tail 1 awk awk f.mug expr fre...

使用 shell 指令碼監控 Linux 主機

編寫以下指令碼命令 root localhost sh vim sysmon.sh bin bash 提取效能監控指標 磁碟占用 cpu使用 記憶體使用 dug df h grep awk awk f cug expr 100 mpstat tail 1 awk awk f.mug expr fre...