本章節所有的服務器系統均為CentOS release 6.4,其他信息如下:
監控端ip:172.16.56.131,主機名:monitors,nagios-core、nagios-plugins和NRPE已經安裝配置完畢;被監控端MySQL主庫IP:192.183.3.145,主機名:kk,MySQL版本為5.6.32,已經安裝并配置好了nagios-plugins和NRPE;被監控端MySQL備庫IP:172.16.56.129,主機名:nn,MySQL版本為5.6.30。關于MySQL監控的插件可以參詳:https://exchange.nagios.org/directory/Plugins/Databases/MySQL。本文介紹監控MySQL的兩種方法:一種是通過Nagios官方的check_mysql插件(還有第三方類似的check_mysqld插件,據介紹擁有check_mysql及其他功能),另一種第三方的check_mysql_health插件。
參考https://github.com/nagios-plugins/nagios-plugins上的REQUIREMENTS說明(https://github.com/nagios-plugins/nagios-plugins/blob/master/REQUIREMENTS),check_mysql、check_mysql_query插件使用要求MySQL庫可用,可以從http://dev.mysql.com/downloads/下載對應lib庫:libmysql、libmysqlclient;如果監控系統有Redhat源可以yum安裝mysql-devel和mysql-libs,另外必須在PATH中指定mysql_config或編譯時用--with-mysql=DIR指定DIR/bin/mysql_config。
因為在這之前,nagios監控端已經安裝了nagios-plugins,此時libexec目錄并無check_mysql:
[root@monitors ~]#ll /usr/local/nagios/libexec/check_mysql*ls: 無法訪問/usr/local/nagios/libexec/check_mysql*: 沒有那個文件或目錄下面重裝nagios-plugins試試,關于它的包含的插件官方介紹參詳http://nagios-plugins.org/doc/man/index.html。
1.監控端重裝nagios-plugins
[root@monitors tmp]# cd /home/nagios/[root@monitors nagios]# yum -y install mysql-devel mysql-libs[root@monitors nagios]# wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz[root@monitors nagios]# tar zxvf nagios-plugins-2.1.2.tar.gz[root@monitors nagios]# cd nagios-plugins-2.1.2[root@monitors nagios-plugins-2.1.2]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-apt-get-command: --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s --with-ping-command: /bin/ping -n -U -w %d -c %d %s --with-ipv6: yes --with-mysql: /usr/bin/mysql_config --with-openssl: yes --with-gnutls: no --enable-extra-opts: yes --with-perl: /usr/bin/perl --enable-perl-modules: no --with-cgiurl: /nagios/cgi-bin --with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin --enable-libtap: no[root@monitors nagios-plugins-2.1.2]# make && make installmake[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2/po'make[1]: Entering directory `/home/nagios/nagios-plugins-2.1.2'make[2]: Entering directory `/home/nagios/nagios-plugins-2.1.2'make[2]: Nothing to be done for `install-exec-am'.make[2]: Nothing to be done for `install-data-am'.make[2]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'[root@monitors nagios-plugins-2.1.2]# ll /usr/local/nagios/libexec/check_mysql*-rwxr-xr-x 1 nagios nagios 193142 2月 22 22:23 /usr/local/nagios/libexec/check_mysql-rwxr-xr-x 1 nagios nagios 179988 2月 22 22:23 /usr/local/nagios/libexec/check_mysql_query1)查看重裝效果[root@monitors nagios-plugins-2.1.2]# /usr/local/nagios/libexec/check_mysql -H 192.183.3.145 -unagios -dnntest -p123456Uptime: 274340 Threads: 2 Questions: 4005 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.014|Connections=914c;;; Open_files=19;;; Open_tables=63;;; Qcache_free_memory=1031352;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_PRunes=0c;;; Qcache_not_cached=3c;;; Qcache_queries_in_cache=0;;; Queries=4006c;;; Questions=4004c;;; Table_locks_waited=0c;;; Threads_connected=2;;; Threads_running=2;;; Uptime=274340c;;;2)監控配置先看一下check_mysql命令參數的含義,都比較簡單,就不詳述了:
[root@monitors objects]# /usr/local/nagios/libexec/check_mysql -hcheck_mysql v2.1.2 (nagios-plugins 2.1.2)Copyright (c) 1999-2014 Nagios Plugin Development Team<devel@nagios-plugins.org>This program tests connections to a MySQL serverUsage:check_mysql [-d database] [-H host] [-P port] [-s socket][-u user] [-p passWord] [-S] [-l] [-a cert] [-k key][-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]Options:-h, --helpPrint detailed help screen-V, --versionPrint version information--extra-opts=[section][@file]Read options from an ini file. Seehttps://www.nagios-plugins.org/doc/extra-opts.htmlfor usage and examples.-H, --hostname=ADDRESSHost name, IP Address, or unix socket (must be an absolute path)-P, --port=INTEGERPort number (default: 3306)-n, --ignore-authIgnore authentication failure and check for mysql connectivity only-s, --socket=STRINGUse the specified socket (has no effect if -H is used)-d, --database=STRINGCheck database with indicated name-f, --file=STRINGRead from the specified client options file-g, --group=STRINGUse a client options group-u, --username=STRINGConnect using the indicated username-p, --password=STRINGUse the indicated password to authenticate the connection==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==Your clear-text password could be visible as a process table entry-S, --check-slaveCheck if the slave thread is running properly.-w, --warningExit with WARNING status if slave server is more than INTEGER secondsbehind master-c, --criticalExit with CRITICAL status if slave server is more then INTEGER secondsbehind master-l, --sslUse ssl encryptation-C, --ca-cert=STRINGPath to CA signing the cert-a, --cert=STRINGPath to SSL certificate-k, --key=STRINGPath to private SSL key-D, --ca-dir=STRINGPath to CA directory-L, --ciphers=STRINGList of valid SSL ciphersThere are no required arguments. By default, the local database is checkedusing the default unix socket. You can force TCP on localhost by using anIP address or FQDN ('localhost' will use the socket as well).Notes:You must specify -p with an empty string to force an empty password,overriding any my.cnf settings.Send email to help@nagios-plugins.org if you have questions regarding use of this software. To submit patches or suggest improvements, send email to devel@nagios-plugins.org這里就簡單配置一個命令模板:
[root@monitors objects]# vi commands.cfg #check_mysql command definitiondefine command{ command_name check_mysql command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456}[root@monitors objects]# vi mysqlservices.cfg # check_mysql service definitiondefine service{use local-servicehost_name kkservice_description check_mysqlcheck_command check_mysql}3)檢查配置并重啟nagios[root@monitors objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgNagios Core 4.2.0Copyright (c) 2009-present Nagios Core Development Team and Community ContributorsCopyright (c) 1999-2009 Ethan GalstadLast Modified: 08-01-2016License: GPLWebsite: https://www.nagios.orgReading configuration data... Read main config file okay...Warning: Duplicate definition found for service 'Swap Usage' on host 'kk' (config file '/usr/local/nagios/etc/objects/publinuxserver.cfg', starting on line 75)Warning: Duplicate definition found for service 'Current Users' on host 'kk' (config file '/usr/local/nagios/etc/objects/publinuxserver.cfg', starting on line 38) Read object config files okay...Running pre-flight check on configuration data...Checking objects... Checked 22 services. Checked 2 hosts. Checked 2 host groups. Checked 0 service groups. Checked 2 contacts. Checked 1 contact groups. Checked 27 commands. Checked 5 time periods. Checked 0 host escalations. Checked 0 service escalations.Checking for circular paths... Checked 2 hosts Checked 0 service dependencies Checked 0 host dependencies Checked 5 timeperiodsChecking global event handlers...Checking obsessive compulsive processor commands...Checking misc settings...Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check重啟nagios:[root@monitors objects]# service nagios restartRunning configuration check...Stopping nagios: done.Starting nagios: done.打開nagios的web界面,check_mysql監控項在其中:
1.2.check_mysql_health
1.2.1.check_mysql_health簡介
相較于check_mysql,check_mysql_health的功能更為強大一些,不但能監控MySQL是否正常運行,還能監控MySQL連接數情況、MySQL慢查詢等多種監控指標。
check_mysql_health是一個用于檢查MySQL數據庫的各種參數的插件,監控項包括登錄時間,索引使用,緩沖池命中率,查詢緩存命中率,慢查詢,磁盤上的臨時表,表緩存命中率,連接的線程等等,可參詳https://labs.consol.de/nagios/check_mysql_health/index.html。
1.命令行參數
--Hostname:數據庫服務器地址--Username:數據庫用戶--Password:數據庫用戶密碼--database:連接的數據庫名,默認值:information_schema --Mode:監控項--Name:目前僅限于sql--Name2:如果使用--mode=sql,輸出結果中將有SQL語句與sql運行結果;--Warning:觸發warning警告的閾值;--critical:觸發critical警告的閾值;--environment:傳遞環境參數到腳本中;--method:連接數據庫的方式--units <%|KB|MB|GB>:用于--mode=sql的輸出美化2.監測項
| Keyword | Description | Range | 
| connection-time | 連接到數據庫服務器的時間 | 0..n Seconds (1, 5) | 
| uptime | MySQL服務運行的時間 | 0..n Seconds (10:, 5: Minutes) | 
| threads-connected | 數據庫服務器當前打開的連接數 | 1..n (10, 20) | 
| threadcache-hitrate | 緩存的線程命中率 | 0%..100% (90:, 80:) | 
| threads-created | 每秒創建的線程數 | 
 | 
| threads-running | 當前運行的線程數 | 
 | 
| threads-cached | 當前緩存的線程數 | 
 | 
| connects-aborted | 每秒連接失敗的連接請求 | 
 | 
| clients-aborted | 每秒客戶端導致連接失敗的連接數量 | 
 | 
| q[uery]cache-hitrate | 查詢緩存的命中率 | 0%..100% (90:, 80:) | 
| q[uery]cache-lowmem-prunes | 從查詢緩存中清除出去的查詢語句量 | n/sec (1, 10) | 
| [myisam-]keycache-hitrate | Myisam引擎對象索引的緩存命中率 | 0%..100% (99:, 95:) | 
| [innodb-]bufferpool-hitrate | InnoDB引擎對象的緩存池命中率 | 0%..100% (99:, 95:) | 
| [innodb-]bufferpool-wait-free | InnoDB緩沖池可用的待清理頁 | 0..n/sec (1, 10) | 
| [innodb-]log-waits | 因log buffer過小導致的InnoDB日志等待比率 | 0..n/sec (1, 10) | 
| tablecache-hitrate | 表緩存命中率 | 0%..100% (99:, 95:) | 
| table-lock-contention | 表鎖爭用比率 | 0%..100% (1, 2) | 
| index-usage | 索引利用率(與全表掃描相反) | 0%..100% (90:, 80:) | 
| table-fragmentation | 需要分析的表對象 | 
 | 
| tmp-disk-tables | 在磁盤上創建臨時表的比率 | 0%..100% (25, 50) | 
| slow-queries | 每秒觸發的慢查詢數量 | 0..n/sec (0.1, 1) | 
| long-running-procs | 運行時間超過1分鐘的會話數總和 | 0..n (10, 20) | 
| slave-lag | 從庫與主庫之間的延遲 | 0..n Seconds | 
| slave-io-running | 檢查從庫DB的IO線程是否在運行 | 
 | 
| slave-sql-running | 檢查從庫DB的SQL線程是否在運行 | 
 | 
| sql | 根據執行的SQL進行輸出,與參數--name和--name2聯合使用。 | 0..n | 
| open-files | 已打開文件數/可打開文件數量總和 | 0%..100% (80, 95) | 
| encode | 讀取標準輸入(STDIN)并輸出編碼字符串 | 
 | 
| cluster-ndb-running | 檢查所有群集節點是否正在運行 | 
 | 
其中:Query-Cache=Qcache_hits/(Qcache_hits+Com_select).
3.連接字符串
使用--username和--password連接到數據庫,加上--hostname、--socket、--port參數可以更為精確的指定數據庫服務器。
如果在環境變量中提供相應的連接參數,可以忽略--hostname,--username和--password以及--socket和--port。從版本3.x開始,可以通過自己的屬性(自定義對象變量)擴展Nagios中的服務定義,這些將在環境檢查命令執行時呈現,這些環境變量包括:
NAGIOS__SERVICEMYSQL_HOST(_mysql_host)NAGIOS__SERVICEMYSQL_USER(_mysql_user)NAGIOS__SERVICEMYSQL_PASS(_mysql_pass)NAGIOS__SERVICEMYSQL_PORT(_mysql_port)NAGIOS__SERVICEMYSQL_SOCK(_mysql_sock)4.安裝
編譯安裝選項:
--prefix=BASEDIRECTORY,指定應存儲check_mysql_health的目錄,(默認值:/usr/local/nagios);--with-nagios-user=SOMEUSER,此用戶將是check_mysql_health文件的所有者(默認值:nagios);--with-nagios-group=SOMEGROUP,check_mysql_health插件的組,(默認值:nagios);--with-perl=PATHTOPERL,指定要使用的perl解釋器的路徑,(默認值:PATH中的perl)。通過check_mysql、check_mysql_health監控mysql,都需要在被監控的遠程服務器上(mysql服務器上)要先創建一個可以遠程登陸mysql服務器的賬號。
1.數據庫創建nagios用戶
[root@kk /]#mysql -uroot -S /var/lib/mysql/mysql.sock -P 3306 -pmysql> use mysql;mysql> grant select on *.* to nagios@'%' identified by '123456';或者:grant usage on *.* to 'nagios'@'nagiosserver' identified by 'nagiospassword'mysql> select User,Password,Host from user;mysql> select User,Password,Host from mysql.user;mysql> flush privileges;2.安裝check_mysql_health
check_mysql_health官方介紹主頁:http://labs.consol.de/nagios/check_mysql_health/,最新版本的check_mysql_health-2.2.2,下載地址:https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2。
[root@monitors ~]# cd /home/nagios/[root@monitors nagios]# wget https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2.tar.gz[root@monitors nagios]# tar zxvf check_mysql_health-2.2.2.tar.gz [root@monitors nagios]# cd check_mysql_health-2.2.2[root@monitors check_mysql_health-2.2.2]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-perl --with-statefiles-dir=/tmp[root@monitors check_mysql_health-2.2.2]# make && make install[root@monitors check_mysql_health-2.2.2]#cd /usr/local/nagios/libexec/[root@monitors libexec]# ./check_mysql_health因為check_mysql_health是用perl寫的,將check_mysql_health的第一行由原來的#!yes -w修改為#!/usr/bin/perl -w。
[root@monitors libexec]# vim check_mysql_health[root@monitors libexec]# ./check_mysql_health -h解決方法:
[root@monitors libexec]# yum -y install perl-Time-HiRes再次執行:
[root@monitors libexec]# ./check_mysql_health --hostname 192.183.3.145 --port 3306 -username nagios --password 123456 --mode connection-time出現Can't locate DBI.pm的錯誤時:是由于沒有安裝perl DBD-MySQL驅動,可以直接用yum安裝yum install perl-DBI perl-DBD-MySQL -y,也可以利用源碼安裝perl DBD-MySQL驅動,http://www.cpan.org/modules/by-module/DBD/找到最新的版本,現在最新的為DBD-mysql-4.036.tar.gz
[root@monitors libexec]# yum install perl-DBI perl-DBD-MySQL -y再次執行:
[root@monitors libexec]# ./check_mysql_health --hostname 192.183.3.145 --port 3306 -username nagios --password 123456 --mode connection-time --warning 0.001 --critical 0.00113.配置監控項
將check_mysql_health的監控項定義到nagios監控系統組,以便自動輸出監控信息和狀態報告。
1)配置nagios命令行文件
[root@monitors nagios]# vi etc/objects/commands.cfg# check_mysql_health command definitiondefine command{command_name check_mysql_healthcommand_line $USER1$/check_mysql_health --hostname $ARG1$ --port $ARG2$ --username $ARG3$ --password $ARG4$ --mode $ARG5$}2)配置監控服務項[root@monitors nagios]# vi etc/objects/mysqlservices.cfgdefine service{use local-servicehost_name kkservice_description connection-timecheck_command check_mysql_health!192.183.3.145!3306!nagios!123456!connection-time}define service{use local-servicehost_name kkservice_description uptimecheck_command check_mysql_health!192.183.3.145!3306!nagios!123456!uptime}2)添加服務文件到nagios配置文件
[root@monitors etc]# vi nagios.cfg #mysql-servicecfg_file=/usr/local/nagios/etc/objects/mysqlservices.cfg3)重啟nagios服務[root@monitors etc]# service nagios restartRunning configuration check...Stopping nagios: done.Starting nagios: done.[root@monitors etc]# service httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [ OK ]打開nagios管理界面,可見新配置的connection-time、uptime監控項:
2.MySQL主從監控
對于MySQL主從的情況,我們想了解的情況主要有3個:second_behind_master、slave_io_running、slave_sql_running,實現該監控功能有2個方面,一是MySQL自帶的check_mysql插件,另外就是第三方編寫的plugins,比如check_mysql_slave、check_mysql_replication和check_replication_slave等(參詳https://exchange.nagios.org/directory/Plugins/Databases/MySQL),原理都是通過執行“show slave stutas/G”來獲取備庫的狀態,本文就簡單介紹通過check_mysql、check_mysql_health來實現主從監控。
2.1.check_mysql監控MySQL主從
2.1.1.MySQL備庫安裝配置nagios-plugins和NRPE
1.安裝前準備
[root@nn ~]#useradd nagios[root@nn ~]# yum -y install mysql-devel mysql-libs顯示fail,因為yum源提供的事5.1版本的,與從庫5.6的存在“conflicts with”問題,所以手動下載對應版本的mysql-devel、mysql-libs安裝。
注意:5.6版本libmysql,libmysqlclient包含在MySQL-shared、MySQL-shared-compat中,安裝這2個即可。
[root@nn ~]#cd /home/mysql/MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle/[root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-devel-5.6.30-1.linux_glibc2.5.x86_64.rpmwarning: MySQL-devel-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYPreparing... ########################################### [100%] 1:MySQL-devel ########################################### [100%][root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-shared-5.6.30-1.linux_glibc2.5.x86_64.rpmwarning: MySQL-shared-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYPreparing... ########################################### [100%] 1:MySQL-shared ########################################### [100%][root@nn MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-bundle]#rpm -ivh MySQL-shared-compat-5.6.30-1.linux_glibc2.5.x86_64.rpm warning: MySQL-shared-compat-5.6.30-1.linux_glibc2.5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYPreparing... ########################################### [100%] 1:MySQL-shared-compat ########################################### [100%]2.下載并安裝nagios-plugins:
[root@nn ~]#cd /home/nagios/[root@nn nagios]#wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz--2017-02-24 10:09:18-- http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz正在解析主機 nagios-plugins.org... 72.14.186.43正在連接 nagios-plugins.org|72.14.186.43|:80... 已連接。已發出 HTTP 請求,正在等待回應... 200 OK長度:2695301 (2.6M) [application/x-gzip]正在保存至: “nagios-plugins-2.1.2.tar.gz”100%[==========================================================>] 2,695,301 1.05M/s in 2.4s 2017-02-24 10:09:21 (1.05 MB/s) - 已保存 “nagios-plugins-2.1.2.tar.gz” [2695301/2695301])[root@nn nagios]#tar -xzf nagios-plugins-2.1.2.tar.gz [root@nn nagios]#cd nagios-plugins-2.1.2[root@nn nagios-plugins-2.1.2]#./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-apt-get-command: --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s --with-ping-command: /bin/ping -n -U -w %d -c %d %s --with-ipv6: yes --with-mysql: /usr/bin/mysql_config --with-openssl: yes --with-gnutls: no --enable-extra-opts: yes --with-perl: /usr/bin/perl --enable-perl-modules: no --with-cgiurl: /nagios/cgi-bin --with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin --enable-libtap: no[root@nn nagios-plugins-2.1.2]#make&make installmake[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2/po'make[1]: Entering directory `/home/nagios/nagios-plugins-2.1.2'make[2]: Entering directory `/home/nagios/nagios-plugins-2.1.2'make[2]: Nothing to be done for `install-exec-am'.make[2]: Nothing to be done for `install-data-am'.make[2]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'make[1]: Leaving directory `/home/nagios/nagios-plugins-2.1.2'[1]+ Exit 2 make[root@nn nagios-plugins-2.1.2]#chown -R nagios:nagios /usr/local/nagios[root@nn nagios-plugins-2.1.2]#ls /usr/local/nagios/libexec/check_mysql*/usr/local/nagios/libexec/check_mysql /usr/local/nagios/libexec/check_mysql_query3.下載安裝NRPE
[root@nn nagios-plugins-2.1.2]#cd /home/nagios/[root@nn nagios]#tar zxf nrpe-3.0.1.tar.gz[root@nn nagios]#cd nrpe-3.0.1[root@nn nrpe-3.0.1]#yum -y install openssl openssl-devel[root@nn nrpe-3.0.1]#./configure --with-nagios-user=nagios --with-nagios-group=nagios*** Configuration summary for nrpe 3.0.1 09-08-2016 ***: General Options: ------------------------- NRPE port: 5666 NRPE user: nagios NRPE group: nagios Nagios user: nagios Nagios group: nagiosReview the options above for accuracy. If they look okay,type 'make all' to compile the NRPE daemon and clientor type 'make' to get a list of make options.[root@nn nrpe-3.0.1]#make allcd ./src/; makemake[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'gcc -g -O2 -I/usr/include/krb5 -DHAVE_CONFIG_H -I ../include -I ./../include -o nrpe ./nrpe.c ./utils.c ./acl.c -lssl -lcrypto -lnsl gcc -g -O2 -I/usr/include/krb5 -DHAVE_CONFIG_H -I ../include -I ./../include -o check_nrpe ./check_nrpe.c ./utils.c -lssl -lcrypto -lnsl make[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'*** Compile finished ***You can now continue with the installation or upgrade process.Read the PDF documentation (NRPE.pdf) for information on the nextsteps you should take to complete the installation or upgrade.[root@nn nrpe-3.0.1]#make install-plugincd ./src/; make install-pluginmake[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'/usr/bin/install -c -m 755 -d /usr/local/nagios/bin/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexecmake[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'[root@nn nrpe-3.0.1]#make install-daemoncd ./src/; make install-daemonmake[1]: Entering directory `/home/nagios/nrpe-3.0.1/src'/usr/bin/install -c -m 755 -d /usr/local/nagios/bin/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall/usr/bin/install -c -m 755 nrpe /usr/local/nagios/bin/usr/bin/install -c -m 755 -o nagios -g nagios -d /usr/local/nagios/varmake[1]: Leaving directory `/home/nagios/nrpe-3.0.1/src'[root@nn nrpe-3.0.1]#make install-config/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc4.配置nrpe
[root@nn nrpe-3.0.1]# vim /usr/local/nagios/etc/nrpe.cfg修改allowed_hosts=192.183.3.145,172.16.56.131,172.16.56.129,允許Nagios服務器端訪問;
在命令行測試如下的監測命令,這里根據自己的監測需求對命令進行修改:
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load/usr/local/nagios/libexec/check_nrpe -H localhost -c check_sda1/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs/usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs查看配置結果:
[root@nn nrpe-3.0.1]#grep -v '^#' /usr/local/nagios/etc/nrpe.cfg |sed '/^$/d'log_facility=daemondebug=0pid_file=/usr/local/nagios/var/nrpe.pidserver_port=5666nrpe_user=nagiosnrpe_group=nagiosallowed_hosts=192.183.3.145,172.16.56.131,172.16.56.129dont_blame_nrpe=0allow_bash_command_substitution=0command_timeout=60connection_timeout=300command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Zcommand[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 2005.啟動nrpe
添加端口:
[root@nn nrpe-3.0.1]#vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT[root@nn nrpe-3.0.1]#/etc/init.d/iptables restart[root@nn nrpe-3.0.1]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d[root@nn nrpe-3.0.1]#netstat -tulpn | grep nrpe tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 59428/nrpe tcp 0 0 :::5666 :::* LISTEN 59428/nrpe為nrpe編寫啟動腳本,使得nrpe以standard alone方式運行:
[root@nn nrpe-3.0.1]#vi /etc/init.d/nrped#!/bin/bash# chkconfig: 2345 88 12 # description: NRPE DAEMON NRPE=/usr/local/nagios/bin/nrpe NRPECONF=/usr/local/nagios/etc/nrpe.cfg case "$1" in start) echo -n "Starting NRPE daemon..." $NRPE -c $NRPECONF -d echo " done." ;; stop) echo -n "Stopping NRPE daemon..." pkill -u nagios nrpe echo " done." ;; restart) $0 stop sleep 2 $0 start ;; *) echo "Usage: $0 start|stop|restart" ;; esac exit 0 [root@nn nrpe-3.0.1]#chmod +x /etc/init.d/nrped [root@nn nrpe-3.0.1]#chkconfig --add nrped [root@nn nrpe-3.0.1]#chkconfig nrped on[root@nn nrpe-3.0.1]#service nrped start Starting NRPE daemon... done.6.測試nrpe
[root@nn nrpe-3.0.1]#/usr/local/nagios/libexec/check_nrpe -H 172.16.56.129 -c check_usersUSERS OK - 2 users currently logged in |users=2;5;10;07.Nagios監控端添加MySQL備庫監控
[root@monitors ~]# vim /usr/local/nagios/etc/objects/hosts.cfg define host{ host_name nn alias slave-server use linux-server address 172.16.56.129 max_check_attempts 5 check_period 24x7 check_interval 5 retry_interval 1 max_check_attempts 10 check_command check-host-alive notification_period 24x7 notification_interval 30 notification_options d,r contact_groups admins }define hostgroup{ hostgroup_name remote-linux-servers ; The name of the hostgroup alias remoteLinux Servers ; Long name of the group members kk,nn ; Comma separated list of hosts that belong to this group }將prilinuxserver.cfg、mysqlservices.cfg中的“host_name kk”全部改為“hostgroup_name remote-linux-servers”,并重啟nagios服務。
[root@monitors ~]# vim /usr/local/nagios/etc/objects/prilinuxserver.cfg [root@monitors ~]# vim /usr/local/nagios/etc/objects/mysqlservices.cfg [root@monitors objects]# service nagios configtestNagios Core 4.2.0Copyright (c) 2009-present Nagios Core Development Team and Community ContributorsCopyright (c) 1999-2009 Ethan GalstadLast Modified: 08-01-2016License: GPLWebsite: https://www.nagios.orgReading configuration data... Read main config file okay... Read object config files okay...Running pre-flight check on configuration data...Checking objects... Checked 26 services. Checked 3 hosts. Checked 2 host groups. Checked 0 service groups. Checked 2 contacts. Checked 1 contact groups. Checked 27 commands. Checked 5 time periods. Checked 0 host escalations. Checked 0 service escalations.Checking for circular paths... Checked 3 hosts Checked 0 service dependencies Checked 0 host dependencies Checked 5 timeperiodsChecking global event handlers...Checking obsessive compulsive processor commands...Checking misc settings...Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight checkObject precache file created:/usr/local/nagios/var/objects.precache[root@monitors objects]# service nagios restartRunning configuration check...Stopping nagios: done.Starting nagios: done.2.1.2.check_mysql之MySQL主從監控配置
1.nrpe與check_mysql結合的方式監控MySQL主從
主庫創建監控用戶:
mysql> grant Replication client on *.* to 'nagios'@'%' identified by '123456';mysql> flush privileges;Query OK, 0 rows affected (0.19 sec)[root@nn libexec]#mysql -unagios -p123456 -e "show slave status/G"Warning: Using a password on the command line interface can be insecure.*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.183.3.145 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000007 Read_Master_Log_Pos: 1267 Relay_Log_File: nn-relay-bin.000003 Relay_Log_Pos: 564 Relay_Master_Log_File: master-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1267 Relay_Log_Space: 1543 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 129 Master_UUID: 5a22d499-20b4-11e6-a81e-d43d7e101ba1 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0MySQL備庫:
[root@nn ~]#/usr/local/nagios/libexec/check_mysql -H172.16.56.129 -unagios -p123456 -S -w 20 -c 60Uptime: 17435 Threads: 3 Questions: 48 Slow queries: 0 Opens: 71 Flush tables: 1 Open tables: 64 Queries per second avg: 0.002 Slave IO: Yes Slave SQL: Yes Seconds Behind Master: 0|Connections=16c;;; Open_files=22;;; Open_tables=64;;; Qcache_free_memory=1031352;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=4c;;; Qcache_queries_in_cache=0;;; Queries=49c;;; Questions=36c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=17435c;;; 'seconds behind master'=0.000000s;20.000000;60.000000;[root@nn ~]#service nrped stop[root@nn ~]#vim /usr/local/nagios/etc/nrpe.cfg command[check_mysql_slave]=/usr/local/nagios/libexec/check_mysql -H172.16.56.129 -unagios -p123456 -S -w 20 -c 60[root@nn ~]#service nrped startStarting NRPE daemon... done.Nagios監控端:
[root@monitors objects]# vi commands.cfg #check_mysql_slave command definitiondefine command{ command_name check_mysql_slave command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456 -S}[root@monitors objects]# vi prilinuxserver.cfg # monitoring the mysql-slave-status on the remote hostdefine service{use local-servicehost_name nnservice_description slavestatuscheck_command check_nrpe!check_mysql_slavecontact_groups admins}[root@monitors objects]# service nagios configtest[root@monitors objects]# service nrped restart打開nagios web端可見新配置的監控項:2.check_mysql方式監控MySQL主從1)配置command.cfg[root@monitors libexec]# vi /usr/local/nagios/etc/objects/commands.cfg #check_mysql_slave command definitiondefine command{ command_name check_mysql_slave command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagios -p123456 -S}2)配置service文件[root@monitors libexec]# vi /usr/local/nagios/etc/objects/mysqlservices.cfg # check_mysql_slave service definitiondefine service{use local-servicehost_name nnservice_description slave_statuscheck_command check_mysql_slave}重啟nagios即可看到配置結果:2.2 check_mysql_health監控MySQL主從
check_mysql_health監控MySQL主從非常簡單,只需要新增服務的監控項即可:[root@monitors libexec]# vi /usr/local/nagios/etc/objects/mysqlservices.cfgdefine service{use local-servicehostgroup_name remote-linux-serversservice_description slave-io-runningcheck_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-io-running}define service{use local-servicehostgroup_name remote-linux-serversservice_description slave-sql-runningcheck_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-sql-running}define service{use local-servicehostgroup_name remote-linux-serversservice_description slave-lagcheck_command check_mysql_health!172.16.56.129!3306!nagios!123456!slave-lag}重啟nagios服務:[root@monitors libexec]# /etc/init.d/nagios restartRunning configuration check...Stopping nagios: done.Starting nagios: done.刷新nagios監控頁面:至此,MySQL的主從監控安裝配置完畢,不足之處,歡迎批評指正!
新聞熱點
疑難解答