shell 服务的监控脚本

监控数据库的shell脚本经典案例

安装mysql,和开启mysql服务

yum install mariadb-server -y
systemctl start mariadb
firewall-cmd --add-service=mysql

  
  
  • 1
  • 2
  • 3

1. 检测端口命令

方法一

[root@localhost sbin]# netstat -antlpe|grep 3306|awk -F "[ :]+" '{print $5}'
3306
[root@localhost sbin]# ss -antlpe | grep mysql |wc -l
1

  
  
  • 1
  • 2
  • 3
  • 4

方法二:lsof

[root@localhost sbin]# yum whatprovides */lsof
Loaded plugins: langpacks
rhel-7.3/filelists_db                            | 3.3 MB     00:00     
lsof-4.87-4.el7.x86_64 : A utility which lists open files on a
                       : Linux/UNIX system
Repo        : rhel-7.3
Matched from:
Filename    : /usr/sbin/lsof
lsof-4.87-4.el7.x86_64 : A utility which lists open files on a
                       : Linux/UNIX system
Repo        : @rhel_dvd
Matched from:
Filename    : /usr/sbin/lsof
[root@localhost sbin]# yum install lsof-4.87-4.el7.x86_64 -y
Loaded plugins: langpacks
Package lsof-4.87-4.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost sbin]# lsof -i tcp:3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  11023 mysql   14u  IPv4  86649      0t0  TCP *:mysql (LISTEN)
[root@localhost sbin]# lsof -i tcp:3306|wc -l   # 包含第一行的标题栏
2

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

方法三:ss与netstat类似

[root@localhost init.d]# ss -antlpe|grep mysql
LISTEN     0      50                        *:3306                     *:*      users:(("mysqld",6348,14)) uid:27 ino:33847 sk:ffff88007bdecf00 <->
[root@localhost init.d]# ss -antlpe|grep mysql|wc -l
1

  
  
  • 1
  • 2
  • 3
  • 4

2. 远程服务器检测本地端口

yum install nmap-6.40-7.el7.x86_64 -y
yum install telnet nc -y

  
  
  • 1
  • 2

方法一:使用nmap命令

# 查看远程端口是否开启,开启的话state状态是open
[root@server2 ~]# nmap 172.25.60.251 -p 3306
Starting Nmap 6.40 ( http://nmap.org ) at 2020-02-16 23:01 EST
Nmap scan report for 172.25.60.251
Host is up (0.00031s latency).
PORT     STATE SERVICE
3306/tcp open  mysql
MAC Address: 52:54:00:00:05:0A (QEMU Virtual NIC)
Nmap done: 1 IP address (1 host up) scanned in 13.43 seconds
[root@server2 ~]# nmap 172.25.60.251 -p 3306|grep open
3306/tcp open  mysql
[root@server2 ~]# nmap 172.25.60.251 -p 3306|grep open|wc -l
1

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

方法二:telnet命令

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

[root@server2 ~]# telnet 172.25.60.251 3306
Trying 172.25.60.251...
Connected to 172.25.60.251.
Escape character is '^]'.
H Host '172.25.60.252' is not allowed to connect to this MariaDB serverConnection closed by foreign host.
[root@server2 ~]# telnet 172.25.60.251 3306|grep Connected
Connected to 172.25.60.251.
Connection closed by foreign host.
[root@server2 ~]# telnet 172.25.60.251 3306|grep Connected|wc -l
Connection closed by foreign host.
1

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

[root@localhost sbin]# ps -ef|grep mysql|wc -l  # 此处把过滤的动作也当作了一个进程
3
[root@localhost sbin]# ps -ef|grep mysql
mysql    10864     1  0 03:15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql    11023 10864  0 03:15 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root     17200  3406  0 03:59 pts/1    00:00:00 grep --color=auto mysql

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

过滤掉grep那句,只剩下mysql的进程

[root@localhost sbin]# ps -ef|grep mysql|grep -v grep
mysql    10864     1  0 03:15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql    11023 10864  0 03:15 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
[root@localhost sbin]# ps -ef|grep mysql|grep -v grep|wc -l
2

  
  
  • 1
  • 2
  • 3
  • 4
  • 5

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

脚本一

[root@localhost 监控mysql数据库]# cat check_01.sh 
#!/bin/bash
if [ "`netstat -antlpe | grep 3306 | awk -F '[ :]+' '{print $5}'`" = "3306" ]
then
	echo "mysql is running"
else
	echo "mysql is stop,now start it"
	systemctl start mariadb
fi
[root@localhost 监控mysql数据库]# sh check_01.sh 
mysql is running

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

脚本二

[root@localhost 监控mysql数据库]# cat check_02.sh 
#!/bin/bash
if [ `netstat -antlpe | grep 3306 | wc -l` -eq 1 ]
then
	echo "mysql is running"
else
	echo "mysql is stop,now start it"
	systemctl start mariadb
fi
[root@localhost 监控mysql数据库]# sh check_02.sh 
mysql is running

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

脚本三

[root@localhost 监控mysql数据库]# cat check_03.sh 
#!/bin/bash
if [ `lsof -i tcp:3306 | wc -l` -eq 2 ]
then
	echo "mysql is running"
else
	echo "mysql is stop,now start it"
	systemctl start mariadb
fi
[root@localhost 监控mysql数据库]# sh check_03.sh 
mysql is running

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

脚本四

[root@localhost 监控mysql数据库]# cat check_04.sh 
#!/bin/bash
if [ `ss -antlpe |grep mysql| wc -l` -eq 1 ]
then
	echo "mysql is running"
else
	echo "mysql is stop,now start it"
	systemctl start mariadb
fi
[root@localhost 监控mysql数据库]# sh check_04.sh 
mysql is running

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
发布了107 篇原创文章 · 获赞 0 · 访问量 1449

猜你喜欢

转载自blog.csdn.net/weixin_45029822/article/details/104365616