當前位置:首頁 » 編程軟體 » shell腳本監控

shell腳本監控

發布時間: 2023-05-23 01:32:04

linux上使用shelle腳本如何實時監控文件屬性時間更新後,執行另外shell腳本

想必你已經在chinaunix上找到答案了吧。
a=`stat -c %Y filename`;b=`date +%s`;if [ $[ $b - $a ] -gt 180 ];then command;else othercommand;fi就挺好的
但是你要注意在crontab中執行的時候需要加source一下環境變數,不然有些命令會找不到路徑。
如:
0-59 * * * * . /etc/profile;a=`stat -c %Y filename`;b=`date +%s`;if [ $[ $b - $a ] -gt 180 ];then command;else othercommand;fi
. /etc/profile可以換成你用戶目錄下的配置文件.
C shell的為 .cshrc 在用戶主目錄下
bashell的為 .bashrc 在用戶主目錄下
kshell的為.profile 在用戶主目錄下

❷ 如何利用shell腳本監控伺服器溫度

統:centos 5.5 1.監控伺服器狀態腳本內容:
01 #!/bin/bash 02
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
03
04 cpu_num=`grep -c 'model name' /proc/cpuinfo` 05
06 load_15=`uptime | awk '{print $NF}'` 07 08
average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc`
09
10 average_int=`echo $average_load | cut -f 1 -d "."` 11
12 load_warn=0.70 13
14 if (($average_int > 0)); then
15 echo "$IP伺服器15分鍾的系統平均負載為$average_load,超過警
戒值1.0,請立即處理!!!" | mailx -s "$IP 伺服器系統負載嚴重告警!!!
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
" [email protected] 16 else 17
18 load_now=`expr $average_load \> $load_warn` 19
20 if (($load_now == 1)); then
21 echo "$IP伺服器15分鍾的系統平均負載達到 $average_load,超過警戒值0.70,請及時處理。" | mailx -s "$IP 伺服器系統負載告警
" [email protected] 22 fi 23 24 fi 25
26 cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."` 27
28 if (($cpu_idle < 20)); then
29
echo "$IP伺服器cpu剩餘$cpu_idle%,使用率已經超過80%,請及時
處理。" | mailx -s"$IP 伺服器CPU告警" [email protected]
30 fi 31
32 swap_total=`free -m | grep Swap | awk '{print $2}'` 33
猜你喜歡租賃一體電腦dell電腦特約維修蘋果肌注射後休息蘋果店怎麼分期付款蘋果6s64g分期付款蘋果6s零首付分期付款加固筆記本品牌孩子 培訓 電腦手機微店 app蘋果6s5.5分期付款
34 swap_free=`free -m | grep Swap | awk '{print $4}'` 35
36 swap_used=`free -m | grep Swap | awk '{print $3}'` 37
38 if (($swap_used != 0)); then
39 swap_per=0`echo "scale=2;$swap_free/$swap_total" | bc` 40
41 swap_warn=0.20 42
43 swap_now=`expr $swap_per \> $swap_warn` 44
45 if (($swap_now == 0)); then
46 echo "$IP伺服器swap交換分區只剩下 $swap_free M 未使用,剩餘不足20%,使用率已經超過80%,請及時處理。" | mailx -s "$IP 伺服器
內存告警" [email protected] 47 fi 48 49 fi 50
51
#disk_sda=`df -h | grep /dev/sda3 | awk '{print $5}' | cut -f 1 -d "%"`
52 disk_sda=`df -Ph |grep /dev/mapper/VolGroup00-LogVol00|awk '{print

❸ 如何用shell腳本監控伺服器負載,cpu,內存,硬碟和登錄用戶數

試試檔廳這個方法:告襪Check if this entry is a directory or a file.
const size_t filenameLength = strlen(fileName);
if (fileName[filenameLength-1] == '行友隱/')
{
{

❹ 如何部署linux伺服器監控shell腳本

1、查看主機網卡流量

#!/bin/bash
#network
#Mike.Xu
while : ; do
time=』date +%m」-」%d」 「%k」:」%M』
day=』date +%m」-」%d』
rx_before=』ifconfig eth0|sed -n 「8″p|awk 『{print $2}』|cut -c7-』
tx_before=』ifconfig eth0|sed -n 「8″p|awk 『{print $6}』|cut -c7-』
sleep 2
rx_after=』ifconfig eth0|sed -n 「8″p|awk 『{print $2}』|cut -c7-』
tx_after=』ifconfig eth0|sed -n 「8″p|awk 『{print $6}』|cut -c7-』
rx_result=$[(rx_after-rx_before)/256]
tx_result=$[(tx_after-tx_before)/256]
echo 「$time Now_In_Speed: 「$rx_result」kbps Now_OUt_Speed: 「$tx_result」kbps」
sleep 2
done

2、系統狀況監控

#!/bin/sh
#systemstat.sh
#Mike.Xu
ip=192.168.1.227
top -n 2| grep 「Cpu」 >>./temp/cpu.txt
free -m | grep 「Mem」 >> ./temp/mem.txt
df -k | grep 「sda1″ >> ./temp/drive_sda1.txt
#df -k | grep sda2 >> ./temp/drive_sda2.txt
df -k | grep 「/mnt/storage_0″ >> ./temp/mnt_storage_0.txt
df -k | grep 「/mnt/storage_pic」 >> ./temp/mnt_storage_pic.txt
time=`date +%m」.」%d」 「%k」:」%M`
connect=`netstat -na | grep 「219.238.148.30:80″ | wc -l`
echo 「$time $connect」 >> ./temp/connect_count.txt

3、監控主機的磁碟空間,當使用空間超過90%就通過發mail來發警告

#!/bin/bash
#monitor available disk space
SPACE=』df | sed -n 『/ / $ / p』 | gawk 『{print $5}』 | sed 』s/%//』
if [ $SPACE -ge 90 ]
then
[email protected]
fi

4、監控CPU和內存的使用情況

#!/bin/bash
#script to capture system statistics
OUTFILE=/home/xu/capstats.csv
DATE=』date +%m/%d/%Y』
TIME=』date +%k:%m:%s』
TIMEOUT=』uptime』
VMOUT=』vmstat 1 2′
users=』echo $TIMEOUT | gawk 『{print $4}』 『
LOAD=』echo $TIMEOUT | gawk 『{print $9}』 | sed 「s/,//』 『
FREE=』echo $VMOUT | sed -n 『/[0-9]/p』 | sed -n 』2p』 | gawk 『{print $4} 『 『
IDLE=』echo $VMOUT | sed -n 『/[0-9]/p』 | sed -n 』2p』 |gawk 『{print $15}』 『
echo 「$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE」 >> $OUTFILE

5、全方位監控主機

#!/bin/bash
# check_xu.sh
# 0 * * * * /home/check_xu.sh
DAT=」`date +%Y%m%d`」
HOUR=」`date +%H`」
DIR=」/home/oslog/host_${DAT}/${HOUR}」
DELAY=60
COUNT=60
# whether the responsible directory exist
if ! test -d ${DIR}
then
/bin/mkdir -p ${DIR}
fi
# general check
export TERM=linux
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 &
# cpu check
/usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 &
# memory check
/usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 &
# I/O check
/usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 &
# network check
/usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 &
#/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 &

放在crontab里每小時自動執行:
0 * * * * /home/check_xu.sh

這樣會在/home/oslog/host_yyyymmdd/hh目錄下生成各小時cpu、內存、網路,IO的統計數據。如果某個時間段產生問題了,就可以去看對應的日誌信息,看看當時的主機性能如何。

❺ aix shell腳本 對某個目錄監控

#!/bin/sh
date=`date +%Y%m%d`
find /ftpdata/hjjk/ftp_down/hospital_register -name \
scoure*$date.txt \
chenggong*$date.txt \
quxiao*$date.txt \
user-$date.txt \
yytable-$date.txt \
yymsg-$date.txt \
>result 2>/dev/null
if [ $? -eq 0 ]
then
case file in
"scoure*$date.txt")
cat -n scoure*$date.txt | grep 12 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "soure*$date.txt is not nomal"
else echo "soure*$date.txt is nomal"
fi
;;
"chenggong*$date.txt")
cat -n chenggong*$date.txt | grep 17 | grep 1\^abc\^2\^abcd \
2>/dev/null && \
cat -n chenggong*$date.txt | grep 17 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "chenggong*$date.txt is not nomal"
else echo "chenggong*$date.txt is nomal"
fi
;;
"quxiao*$date.txt")
cat -n quxiao*$date.txt | grep 18 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "quxiao*$date.txt is not nomal"
else echo "quxiao*$date.txt is nomal"
fi
;;
"user-$date.txt")
cat -n user-$date.txt | grep 10 | grep 1\^abc\^2\^abcd \
2>/dev/null && \
cat -n user-$date.txt | grep 9 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "user-$date.txt is not nomal"
else echo "user-$date.txt is nomal"
fi
;;
"yytable-$date.txt")
cat -n yytable-$date.txt | grep 3 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "yytable-$date.txt is not nomal"
else echo "yytable-$date.txt is nomal"
fi
;;
"yymsg-$date.txt")
cat -n yymsg-$date.txt | grep 14 | grep 1\^abc\^2\^abcd \
2>/dev/null
if [ $? -ne 0 ]
then echo "yymsg-$date.txt is not nomal"
else echo "yymsg-$date.txt is nomal"
fi
esac

else
echo "file not find"
fi
rm -rf result

❻ 如何監控shell腳本是否執行失敗

你好,
舉個例子,執行一條基本語句建鎮者含立一個文件
#!/bin/bashtouch test.txtif [ $? -eq 0 ]then echo "執行成功"else echo "執行失御笑敗"fi$?可以判斷上一條命令是否成功,成功為0,不成功為非嫌橋0

❼ 系統之家Linux使用shell腳本監控rsync文件傳輸的完整性怎麼辦

#/bin/bashtrue=1F_root=/tmp/某張表Vol=`-ks$F_root|sed's/M//'`whiletruedoif[$Vol>50]thencat/dev/null>$F_rootsleep1800fidone這個腳本一旦運行就無法停止,那個某張表的目錄因你個人的情況而設置,因為循環條件whiletrue永遠成立,如果要停止監控,只要kill-9"進程號"就可以了

❽ shell腳本,如何監控目錄下的文件內容是否被修

監控文件的change time,當有修改時,change time會變

❾ 編寫SHELL腳本對某目錄下指定文件進行監控

純手工前碰寫出來的燃悔穗啊!
filename=file_`date +%Y%m%d`.txt
filenum=`find /home/oracle -name $filename|wc -l`
if [ $filenum -eq 0 ]
then
echo " file not find! "
else
infonum=`cat /皮卜home/oracle/$filename|grep "\^"|wc -l`
if [ $infonum -eq 0 ]
then
echo "^ not find! "
else
echo " file normal! "
fi
fi

熱點內容
壓縮長抱枕 發布:2025-07-18 16:13:38 瀏覽:503
如何使用wifi熱點配置網路 發布:2025-07-18 16:06:25 瀏覽:968
android軟鍵盤數字 發布:2025-07-18 16:03:18 瀏覽:610
三菱plc編程軟體官網 發布:2025-07-18 15:59:59 瀏覽:436
gse源碼 發布:2025-07-18 15:58:15 瀏覽:626
編譯鏈c語言執行速度 發布:2025-07-18 15:52:51 瀏覽:554
在線編程課程 發布:2025-07-18 15:46:56 瀏覽:172
生兔子演算法循環 發布:2025-07-18 15:44:32 瀏覽:46
背包規劃演算法 發布:2025-07-18 15:44:27 瀏覽:109
微信安卓手機哪裡設置鈴聲 發布:2025-07-18 15:36:54 瀏覽:780