分析Linux系統(tǒng)以及狀態(tài)的監(jiān)視,提取Linux操作系統(tǒng)信息,獲取操作系統(tǒng)運行狀態(tài),分析應(yīng)用狀態(tài)nginx,mysql,應(yīng)用日志分析。
- VIM編輯器
- 設(shè)置
永久設(shè)置:修改vimrc文件
臨時關(guān)閉高亮模式:syntax off
永久開啟高亮模式,針對所有用戶/etc/vimrcsyntax on針對某個用戶下,vim ~/.vimrc - 語法高亮
syntax on - 顯示行號
set numberset nonumber - 自動縮進
set autoindentset cindent - 自動加入文件頭
- Shell編程高級知識
- Shell高亮顯示
echo -e是處理特殊字符
echo -e 終端顏色+顯示內(nèi)容+結(jié)束后顏色
1為設(shè)置終端顏色,30m為灰色,0m為黑色
echo -e "\e[1;30m Jeson say Hi~ \e[1;0m"
tput sgr0初始化終端為正常屏幕,先設(shè)置文字顏色,再初始化終端
echo -e "\e[1;30m" "Jeson say Hi~" $(tput sgr0) - Shell關(guān)聯(lián)數(shù)組
普通數(shù)組:只能用整數(shù)作為數(shù)組索引
關(guān)聯(lián)數(shù)組:可以使用字符串作為數(shù)組索引
declare -A ass_array1聲明關(guān)聯(lián)數(shù)組變量
ass_array1[index1]=pear賦值
- 主控腳本
#!/bin/bash
#經(jīng)常使用的命令
resettem=$(tput sgr0)
declare -A ssharray
#關(guān)聯(lián)數(shù)組
#declare -A聲明數(shù)組變量-x變成環(huán)境變量-i定義為整數(shù)
i=0
numbers=""
#ls -I 排除后面的字符串ignore
for script_file in `ls -I "monitor.sh" ./`
do
echo -e '\e[35m'"The script:" ${i} '==>' ${resettem} ${script_file}
grep -E "^\#Program function" ${script_file}
ssharray[$i]=${script_file}
#echo ${ssharray[$i]}
numbers="${numbers} | ${i}"
i=$((i+1))
done
while true
do
read -p "Please input one number in [ ${numbers} ]:" execshell
#使用模式匹配,雙方括號,非數(shù)字開頭+表示一次或者多次
if [[ ! ${execshell} = ~^[0-9]+ ]];then
exit 0
fi
/bin/bash ./${ssharray[$execshell]}
done```
3. Shell對于系統(tǒng)資源提取及運行狀態(tài)分析
提取操作系統(tǒng)信息,內(nèi)核,系統(tǒng)版本,網(wǎng)絡(luò)地址,分析系統(tǒng)的運行狀態(tài),CPU負載,內(nèi)存及磁盤使用率。
操作系統(tǒng)使用內(nèi)存和應(yīng)用程序使用內(nèi)存
`free -m`
系統(tǒng)使用內(nèi)存=Total-Free
應(yīng)用使用內(nèi)存=Total-(Free+Cached+Buffers)
內(nèi)存中cache和buffer區(qū)別
緩存區(qū),cache用于打開的文件,最少使用原則
buffer,分緩存主要用于目錄項,inode等文件系統(tǒng),先進先出策略
!/bin/bash
clear
if [ $# -eq 0 ];then
#uname命令打印當(dāng)前系統(tǒng)相關(guān)信息
reset_terminal=$(tput sgr0)
#check os type
os=$(uname -o)
echo -e '\e[1;32m'" operating system type: " $reset_terminal $os
#check os release version and name
os_name=$(cat /etc/issue|grep -e "Server")
echo -e '\E[32m'"check os release version and name:" $reset_terminal $os_name
#check architectureCPU指令集
architecture=$(uname -m)
echo -e '\E[32m'"check architecture" $reset_terminal $architecture
#check kernel release
kernelrelease=$(uname -r)
echo -e '\E[32m'"check kernel release" $reset_terminal $kernelrelease
#check hostname
hostname=$(uname -n)
#hostname=$(set|grep HOSTNAME)
#hostname=$(echo HOSTNAME)
echo -e '\E[32m'"check hostname" $reset_terminal $hostname
#check internal ip內(nèi)網(wǎng)ip
internelip=$(hostname -I)
echo -e '\E[32m'"check internal ip" $reset_terminal $internelip
#check external ip外網(wǎng)ip返回出口ip,curl -s靜默發(fā)送請求
externalip=$(curl -s http://ipecho.net/plain)
echo -e '\E[32m'"check external ip" $reset_terminal $externalip
#check dns
dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $NF}')
echo -e '\E[32m'"check dns" $reset_terminal $dns
#check if connected to internet or not
#比用$?要方便多了
ping -c 2 imooc.com &>/dev/null && echo "Internet:connected" || echo "Internet:disconnected"
#check logged in users
who>/tmp/who
echo -e '\E[32m'"logged in Users" $reset_terminal && cat /tmp/who
rm -f /tmp/who
fi```
根分區(qū)下/proc 讀取文件分析系統(tǒng)狀態(tài)
應(yīng)用程序使用內(nèi)存awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}' /proc/meminfo
操作系統(tǒng)使用內(nèi)存awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
- 操作系統(tǒng)負載
load=1.0 滿負荷 =0.5 沒有滿負荷 =1.7
top -n 1 -b-n表示讀取一次,-b表示讀取信息更加全面
top -n 1 -b | grep "load average"|awk '{print $12 $13 $14}' - 操作系統(tǒng)磁盤容量
df -hP | grep -vE 'Filesystem|tmpfs' | awk '{print $1 " " $5}
- Shell分析程序或者應(yīng)用狀態(tài)
利用操作系統(tǒng)命令ping,nslookup(檢查dns),nm-tool,traceroute,dig,telnet,nc,curl(檢查http響應(yīng)是否成功)
監(jiān)控進程ps,netstat,pgrep(獲得正在被調(diào)度的進程的相關(guān)信息)
客戶端命令mysql,ab,mongo,php,jstack,nginxstatus,nagios-libexec
- nginx
curl -m 5 設(shè)置最大傳輸時間 -s 靜默模式 -w %{http_code}顯示http返回狀態(tài)碼 -o /dev/null 輸出 - MySQL
監(jiān)控MySQL主從復(fù)制狀態(tài),通過IO/Thread同步
shell,connect->show slave status->return data
搭建主從復(fù)制環(huán)境(MySQL相關(guān)課程)?;趍ysql客戶端狀態(tài),獲取主從復(fù)制狀態(tài),show slave status\G;格式化輸出,每行輸出,Slave_IO_RunningIO線程是否有鏈接到主服務(wù)器上,Seconds_Behind_Master主從同步的延時時間
nc用于設(shè)置路由器 -z使用0輸入/輸出模式,只在掃描通信端口時使用,-w2設(shè)置等待連線的時間,mysql默認端口3306
給監(jiān)控單獨配置一個用戶,從服務(wù)器上。
mysql -urep -prep -h10.156.11.233 -e "show slave status\G"
也就是首先判斷端口是否鏈接,然后登錄客戶端,然后執(zhí)行相應(yīng)的管理語句,返回狀態(tài)信息進行判斷。
整個檢測判斷分析代碼:
#!/bin/bash
NginxServer='http://10.156.11.173/nginx_status'
Check_Nginx_Server(){
Status=$(curl -m 5 -s -w %{http_code} ${NginxServer} -o /dev/null)
if[ $Status -eq 000 -o $Status -ge 500 ];then
echo -e 'check http server error! Response status code is ' $Status
else
#成功的話重新發(fā)起請求,獲取數(shù)據(jù)
Http_content=$(curl -s $(NginxServer))
echo "check http server ok! Response status code is " $Http_content
fi
}
Check_Nginx_Server
Mysql_Slave_Server='10.156.11.233'
Mysql_User='rep'
Mysql_Pass='rep'
Check_Mysql_Server(){
#判斷端口是否鏈接
nc -z -w2 ${Mysql_Slave_Server} 3306 &>/dev/null
if [ $? -eq 0 ];then
echo "Connect ${Mysql_Slave_Server} ok!"
mysql -u${Mysql_User} -p${Mysql_Pass} -h${Mysql_Slave_Server} -e "show slave status\G" | grep "Slave_IO_Running" | awk '{if($2!="yes"){print "slave IO thread not running";exit 1}}'
if [ $? -eq 0 ];then
mysql -u${Mysql_User} -p${Mysql_Pass} -h${Mysql_Slave_Server} -e "show slave statys\G" | grep "Seconds_Behind_Master"
else
echo "Connect Mysql server not succeeded"
fi
}
Check_Mysql_Server```
5. Shell日志提取分析
nginx日志分析,webserver
- 常見系統(tǒng)日志文件
- 系統(tǒng)日志
/var/log/messages 系統(tǒng)主日志文件
/var/log/secure 認證,安全日志文件
/var/log/dmesg 和系統(tǒng)啟動相關(guān)的日志文件
- 應(yīng)用服務(wù)
access.log nginx訪問日志
mysqld.log mysql運行日志
xferlog 訪問ftp服務(wù)器相關(guān)
- 程序腳本
開發(fā)語言的日志:C,C++,Java,php
框架日志:Django,MVC,Servlet
腳本語言,shell,Python
log_format,日志的格式
`cat /etc/nginx/nginx.conf | grep -A 2 log_format`
- http狀態(tài)碼
- 1** 信息,服務(wù)器收到請求,需要請求者繼續(xù)執(zhí)行操作
- 2** 成功,操作被成功接收并處理
- 3** 重定向,需要進一步操作以完成請求
- 4** 客戶端錯誤,請求包含語法錯誤或無法完成請求
- 5** 服務(wù)器錯誤,服務(wù)器在處理請求的過程中發(fā)生了錯誤
`cat /opt/logs/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | more`
uniq命令刪除文件中的重復(fù)行,-c在輸出行前面加上每行在輸入文件中出現(xiàn)的次數(shù)
sort排序,-r逆序,-n以數(shù)值排序,而不是字符
數(shù)據(jù)清洗,按照每一條數(shù)據(jù)特征點進行匹配,grep -i不區(qū)分大小寫,-o精確輸出,-E正則,使用原字符,`cat /opt/logs/nginx/access.log | grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" `
!/bin/bash
logfile_path='/opt/logs/nginx/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 100-200 ${http_status_codes[0]}
echo 200-300 ${http_status_codes[1]}
echo 300-400 ${http_status_codes[2]}
echo 400-500 ${http_status_codes[3]}
echo 500+ ${http_status_codes[4]}
echo total ${http_status_codes[5]}
}
Check_http_status
Check_http_code(){
#關(guān)聯(lián)數(shù)組
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 404 $http_code[0]
echo 403 $http_code[1]
echo total $http_code[2]
}
Check_http_code