Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。 tar jxf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
useradd nagios
usermod -G nagios nginx
yum install gd-devel perl-ExtUtils-Embed
./configure --enable-embedded-perl
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconfig //在apache下输入这条命令能快速整合,nginx下不能使用
这里安装的是nagios的主程序,安装完成我们发现,好多命令都没有,下面我们就让那些命令出现
tar zxf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --enable-extra-opts --enable-libtap --enable-perl-modules --with-nagios-user=nagios --with-nagios-group=nagios //这里加入了一些模块,并且指定安装之后的命令属于nagios用户
make
make install
这个命令可以检查nagios配置文件是否有语法错误
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
tar zxf ExtUtils-Constant-0.23.tar.gz
使nagios在nginx支持
cd ExtUtils-Constant-0.23
perl Makefile.PL
make && make install
tar zxf FCGI-0.74.tar.gz
cd FCGI-0.74 perl Makefile.PL
make
make install
tar zxf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL make
make install
tar zxf Getopt-Long-2.38.tar.gz
cd Getopt-Long-2.38 perl Makefile.PL make
make install
tar zxf IO-All-0.44.tar.gz
cd IO-All-0.44
perl Makefile.PL
make make install
tar zxf Socket-2.001.tar.gz cd Socket-2.001
perl Makefile.PL
make && make install
期间可能需要的包
yum install perl-ExtUtils-CBuilder
这个文件是下载得到的
cp fastcgi-wrapper.pl /usr/bin/
[root@server4 ~]# fastcgi-wrapper.pl //开启fastcgi [root@server4 ~]# netstat -antlp|grep 8999 //检查是否开启 tcp 0 0 127.0.0.1:8999 0.0.0.0:* LISTEN 14456/perl
编辑nginx配置文件,在网页中显示
cd /usr/local/lanmp/nginx/conf/
vim nginx.conf
在其中添加一下内容(添加一个虚拟主机)
server{ listen 80; charset utf-8; #默认字符集,解决页面乱码 server_name nagios.westos.org; root /usr/local/nagios/share; index index.php index.html; auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/htpasswd.users; location /nagios { alias /usr/local/nagios/share; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; } location ~ ^/nagios/cgi-bin/ { root /usr/local/nagios/sbin/;
#在执行nagios命令时,网页之间访问的是nagios/cgi-bin/*.cgi, #这里我们要进行转码,以*.cgi的方式直接访问所以这里这样写。。。 rewrite ^/nagios/cgi-bin(.*)\.cgi /$1.cgi break; include fastcgi.conf; fastcgi_param AUTH_USER $remote_user; #nagios认证 fastcgi_param REMOTE_USER $remote_user; fastcgi_pass 127.0.0.1:8999; }
这其中需要建立一个认证文件
需要安装这个包
yum install httpd-tools-2.2.15-15.el6_2.1.x86_64
htpasswd -m /usr/local/nagios/etc/htpasswd.users nagiosadmin //修改nagiosadmin密码 //htpasswd.users这个文件已经存在,我们只需要把里面的管理员密码修改一下
nginx -t nginx -s reload //重启nginx,然后我们就可以在网页中显示nagios了,^_^
现在访问某些页面可能会有一些错误出现
所以我们需要安装一些包
rpm -ivh rrdtool-perl-1.3.8-6.el6.x86_64.rpm yum install perl-CGI //使网页正常显示图形
yum install -y cjkuni-uming-fonts //使网页正常显示中文
之后配置nagios,显示我们要的主机信息,和服务信息
cd /usr/local/nagios/etc/
vim nagios.cfg
添加2项,注释一项
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //为了分开管理,我们定义一个主机管理文件 和服务管理文件
cfg_file=/usr/local/nagios/etc/objects/services.cfg //服务管理文件
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cd objects/
cp localhost.cfg hosts.cfg //拷贝模板
define host{ //主机的管理 use linux-server //使用默认的主机规则 host_name server4.example.com //主机名 alias Nagios server4 //主机别名 address 192.168.0.104 //ip地址 icon_image switch.gif //图标 statusmap_image switch.gd2 2d_coords 100,200 //2D图像的显示坐标 3d_coords 100,200,100 //3D图像的显示坐标
vim hosts.cfg 编辑
}
define hostgroup{ //主机组的管理 hostgroup_name linux-servers ; //主机组的名字 alias Linux Servers ; //别名 members * ; //*代表包含上面所有主机
}
cp localhost.cfg services.cfg
vim services.cfg
define servicegroup{ servicegroup_name 系统负荷检查 alias 负荷检查 members server4.example.com,进程总数,server4.example.com,登录用户数,server4.example.com,根分区,server4.example.com,交换空间利用率 }
define service{ use local-service ; Name of service template to use host_name * service_description PING //表示监控ping服务 check_command check_ping!100.0,20%!500.0,60% } define service{ use local-service ; Name of service template to use host_name server4.example.com service_description 根分区 check_command check_local_disk!20%!10%!/
}
define service{ use local-service ; Name of service template to use host_name server4.example.com service_description 登录用户数 check_command check_local_users!20!50 } define service{ use local-service ; Name of service template to use host_name server4.example.com service_description 进程总数 check_command check_local_procs!250!400!RSZDT }
define service{ use local-service ; Name of service template to use host_name server4.example.com service_description 系统负荷 check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0 }
define service{ use local-service ; Name of service template to use host_name server4.example.com service_description 交换空间利用率 check_command check_local_swap!20!10 }
define service{ use local-service ; Name of service template to use host_name server4.example.com service_description SSH check_command check_tcp!22!1.0!10.0 notifications_enabled 0 //这里表示不进行通知 }
define service{ use local-service ; Name of service template to use host_name server4.example.com service_description HTTP check_command check_http notifications_enabled 0 }
完成之后检查是否有语法错误
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios reload //重新读配置文件