一,安装LAMP环境
#yum -y install httpd gccg libcg libc-commonphpgdgd-devel libpng libmng libjpeg zlib
#service httpd restart
#echo "<?phpinfo();?>" >/var/www/html/index.php
在浏览器里输入http://192.168.77.83//主机IP
Lamp环境成功安装!
二,安装nagios
#groupaddnagcmd
#useradd-mnagios
#usermod-Gnagcmdnagios
#usermod-Gnagcmdapache
#tarzxvfnagios-3.2.0.tar.gz-C/usr/src//将nagios安装程序解压到这个目录下
#cd/usr/src/nagios-3.2.0
#./configure--with-command-group=nagcmd--with-gd-lib=/usr/lib/--with-gd-inc=/usr/include
#makeall//安装二进制运行程序、初始化脚本、配置文件样本并设置运行目录权限
#makeinstall//安装生成/usr/local/nagios/share的目录(即nagiosWEB访问界面的站点目录)
#makeinstall-init//安装生成/etc/rc.d/init.d/nagios启动脚本
#makeinstall-config//安装生成/usr/local/nagios/etc下的nagios相关配置文件
#makeinstall-webconf//安装Nagios的WEB配置文件到Apache的conf.d目录下
#makeinstall-commandmode//设定相应nagios工作目录的权限
检测nagios安装情况---------nagios默认安装到/usr/local/nagios。
#ls/usr/local/nagios
如有bin、etc、share、sbin、var这5个目录,则表示安装成功。
nagios目录说明:
bin有一个重要的nagios命令,用于检查nagios主配置文件nagios.cfg是否有语法等错误
sbinNagiosCgi文件所在目录,也就是执行外部命令所需文件所在的目录
share此目录就是上面WEB接口访问的站点目录
etc配置文件
var存放启动nagios后的相关文件及日志
二,配置nagios的web接口
#vi/etc/httpd/conf.d/nagios.conf
ScriptAlias/nagios/cgi-bin"/usr/local/nagios/sbin"
<Directory"/usr/local/nagios/sbin">
#SSLRequireSSL
OptionsExecCGI
AllowOverrideNone
Orderallow,deny
Allowfromall
#Orderdeny,allow
#Denyfromall
#Allowfrom127.0.0.1
AuthName"NagiosAccess"
AuthTypeBasic
AuthUserFile/usr/local/nagios/etc/htpasswd.users
Requirevalid-user
</Directory>
Alias/nagios"/usr/local/nagios/share"
<Directory"/usr/local/nagios/share">
#SSLRequireSSL
OptionsNone
AllowOverrideNone
Orderallow,deny
Allowfromall
#Orderdeny,allow
#Denyfromall
#Allowfrom127.0.0.1
AuthName"NagiosAccess"
AuthTypeBasic
AuthUserFile/usr/local/nagios/etc/htpasswd.users
Requirevalid-user
</Directory>
创建nagios用户登录。用户名为nagiosadmin密码123456
#htpasswd-c/usr/local/nagios/etc/htpasswd.usersnagiosadmin
然后查看这个用户是否创建
#cat/usr/local/nagios/etc/htpasswd.users
这里的默认用户为nagiosadmin,自定义的用户请在cgi.cfg配置文件里添加
后面用逗号隔开
三,安装nagios插件及汉化包
1,安装nagios插件(mysql-devel)
#tarzxvfnagios-plugins-1.4.14.tar.gz-C/usr/scr
#cd/usr/src/nagios-plugins-1.4.14
#./configure--with-nagios-user=nagios--with-nagios-group=nagcmd
#make
#makeinstall
2,安装nrpe
#yum-yinstallopenssl-devel
#tarzxvfnrpe-2.12.tar.gz-C/usr/src/
#cd/usr/src/nrpe-2.12
#./configure
#makeall&&makeinstall
#makeinstall-plugin
3,安装汉化包
#tarjxvfnagios-cn-3.2.0.tar.bz2-C/usr/src
#cd/usr/src/nagios-cn-3.2.0
#./configure
#makeall
#makeinstall
SELinux下指定强制目标模式:
#chcon-R-thttpd_sys_content_t/usr/local/nagios/sbin/
#chcon-R-thttpd_sys_content_t/usr/local/nagios/share/
启动nagios
#chkconfig--addnagios
#chkconfig--level35nagioson
#chkconfig--listnagios
#servicenagiosrestart
登录测试:
http://IP/nagios/用户名:nagiosadmin密码:123456
四,添加要监控的主机和服务
1,我们为了方便维护,首先将主机和主机组以及服务和服务组都划分开来。
#cd/usr/local/nagios/etc
#mkdirhosts//用来定义主机
#mkdirservice//用来定义服务
#vi/usr/local/nagios/etc/nagios.cfg
在这里添加如下两行,对应hosts和service这两个路劲
cfg_dir=/usr/local/nagios/etc/hosts
cfg_dir=/usr/local/nagios/etc/service
然后我将定义主机组的配置文件放到objects这个目录下,名字为hosts.cfg
#cd/usr/local/nagios/etc/objects
#vihosts.cfg
definehostgroup{
hostgroup_namelinux-servers;linux主机组名
aliashkrtlinuxserver;别名
members201;成员主机,这里我们以201这台主机为例
}
definehostgroup{
hostgroup_namewindows-servers;windows主机组名
aliashkrtwinserver;别名
members126;主机成员
}
将主机组这个配置文件的路劲添加到nagios.cfg文件中
#cd/usr/local/nagios/etc/nagios.cfg
添加以下一行内容
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
2,我们要监控一台linux主机,以192.168.0.201这台为例。配置文件为201.cfg
#vi/usr/local/nagios/etc/hosts/201.cfg//这里是201主机的配置文件
添加以下内容:
definehost{
host_name201//主机名
alias201//别名
address192.168.1.201//主机的IP
check_commandcheck-host-alive
uselinux-server
max_check_attempts10
check_period24x7
event_handlernotify-host-by-email
process_perf_data1
contact_groupsadmins
notification_interval20
notification_period24x7
notification_optionsd,r
2d_coords0,700
3d_coords0,700,000
}
#vi/usr/local/nagios/etc/service/201.cfg//配置要监控的服务
添加以下内容:
defineservice{
service_descriptionHTTP
check_commandcheck_nrpe!check_http
host_name201
check_period24x7
notification_period24x7
contact_groupsadmins
notification_interval15
notification_optionsw,u,c,r
max_check_attempts3
check_interval5
retry_interval1
active_checks_enabled1
passive_checks_enabled0
notifications_enabled1
check_freshness0
freshness_threshold86400
usegeneric-service
}
4,添加联系人,修改邮箱地址
#vi/usr/local/nagios/etc/objects/contacts.cfg
5,#vi/usr/local/nagios/etc/objects/commands.cfg
在最最后面添加以下内容
definecommand{
command_namecheck_nrpe
command_line$USER1$/check_nrpe-H$HOSTADDRESS$-c$ARG1$-t30
}
#/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg//检查nagios配置
五,配置飞信报警
#tarzxvffetion20091117-linux.tar.gz–C/usr/local/feixin
#vi/etc/ld.so.conf
底部添加
/usr/local/feixin
#chmod777/usr/local/feixin/fetion
#ldconfig
执行#/usr/local/feixin/fetion将输出一下信息
测试:
#/usr/local/feixin/fetion--mobile=13800138000--pwd=123456--to=13800138000--msg-gb="Hellonagios"–debug
1,#vi/usr/local/nagios/etc/objects/commands.cfg
#byfetionsms
definecommand{
command_namenotify-service-by-sms
command_line/usr/local/feixin/fetion--mobile=13800138000--pwd=123456--to=$CONTACTPAGER$--msg-utf8="'$HOSTADDRESS$'$HOSTALIAS$/$SERVICEDESC$is$SERVICESTATE$"
}
definecommand{
command_namenotify-host-by-sms
command_line/usr/local/feixin/fetion--mobile=13800138000--pwd=123456--to=$CONTACTPAGER$--msg-utf8="HOST$HOSTSTATE$alertfor$HOSTNAME$!on'$DATETIME$'"
}
2,#viobjects/templates.cfg
definecontact{
namegeneric-contact;Thenameofthiscontacttemplate
service_notification_period24x7;servicenotificationscanbesentanytime
host_notification_period24x7;hostnotificationscanbesentanytime
service_notification_optionsw,u,c,r,f,s;sendnotificationsforallservicestates,flappingevents,andscheduleddowntimeevents
host_notification_optionsd,u,r,f,s;sendnotificationsforallhoststates,flappingevents,andscheduleddowntimeevents
service_notification_commandsnotify-service-by-email,notify-service-by-sms;sendservicenotificationsviaemail
host_notification_commandsnotify-host-by-email,notify-host-by-sms;
3,#viobjects/contacts.cfg
definecontact{
contact_namenagiosadmin;Shortnameofuser
usegeneric-contact;Inheritdefaultvaluesfromgeneric-contacttemplate(definedabove)
aliasNagiosAdmin;Fullnameofuser
emailmrw621@yahoo.cn;<<*****CHANGETHISTOYOUREMAILADDRESS******
pager13800138000
}
服务器端已搭建完毕!