继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Shell实战笔记

东夋壬
关注TA
已关注
手记 33
粉丝 6
获赞 29

Shell典型应用之主控脚本实现

场景:

  • 提取Linux操作系统信息
  • 获取操作系统运行状态
  • 分析应用状态
  • 应用日志分析

shell 高亮显示:echo -e 终端颜色 + 显示内容 + 结束后的颜色,echo -e '\033[1;36m This id a test! \033[1;0m',1 设置颜色,36m开始颜色,0m结束颜色,033格式转换等同于e

脚本结构:input -> 控制脚本(monitor_man.sh) -> 提取系统信息、应用运行分析、日志分析 -> input(循环)

关联属组:可以使用字符串作为属组索引,普通属组使用整数

  • 声明:declare -A ass_array1
  • 调用赋值:ass_array1[index1] = pear
#!/bin/bash
i=0
# reset system color
resettem=$(tput sgr0)
declare -A ssharray
numbers=""

for script_file in `ls -I monitor.sh ./`
  do
    echo -e "\e[1;36m The Script:" ${i} "===>" ${resettem} ${script_file}
    ssharray[$i]=${script_file}
    numbers="${numbers} | ${i}"
    i=$((i+1))
  done

 while true
  do
    read -p "Please input a number [ $numbers ]:" execshell
    echo $execshell
    if [[ ! $execshell =~ ^[0-2]+$ ]];then
      exit 0
    fi
    /usr/bin/sh ./${ssharray[$execshell]}
  done

# for file in $(ls ./)	
#  do 
#    echo $file
#  done

Shell典型应用之系统信息及运行状态获取

功能:

  • 提取操作系统信息(内核、版本信息、网络地址等)
  • 分析系统的运行状态(CPU负载、内存及磁盘使用率等)
#!/bin/bash
clear
if [[ $# -eq 0 ]]
  then
  reset_terminal=$(tput sgr0)

  # os type
  os=$(uname -o)
  echo -e '\E[32m'"Operating System Type :" $reset_terminal $os
  # os release version and name
  # os_name=$(cat /etc/issue | grep -e Server)
  os_name=$(cat /etc/system-release)
  echo -e '\E[32m'"Check OS Realse Version and Name :" $reset_terminal $os_name
  # architecture
  architecture=$(uname -m)
  echo -e '\E[32m'"Check Architecture :" $reset_terminal $architecture
  # kernel release
  kernelrelease=$(uname -r)
  echo -e '\E[32m'"Check Kernel Release :" $reset_terminal $kernelrelease
  # hostname
  hostname=$(uname -n)
  echo -e '\E[32m'"Hostname :" $reset_terminal $hostname
  # internal IP
  internalip=$(hostname -I)
  echo -e '\E[32m'"Check Internal IP :" $reset_terminal $internalip
  # external IP
  externalip=$(curl -s http://ipecho.net/plain)
  echo -e '\E[32m'"Check External Ip :" $reset_terminal $externalip
  # DNS
  nameservers=$(cat /etc/resolv.conf | grep -E "\<nameserver[ ]+" | awk '{print $NF}')
  echo -e '\E[32m'"Check DNS :" $reset_terminal $nameservers
  # connected to internet or not
  ping -c 2 www.baidu.com  &> /dev/null && echo "Internet: Connected" || echo "Internet: Disconnected"
  # logged in users
  who > /tmp/who
  echo -e '\E[32m'"Logged In Users :" $reset_terminal && cat /tmp/who
  rm -f /tmp/who
  
  ###############################################################
  system_mem_usages=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo)
  app_mem_usages=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}' /proc/meminfo)
  echo -e '\E[32m'"System memuserages " $reset_terminal $system_mem_usages
  echo -e '\E[32m'"Apps memuserages " $reset_terminal $app_mem_usages
  loadaverage=$(top -n 1 -b | grep "load average:" | awk '{print $12 $13 $14}') 
  echo -e '\E[32m'"Load averages :" $reset_terminal $loadaverage
  diskaverage=$(df -hP | grep -vE 'Filesystem|tmpfs' | awk '{print $1 " " $5}')
  echo -e '\E[32m'"Disk averages :" $reset_terminal $diskaverage
 fi

Shell典型应用之nginx和mysql应用状态分析

查看应用运行脚本:

  • 网络命令:ping nslookup nm-tool traceroute dig telnet nc curl
  • 监控进程:ps netstat pgrep
  • 应用自带命令:mysql ab mongo php jstack
  • 服务端接口支持:nginx - http_stub_status_module等
#!/bin/bash
Resettem=$(tput sgr0)
Nginxserver='http://www.baidu.com'

Mysql_Slave_Server='192.168.75.129'
Mysql_User='rep'
Mysql_Pass='imooc'

Check_Nginx_Server()
{
  Status_code=$(curl -m 5 -s -w %{http_code} ${Nginxserver} -o /dev/null)
  if [ $Status_code -eq 000 -o $Status_code -ge 500 ] ;then
    echo -e '\E[32m'"Check http server error! Response status code is" $Resettem $Status_code
  else
    Http_content=$(curl -s ${Nginxserver} -o /dev/null )
    echo -e '\E[32m'"Check http server ok! \n" $Resettem $Http_content
  fi
}
Check_Nginx_Server

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 threas not running!";exit 1}}'	    
    if [ $? -eq 0 ] ;then
      mysql -u${Mysql_User} -p${Mysql_Pass} -h${Mysql_Slave_Server} -e     "show slave status\G" | grep "Seconds_Behind_Master"
    fi
  else
    echo "Connect Mysql server not succeeded"
  fi
}
Check_Mysql_Server

Shell典型应用之应用日志分析

系统日志:

  • 系统主日志文件:/var/log/messages
  • 认证、安全:/var/log/secure
  • 和系统启动相关:/var/log/dmesg

应用服务:

  • nginx访问日志:access.log
  • mysqld运行日志:mysqld.log
  • 访问FTP 服务器相关:xferlog

程序脚本,自定义目录:

  • 开发语言:c、c++、java、php
  • 框架:Django、MVC、Servlet
  • 脚本语言:shell、python
#!/bin/bash
resettem=$(tput sgr0)
Logfile_path='/usr/local/nginx/logs/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[33m'"The number of http status [100+] :" ${resettem} ${Http_status_codes[0]}
  echo -e '\E[33m'"The number of http status [200+] :" ${resettem} ${Http_status_codes[1]}
  echo -e '\E[33m'"The number of http status [300+] :" ${resettem} ${Http_status_codes[2]}
  echo -e '\E[33m'"The number of http status [400+] :" ${resettem} ${Http_status_codes[3]}
  echo -e '\E[33m'"The number of http status [500+] :" ${resettem} ${Http_status_codes[4]}
}
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[33m'"The number of http status [404] :" ${resettem} ${Http_code[0]}
  echo -e '\E[33m'"The number of http status [403] :" ${resettem} ${Http_code[1]}
  echo -e '\E[33m'"All request numbers:" ${resettem} ${Http_code[2]}
}
Check_http_status
Check_http_code
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP