Skip to content

Latest commit

 

History

History
302 lines (251 loc) · 8.06 KB

6.Monitor-MySQL.md

File metadata and controls

302 lines (251 loc) · 8.06 KB

Monitor MySQL trong nagios

Nội dung

1. Thông tin host MySQL

OS: CentOS 7
IP: 192.168.100.197
Service: MySQL or MariaDB

2. Chuẩn bị và cài đặt Plugin

2.1. Chuẩn bị trên host Database

  • Bước 1: Cho phép Bind đến DB

Sửa file: vi /etc/my.cnf (Ubuntu) hoặc vi /etc/my.cnf.d/server.cnf (CentOS/RHEL) và thêm ở phần [mysqld]

...
[mysqld]
bind-address = 0.0.0.0
...
  • Bước 2: Restart lại dịch vụ
systemctl restart mariadb

hoặc

systemctl restart mysql
  • Bước 3: Tạo user kiểm tra cho nagios

Ở phần này chúng ta cần tạo một USER có quyền USAGE trên host DB (192.168.100.197) (MySQL hoặc MariaDB).

GRANT usage ON *.* TO 'checker'@'192.168.100.196' IDENTIFIED BY '123';
  • checker : Tên của user dùng cho nagios có thể truy xuất vào DB
  • 123 : Mật khẩu của checker (Tùy chọn)
  • 192.168.100.196 : Địa chỉ IP của Nagios Server

Như vậy là ta đã hoàn thành công việc ở host DB.

2.2. Cài đặt Plugin trên Nagios Server

  • Bước 1: Cài đặt các gói đi kèm

Trên Nagios server, chúng ta sẽ thực hiện cài đặt Plugin. Trước hết, cài đặt những gói đi kèm.

Đối với CentOS 7, chúng ta thêm repo của MariaDB.

vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Cài gói MariaDB-client và DBI, DBD::mysql

yum install MariaDB-client perl-DBI perl-DBD-MySQL -y
  • Bước 2: Cài đặt plugin check-mysql-health
wget https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2.tar.gz 
tar zxvf check_mysql_health-2.2.2.tar.gz 
cd check_mysql_health-2.2.2
./configure
make
make install
  • Bước 3: Kiểm tra plugin

Chuyển đến thư mục /usr/local/nagios/libexec/ và kiểm tra plugin

Chúng ta có thể kiểm tra plugin có hoạt động hay không bằng lệnh sau:

cd /usr/local/nagios/libexec/

./check_mysql_health --hostname 192.168.100.197 --username checker --password 123 --mode connection-time

Kết quả trả về có dạng, cho biết plugin hoạt động ổn định:

2.3 Thêm thông tin host và giám sát

  • Bước 1: Thêm command vào Nagios

Đầu tiên, chúng ta thêm một command mới với nội dung:

vi /usr/local/nagios/etc/objects/commands.cfg
...
# MySQL Health

define command{
command_name check_mysql_health
command_line $USER1$/check_mysql_health -H $ARG4$ --username $ARG1$ --password $ARG2$ --port $ARG5$ --mode $ARG3$
}
  • Bước 2: Tạo file cấu hình cho host DB

Sau đó, chúng ta khai báo thông tin về máy chủ DB.

Mở file /usr/local/nagios/etc/servers/mysql-server.cfg

define host{
        use                             linux-server
        host_name                       db-mysql01         
        alias                           mysql          
        address                         192.168.100.197   
        max_check_attempts              5
        check_period                    24x7
        notification_interval           30
        notification_period             24x7
}
define service{
use local-service
host_name db-mysql01
service_description MySQL connection-time
check_command check_mysql_health!checker!123!connection-time!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL uptime
check_command check_mysql_health!checker!123!uptime!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL threads-connected
check_command check_mysql_health!checker!123!threads-connected!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL threads-created
check_command check_mysql_health!checker!123!threads-created!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL threads-running
check_command check_mysql_health!checker!123!threads-running!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL threads-cached
check_command check_mysql_health!checker!123!threads-cached!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL connects-aborted
check_command check_mysql_health!checker!123!connects-aborted!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL clients-aborted
check_command check_mysql_health!checker!123!clients-aborted!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL qcache-hitrate
check_command check_mysql_health!checker!123!qcache-hitrate!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL qcache-lowmem-prunes
check_command check_mysql_health!checker!123!qcache-lowmem-prunes!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL keycache-hitrate
check_command check_mysql_health!checker!123!keycache-hitrate!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL bufferpool-hitrate
check_command check_mysql_health!checker!123!bufferpool-hitrate!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL bufferpool-wait-free
check_command check_mysql_health!checker!123!bufferpool-wait-free!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL log-waits
check_command check_mysql_health!checker!123!log-waits!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL tablecache-hitrate 
check_command check_mysql_health!checker!123!tablecache-hitrate !192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL table-lock-contention
check_command check_mysql_health!checker!123!table-lock-contention!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL index-usage
check_command check_mysql_health!checker!123!index-usage!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL tmp-disk-tables 
check_command check_mysql_health!checker!123!tmp-disk-tables !192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL table-fragmentation
check_command check_mysql_health!checker!123!table-fragmentation!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL open-files
check_command check_mysql_health!checker!123!open-files!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL slow-queries
check_command check_mysql_health!checker!123!slow-queries!192.168.100.197!3306!
}
define service{
use local-service
host_name db-mysql01
service_description MySQL long-running-procs
check_command check_mysql_health!checker!123!long-running-procs!192.168.100.197!3306!
}

Chi tiết mode xem tại đây.

  • Bước 3: Khởi động lại Nagios Server

Sau khi thêm các dịch vụ theo dõi DB mà bạn muốn, chúng khởi động lại nagios để nó nhận những gì mình vừa cấu hình.

systemctl restart nagios
  • Bước 4: Kiểm tra trên Web UI

Kiểm tra lại trên Web, Current status > Services > db-mysql01