Shell典型应用
主控脚本
vim编辑器基本设置
临时性质的设置 直接在未行设置
永久性质的设置 在~/.vimrc
文件中配置
syntax on
set number
set tabstop=4
set expandtab
%ret!
set autoindent
set shiftwidth=4
syntax on
开启语法高亮
set number
显示行号
set expandtab
用space替代tab的输入
set autoindent
自动缩进
set shiftwidth=4
设置缩进的空格数
shell高亮显示
基本格式: echo -e 终端颜色 + 显示内容 + 结束后的颜色
echo -e "\e[1;30m 高亮显示测试 \e[1;0m"
shell中的关联数组
普通数组: 只能使用整数作为数组索引
关联数组: 可以使用字符串作为数组索引
定义关联数组: declare -A ass_array1
主控脚本
# monitor_man.sh
#!/bin/bash
resettem=$(tput sgr0)
declare -A ssharray
i=0
numbers=""
for script_file in `ls -I "monitor_man.sh" ./`
do
echo -e '\e[1;35m' "The Script: " ${i} '==>' ${resettem} ${script_file}
ssharray[$i]=${script_file}
echo ${ssharray[$i]}
numbers="${numbers} | ${i}"
i=$(( i+1 ))
done
while true
do
read -p "Please input a number [ ${numbers} ]: " execshell
if [[ ! ${execshell} =~ ^[0-9]+ ]]; then
exit 0
fi
/bin/sh ./${ssharray[${execshell}]}
done
系统信息及运行状态获取脚本
system_monitor.sh
#!/bin/bash
clear
echo -e
if [ $# -eq 0 ]; then
# Define Variable reset_terminal
reset_terminal=$(tput sgr0)
colour='\e[1;32m'
ping_url=baidu.com
# 系统信息-------------------------------------------------------
# 系统与内核相关信息
system_uname=$(uname -a)
echo -e $colour " 系统与内核:" $reset_terminal $system_uname
# 当前Linux系统的发行版本
os_name=$(cat /etc/issue | sed 's/\\//g')
echo -e $colour " 当前Linux系统的发行版本:" $reset_terminal $os_name
echo -e
# 系统资源
echo -e $colour " 系统资源(vmstat 1 3):" $reset_terminal
vmstat 1 3
echo -e
# 当前登录用户
# echo -e $colour " 当前登录用户:" $reset_terminal
# who
# 磁盘-------------------------------------------------------
# 磁盘使用量
diskavege=$(df -P | grep -vE 'Filesystem|tmpfs|文件系统' | awk '{print $1 " " $5 ",\t" }')
echo -e $colour " 磁盘使用量(df -P):" $reset_terminal $diskavege
# 磁盘IO
echo -e $colour " 磁盘IO情况(iostat):" $reset_terminal
iostat
echo -e
# 内存-------------------------------------------------------
mem_usages=( $(awk '
/MemTotal/ { total = $2 }
/MemFree/ { free = $2 }
/^Cached/ { cached = $2 }
/Buffers/ { buffers = $2 }
END {
allUsage = total - free;
appUsage = allUsage - (cached + buffers);
total = total / 1024;
total_s = total < 1024 ? (total)"M" : (total / 1024)"G";
allUsage = allUsage / 1024;
allUsage_s = allUsage < 1024 ? (allUsage)"M" : (allUsage / 1024)"G";
appUsage = appUsage / 1024;
appUsage_s = appUsage < 1024 ? (appUsage)"M" : (appUsage / 1024)"G";
print total_s;
print allUsage_s;
all_percent = (allUsage / total) * 100;
print all_percent"%";
print appUsage_s;
app_percent = (appUsage / total) * 100;
print app_percent"%";
}
' /proc/meminfo
))
echo -e $colour " 总内存(/proc/meminfo):" $reset_terminal ${mem_usages[0]}
echo -e $colour " 总使用内存, 总使用占比:" $reset_terminal ${mem_usages[1]}", "${mem_usages[2]}
echo -e $colour " 应用使用内存, 应用使用占比:" $reset_terminal ${mem_usages[3]}", "${mem_usages[4]} "\t(减去Cached与Buffers)"
echo -e
# 负载-------------------------------------------------------
loadaverge=$(uptime)
echo -e $colour " 平均负载(uptime):" $reset_terminal $loadaverge
echo -e
# 网络-------------------------------------------------------
# 内网IP
# internalip=$(hostname -I)
# echo -e $colour " 内网IP:" $reset_terminal $internalip
# 公网IP
# externnalip=$(curl -s http://ipecho.net/plain)
# echo -e $colour " 公网IP:" $reset_terminal $externnalip
# DNS
# nameservers=$( cat /etc/resolv.conf | grep -E "\<nameserver[ ]+" | awk '{print $NF}' )
# echo -e $colour " DNS:" $reset_terminal $nameservers
# 判断网络是否通畅 (公网IP)
# netstatus=$(ping -c 2 $ping_url &> /dev/null && echo " 通畅 " || echo " ping不通 ")
echo -e $colour " 网络状态(ping $ping_url): " $reset_terminal $netstatus
echo -e
# 网络连接状态统计
echo -e $colour " 网络连接状态统计(netstat -n):" $reset_terminal
netstat -n | awk '
BEGIN { print "连接状态\t连接数" }
/^tcp/ { ++states[$NF] }
END {
for (key in states) {
print key, "\t", states[key];
}
}
'
echo -e
# tomcat-------------------------------------------------------
tomcat_home="/home/chencye/tomcat/apache-tomcat-7.0.68"
tomcat_port=8080
tomcat_url=http://192.168.1.104:8080
tomcat_pid=$(jps -lvm | grep $tomcat_home | awk '{print $1}')
# tomcat并发连接数
tomcatLinkNumber=$(netstat -na | grep ESTAB | grep $tomcat_port | wc -l)
echo -e $colour " tomcat并发连接数:" $reset_terminal $tomcatLinkNumber
echo -e
# tomcat是否通畅
tomcat_status_code=$(curl -m 5 -s -w %{http_code} $server -o /dev/null)
echo -e $colour " tomcat链接($tomcat_url)返回码:" $reset_terminal $tomcat_status_code
echo -e
# tomcat对应的jvm信息
echo -e $colour " tomcat对应的jvm信息(pid=$tomcat_pid):" $reset_terminal
jinfo $tomcat_pid &> /tmp/$tomcat_pid".jinfo"
jvminfos="java.version\b|java.vendor\b|java.vm.version\b|java.vm.vendor\b|java.vm.name\b|java.runtime.version\b|java.class.version\b|java.io.tmpdir\b|user.dir\b|user.name\b|user.home\b|user.language\b|file.encoding\b|catalina.home\b|catalina.base\b|VM\sFlags\b"
grep -P $jvminfos /tmp/$tomcat_pid".jinfo"
echo -e
rm -f /tmp/$tomcat_pid".jinfo"
# tomcat的GC情况
echo -e $colour " tomcat的GC情况:" $reset_terminal
jstat -gc $tomcat_pid 1000 5
echo -e
fi
应用运行状态监控脚本
网络命令: ping
、nslookup
、nm-tool
、tracertroute
、dig
、telnet
、nc
、curl
监控进程: ps
、netstat
、pgrep
应用客户端: mysql
、ab
、mongo
、php
、jstack
第三方工具包: nginxstatus
、nagios-libexec
check_server.sh
#!/bin/bash
reset_terminal=$(tput sgr0)
colour='\e[1;32m'
server=http://www.baidu.com
check_server()
{
status_code=$(curl -m 5 -s -w %{http_code} $server -o /dev/null)
if [ $status_code -eq 000 -o $status_code -ge 500 ]; then
echo -e $colour "服务器异常!返回码是: " $reset_terminal $status_code
fi
}
check_server
mysql_server=192.168.1.104
mysql_port=3306
# 对mysql新创建用户,只赋需要的权限即可
mysql_user=rep
mysql_passwd=rep
# grant replication slave, relication client on *.* to rep@'%' identified by 'rep'
check_mysql()
{
nc -z w2 $mysql_server $mysql_port &> /dev/null
if [ $? -eq 0 ]; then
mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_server} -e "show slave status\G" | grep "Slave_IO_Running" | awk '{if ($2!="Yes"){print "Slave thread not running!"; exit 1}}'
if [ $? -eq 0 ]; then
# 主从同步延时
mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_server} -e "show slave status\G" | grep "Seconds_Behind_Master"
fi
else
echo -e $colour "连接 $server_mysql 异常" $reset_terminal
fi
}
check_mysql
应用日志分析
系统日志
/var/log/messages
系统主日志文件
/var/log/secure
认证、安全
/var/log/dmesg
和系统启动相关
应用服务
access.log
nginx访问日志
mysqld.log
mysqld运行日志
xferlog
和访问FTP服务器相关
程序脚本
开发语言: c、c++、java、php
框架: Django、MVC、Servlet
脚本语言: Shell、Python
HTTP状态码
1**
信息,服务器收到请求,需要请求者继续执行操作
2**
成功,操作被成功并处理
3**
重定向,需要进一步的操作以完成请求
4**
客户端错误,请求包含语法错误或无法完成请求
5**
服务器请求,
脚本
check_http_log.sh
#!/bin/bash
reset_terminal=$(tput sgr0)
colour='\e[1;32m'
logfile='/home/chencye/empty/access.log'
check_http_status()
{
http_statu_codes=($( cat $logfile | grep -ioP "HTTP/1\.[01]\"\s+\d{3}\b" | 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 $colour " 状态[100+]的数量:" $reset_terminal ${http_statu_codes[0]}
echo -e $colour " 状态[200+]的数量:" $reset_terminal ${http_statu_codes[1]}
echo -e $colour " 状态[300+]的数量:" $reset_terminal ${http_statu_codes[2]}
echo -e $colour " 状态[400+]的数量:" $reset_terminal ${http_statu_codes[3]}
echo -e $colour " 状态[500+]的数量:" $reset_terminal ${http_statu_codes[4]}
echo -e $colour " 所有状态的数量:" $reset_terminal ${http_statu_codes[5]}
}
check_http_code()
{
http_codes=($( cat $logfile | grep -ioP "HTTP/1\.[01]\"\s+\d{3}\b" | awk -v total=0 -F "[ ]+" '
{
if ($2!="") {
code[$2]++;
total++
} else {
exit
}
} END {
print code[404]?code[404]:0, code[500]?code[500]:0, total
}
'
))
echo -e $colour " 404状态的数量:" $reset_terminal ${http_codes[0]}
echo -e $colour " 500状态的数量:" $reset_terminal ${http_codes[1]}
echo -e $colour " 总数量:" $reset_terminal ${http_codes[2]}
}
check_http_status
check_http_code