根據(jù)公司需求寫的一個linux 巡檢小腳本
更新時間:2017年04月30日 15:56:19 投稿:mdxy-dxy
這篇文章主要介紹了根據(jù)公司需求寫的一個linux 巡檢小腳本,可以用來檢查服務(wù)器的一些運(yùn)行狀況,需要的朋友可以參考下
核心代碼:
#!/bin/bash check_process(){ tolprocess=`ps auxf|grep DisplayMa[nager]|wc -l` #if [ "$tolprocess" -lt "1" ];then if [ "$tolprocess" -ge "1" ];then echo 'process ok' else echo 'fail' fi } check_log(){ if [ -e /etc/syslog-ng/syslog-ng.conf ];then conlog=`cat '/etc/syslog-ng/syslog-ng.conf'|grep "10.70.72.253"|wc -l` if [ "$conlog" -ge "1" ];then echo 'syslog-ng ok' fi elif [ -e /etc/syslog.conf ];then conlog=`cat '/etc/syslog.conf'|grep "10.70.72.253"|wc -l` if [ "$conlog" -ge "1" ];then echo 'syslog ok' fi else echo 'log not find or error' fi } check_cpuidle(){ mincpu=`sar -u 2 10|grep all|awk '{print $NF}'|sort -nr|tail -1` if [ $(echo "${mincpu} < 20" | bc) = 1 ];then #if [ "$mincpu" -le "20" ];then echo 'cpu idle is less than 20% ,please check' else echo 'cpu idle is more than 20%, it is ok ' fi } check_mem(){ vmstat 2 10 } check_disk(){ chkdsk=`fdisk -l|egrep 'failed|unsynced|unavailable'|wc -l` if [ "$chkdsk" -ge "1" ];then echo 'fdisk check ok ' else echo 'fdisk check find error,please check your disk ' fi } check_io(){ util=`sar -d 2 10|egrep -v 'x86|^$|await'|awk '{print $NF}'|sort -nr|tail -1` await=`sar -d 2 10|egrep -v 'x86|^$|await'|awk '{print $(NF-2)}'|sort -nr|tail -1` if [ $(echo "${util} < 80" | bc) = 1 ] && [ $(echo "${await} < 100" | bc) = 1 ] ;then echo 'disk io check is fine' else echo 'disk io use too high ' fi } check_swap(){ tolswap=`cat /proc/meminfo|grep SwapTotal|awk '{print $2}'` #awk '/SwapTotal/{total=$2}/SwapFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo useswap=`awk '/SwapTotal/{total=$2}/SwapFree/{free=$2}END{print (total-free)}' /proc/meminfo ` util=`awk 'BEGIN{printf "%.1f\n",'$useswap'/'$tolswap'}'` if [ $(echo "${util} < 0.3" | bc) = 1 ] || [ $(echo "${useswap} < 1024" | bc) = 1 ] ;then echo 'swap use is ok ' else echo "useswap: $useswap kb, swap util is $util" fi } check_dmesg(){ chkdm=`dmesg |egrep 'scsi reset|file system full'|wc -l` if [ "$chkdm" -ge "1" ];then echo 'dmesg test ok ' else echo 'dmesg check find error ' fi } check_boot(){ chkdm=`cat /var/log/boot.msg|egrep 'scsi reset|file system full'|wc -l` if [ "$chkdm" -ge "1" ];then echo 'boot check fine ' else echo 'boot check find error ' fi } check_inode(){ maxinode=`df -i|awk '{print $5}'|egrep -v 'IUse|-' |sed 's/%//g'|sort -nr|head -1` if [ $(echo "${maxinode} < 80" | bc) = 1 ];then echo 'inode check ok ' else echo 'inode used more than 80% ' fi } check_df(){ dfuse=`df -HT|awk '{print $6}'|grep -v Use|sed 's/%//g'|sort -nr|head -1` if [ $(echo "${dfuse} < 80" | bc) = 1 ];then echo 'disk used is less than 80% ,it is ok !' elif [ $(echo "${dfuse} > 80" | bc) = 1 ] && [ $(echo "${dfuse} < 90" | bc) = 1 ];then echo 'warning , disk used more than 80% and less than 90% ' else echo ' Critical, disk used more than 90% ' fi } echo '################### check process ###################' check_process echo '################### check syslog ####################' check_log echo '################### check cpuidle ###################' check_cpuidle echo '################### echo memory stat ################' check_mem echo '################### check fdisk #####################' check_disk echo '################### check io used ###################' check_io echo '################### check swap used #################' check_swap echo '################### check dmesg #####################' check_dmesg echo '################### check inode #####################' check_inode echo '################### check disk used #################' check_df
保存為/system/check_linux.sh
相關(guān)文章
Shell特殊變量(Shell $#、$*、$@、$?、$$)的使用
這篇文章主要介紹了Shell特殊變量(Shell $#、$*、$@、$?、$$)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03shell 腳本中獲取命令的輸出的實(shí)現(xiàn)示例
本文主要介紹了shell 腳本中獲取命令的輸出的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Shell腳本創(chuàng)建指定大小文件的測試數(shù)據(jù)
這篇文章主要介紹了Shell腳本創(chuàng)建指定大小文件的測試數(shù)據(jù),本文同時給出使用DD命令和Shell腳本解決這個需求,給出的腳本功能更加強(qiáng)大,需要的朋友可以參考下2015-03-03Shell調(diào)用curl實(shí)現(xiàn)IP歸屬地查詢的腳本
這篇文章主要介紹了Shell調(diào)用curl實(shí)現(xiàn)IP歸屬地查詢,文中給大家提到了查詢IP歸屬地的shell腳本,在批量查找數(shù)據(jù)的時候經(jīng)常會遇到,今天給大家分享出來,需要的朋友可以參考下2021-07-07詳解Shell編程之if語句實(shí)戰(zhàn)(小結(jié))
本篇文章主要介紹了詳解Shell編程之if語句實(shí)戰(zhàn)(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02