A-A+

nagios安装配置

2015年09月28日 nagios+cacti 暂无评论 阅读 2,042 views 次

nagios 的安装

直接上命令

useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/
cd  /usr/local/nagios/
wget http://roma01.haima.ileadgame.net/nagios.zip
tar -zxf nagios-3.4.1.tar.gz
cd nagios
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
chkconfig --add nagios
chkconfig nagios on
chkconfig --list nagios
yum install openssl-devel

nagios 插件安装

tar -zxf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-openssl=/usr/include/openssl --prefix=/usr/local/nagios
make
make install

汉化

tar jxf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
./configure
make all
make install

如果出现以下错误:/usr/bin/ld: i386:x86-64 architecture of input file `extcmd_list.o' is incompatible with i386 output
把英文版的/cgi/cmd.cgi复制到中文版的就ok

apache配置文件修改

vi /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var ----->DirectoryIndex index.html index.php
#增加
AddType application/x-httpd-php .php

增加以下部分

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

<Directory "/usr/local/nagios/sbin">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>

Alias /nagios "/usr/local/nagios/share"

<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>

配置访问用户

htpasswd -c /usr/local/nagios/etc/htpasswd username
New password:
Re-type new password:
Adding password for user username

输入密码
GcGG53HdcbRxoNXJFwIosPL9iC4FOq

/usr/local/nagios/bin/nagios -v nagios.cfg #检查配置

如出现以下错误的解决方法:Error in configuration file '/usr/local/nagios/etc/nagios.cfg' - Line 1031 (UNKNOWN VARIABLE)
1031 #service_check_timeout_state=c 注释掉

修改完配置文件需要重启nagios:service nagios reload
需要注意:使用到的所有cfg文件包括服务器监控配置文件都需要在nagios.cfg中引用。

vi cgi.cfg #修改管理用户
default_user_name=username
authorized_for_system_information=nagiosadmin,username
authorized_for_configuration_information=nagiosadmin,username
authorized_for_system_commands=nagiosadmin,username et
authorized_for_all_services=nagiosadmin,username
authorized_for_all_hosts=nagiosadmin,username
authorized_for_all_service_commands=nagiosadmin,username
authorized_for_all_host_commands=nagiosadmin,username

重启nagios

service nagios reload
service nagios restart

访问网页测试(如不能打开 修改vi /etc/passwd 把/sbin/nologin改为/bin/bash)

配置报警发送邮件

修改objects目录下contacts.cfg,在email那行加上各个邮箱

define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user

email 1388888888@139.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

define contact{
contact_name nagioshu ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user

email 1388888888@139.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

define contact{
contact_name nagioslin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user

email 1388888888@139.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,nagiossu,nagiosuser
}

objects 下commands.cfg 的 两个command中发件邮件配置的修改

# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f nagiosmonitor@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu nagiosmonitor -xp password -l /var/log/sendEmail.log
}

# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f nagiosmonitor@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu nagiosmonitor -xp password -l /var/log/sendEmail.log
}

邮件发送程序sendmail

###############################################################
sendEmail安装

#tar -zxf sendEmail-v1.56.tar.gz
#cd sendEmail-v1.56
#cp sendEmail /usr/local/bin

然后给确认确实它具有执行权限

ll /usr/local/bin/sendEmail
-rwxr-xr-x 1 root root 77882 11-03 14:23 /usr/local/bin/sendEmail

这样程序就装好了,使用也很简单.直接运行sendEmail就会显示详细的用法
先看一个典型的例子

/usr/local/bin/sendEmail –f sender@163.com –t receiver@163.com –s smtp.163.com –u “from nagios” –xu sender –xp 123456 –m happy

解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容
如果你不带-m参数的话,就会提示你自行输入
Reading message body from STDIN because the ‘-m’ option was not used.
If you are manually typing in a message:
- First line must be received within 60 seconds.
- End manual input with a CTRL-D on its own line

输入完成后使用CTRL-D来结束
当然我们也可以将一个文件的内容作为邮件的正文发出去的
那么就可以使用:
cat 文件名 | /usr/local/bin/sendEmail –f sender@163.com –t receiver@163.com –s smtp.163.com –u "subject” –xu sender –xp 123456 –m happy
有关sendEmail的用法就讲到这里
既然nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中关于发邮件的命令的定义,我们现在来修改notify-by-email这个命令,如下(注意其中粗体的部分)
# 'notify-by-email' command definition
define command{
command_name notify-by-email
command_line /usr/bin/printf "%b" "***** Nagios 2.9 *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail -f sender@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu sender -xp 123456
}

注:其实sendEmail是一个十分有用的程序,我们在这个地方用了它,其实别的地方也可以用,典型的好处就是你不需要每台机器都装sendmail,开启smtp服务.直接用现成的一台邮件服务器就行了,这无疑很大的加强了系统的安全性,也节约了资源

nagios

给我留言

本站理念:

致力于运维技术的分享,运维前源技术的探讨,欢迎广大朋友一起参与,一起分享,共同成长。

交流探讨:

QQ群:26489714