Linux很多比较好用的shell脚本程序用于监控服务器状态查询一些信
Linux很多比较好用的shell脚本程序用于监控服务器状态查询一些信息
查看linux系统信息
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-03-16
#FileName: systeminfo.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
export Start_Color="\e[1;32m"
export End_Color="\e[0m"
Ipaddr=`ifconfig | grep -Eo --color=auto "(\
-9]{2}|2[0-4][0-9]|25[0-5])\>\.){3}\
[0-4][0-9]|25[0-5])\>"| head -1`
Version=`cut -d" " -f4 /etc/redhat-release`
KerneVer=`uname -r`
Cpu_Info=`lscpu | grep "Model name:"|tr -s " " |cut -d: -f2`
Mem_Info=`free -mh|tr -s " "|cut -d" " -f2 |head -2| tail -1`
HD_Info=`lsblk | grep "^sd\{1,\}"| tr -s " "|cut -d" " -f1,4`
echo -e "The System Hostname is : $Start_Color `hostname` $End_Color"
echo -e "The System IP ADDER is : $Start_Color $Ipaddr $End_Color"
echo -e "The System Version is : $Start_Color $Version $End_Color"
echo -e "The System kerneVer is : $Start_Color $KerneVer $End_Color"
echo -e "The System Cpu_Info is :$Start_Color $Cpu_Info $End_Color"
echo -e "The System Mem_Info is : $Start_Color $Mem_Info $End_Color"
echo -e "The System HD_Info is : $Start_Color \n$HD_Info $End_Color"
disk.sh
unset Start_Color
unset End_Color
unset Ipaddr
unset Version
unset KerneVer
unset Cpu_Info
unset Mem_Info
unset HD_Info
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-03-20
#FileName: argsum.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
if [ $# -lt 1 ];then
echo "At least one argument(must be file)."
exit 10
elif [ -e $1 ] && [ -f $1 ];then
Space_Line=`grep "^$" $1 | wc -l`
echo "the $1 has ${Space_Line} space line"
fi
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-03-20
#FileName: checkdisk.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
Use_disk=`df |grep "^/dev/sd"| grep -Eo "\%"|tr -d %|sort -nr
|head -1`
[ $Use_disk -ge 17 ] && wall "space will full"
ping -c1 -W1 $1 &> /dev/null && echo "$1 is up" || echo "$1 is donw"
显示两个文件空行的和
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-03-19
#FileName: sumspace.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
if [ $# -lt 2 ];then
echo "Usage: sumspace.sh /PATH/TO/SOMEFILE /PATH/TO/SOMEFILE"
exit 10
elif [ $# -gt 2 ];then
echo "two argument only."
exit 20
else
FILE1=`grep "^$" $1|wc -l`
FILE2=`grep "^$" $2|wc -l`
SPACE_SUM=$[$FILE1+$FILE2]
echo "The Space SUM is: $SPACE_SUM."
fi
选择YES or NO
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-03-20
#FileName: yesorno.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
read -p "Do you agree?(yes or NO):" ANS
ans1=`echo $ANS | grep -E -i "^[y]([e][s])?$"|tr [:upper:] [:lower:]`
ans2=`echo $ANS | grep -E -i "^[n][o]$"|tr [:upper:] [:lower:]`
if [ "$ans1" = "yes" ];then
echo "It's ok"
exit
elif [ "$ans2" = "no" ];then
echo "It's not ok"
exit
else
echo "Wrong input"
fi
DIR=/data/script36/workdir/
cd $DIR
END=log
BC="\e[1;"
EC="\e[0m"
for file in *;do
file_name=`echo $file | sed -r '[email protected]^(.*)\..*[email protected]\[email protected]'`
mv $file ${file_name}.$END
echo -e "${BC}31m $file $EC is rename ${BC}35m ${file_name}.$END $EC success"
done
监控http服务运行状况(while)
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-04-09
#FileName: monitor.httpd_while.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
SLEEPING=20
SERVICE=httpd
LOG=/var/log/monitor_$SERVICE.log
while true;do
if killall -0 $SERVICE &> /dev/null;then
true
else
systemctl restart $SERVICE
echo "AT `date +'%F %T'` $SERVICE restart" | tee -a $LOG | mail -s "warning $SERVICE" root
fi
sleep $SLEEPING
done
根据访问lastb,禁止10次以上ip访问(while)
#!/bin/bash
#
#---------------------------------------
#Author: MartinHe
#Date: 2019-04-10
#FileName: denyip_while.sh
#URL: https://blog.51cto.com/5033330
#Desciption: The test Scirpt
#Copyright(c) 2019 All rights reserved
#---------------------------------------
LOG=cat /data/script36/ss.log | sed -nr '/^ESTAB/[email protected]* ([0-9.]+):[0-9]+.* ([0-9.]+):[0-9]+.*[email protected]\[email protected]'| sort | uniq -c
cat $LOG | while read num ip;do
if [ $num -gt 10 ];then
iptables -A INPUT -s $ip -j REJECT
fi
done
转载 https://blog.51cto.com/5033330/2367417
Linux很多比较好用的shell脚本程序用于监控服务器状态查询一些信相关教程