shell脚步源代码:
[liuxiao@aliyun_ECS imooc_shell]$ ls check_http_log.sh check_nginxserver.sh master_control.sh system_info.sh
master_control.sh:为主控脚步,其它脚步由此脚本调用
##############################################
# File Name: master_control.sh
# Author: liuxiao
# Mail: liuxiao255@qq.com
# Created Time: 2016-08-16 14:55
##############################################
#!/bin/bash
declare -A ssharray
i=1
numbers="0"
for script_file in `ls -I "master_control.sh" ./`
do
echo -e "\e[1;34mThe Script: ${i} ==> \e[0m""\e[1;35m${script_file}\e[0m"
grep -E "^\#Program function" ${script_file}
ssharray[$i]=${script_file}
numbers="${numbers} | ${i}"
i=$((i+1))
done
while true
do
read -p "Please input a number,the ‘0‘ is exit, [ ${numbers} ]: " execshell
if [[ ! ${execshell} =~ ^[0-9]+ ]];then
exit 0
fi
if [ $execshell -eq 0 ];then
exit 0
else
/bin/bash ./${ssharray[$execshell]}
fi
donesystem_info.sh:显示系统的一些信息
##############################################
# File Name: system_info.sh
# Author: liuxiao
# Mail: liuxiao255@qq.com
# Created Time: 2016-08-16 19:30
##############################################
#!/bin/bash
#Program function:Display some system information.
clear
OS_bit=$(uname -m)
echo -e "\e[1;34mOS_bit ==> \e[1;32m$OS_bit\e[0m"
OS_release=$(lsb_release -a | grep Description | cut -f 2)
echo -e "\e[1;34mOS_release ==> \e[1;32m$OS_release\e[0m"
Kernel_release=$(uname -r)
echo -e "\e[1;34mKernel_release ==> \e[1;32m$Kernel_release\e[0m"
echo
#Network card info
Net_card_name=$(ip addr | grep inet | awk ‘{print $NF}‘)
Net_card_ip=$(ip addr | grep inet | awk ‘{print $2}‘ | cut -f 1 -d "/")
i=0;j=0
for name in $Net_card_name #将所有网卡的名称放入name_array数组中
do
name_array[i]=$name
i=$((i+1))
done
for ip in $Net_card_ip #将所有网卡的IP放入ip_array数组中
do
ip_array[j]=$ip
j=$((j+1))
done
i=0;j=0
for time in $Net_card_name #打印网卡名称和对应的IP,time为控制循环次数的变量
do
echo -e "\e[1;34m${name_array[i]} ==> \e[1;32m${ip_array[j]}\e[0m"
i=$((i+1));j=$((j+1))
done
echo
#Network status
ping -c 2 www.baidu.com &>/dev/null && echo -e "\e[1;34mNetwork connection is OK\e[0m" || echo -e "\e[1;34mNetwork connection is False\e[0m"
echo
#Logged Users
echo -e "\e[1;34mLogged Users\e[0m"
echo -e "\e[1;34m`who`\e[0m"
echo
#Mem_usages
system_mem_usage=$(awk ‘/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}‘ /proc/meminfo) #系统已经使用的内存
apps_mem_usage=$(awk ‘/MemTotal/{total=$2}/MemFree/{free=$2}/^Cache/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}‘ /proc/meminfo) #应用程序使用的内存
echo -e "\e[1;34mSystem_mem_usage ==> \e[1;32m${system_mem_usage}M\e[0m"
echo -e "\e[1;34mApps_mem_usage ==> \e[1;32m${apps_mem_usage}M\e[0m"
echo
#Load average
load_average=$(top -n 1 -b | grep "load average:" | awk ‘{print $12 $13 $14}‘)
echo -e "\e[1;34mLoad_average ==> \e[1;32m${load_average}\e[0m"
echo
#Disk info
disk_info=$(df -hP | grep -vE "Filesystem|tmpfs" | awk ‘{print $1 " " $5}‘)
echo -e "\e[1;34mdisk_info:\n\e[1;32m${disk_info}\e[0m"check_nginxserver.sh:检查Nginx状态
##############################################
# File Name: check_nginxserver.sh
# Author: liuxiao
# Mail: liuxiao255@qq.com
# Created Time: 2016-08-20 10:44
##############################################
#Program function:Check Nginxserver Status Code.
#!/bin/bash
Nginxserver=‘http://www.baidu.com‘
Check_Nginx_Server()
{
echo -e "\e[1;34mNginxserver = \e[1;32m${Nginxserver}\e[0m"
Status_code=`curl -m 5 -s -w %{http_code} ${Nginxserver} -o /dev/null`
echo -e "\e[1;34mStatus_code = \e[1;32m${Status_code}\e[0m"
if [ $Status_code -ne 200 ];then
echo -e "\e[1;34mThe HTTP Status Code is abnormal,Please check your Nginxserver\e[0m"
elif [ $Status_code -eq 200 ];then
echo -e "\e[1;34mThe Nginxserver is OK\e[0m"
fi
}
Check_Nginx_Servercheck_http_log.sh:检查http访问日志
##############################################
# File Name: chech_http_log.sh
# Author: liuxiao
# Mail: liuxiao255@qq.com
# Created Time: 2016-08-19 21:59
##############################################
#Program function:Nginx‘s log analysis
#!/bin/bash
Logfile_path=‘/home/wwwroot/index/log/access.log‘
Check_http_status()
{
Http_status_codes=(`cat $Logfile_path | grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" | awk -F‘[ ]+‘ ‘{
if($2>=100&&$2<200)
{i++}
else if($2>=200&&$2<300)
{j++}
else if($2>=300&&$2<400)
{k++}
else if($2>=400&&$2<500)
{n++}
else if($2>=500)
{p++}
}END{
print i?i:0,j?j:0,k?k:0,n?n:0,p?p:0,i+j+k+n+p
}‘
`)
echo -e "\e[1;34mThe number of http status[100+]: \e[1;32m${Http_status_codes[0]}\e[0m"
echo -e "\e[1;34mThe number of http status[200+]: \e[1;32m${Http_status_codes[1]}\e[0m"
echo -e "\e[1;34mThe number of http status[300+]: \e[1;32m${Http_status_codes[2]}\e[0m"
echo -e "\e[1;34mThe number of http status[400+]: \e[1;32m${Http_status_codes[3]}\e[0m"
echo -e "\e[1;34mThe number of http status[500+]: \e[1;32m${Http_status_codes[4]}\e[0m"
echo -e "\e[1;34mAll request numbers: \e[1;32m${Http_status_codes[5]}\e[0m"
}
Check_http_code()
{
Http_Code=(`cat $Logfile_path | grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" | awk -v total=0 -F ‘[ ]+‘ ‘{
if($2!="")
{code[$2]++;total++}
else
{exit}
}END{
print code[404]?code[404]:0,code[403]?code[403]:0,total
}‘`)
echo -e "\e[1;34mThe number of http status[404]: \e[1;32m${Http_Code[0]}\e[0m"
echo -e "\e[1;34mThe number of http status[403]: \e[1;32m${Http_Code[1]}\e[0m"
echo -e "\e[1;34mAll request numbers: \e[1;32m${Http_Code[2]}\e[0m"
}
Check_http_status
Check_http_code本文出自 “We_Need_To_Try” 博客,请务必保留此出处http://liuxiao255.blog.51cto.com/10551403/1840541
原文:http://liuxiao255.blog.51cto.com/10551403/1840541