shell—服务监控

释放双眼,带上耳机,听听看~!

监测mysql数据库异常

文章目录

  • 一、端口监控

  • 1.服务器本地监控端口

    • 2.在远程服务器监控本地端口
  • 二、对服务进程或进程数进行监控

  • 三.开发监控mysql数据库的脚本

  • 四、httpd的监控脚本

一、端口监控

1.服务器本地监控端口

在服务器本地监控端口的命令有:netstat ss lsof
(1)netstat:


1
2
3
4
1netstat -antlpe | grep mysql
2netstat -antlpe | grep 3306 | awk -F "[ :]+" '{print $5}'
3
4
  • 对端口进程进行判断时,尽量先通过grep过滤端口和进程标记特殊字符串,然后结合wc -l(统计输出的行数)将结果转换成行数再进行比较,相对简单有效。而且会经过wc -l命令处理之后的结果一定是数字,这样再进行判断比较简单


1
2
3
1netstat -antlpe | grep mysql |wc -l
2
3

shell---服务监控
(2)ss:类似于netstat,参数选项可通用


1
2
3
1ss -antlpe | grep mysql | wc -l
2
3

shell---服务监控
(3)lsof:


1
2
3
4
1yum install lsof -y
2lsof -i tcp:3306 |wc -l
3
4

shell---服务监控

2.在远程服务器监控本地端口

在远程服务器监控本地端口的命令:nmap telnet nc

在远程主机:

(1)nmap:


1
2
3
4
5
6
1yum install telnet nmap nc -y
2nmap 172.25.254.236 -p 3306
3nmap 172.25.254.236 -p 3306 | grep open
4nmap 172.25.254.236 -p 3306 | grep open | wc -l
5
6

查看远程端口是否开通,过滤open关键字,结果返回1,说明3306端口是通的
shell---服务监控

(2)telnet:


1
2
3
4
5
1telnet 172.25.254.236 3306
2telnet 172.25.254.236 3306 | grep Connected
3telnet 172.25.254.236 3306 | grep Connected | wc -l
4
5

telnet是常用来监测远程服务器端口是否通畅的一个命令,在非交互式需要采用特殊写法。过滤的关键字为Connected,返回1,说明3306端口是通的
shell---服务监控

二、对服务进程或进程数进行监控

对服务进程或进程数进行监控(适合本地服务器)


1
2
3
4
5
1ps -ef | grep mysql |wc -l #此处把过滤的动作也当作了一个进程
2ps -ef | grep mysql |grep -v grep 
3ps -ef | grep mysql |grep -v grep | wc -l
4
5

shell---服务监控

三.开发监控mysql数据库的脚本

脚本1:


1
2
3
4
5
6
7
8
9
1cat check_mysql_01.sh
2#!/bin/bash
3echo method1----------------------
4if [ `netstat -antlpe|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ];then     echo "MySQL is running."
5else
6    echo "MySQL is stopping"     /etc/init.d/mysqld start
7fi  
8
9

关闭mysql


1
2
3
1/etc/init.d/mysqld stop
2
3
  • 注意:

1.最好不要用整数进行比较,因为一旦端口不存在,取值就为空,进行整数比较就会报错
2.不要根据列取具体的值,而是要过滤关键字,通过wc转成行数再判断

用字符串的方式进行比较就好多了,避免了脚本1中整数比较发生的错误

脚本2


1
2
3
4
5
6
7
1#!/bin/bash
2echo method2----------------------
3if [ "`netstat -antlpe|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ];then     echo "MySQL is running."
4else
5    echo "MySQL is stopping"     /etc/init.d/mysqld start fi
6
7

脚本3:


1
2
3
4
5
6
7
8
1#!/bin/bash
2echo method3----------------------
3if [ `netstat -antlpe|grep mysql|wc -l` -gt 0 ];then     echo "MySQL is running."
4else
5    echo "MySQL is stopping"     /etc/init.d/mysql start
6fi
7
8

脚本4:


1
2
3
4
5
6
7
8
1#!/bin/bash
2echo method4--------------------if [ `lsof -i tcp:3306|wc -l` -gt 0 ];then
3   echo "MySQL is running."
4else
5   echo "MySQL is stopping"    /etc/init.d/mysql start
6fi
7
8

脚本5:


1
2
3
4
5
6
7
8
1#!/bin/bash
2echo method5----------------------
3[ `rpm -qa nmap|wc -l` -lt 1 ] && yum install nmap -y &>/dev/null if [ `nmap 127.0.0.1 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ];then     echo "MySQL is running."
4else
5    echo "MySQL is stopping"     /etc/init.d/mysqld start
6fi  
7
8

脚本6


1
2
3
4
5
6
7
8
9
1#!/bin/bash
2echo method6----------------------
3[ `rpm -qa nc|wc -l` -lt 1 ] && yum install nc -y &>/dev/null
4if [ `nc -w 2 127.0.0.1 3306 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ];then     echo "MySQL is running."
5else
6    echo "MySQL is stopping"     /etc/init.d/mysqld start
7fi
8
9

脚本7:


1
2
3
4
5
6
7
1#!/bin/bash
2echo method7-------------------------
3if [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ];then     echo "MySQL is Running."
4else
5    echo "MySQL is Stopping."     /etc/init.d/mysqld start fi
6
7

采用传统的过滤进程的方法,
grep -v grep是排除此命令自身

shell---服务监控

四、httpd的监控脚本


1
2
3
1vim  check_httpd.sh
2
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1#!/bin/bash
2. /etc/init.d/functions rpm -qa httpd &>/dev/null if [ $? -eq 0 ];then     echo "httpd is alreday install"
3else
4    yum install httpd -y
5fi if [ $# -lt 1 ];then
6echo $"Usage: $0 {start|stop|restart|status}"
7fi case "$1" in     start)       systemctl start httpd
8        if[ `netstat -antlpe | grep httpd |wc -l` -eq 1 ];then              action  "httpd is starting" /bin/true
9       fi
10  ;;                 
11     stop)                                   
12        systemctl stop httpd
13        if [ `netstat -antlpe | grep httpd |wc -l` -eq 0 ];then             action  "httpd is stopping" /bin/true
14        fi                                        ;;                                        restart)                
15        if [ `netstat -antlpe | grep httpd |wc -l` -eq 0 ];then               echo "httpd is already stop please input start"         fi
16        if [ `netstat -antlpe | grep httpd | wc -l` -eq 1 ];then
17            pkill httpd && /etc/init.d/httpd start         fi         ;;
18    *)
19       echo $"Usage: $0 {start|stop|restart|status}"
20     ;;          
21esac  
22
23

1
2
3
1 sh check_httpd.sh stop
2
3

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

详解Node.js API系列 Http模块(1) 构造一个简单的静态页服务器

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索