【Linux】【Shell】ngnix和mysql应用状态分析

研究学习 Linux Shell 的系列文章.

这篇文章主要讲使用 Shell 脚本监控 nginx 和 mysql 的运行情况.

1. 内容介绍

主控脚本monoitor_man.sh调用check_server.sh的两个功能:

  1. 监控 nginx 的运行情况.
  2. 监控 mysql 的运行情况.

通过操作系统命令监控

  • 通过网络状态监控:pingnslookupnm-tooltraceroutedigtelnetnccurl
  • 通过进程状态监控:psnetstatpgrep

使用应用客户端、工具监控

  • 应用客户端:mysqlabmongophpjstack
  • 第三方工具包:nginxstatusnagios-libexec

服务端接口支持

  1. nginx:http_stub_status_module
  2. nutcracker 监控集群 (redis、memcache) 状态
  3. Mongodb
  4. etc…

2. 相关知识

2.1 监控 nginx

在编译安装 nginx 时使用 --with-http_stub_status_module 选项开启 http_stub_status_module 模块,修改配置文件后就能通过访问http://xxx.xxx.xxx.xxx/nginx_status (xxx.xxx.xxx.xxx 是 nginx 服务器 IP 地址) 查看 nginx 的性能统计信息.

使用 curl -s 命令也可查看:

$ curl -s 127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 330 330 321 
Reading: 0 Writing: 1 Waiting: 0
  1. Active connections:活动连接数
  2. Server accepts handled requests:(三个数字分别代表)总共处理的连接数,成功握手的连接数量,处理的请求数(正常情况下握手和连接数是相等的,表示没有丢失)
  3. Reading:Nginx 读取到客户端的Header信息数
  4. Writing:Nginx 返回给客户端的Header信息数
  5. Waiting:开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接(在nginx开启了keep-alive,也就是长连接的情况下,客户端跟服务端建立了连接但是没有读写操作的空闲状态)

使用 -w {http_code} 获取页面返回码:

$ curl -m 5 -s -w %{
    
    http_code} 127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 332 332 323 
Reading: 0 Writing: 1 Waiting: 0 
200

使用 -o /dev/null 去除页面内容仅取得状态码:

$ curl -m 5 -s -w %{
    
    http_code} 127..1/nginx_status -o /dev/null
200

如果状态码为000或者大于500则表示有误.

2.2 监控 mysql

nc 命令是 netcat 的缩写,是一个功能强大的网络调试和探测工具.

nc [option] [destination] [port]
  • -g <网关> 设置路由器跃程通信网关,最多可设置8个
  • -G <指向器数目> 设置来源路由指向器,其数值为4的倍数
  • -h 在线帮助
  • -i <延迟秒数> 设置时间间隔,以便传送信息及扫描通信端口
  • -l 使用监听模式,管控传入的资料
  • -n 直接使用IP地址,而不通过域名服务器
  • -o <输出文件> 指定文件名称,把往来传输的数据以16进制字码倾倒成该文件保存
  • -p <通信端口> 设置本地主机使用的通信端口
  • -r 乱数指定本地与远端主机的通信端口
  • -s <来源位址> 设置本地主机送出数据包的IP地址
  • -u 使用UDP传输协议
  • -v 显示指令执行过程
  • -w <超时秒数> 设置等待连线的时间
  • -z 使用0输入/输出模式,只在扫描通信端口时使用

mysql会默认使用3306端口,使用nc命令扫描3306,如果正常,则返回值为0;如果异常,则返回值为1:

$ nc -z -w2 127.0.0.1 3306
$ echo $?
0

关闭 mysql 服务后再次扫描

$ sudo service mysqld stop
$ nc -z -w2 127.0.0.1 3306
$ echo $?
1

如果使用 -z 参数仍有输出,使用 -o 输出到 /dev/null

$ nc -z -w2 127.0.0.1 3306 -o /dev/null

3. 脚本实现

#!/bin/bash
#Program function: To check nginx and mysql's runing status.
Resettem=$(tput sgr0)
Nginx_status='http://172.17.0.12/nginx_status'
Mysql_Slave='172.17.0.12'

Check_Nginx_Server()
{
    
    
    Status_code=$(curl -m 5 -s -w %{
     
     http_code} ${
     
     Nginx_status} -o /dev/null)

    if [ ${Status_code} -eq 000 -o ${Status_code} -ge 500 ]
        then
            echo -e '\e[0;1;35m'"check nginx http server erroe! Response status code is "'\e[0m' ${Status_code}
        else
            Status_content=$(curl -s ${
     
     Nginx_status})
	    echo -e '\e[0;1;35m'"check nginx http server ok! \n"'\e[0m' ${Status_content}
    fi
}

Check_Mysql_Server()
{
    
    
    nc -z -w2 ${Mysql_Slave} 3306 $> /dev/null
    if [ $? -eq 0 ]; then
        echo -e '\e[0;1;35m'"Connect ${Mysql_Slave} OK!"'\e[0m'
    fi
}

Check_Nginx_Server
Check_Mysql_Server

猜你喜欢

转载自blog.csdn.net/RadiantJeral/article/details/112499833