分析Linux系統以及狀態的監視,提取Linux操作系統信息,獲取操作系統運行狀態,分析應用狀態nginx,mysql,應用日志分析。
- VIM編輯器
- 設置
永久設置:修改vimrc文件
臨時關閉高亮模式:syntax off
永久開啟高亮模式,針對所有用戶/etc/vimrcsyntax on
針對某個用戶下,vim ~/.vimrc
- 語法高亮
syntax on
- 顯示行號
set number
set nonumber
- 自動縮進
set autoindent
set cindent
- 自動加入文件頭
- Shell編程高級知識
- Shell高亮顯示
echo -e是處理特殊字符
echo -e 終端顏色+顯示內容+結束后顏色
1為設置終端顏色,30m為灰色,0m為黑色
echo -e "\e[1;30m Jeson say Hi~ \e[1;0m"
tput sgr0
初始化終端為正常屏幕,先設置文字顏色,再初始化終端
echo -e "\e[1;30m" "Jeson say Hi~" $(tput sgr0)
- Shell關聯數組
普通數組:只能用整數作為數組索引
關聯數組:可以使用字符串作為數組索引
declare -A ass_array1
聲明關聯數組變量
ass_array1[index1]=pear
賦值
- 主控腳本
#!/bin/bash
#經常使用的命令
resettem=$(tput sgr0)
declare -A ssharray
#關聯數組
#declare -A聲明數組變量-x變成環境變量-i定義為整數
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
#使用模式匹配,雙方括號,非數字開頭+表示一次或者多次
if [[ ! ${execshell} = ~^[0-9]+ ]];then
exit 0
fi
/bin/bash ./${ssharray[$execshell]}
done```
3. Shell對于系統資源提取及運行狀態分析
提取操作系統信息,內核,系統版本,網絡地址,分析系統的運行狀態,CPU負載,內存及磁盤使用率。
操作系統使用內存和應用程序使用內存
`free -m`
系統使用內存=Total-Free
應用使用內存=Total-(Free+Cached+Buffers)
內存中cache和buffer區別
緩存區,cache用于打開的文件,最少使用原則
buffer,分緩存主要用于目錄項,inode等文件系統,先進先出策略
!/bin/bash
clear
if [ $# -eq 0 ];then
#uname命令打印當前系統相關信息
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內網ip
internelip=$(hostname -I)
echo -e '\E[32m'"check internal ip" $reset_terminal $internelip
#check external ip外網ip返回出口ip,curl -s靜默發送請求
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```
根分區下/proc 讀取文件分析系統狀態
應用程序使用內存awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}' /proc/meminfo
操作系統使用內存awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
- 操作系統負載
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}'
- 操作系統磁盤容量
df -hP | grep -vE 'Filesystem|tmpfs' | awk '{print $1 " " $5}
- Shell分析程序或者應用狀態
利用操作系統命令ping,nslookup(檢查dns),nm-tool,traceroute,dig,telnet,nc,curl(檢查http響應是否成功)
監控進程ps,netstat,pgrep(獲得正在被調度的進程的相關信息)
客戶端命令mysql,ab,mongo,php,jstack,nginxstatus,nagios-libexec
- nginx
curl -m 5 設置最大傳輸時間 -s 靜默模式 -w %{http_code}顯示http返回狀態碼 -o /dev/null 輸出 - MySQL
監控MySQL主從復制狀態,通過IO/Thread同步
shell,connect->show slave status->return data
搭建主從復制環境(MySQL相關課程)。基于mysql客戶端狀態,獲取主從復制狀態,show slave status\G;
格式化輸出,每行輸出,Slave_IO_Running
IO線程是否有鏈接到主服務器上,Seconds_Behind_Master
主從同步的延時時間
nc
用于設置路由器 -z使用0輸入/輸出模式,只在掃描通信端口時使用,-w2設置等待連線的時間,mysql默認端口3306
給監控單獨配置一個用戶,從服務器上。
mysql -urep -prep -h10.156.11.233 -e "show slave status\G"
也就是首先判斷端口是否鏈接,然后登錄客戶端,然后執行相應的管理語句,返回狀態信息進行判斷。
整個檢測判斷分析代碼:
#!/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
#成功的話重新發起請求,獲取數據
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
- 常見系統日志文件
- 系統日志
/var/log/messages 系統主日志文件
/var/log/secure 認證,安全日志文件
/var/log/dmesg 和系統啟動相關的日志文件
- 應用服務
access.log nginx訪問日志
mysqld.log mysql運行日志
xferlog 訪問ftp服務器相關
- 程序腳本
開發語言的日志:C,C++,Java,php
框架日志:Django,MVC,Servlet
腳本語言,shell,Python
log_format,日志的格式
`cat /etc/nginx/nginx.conf | grep -A 2 log_format`
- http狀態碼
- 1** 信息,服務器收到請求,需要請求者繼續執行操作
- 2** 成功,操作被成功接收并處理
- 3** 重定向,需要進一步操作以完成請求
- 4** 客戶端錯誤,請求包含語法錯誤或無法完成請求
- 5** 服務器錯誤,服務器在處理請求的過程中發生了錯誤
`cat /opt/logs/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | more`
uniq命令刪除文件中的重復行,-c在輸出行前面加上每行在輸入文件中出現的次數
sort排序,-r逆序,-n以數值排序,而不是字符
數據清洗,按照每一條數據特征點進行匹配,grep -i不區分大小寫,-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(){
#關聯數組
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