zabbix监控mysql主从从库状态

1、zabbix客户端配置自定义key

[root@localhost ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

最后加一行

UserParameter=MysqlReplication,/home/zabbix/check_slave_status.sh

MysqlReplication 为key名称,逗号后面为执行的脚本

脚本内容:

[root@localhost ~]# cat /home/zabbix/check_slave_status.sh
#!/bin/bash
#check mysql slave status

mysql=/home/software/mysql/bin/mysql
status=`$mysql -e "show slave status\G;" | grep -i Running | egrep "IO|SQL" | grep -i yes | wc -l`
echo $status
exit

脚本zabbix用户需要有权限,包括上级目录。否则执行不了
重启zabbix

2、服务端配置:

可以用命令测试一下,配置是否能返回状态值

配置主机监控项

这个“键值”需要手动写进去,“选择”里面没有

添加图形监控:

选择之前的监控项

添加 触发器实现返回值不等于2的时候,在主页面报警。

N写2 ,完成。

goaccess分析nginx日志

安装:
[root@Rootop tar_gz]# wget -c http://tar.goaccess.io/goaccess-0.8.5.tar.gz
[root@Rootop tar_gz]# tar zxvf goaccess-0.8.5.tar.gz
[root@Rootop tar_gz]# cd goaccess-0.8.5
[root@Rootop goaccess-0.8.5]# ./configure
[root@Rootop goaccess-0.8.5]# make && make install
我这里web服务器为nginx。
可以直接输入goaccess -f +日志路径 进入控制台,操作不多说,百度一大片。

开始日志分析并导出为html文件:

[root@Rootop ~]# goaccess -f /home/wwwlogs/www.rootop.org.log --log-format='%h %^[%d:%^] "%r" %s %b "%R" "%u"' --date-format='%d/%b/%Y' -d -a > test.html

也可以通过指定配置文件方式来分析。就不需要在命令中体现日志格式:
首先下载配置文件:

[root@Rootop ~]# wget -O .goaccessrc https://raw.githubusercontent.com/allinurl/goaccess/master/config/goaccess.conf --no-check-certificate

编辑.goaccess文件,把16和27行的前面注释去掉。(NCSA Combined Log Format)

date-format %d/%b/%Y
log-format %h %^[%d:%^] “%r” %s %b “%R” “%u”

[root@Rootop ~]# goaccess -f /home/wwwlogs/www.rootop.org.log -d -a -p ~/.goaccessrc > test2.html

需要注意这里的日志格式,每个nginx日志格式配置可能不同,goaccess指定的日志格式也不一定一样,需要对照下面的参数和nginx日志格式来指定命令中日志的格式。否则日志
分析的时候会报错。需要对nginx的日志格式有一定了解。

goaccess时间和日志格式支持的参数:
date_format
The date_format variable followed by a space, specifies the log format date containing any combination of regular characters and special format specifiers. They all
begin with a percentage (%) sign. See http://linux.die.net/man/3/strftime

Note that there is no need to use time specifiers since they are not used by GoAccess. It’s recommended to use only date specifiers, i.e., %Y-%m-%d.

log_format
The log_format variable followed by a space or \t , specifies the log format string.

%d date field matching the date_format variable.

%h host (the client IP address, either IPv4 or IPv6)

%r The request line from the client. This requires specific delimiters around the request (as single quotes, double quotes, or anything else) to be parsable. If not, we
have to use a combination of special format specifiers as %m %U %H.

%m The request method.

%U The URL path requested (including any query string).

%H The request protocol.

%s The status code that the server sends back to the client.

%b The size of the object returned to the client.

%R The “Referrer” HTTP request header.

%u The user-agent HTTP request header.

%D The time taken to serve the request, in microseconds.

%T The time taken to serve the request, in seconds or milliseconds. Note: %D will take priority over %T if both are used.

%^ Ignore this field.

PS:
之前配置文件错误,分析nginx日志报错,经过求助软件作者解决,并对goaccess有更深入的了解。赞一下软件作者。
[root@Rootop ~]# goaccess -d -f /home/wwwlogs/www.rootop.org.log -a -p ~/.goaccesssrc > test.html
Parsing… [373,734] [373,734/s]
GoAccess – version 0.8.5 – Nov 20 2014 16:48:39

Fatal error has occurred
Error occured at: goaccess.c – main – 832
Nothing valid to process.

~/.goaccesssrc文件内容:
date_format %d/%b/%Y
log_format %^:%^ %h %^[%d:%^] “%r” %s %b

nginx日志:
58.251.136.61 – – [20/Nov/2014:17:29:21 +0800] “GET /wp-content/themes/g-white/js/all.js HTTP/1.1″200 1292 “https://www.rootop.org/pages/890.html””Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36” –

nagios添加新的主机监控刷新显示再刷新几次又不显示

nagios添加新的主机监控刷新显示再刷新几次又不显示,这个问题弄的无比纠结,以为是浏览器缓存问题,换了几个还是这样,重启nagios服务还是如此,无意间ps了一下,发现启动了两个nagios进程。

找到问题了,用kill杀死,重新启动,问题解决。

至于为什么这样,可能是之前重启服务器的时候没找到锁文件,导致启动了俩进程,一个进程是没添加之前的另一个是添加之后的,导致出现问题。

之前出现的错误:

[root@nagios objects]# service nagios restart
Running configuration check…done.
Stopping nagios: No lock file found in /usr/local/nagios/var/nagios.lock
Starting nagios:This account is currently not available.
done.

nagios添加图表(小太阳)支持

nagios监控服务器只显示当前的信息,没有图表功能,不能查看历史状态,为了解决这个问题
需要安装PNP来实现这个功能,pnp基于PHP和Perl可以利用rrdtool工具将Nagios采集的数据绘制成图表显示主机或服务在一段时间内的运行情况。

系统环境:
centos6.4_x64,selinux关闭,iptables关闭。

下载rrdtool:
http://oss.oetiker.ch/rrdtool/pub/rrdtool.tar.gz
下载pnp:
wget -c http://nchc.dl.sourceforge.net/project/pnp4nagios/PNP/pnp-0.4.14/pnp-0.4.14.tar.gz

安装rrdtool和pnp需要几个rpm包支持,有gd、gd-devel、zlib、zlib-devel、pango、pango-devel、perl-devel、perl-CPAN、perl-Time-HiRes

[root@localhost rrdtool-1.4.8]# yum install pango pango-devel perl-devel perl-CPAN perl-Time-HiRes -y  #其它的包之前已经装过
[root@localhost rrdtool-1.4.8]# ./configure --prefix=/usr/local/rrdtool
[root@localhost rrdtool-1.4.8]# make && make install

安装pnp:
在安装pnp之前需要执行一个命令来检查是否有相关模块:

[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'
Can't locate RRDs.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

提示找不到RRDs.pm,通过软链接解决:

[root@localhost pnp-0.4.14]# ln -s /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/RRDs.pm /usr/lib64/perl5/

再次检查,出现另一个错误:

[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'
Can't locate loadable object for module RRDs in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at -e line 0
Compilation failed in require.
BEGIN failed--compilation aborted.

提示库文件找不到,解决办法:

[root@localhost pnp-0.4.14]# ln -s /usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/auto/RRDs/RRDs.so /usr/lib64/perl5/
[root@localhost pnp-0.4.14]# perl -MRRDs -le 'print q(ok!)'  #重新检查,通过。
ok!
[root@localhost pnp-0.4.14]# mkdir /usr/local/nagios/share/perfdata  #保存RRD Database Files
[root@localhost pnp-0.4.14]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata --with-perl_lib=/usr/local/rrdtool/lib/perl/5.10.1/x86_64-linux-thread-multi/
[root@localhost pnp-0.4.14]# make all
[root@localhost pnp-0.4.14]# make install
[root@localhost pnp-0.4.14]# make install-config
[root@localhost pnp-0.4.14]# make install-init

配置pnp:
在PNP安装完成后,默认安装目录下自带了模板配置文件,只需将模板文件全部复制一份即可。

[root@localhost pnp-0.4.14]# cd /usr/local/nagios/etc/pnp/
[root@localhost pnp]# ls
background.pdf  check_commands  config.php  npcd.cfg-sample  pages  pnp4nagios_release  process_perfdata.cfg-sample  rra.cfg-sample
[root@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@localhost pnp]# cp npcd.cfg-sample npcd.cfg
[root@localhost pnp]# cp rra.cfg-sample rra.cfg
[root@localhost pnp]# chown nagios:nagios *  #将所有配置文件属主属组都改为nagios运行账户nagios

编辑process_perfdata.cfg:
LOG_LEVEL = 2 #把0改为2
修改nagios配置文件来增加小太阳图标:
修改templates.cfg,增加一个定义PNP的host和service。

[root@localhost ~]# vi /usr/local/nagios/etc/objects/templates.cfg
#pnp host definition
define host {
        name                            hosts-pnp
        register                        0
        action_url                      /nagios/pnp/index.php?host=$HOSTNAME$
        process_perf_data               0
}
#pnp service
define service {
        name                            services-pnp
        register                        0
        action_url                      /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
        process_perf_data               0
}

修改nagios.cfg:

[root@localhost ~]# vi /usr/local/nagios/etc/nagios.cfg
process_performance_data=1  #0改为1
host_perfdata_command=process-host-perfdata  #将前面的注释去掉
service_perfdata_command=process-service-perfdata

修改commands.cfg:
process-host-perfdata和process-service-perfdata指令声明了nagios输出哪些值到输出文件中。不过这些定义相对简单,而PNP提供了一个Perl脚本,非常详细地定义了一个输出数据的方法,process_perfdata.pl就是PNP自带的一个脚本,这个脚本在PNP安装完成后会自动生成。因此可以将process-host-perfdata和process-service-perfdata指令中对应的执行命令的内容替换成此脚本。

[root@localhost ~]# vi /usr/local/nagios/etc/objects/commands.cfg
# 'process-host-perfdata' command definition
define command{
        command_name    process-host-perfdata        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }


# 'process-service-perfdata' command definition
define command{
        command_name    process-service-perfdata        command_line    /usr/local/nagios/libexec/process_perfdata.pl
        }

修改hosts.cfg文件和services.cfg文件:
将hosts-pnp和services-pnp引用到hosts.cfg和services.cfg中

define host{
        use                     linux-server,hosts-pnp            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost
        alias
        address                 127.0.0.1
        }

define service{
        use                             local-service,services-pnp         ; Name of service template to use
        host_name                       localhost
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }

重启nagios服务。
完成。

PS:
编译rrdtool时的一个错误,因为当时没安装pango和pango-devel包。

configure: error: Please fix the library issues listed above and try again
[root@localhost rrdtool-1.4.8]# yum install pango pango-devel -y

编译rrdtool时在make的时候一个报错,因为当时没安装perl-devel perl-CPAN:

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 1.
BEGIN failed--compilation aborted at Makefile.PL line 1.

解决方法:
[root@localhost rrdtool-1.4.8]# yum install -y perl-devel perl-CPAN
重新configure

如果不通过perl -MRRDs -le ‘print q(ok!)’来检查环境,可能在编译安装pnp的时候报错:
WARNING: The RRDs Perl Modules are not found on your system
Using RRDs will speedup things in larger installations.

安装pnp,在configure时一个报错,因为最开始的时候没有安装perl-Time-HiRes:
checking for Perl Module Time::HiRes… no
configure: error: Perl Module Time::HiRes not available
解决办法:
[root@localhost pnp-0.4.14]# yum install -y perl-Time-HiRes

 

HTTP WARNING: HTTP/1.1 403 Forbidden – 5237 bytes in 0.001 second response time

HTTP WARNING: HTTP/1.1 403 Forbidden – 5237 bytes in 0.001 second response time

nagios监控了一台服务器的web服务,老提示403错误,后来用火狐的firebug插件才看出来默认apache的欢迎页返回的状态码为403,一直以为欢迎页也属于200状态码。


解决方法:
在/var/www/html下创建个index.html文件即可。
然后过会儿HTTP监控就正常了,或者可以直接重定制该服务的计划检测时间,快速恢复。