nagios监控mysql(check_mysql)及内存使用率(check_mem)
一、监控本地(localhost)内存
1、上传监控脚本 check_mem 到/usr/local/nagios/libexec
#chown-Rnagios.nagioscheck_mem#chmod+xcheck_mem
2、修改commands配置
#vim/usr/local/nagios/etc/objects/commands.cfgdefinecommand{command_namecheck_memcommand_line$USER1$/check_mem-w$ARG1$-c$ARG2$}
3、修改localhost.cfg
#vim/usr/local/nagios/etc/objects/localhost.cfgdefineservice{uselocal-servicehost_namelocalhostservice_descriptioncheck_memcheck_commandcheck_mem!20!10}
4、重启nagios服务
#servicenagiosrestart
5、check_mem 脚本
#!/bin/bashUSAGE="`basename$0`[-w|--warning]<percentfree>[-c|--critical]<percentfree>"THRESHOLD_USAGE="WARNINGthresholdmustbegreaterthanCRITICAL:`basename$0`$*"calc=/tmp/memcalcpercent_free=/tmp/mempercentcritical=""warning=""STATE_OK=0STATE_WARNING=1STATE_CRITICAL=2STATE_UNKNOWN=3#printusageif[[$#-lt4]]thenecho""echo"WrongSyntax:`basename$0`$*"echo""echo"Usage:$USAGE"echo""exit0fi#readinputwhile[[$#-gt0]]docase"$1"in-w|--warning)shiftwarning=$1;;-c|--critical)shiftcritical=$1;;esacshiftdone#verifyinputif[[$warning-eq$critical||$warning-lt$critical]]thenecho""echo"$THRESHOLD_USAGE"echo""echo"Usage:$USAGE"echo""exit0fi#Totalmemoryavailabletotal=`free-m|head-2|tail-1|gawk'{print$2}'`#Totalmemoryusedused=`free-m|head-2|tail-1|gawk'{print$3}'`#Calctotalminususedfree=`free-m|head-2|tail-1|gawk'{print$2-$3}'`#normalvalues#echo"$total"MBtotal#echo"$used"MBused#echo"$free"MBfree#makeitinto%percentfree=((freemem/totalmem)*100)echo"5">$calc#decimalaccuracyecho"k">>$calc#commitecho"100">>$calc#multiplyecho"$free">>$calc#divisionintegerecho"$total">>$calc#divisionintegerecho"/">>$calc#divisionsignecho"*">>$calc#multiplicationsignecho"p">>$calc#printpercent=`/usr/bin/dc$calc|/bin/sed's/^\./0./'|/usr/bin/tr"."""|/usr/bin/gawk{'print$1'}`#percent1=`/usr/bin/dc$calc`#echo"$percent1"if[["$percent"-le$critical]]thenecho"CRITICAL-$freeMB($percent%)FreeMemory"exit2fiif[["$percent"-le$warning]]thenecho"WARNING-$freeMB($percent%)FreeMemory"exit1fiif[["$percent"-gt$warning]]thenecho"OK-$freeMB($percent%)FreeMemory"exit0fi
二、监控客户端内存使用情况
1、上传监控脚本 check_mem 到/usr/local/nagios/libexec
#chown-Rnagios.nagioscheck_mem#chmod+xcheck_mem
2、修改nrpe.cfg
#vim/usr/local/nagios/etc/nrpe.cfgcommand[check_users]=/usr/local/nagios/libexec/check_users-w3-c5command[check_load]=/usr/local/nagios/libexec/check_load-w15,10,5-c30,25,20command[check_xvda]=/usr/local/nagios/libexec/check_disk-w10%-c5%-p/dev/xvdacommand[check_zombie_procs]=/usr/local/nagios/libexec/check_procs-w5-c10-sZcommand[check_total_procs]=/usr/local/nagios/libexec/check_procs-w150-c200command[check_xvdb2]=/usr/local/nagios/libexec/check_disk-w10%-c5%-p/dev/xvdb2command[check_swap]=/usr/local/nagios/libexec/check_swap-w20%-c10%command[check_mem]=/usr/bin/sudo/usr/local/nagios/libexec/check_mem-w20-c10#增加此行
3、在nagios服务器端增加监控服务
#vim/usr/local/nagios/etc/servers/192.168.200.111.cfgdefineservice{usegeneric-servicehost_name192.168.200.111service_descriptioncheck_memcheck_commandcheck_nrpe!check_mem}
三、排错
1、NRPE: Unable to read output
(1)为nagios用户增加sudo权限
#visudonagiosALL=(ALL)NOPASSWD:/usr/local/nagios/libexec/check_mem
(2)注释掉一下行,表示不需要控制终端
#visudo#Defaultsrequiretty
四、nagios监控mysql
1、check_mysql
nagios监控mysql使用的是 check_mysql 这个插件,需要在nagios服务器上先安装mysql-devel,然后再重新安装nagios-plugin,这样才会出现check_mysql。否则即使copy了一份,也照样用不了。
2、编译或重新编译nagios-plugin
#yum-yinstallmysql-devl#cdnagios-plugins-2.0.3#./configure--with-nagios-user=nagios--with-nagios-group=nagios#make&&makeinstall
3、查看 check_mysql
#ls/usr/local/nagios/libexec/check_mysql
4、建立专用数据库
#mysql-uroot-pmysql>createdatabasenagios_monitor;mysql>grantselectonnagios_monitor.*tonagios@'%'identifiedby'123qaz!@#';QueryOK,0rowsaffected(0.00sec)mysql>flushprivileges;QueryOK,0rowsaffected(0.00sec)mysql>selectUser,Password,Hostfrommysql.user;+--------+-------------------------------------------+--------------+|User|Password|Host|+--------+-------------------------------------------+--------------+|root|*B9627CB37815863D1E98D0C41E0233A772355E2B|localhost||root|*B9627CB37815863D1E98D0C41E0233A772355E2B|127.0.0.1||root|*B9627CB37815863D1E98D0C41E0233A772355E2B|::1||cacti|*BC3E1F14C7940F9C8BCDB05A38385754BB55CD64|localhost||nagios|*BC3E1F14C7940F9C8BCDB05A38385754BB55CD64|%|+--------+-------------------------------------------+--------------+8rowsinset(0.00sec)
5、check 一下
#/usr/local/nagios/libexec/check_mysql-H192.168.200.105-unagios-dnagios_monitor-p123qaz!@##报了一个错/usr/local/nagios/libexec/check_mysql:errorwhileloadingsharedlibraries:libmysqlclient.so.18:cannotopensharedobjectfile:Nosuchfileordirectory#解决:ln-sv/usr/local/mysql/lib/libmysqlclient.so.18/usr/lib64/libmysqlclient.so.18#再重新测试#/usr/local/nagios/libexec/check_mysql-H192.168.200.111-unagios-dnagios_monitor-p123qaz!@#Uptime:13991Threads:5Questions:1242101Slowqueries:0Opens:159Flushtables:1Opentables:60Queriespersecondavg:88.778|Connections=315c;;;Open_files=85;;;Open_tables=60;;;Qcache_free_memory=16285768;;;Qcache_hits=1210926c;;;Qcache_inserts=16654c;;;Qcache_lowmem_prunes=0c;;;Qcache_not_cached=2c;;;Qcache_queries_in_cache=283;;;Queries=1242101c;;;Questions=1242101c;;;Table_locks_waited=2c;;;Threads_connected=5;;;Threads_running=1;;;Uptime=13991c;;;
6、监控localhost
(1)修改 commands.cfg
#vim/usr/local/nagios/etc/objects/commands.cfgdefinecommand{command_namecheck_mysqlcommand_line$USER1$/check_mysql-H$HOSTADDRESS$-unagios-dnagios_monitor-p123qaz!@#}
(2)修改 localhost.cfg
#vim/usr/local/nagios/etc/objects/localhost.cfgdefineservice{uselocal-servicehost_namelocalhostservice_descriptioncheck_mysqlcheck_commandcheck_mysql}
(3)重启 nagios
#servicenagiosrestart
7、监控客户端
(1)check 一下
#/usr/local/nagios/libexec/check_mysql-H192.168.200.111-unagios-dnagios_monitor-p123qaz!@#
(2)客户端修改 nrpe.cfg
#vim/usr/local/nagios/etc/nrpe.cfgcommand[check_mysql]=/usr/local/nagios/libexec/check_mysql-H192.168.200.111-unagios-dnagios_monitor-p123qaz!@#
(3)重启 nrpe
#killallnrpe#/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d
(4)服务端定义服务
#vim/usr/local/nagios/etc/servers/192.168.200.111.cfgdefineservice{usegeneric-servicehost_name192.168.200.111service_descriptioncheck_mysqlcheck_commandcheck_nrpe!check_mysql}
(5)重启 nagios 服务
#servicenagiosrestart
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。