Nagios 配置及监控

nagios配置监控datanode的个数 shell监测datanode个数返回给nagios 步骤一:新建check_datanodes.sh文件并修改权限 [root@master libexec]# vim check_datanodes.sh #!/bin/sh # set java environment export JAVA_HOME=/usr/local/src/java export JRE_HOME=/usr/local/src/java/jre export CLASSPATH=.:$CLASSPATH:$JAVA_HOM 阅读详情

Nagios

监控


在互联网日益发展的今天,监控的重要性已经不言而喻。可能打开一个URL要经过6-7层的处理,如果出了问题而没有监控将很难定位到问题所在。那哪些内容需要监控呢?

1.本地资源

(1) 负载:uptime; 
(2) CPU:top,sar,cpu温度; 
(3) 磁盘:df; 
(4) 内存:free; 
(5) IO:iostat; 
(6) RAID 
(7) passwd文件的变化(本地所有文件指纹识别)。

2.网络服务

端口、URL、ping丢包、进程数、IDC网络流量

3.其他设备

路由器、交换机端口流量、打印机、windows等

4.业务数据

用户登录失败次数,用户登录网站次数,输入验证码失败次数、某个API接口流量并发,电商网站订单,支付交易的数量等。这个获取的过程可能是开发或者架构师完成的,但添加的过程就是运维;


监控软件本身仅仅是一个平台,我们想监控的内容,理论上只要在服务器命令行可以获取到就可以被监控软件监控。

1.1 Nagios简介

Nagios又被称为难够死,因为很难。Nagios是一款开源的网络及服务的监控工具,其功能强大、灵活性强。能有效监控windows、Linux和Unix等系统的主机各种状态信息,交换机、路由器等网络设备,以及主机端口及URL服务等。根据不同业务故障级别发出告警信息(邮件、微信、短信、语音报警、飞信、MSN)给管理员,当故障恢复时也会发出恢复消息给管理员。 
Nagios服务端可以在Unix及类Unix系统上运行,目前无法运行在windows。Windows可以作为被监控的主机,但是无法作为监控服务器。

1.2 Nagios构成

Nagios不好的地方在于它只做核心,很多其他功能都是通过插件来实现的。Nagios一般由一个主程序(Nagios),一个插件程序(Nagios-plugins)和一些可选的附加程序(NRPE,NSClient++,NSCA,NDOUtils)等。Nagios本身就是一个监控的平台而已,其具体的监控工作都是通过插件(Nagios-plugins,也可自己编写)来实现的。因此,Nagios主程序和Nagios-plugins插件都是Nagios服务端必须安装的程序组件,并且Nagios-plugins一般也要安装于被监控端。几个附加程序的描述如下: 
1. NRPE:半被动模式 
(1) 存在位置:工作在被监控端,操作系统为Linux/Unix; 
(2) 作用:用于在被监控的远程Linux/Unix主机上执行脚本插件获取数据回传给服务器端,以实现对这些主机资源的监控。主要用于监控本地资源; 
(3) 存在形式:守护进程(agent)模式,开启的端口为5666. 
2. NSClient++:半被动模式 
(1) 存在位置:监控Windows主机; 
(2) 作用:相当于Linux下的NRPE; 
3. NDOUtils:不推荐使用 
(1) 存在位置:Nagios服务器端; 
(2)作用:用于将Nagios的配置信息和各event产生的数据存入数据库以实现对这些数据的检索和处理。但是存入数据库还不如存放在磁盘上,因此推荐使用; 
4. NSCA:纯被动模式的监控 
(1)存在位置:同时安装在Nagios的服务器端和客户端; 
(2) 作用:用于让被监控的远程Linux/Unix主机主动将监控到的信息发送给Nagios服务器。在分布式监控集群模式中要用到,300台服务器以内可以不考虑;

1.3 原理

1.png-34.9kB
2.png-90kB

1.4 安装

三台虚拟机: 
3.png-5.5kB

1.4.1 服务端

调整字符集,如果不安装后面安装一些插件会有错误:

 
  1. [root@nagios ~]# echo 'exprot LC_ALL=C' >>/etc/profile
  2. [root@nagios ~]# . /etc/profile

关闭防火墙:

 
  1. [root@nagios ~]# /etc/init.d/iptables stop
  2. [root@nagios ~]# chkconfig iptables off

关闭selinux:

 
  1. [root@nagios ~]# setenforce 0
  2. [root@nagios ~]# sed -ri 's#^(SELINUX=).*#\1disabled#g' /etc/selinux/config

时间同步,监控的时间同步很重要:

 
  1. echo '# time sync' >>/var/spool/cron/root
  2. echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null' >>/var/spool/cron/root

需要安装web展示相关软件:

 
  1. [root@nagios ~]# yum install -y gcc glibc glibc-common gd gd-devel httpd php php-gd mysql*

gcc glibc glibc-common gcc编译器 
gd gd-devel画图用的 
httpd php \php-gd php环境,官方推荐LAMP,不要改成Nginx 
mysql* 生成MySQL的插件,MySQL不需要启动 
准备所需软件:

 
  1. [root@nagios hadoop]# tree
  2. .
  3. |-- Class-Accessor-0.31.tar.gz # iostat监控插件所需
  4. |-- Config-Tiny-2.12.tar.gz # 同上
  5. |-- Math-Calc-Units-1.07.tar.gz # 同上
  6. |-- Nagios-Plugin-0.34.tar.gz # 同上
  7. |-- Params-Validate-0.91.tar.gz # 同上
  8. |-- Regexp-Common-2010010201.tar.gz # 同上
  9. |-- check_iostat # 用于监控的插件
  10. |-- check_memory.pl # 用于监控的插件
  11. |-- check_mysql # 用于监控的插件
  12. |-- libart_lgpl-2.3.17.tar.gz # iostat监控插件所需
  13. |-- nagios-3.5.1.tar.gz # 主程序
  14. |-- nagios-plugins-1.4.16.tar.gz # 插件管理
  15. |-- nrpe-2.12.tar.gz # agent
  16. |-- pnp-0.4.14.tar.gz # 画图的web程序,这个是展示
  17. `-- rrdtool-1.2.14.tar.gz # pnp的辅助程序,这个才是画图的

软件包下载 密码:usyr

创建所需用户,并将apache和nagios同属于一个组,方便一起管理:

 
  1. useradd -m nagios
  2. groupadd nagcmd
  3. usermod -a -G nagcmd nagios
  4. usermod -a -G nagcmd apache

安装:

 
  1. [root@nagios hadoop]# tar xf nagios-3.5.1.tar.gz
  2. [root@nagios hadoop]# cd nagios
  3. [root@nagios nagios]# ./configure --with-command-group=nagcmd
  4. [root@nagios nagios]# make all
  5. [root@nagios nagios]# make install
  6. …
  7. make install-init
  8. - This installs the init script in /etc/rc.d/init.d
  9. make install-commandmode
  10. - This installs and configures permissions on the
  11. directory for holding the external command file
  12. make install-config
  13. - This installs sample config files in /usr/local/nagios/etc
  14. make[1]: Leaving directory `/admin/hadoop/nagios'

这是编译后出现的信息提示,可以时候以上三种编译方式生成启动脚本、命令和配置文件样例

 
  1. [root@nagios nagios]# make install-init && make install-config && make install-commandmode
  2. [root@nagios nagios]# make install-webconf # 生成了如何在httpd中显示Nagios的配置文件
  3. /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf # 就是这个文件

配置web认证:

 
  1. htpasswd -cb /usr/local/nagios/etc/htpasswd.users oldboy 123456

# -b:非交互 
安装插件:

 
  1. yum install -y perl-devel
  2. tar xf nagios-plugins-1.4.16.tar.gz
  3. cd nagios-plugins-1.4.16
  4. ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
  5. make && make install
  6. cd ..
  7. ls /usr/local/nagios/libexec/|wc -l # 插件有61个,多比少好

安装NRPE,之所以服务端也安装NRPE,一是是因为要在…/nagios/libexec/目录下生成check_nrpe命令;二是服务端也要通过NRPE进行监控:

 
  1. tar xf nrpe-2.12.tar.gz
  2. cd nrpe-2.12
  3. ./configure
  4. make all
  5. make install-plugin
  6. make install-daemon
  7. make install-daemon-config
  8. cd ..

最后启动,Nagios是不会监听端口的,因为不会有程序发数据给它。

 
  1. /etc/init.d/httpd start
  2. /etc/init.d/nagios start
  3. lsof -i :80
  4. ps aux |grep nagios

1.4.2 被监控端

调整字符集,如果不安装后面安装一些插件会有错误:

 
  1. echo 'exprot LC_ALL=C' >>/etc/profile
  2. . /etc/profile

关闭防火墙:

 
  1. /etc/init.d/iptables stop
  2. chkconfig iptables off

时间同步,监控的时间同步很重要:

 
  1. echo '# time sync' >>/var/spool/cron/root
  2. echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null' >>/var/spool/cron/root

创建用户:

 
  1. [root@lamp ~]# useradd -m nagios -s /sbin/nologin

安装perl

 
  1. yum install -y perl-devel perl-CPAN openssl*
  2. yum install -y mysql-server # 这是为了解决报错

安装插件:

 
  1. tar xf nagios-plugins-1.4.16.tar.gz
  2. cd nagios-plugins-1.4.16
  3. ./configure --prefix=/usr/local/nagios --enable-perl-modules --enable-redhat-pthread-workaround #--with-mysql=/usr/bin/mysql
  4. # --enable-redhat-pthread-workaround:如果redhat系统不加这个会很慢
  5. # --with-mysql是为了解决make: *** [all] Error 2
  6. make && make install
  7. cd ..
  8. ls /usr/local/nagios/libexec/|wc -l # 结果为59而不是61估计跟MySQL有关

安装NRPE:

 
  1. tar xf nrpe-2.12.tar.gz
  2. cd nrpe-2.12
  3. ./configure
  4. make all
  5. make install-plugin
  6. make install-daemon
  7. make install-daemon-config
  8. cd ..

安装iostat:

 
  1. tar xf Params-Validate-0.91.tar.gz
  2. cd Params-Validate-0.91
  3. perl Makefile.PL
  4. make
  5. make install
  6. cd ..
  7. tar xf Class-Accessor-0.31.tar.gz
  8. cd Class-Accessor-0.31
  9. perl Makefile.PL
  10. make && make install
  11. cd ..
  12. tar xf Config-Tiny-2.12.tar.gz
  13. cd Config-Tiny-2.12
  14. perl Makefile.PL
  15. echo $?
  16. make && make install
  17. cd ..
  18. tar xf Math-Calc-Units-1.07.tar.gz
  19. cd Math-Calc-Units-1.07
  20. perl Makefile.PL
  21. make && make install
  22. cd ..
  23. tar xf Regexp-Common-2010010201.tar.gz
  24. cd Regexp-Common-2010010201
  25. perl Makefile.PL
  26. make && make install
  27. cd ..
  28. tar xf Nagios-Plugin-0.34.tar.gz
  29. cd Nagios-Plugin-0.34
  30. perl Makefile.PL
  31. make
  32. make install
  33. cd ..
  34. yum install -y sysstat
  35. cp /root/tools/check_memory.pl /usr/local/nagios/libexec/
  36. cp /root/tools/check_iostat /usr/local/nagios/libexec/
  37. chmod 755 /usr/local/nagios/libexec/check_memory.pl
  38. chmod 755 /usr/local/nagios/libexec/check_iostat
  39. yum install -y dos2unix
  40. dos2unix /usr/local/nagios/libexec/check_iostat
  41. dos2unix /usr/local/nagios/libexec/check_memory.pl

修改客户端的配置文件:

 
  1. cd /usr/local/nagios/etc/
  2. cp nrpe.cfg{,.bak}
  3. sed -ri 's@^allowed_hosts.*@&,10.0.0.1@g' nrpe.cfg # 允许10.0.0.1监控
  4. sed -ri '199,203d' nrpe.cfg

# 以下是用来告诉NRPE这个守护进程怎么去监控

 
  1. echo "command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20" >>nrpe.cfg
  2. echo "command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%" >>nrpe.cfg
  3. echo "command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /" >>nrpe.cfg
  4. echo "command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%" >>nrpe.cfg
  5. echo "command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10" >>nrpe.cfg

# [check_load]:相当于模块名 
# /usr/local/nagios/libexec/check_load:获取资源的命令 
# 也就是说通过调用check_load就相当于执行/usr/local/nagios/libexec/check_load这个命令 
启动NRPE:

 
  1. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

# -c:指定配置文件 
# -d:daemon

 
  1. echo "# nagios nrpe process cmd" >>/etc/rc.local # 开机自启
  2. echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >>/etc/rc.local
  3. ss -lnt|grep 5666

1.5 开始监控

监控之前也要在服务器端安装客户端软件。 
Nagios安装生成的文件:

 
  1. [root@nagios nagios]# pwd
  2. /usr/local/nagios
  3. [root@nagios nagios]# tree -L 1
  4. .
  5. |-- bin
  6. |-- etc
  7. |-- libexec # 插件
  8. |-- sbin # CGI程序
  9. |-- share # Nagios界面展示的php程序等内容的目录,被Nagios在httpd配置文件目录生成的配置文件所调用
  10. `-- var # 日志和数据
  11. 6 directories, 0 files

配置文件目录:

 
  1. [root@nagios nagios]# tree etc/
  2. etc/
  3. |-- cgi.cfg # 被主配置文件包含
  4. |-- nagios.cfg # 主配置文件
  5. |-- objects # 被主配置文件包含
  6. | |-- commands.cfg # 存放Nagios命令相关配置(也可指定commands目录),这里的命令不是系统命令
  7. | |-- contacts.cfg
  8. | |-- localhost.cfg # 对本机的监控,我们不使用它进行监控,而是将本机当成客户端来监控
  9. | |-- printer.cfg # 打印机
  10. | |-- switch.cfg # 交换机
  11. | |-- templates.cfg # 模板配置文件
  12. | |-- timeperiods.cfg
  13. | `-- windows.cfg # windows
  14. `-- resource.cfg # 被主配置文件包含,这里面是Nagios内置变量的定义,如$USER1$,$USER2$,$USER3$等

内容解释:

#commands.cfg:存放Nagios命令相关配置(也可指定commands目录),这里的命令不是系统命令,而是实现把Nagios中定义的命令和Linux系统中的插件命令关联的一个文件。如oldboy=/bin/cat,oldboy就是Nagios命令,而/bin/cat则是系统命令; 
services.cfg:存放具体被监控的服务相关的配置内容(对哪些服务进行监控),上百台以上可指定services目录,默认不存在; 
hosts.cfg:存放具体被监控的主机相关配置,上百台以上可指定hosts目录,默认不存在; 
contacts.cfg:存放报警联系人相关配置的文件; timeperiods.cfg:存放报警周期时间等相关配置; 
template.cfg:模板配置文件,模板的存在是为了方便的配置服务器配置,类似shell里的函数功能

编辑主配置文件: 
# 主配置文件是通过cfg_file来包含其他配置文件的,我们可以让其包含上述两个并不存在的配置文件:

 
  1. [root@nagios etc]# sed -ri '33acfg_file=/usr/local/nagios/etc/objects/hosts.cfg' nagios.cfg
  2. [root@nagios etc]# sed -ri '33acfg_file=/usr/local/nagios/etc/objects/services.cfg' nagios.cfg

# 注释掉localhost

 
  1. [root@nagios etc]# sed -ri 's@cfg_file=/usr/local/nagios/etc/objects/localhost.cfg@#&@g' nagios.cfg

# 主配置文件还可以包含目录,只要目录下面存在以.cfg结尾的文件都会被包含进来。在配置文件很多时还是可以使用的:

 
  1. [root@nagios etc]# sed -ri 's@#(cfg_dir=/usr/local/nagios/etc/servers)@\1@g' nagios.cfg

创建目录和文件:

 
  1. mkdir servers
  2. chown -R nagios.nagios servers/
  3. cd objects/
  4. head -51 localhost.cfg >hosts.cfg
  5. chown nagios.nagios hosts.cfg
  6. touch services.cfg # 暂时留空
  7. chown nagios.nagios services.cfg

加入被监控主机: 
# 编辑hosts.cfg配置文件,也就是head localhost文件生成的

 
  1. [root@nagios objects]# vim hosts.cfg
  2. define host{
  3. use
  4. host_name 02-client1
  5. alias 02-client1
  6. address 10.0.0.2
  7. }
  8. define host{ # 将本机也加入监控
  9. use linux-server
  10. host_name 01-nagios
  11. alias 01-nagios
  12. address 10.0.0.1
  13. }

解释:

这一个标签就是一个被监控的主机 
use表示一个使用名为linux-server的模板,该模板就是和hosts.cfg在同一个目录下的timeperiods.cfg,在该文件中可以搜索到linux-server定义的属性。可以也可以将该模板中定义的熟悉写在define 
host{}标签内,如果在该标签中定义了,那么就以该标签中的内容为准;如果没定义就会模板配置文件中找。使用模板配置文件的好处就在于在现在这个标签内只需定义四行即可。

 
  1. define hostgroup{ # 再将两个客户端加入一个组
  2. hostgroup_name linux-servers
  3. alias Linux Servers
  4. members 02-client1,01-nagios
  5. }

检查配置文件: 
# 检查配置文件的方法有两种,第一种为:

 
  1. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# 第二种:

 
  1. /etc/init.d/nagios checkconfig

# 其实第二种就是调用第一种的命令,但是它将信息都定义到了/dev/null中了,根本看不出来什么地方出错了,因此我们可以修改启动脚本中的内容:

 
  1. [root@nagios objects]# vim /etc/init.d/nagios +183
  2. checkconfig)
  3. printf "Running configuration check..."
  4. $NagiosBin -v $NagiosCfgFile # 将后面的重定向去掉

# 执行命令

 
  1. [root@nagios objects]# /etc/init.d/nagios checkconfig
  2. ...
  3. Error: There are no services defined! # 这就是错误,没有定义服务。下面就是统计的
  4. ...
  5. Total Warnings: 2 # 警告无所谓
  6. Total Errors: 1 # 但是错误一定要解决
  7. ...

# 既然没有定义服务,那我们就定义一个:

 
  1. define service {
  2. use generic-service # 模板
  3. host_name 02-client1,01-nagios # 被监控主机
  4. service_description Disk Partition # 检查磁盘分区
  5. check_command check_nrpe!check_disk
  6. }

# check_nrpe:Nagios的命令,需要在commands.cfg文件中定义 
# check_disk:调用客户端配置文件nrpe.cfg中[check_disk]标签后面的命令

command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% 
-p /

# 继续编辑commands.cfg配置文件,在配置文件的结尾加入以下几行:

 
  1. # 'check_nrpe' command definition
  2. define command{
  3. command_name check_nrpe
  4. command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  5. }

# -c:指定一个参数,这个参数就是check_disk

# 其实定义这么多的参数无非就是为了执行这一条命令:

 
  1. [root@nagios objects]# /usr/local/nagios/libexec/check_nrpe -H 10.0.0.2 -c check_disk
  2. DISK OK - free space: / 57841 MB (95% inode=97%);| /=2760MB;51080;58742;0;63851

# 再次检查配置文件,结果ok:

 
  1. [root@nagios objects]# /etc/init.d/nagios checkconfig

1.png-74.6kB

启动服务并访问web界面:

 
  1. /etc/init.d/nagios start
  2. /etc/init.d/httpd start

2.png-116.7kB

我们点击左边的services,会出现报错信息,解决办法就是编辑cgi.cfg: 
3.png-9.1kB

 
  1. [root@nagios etc]# pwd
  2. /usr/local/nagios/etc
  3. [root@nagios etc]# grep nagiosadmin cgi.cfg
  4. authorized_for_system_information=nagiosadmin
  5. authorized_for_configuration_information=nagiosadmin
  6. authorized_for_system_commands=nagiosadmin
  7. authorized_for_all_services=nagiosadmin
  8. authorized_for_all_hosts=nagiosadmin
  9. authorized_for_all_service_commands=nagiosadmin
  10. authorized_for_all_host_commands=nagiosadmin

# 从以上信息可以看出nagiosadmin就是Nagios的管理员,我们现在是没有权限,因此我们可以将nagiosadmin改为我们一开始添加的认证的用户oldboy。当然也可以使用nagiosadmin作为认证用户:

 
  1. [root@nagios etc]# sed -i 's/nagiosadmin/oldboy/g' cgi.cfg
  2. [root@nagios etc]# grep oldboy cgi.cfg
  3. authorized_for_system_information=oldboy
  4. authorized_for_configuration_information=oldboy
  5. authorized_for_system_commands=oldboy
  6. authorized_for_all_services=oldboy
  7. authorized_for_all_hosts=oldboy
  8. authorized_for_all_service_commands=oldboy
  9. authorized_for_all_host_commands=oldboy

重启服务:

 
  1. [root@nagios etc]# /etc/init.d/nagios reload

4.png-13kB

1.5.1 刷新时间的定义

 
  1. status_update_interval=3 # 默认10s刷新状态之类的数据
  2. [root@nagios etc]# sed -ri 's/(^status_u.*=).*/\13/g' nagios.cfg
  3. max_service_check_spread=10 # 最大的服务检查间隔
  4. [root@nagios etc]# sed -ri 's/(^max_s.*=).*/\110/g' nagios.cfg
  5. auto_rescheduling_interval=5 # 尝试自动检查的时间间隔
  6. [root@nagios etc]# sed -ri 's/(^auto_rescheduling_i.*=).*/\15/g' nagios.cfg
  7. command_check_interval=-1 # -1的意思是尽可能的经常检查,但这不是我们想要的
  8. [root@nagios etc]# sed -ri 's/(^command_c.*=).*/\12s/g' nagios.cfg # 改为2s
  9. retention_update_interval=10
  10. [root@nagios etc]# sed -ri 's/(^rete.*=).*/\110/g' nagios.cfg
  11. interval_length=10
  12. [root@nagios etc]# sed -ri 's/(^in.*=).*/\110/g' nagios.cfg
  13. service_freshness_check_interval=10
  14. [root@nagios etc]# sed -ri 's/(^service_f.*=).*/\110/g' nagios.cfg
  15. host_freshness_check_interval=10
  16. [root@nagios etc]# sed -ri 's/(^host_f.*=).*/\110/g' nagios.cfg

1.5.2 一些排错的思路

(1) 客户端获取值失败:

 
  1. [root@client1 ~]# /usr/local/nagios/libexec/check_nrpe -H 10.0.0.2 -c check_disk
  2. CHECK_NRPE: Error - Could not complete SSL handshake. # 握手失败

# 这种问题的解决办法很简单,只需要执行下面这条命令即可:

 
  1. [root@client1 ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk

# 如果能够获得值,那就是没有添加网卡地址,在nrpe.cfg中修改allowed_hosts=127.0.0.1这一行 
(2) 状态为CRITICAL 
<img src="http://static.zybuluo.com/abcdocker/6o71skfpe45lmda82uegogoq/%7B%60%5B%7DEO%60GAYB~UR%7B2PXBZ80O.png" alt="{[}EOGAYB~UR{2PXBZ80O.png-4.3kB” title=””>

# 这种问题就是连接失败,要么是服务没起,要么就是防火墙没关。我们可以现在本地执行:

 
  1. /usr/local/nagios/libexec/check_nrpe -H 10.0.0.2 -c check_disk

# 当然ip和参数都可以改,通过该命令就能得到答案,因为改命令就是Nagios获取监控值的过程 
(3) 命令行执行能够获取数值,但是web界面去获取不到。

 
  1. define service {
  2. use generic-service
  3. host_name 02-client1,01-nagios
  4. service_description Disk Partition
  5. check_command check_nrpe!check_disk # 肯定是这个参数定义错了
  6. }

(4) Unable to read output

1.png-3.7kB

# 出现这种问题的原因就是获取值的插件没有执行权限,或者是这插件就是有问题的,总之就是插件的错。

 
  1. command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3% # check_memory.pl就是插件
  2. [root@nagios libexec]# chmod +x check_memory.pl # 执行该命令,如果还是不行,那就是插件本身的问题了

总结,当web界面显示出现问题时: 
(1) Nagios自身和配置文件; 
(2) 在服务器端执行: 
/usr/local/nagios/libexec/check_nrpe -H 被监控主机地址-c 获取值的命令 
(3) 在客户端本地执行: 
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c获取值的命令 
(4) 执行nrpe.cfg配置文件中的获取值的命令:

command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% 
-p / # 执行该命令

老男孩老师补几个提醒: 
(1) 检查客户端系统自带的防火强,是否drop了5666端口; 
(2) nrpe添加完命令后,有没有真正重启; 
(3) nrpe.cfg配置文件中的allow_hosts这行的ip千万不要加错了。

1.5.3 添加其他监控项

 
  1. [root@nagios objects]# cat services.cfg
  2. define service {
  3. use generic-service
  4. host_name 02-client1,01-nagios
  5. service_description Disk Partition
  6. check_command check_nrpe!check_disk
  7. }
  8. define service {
  9. use generic-service
  10. host_name 02-client1,01-nagios
  11. service_description Memory Partition
  12. check_command check_nrpe!check_mem
  13. }
  14. define service {
  15. use generic-service
  16. host_name 01-nagios,02-client1
  17. service_description load
  18. check_command check_nrpe!check_load
  19. }
  20. define service {
  21. use generic-service
  22. host_name 01-nagios,02-client1
  23. service_description swap
  24. check_command check_nrpe!check_swap
  25. }
  26. define service {
  27. use generic-service
  28. host_name 01-nagios,02-client1
  29. service_description iostat
  30. check_command check_nrpe!check_iostat
  31. }

1.6 主动模式

按照监控的行为,老男孩老师把Nagios的监控分为主动监控和被动监控(NRPE半被动和NSCA全被动)。 
(1) 主动监控:所谓的主动模式就是Nagios服务器端发起的监控,如URL地址、端口监控等。主动模式获取值的命令无需经过nrpe,Nagios自身能够直接发起,也就是说不需要在客户端安装任何插件。当然,主动监控模式也能配置成被动模式。 
(2) 半被动监控:我们把对负载、内存、硬盘、虚拟内存、磁盘IO、温度、风扇转速等这些本地资源而非系统对外提供的服务的监控称为半被动模式。半被动模式的特点是对于这些本地资源的监控一般由Nagios插件定时去连接client的NRPE服务,定期获取信息发回Nagios服务器端。基本上只要安装了类似NRPE的agent端,且通过插件的方式的监控我们都认为是半被动监控。 
上面讲到的都是被动模式。

如何选择主动和被动? 
(1) 对于本地资源的监控,一般都使用被动模式。如负载、磁盘、内存、虚拟内存、磁盘IO、温度、风扇等的监控(我们也可以通过snmp来监控部分系统资源); 
(2) 对于web服务、数据库这种能对外提供服务的监控,一般采用主动模式。如监控http、ssh、MySQL、rsync等服务; 
(3) 主动和被动是相对的,并且是可以相互转换的。即主动模式的服务可以改成被动的;而被动模式有时也可以改为主动。 
总之: 
(a) 主动模式就和NRPE无关了,就是利用服务端本地插件直接获取信息; 
(b) 被动模式则是Nagios主程序通过check_nrpe插件和客户端NRPE进程沟通,调用本地插件获取数据。

主动模式有check_tcp和check_http这两个非常常用的插件,一个是检查端口,另一个则是检查URL,它们都能够使用–help查看使用帮助。

 
  1. [root@nagios libexec]# ./check_http --help
  2. [root@nagios libexec]# ./check_tcp --help

# 以下是最基本的使用方法

 
  1. [root@nagios libexec]# ./check_tcp -H 10.0.0.2 -p 22
  2. TCP OK - 0.000 second response time on port 22|time=0.000299s;;;0.000000;10.000000
  3. [root@nagios libexec]# ./check_http -I 10.0.0.2
  4. HTTP OK: HTTP/1.1 200 OK - 248 bytes in 0.024 second response time |time=0.023816s;;;0.000000 size=248B;;;0

1.6.1 监控一个域名或URI

先说说使用主配置文件包含一个目录的好处: 
(1) 该目录下的所有所有以.cfg为后缀的配置文件都会被Nagios加载,因此当我们不想使用其中的配置文件时,只需要改变其后缀名即可; 
(2) 我们一个以一个服务名为目录名,下面的配置文件都是监控这个服务的;还可以以一个主机命名,下面的配置文件都是监控这个主机的。可以根据业务需要进行配置。

1. 添加一个服务:

 
  1. [root@nagios etc]# vim servers/http.cfg # server是主配置文件中包含的目录
  2. define service {
  3. use generic-service
  4. host_name 02-client1
  5. service_description blog_url
  6. check_command check_weburl!-I 10.0.0.2
  7. max_check_attempts 3
  8. normal_check_interval 2
  9. retry_check_interval 1
  10. check_period 24x7
  11. notification_interval 30
  12. notification_period 24x7
  13. notification_options w,u,c,r
  14. contact_groups admins
  15. }

check_weburl:check_weburl这个可以直接换成check_http,但是也可以在commands.cfg文件中定义,让它和这个check_http关联起来; 
-I 10.0.0.2 可以使用域名而非ip,工作中肯定是使用域名的,这里只是测试

2. 编辑commands.cfg,加入check_weburl

 
  1. [root@nagios etc]# vim objects/commands.cfg
  2. # 'check_http' command definition # 这里面已经存在了check_http,因此可以直接替换上面的check_weburl
  3. define command{
  4. command_name check_http
  5. command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
  6. }
  7. # 'check_weburl' command definition
  8. define command{
  9. command_name check_weburl
  10. command_line $USER1$/check_http $ARG1$ -w 10 -c 30
  11. }

# 之所以重新定义check_weburl,是为了增加-w 10 -c 30,10秒钟之内返回结果为警告,30秒内返回结果那就很严重了

3. 重新加载配置文件:

 
  1. [root@nagios etc]# /etc/init.d/nagios checkconfig
  2. [root@nagios etc]# /etc/init.d/nagios reload

以上是ip,下面是域名:

 
  1. [root@nagios etc]# cat servers/http.cfg
  2. define service {
  3. use generic-service
  4. host_name 02-client1
  5. service_description blog_url
  6. check_command check_weburl!-I 10.0.0.2
  7. max_check_attempts 3
  8. normal_check_interval 2
  9. retry_check_interval 1
  10. check_period 24x7
  11. notification_interval 30
  12. notification_period 24x7
  13. notification_options w,u,c,r
  14. contact_groups admins
  15. }
  16. define service {
  17. use generic-service
  18. host_name 02-client1
  19. service_description blog_url2
  20. check_command check_weburl!-H blog.etiantian.org # 域名
  21. max_check_attempts 3
  22. normal_check_interval 2
  23. retry_check_interval 1
  24. check_period 24x7
  25. notification_interval 30
  26. notification_period 24x7
  27. notification_options w,u,c,r
  28. contact_groups admins
  29. }
  30. define service {
  31. use generic-service
  32. host_name 02-client1
  33. service_description blog_url3
  34. check_command check_weburl!-H blog.etiantian.org -u "/exec/test" # URI
  35. max_check_attempts 3
  36. normal_check_interval 2
  37. retry_check_interval 1
  38. check_period 24x7
  39. notification_interval 30
  40. notification_period 24x7
  41. notification_options w,u,c,r
  42. contact_groups admins
  43. }

# 编辑hosts文件,域名都有公网DNS解析了,为什么还要添加hosts呢?因为使用公网DNS很容易误报,如果监控服务器本身DNS出问题了就会导致误报。缺点是不能监控到DNS导致的域名解析故障。

 
  1. [root@nagios etc]# vim /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 nagios
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 10.0.0.2 blog.etiantian.org

# curl一下,看看是否有问题

 
  1. [root@nagios etc]# curl blog.etiantian.org
  2. blog.etiantian.org

# 再次检查:

 
  1. [root@nagios etc]# /usr/local/nagios/libexec/check_http -H blog.etiantian.org
  2. HTTP OK: HTTP/1.1 200 OK - 249 bytes in 0.001 second response time |time=0.000688s;;;0.000000 size=249B;;;0

# 这是对URL做监控

 
  1. [root@nagios etc]# /usr/local/nagios/libexec/check_http -H blog.etiantian.org -u /exec/test
  2. HTTP OK: HTTP/1.1 200 OK - 243 bytes in 0.001 second response time |time=0.000784s;;;0.000000 size=243B;;;0

# 重载配置文件:

 
  1. [root@nagios etc]# /etc/init.d/nagios checkconfig
  2. [root@nagios etc]# /etc/init.d/nagios reload

2.png-28.5kB

如果不是监控域名而是URI的话,当该URI有特殊符号,也就是没有做伪静态时,使用这个URL一定要用引号一起来不然就会报错:

 
  1. define service {
  2. use generic-service
  3. host_name 02-client1
  4. service_description blog_url
  5. check_command check_weburl!-H blog.etiantian.org -u "/main_free.jsp?dirId=32234&gId=3"

我们可以删除URI中的文件,查看监控的反应: 
2.png-5.4kB

1.6.2 监控端口

监控端口使用的是check_tcp这个插件,这个命令已经在commands.cfg中定义了,我们直接拿来用就行。比如监听80端口:

 
  1. [root@nagios etc]# vim servers/http.cfg
  2. define service {
  3. use generic-service
  4. host_name 02-client1
  5. service_description blog_prot_80
  6. check_command check_tcp!80 # 已经在commands.cfg中定义了
  7. max_check_attempts 3
  8. normal_check_interval 2
  9. retry_check_interval 1
  10. check_period 24x7
  11. notification_interval 30
  12. notification_period 24x7
  13. notification_options w,u,c,r
  14. contact_groups admins
  15. }

# 重载配置文件

 
  1. [root@nagios etc]# /etc/init.d/nagios checkconfig
  2. [root@nagios etc]# /etc/init.d/nagios reload

所有端口都可以这么监控,只需要改个描述和端口号即可。

1.6.3 集群节点控制

利用别名实现对集群下面同样的节点的URL监控,因为相同的域名下所有节点都是一样的,这是就可以通过别名区分同一个域名解析下的所有主机:

web1 blog.etiantian.org,blog1.etiantian.org 
web2 blog.etiantian.org,blog2.etiantian.org

1.6.4 小结

一般客户端对外开启的服务,用主动模式监控。如port,url。

主动模式的监控配置过程: 
(1) 在服务端的命令行把要监控的命令先调试好; 
(2) 在commands.cfg中定义Nagios命令,同时调用命令行的插件; 
(3) 在服务的配置文件中定义要监控的服务,调用commands.cfg中定义Nagios的监控命令。

1.6.5 被动模式监控端口

被动模式就要在客户端进行 
1) 先测试命令在命令行是否执行成功:

 
  1. [root@client1 etc]# /usr/local/nagios/libexec/check_tcp -H 10.0.0.2 -p 80
  2. TCP OK - 0.000 second response time on port 80|time=0.000132s;;;0.000000;10.000000

2) 然后加入到nrpe.cfg中:

 
  1. [root@client1 etc]# echo "command[check_port_80]=/usr/local/nagios/libexec/check_tcp -H 10.0.0.2 -p 80 -w 5 -c 10" >>nrpe.cfg

3) 重启服务:

 
  1. ps aux|grep nrpe
  2. pkill nrpe
  3. ps aux|grep nrpe
  4. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  5. ps aux|grep nrpe

4) 在服务端命令测试:

 
  1. [root@nagios libexec]# ./check_nrpe -H 10.0.0.2 -c check_port_80
  2. TCP OK - 0.000 second response time on port 80|time=0.000162s;5.000000;10.000000;0.000000;10.000000

5) 添加一个services:

 
  1. [root@nagios etc]# vim servers/http.cfg
  2. define service {
  3. use generic-service
  4. host_name 02-client1
  5. service_description blog_prot_80_passive
  6. check_command check_nrpe!check_port_80
  7. max_check_attempts 3
  8. normal_check_interval 2
  9. retry_check_interval 1
  10. check_period 24x7
  11. notification_interval 30
  12. notification_period 24x7
  13. notification_options w,u,c,r
  14. contact_groups admins
  15. }

6) 重载配置文件

 
  1. [root@nagios etc]# /etc/init.d/nagios checkconfig
  2. [root@nagios etc]# /etc/init.d/nagios reload

222.png-4.3kB

1.7 服务分组

先定义一个配置文件,然后写入配置:

 
  1. [root@nagios etc]# vim servers/servergroup.cfg
  2. define servicegroup{
  3. servicegroup_name swap # 这个swap要和前面定义的服务名对应上
  4. alias Linux Servers
  5. members 02-client1,swap,01-nagios,swap # 监控的主机,服务的组名,第二个监控主机,服务的组名... 这样就达到了服务分组的目的
  6. }

重载服务后,在哪里查看呢?点击左侧栏的Service Groups: 
2.png-47.6kB

今后就能够通过这种方式进行分组了,比如将所有要监控swap、cpu、磁盘等等的主机放在一个组里面。但是这个并不是太重要。

1.8 Nagios日志

Nagios也可以通过日志排查问题,日志所在位置为/usr/local/nagios/var/nagios.log,但是排错的思路前面已经讲到了,无需看日志。

1.9 主机和服务监控的重要参数

主机:

 
  1. define host { # 定义主机
  2. use linux-server # 主机使用的模板,详见templates.cfg
  3. host_name 02-client1 # 这个主机名和关键,很多监控的定义都会引用这个主机名
  4. alias 02-client1 # 主机别名
  5. address 10.0.0.2 # ip
  6. check_command check-host-alive # 检测主机存活的命令,来自commands.cfg
  7. max_check_attempts 3 # 故障后的最大检测次数
  8. normal_check_interval 2 # 正常的检查间隔,默认单位为分钟
  9. retry_check_interval 1 # 故障后重试的检查间隔,默认单位为分钟
  10. check_period 24x7 # 检查周期24x7,具体详见timeperiods.cfg
  11. notification_interval 30 # 故障后,两次报警的通知间隔,默认单位为分钟
  12. notification_period 24x7 # 一天之内通知的周期,如全天、半天等,详见timeperiods.cfg
  13. notification_options d,u,r # 哪些问题会报警,d-down,u-unreachable(不可达),r-recovery(主机恢复)
  14. contact_groups admins # 报警到admins用户组,在contacts.cfg中定义
  15. }

服务:

 
  1. define service {
  2. use generic-service
  3. host_name 02-client1
  4. service_description blog_prot_80_passive # 报警服务的描述
  5. check_command check_nrpe!check_port_80 # 检查服务的命令
  6. max_check_attempts 3
  7. normal_check_interval 2
  8. retry_check_interval 1
  9. check_period 24x7
  10. notification_interval 30
  11. notification_period 24x7
  12. notification_options w,u,c,r # w-warning,u-unknown(状态不知道),c-critical(特别严重),r-recovery(恢复)
  13. contact_groups admins
  14. process_perf_data 1 # PNP出图记录数据相关
  15. }

1.9.1 服务模板

不管是服务还是主机,一半只会定义前四项,其他参数都会定义在模板文件中。如果我们对监控的服务进行分类的话,甚至可以只写两行。我们来看看模板文件中的内容:

 
  1. [root@nagios objects]# vim templates.cfg
  2. define service{
  3. name generic-service # 这就是服务的模板
  4. active_checks_enabled 1
  5. passive_checks_enabled 1
  6. parallelize_check 1
  7. obsess_over_service 1
  8. check_freshness 0
  9. notifications_enabled 1
  10. event_handler_enabled 1
  11. flap_detection_enabled 1
  12. failure_prediction_enabled 1
  13. process_perf_data 1
  14. retain_status_information 1
  15. retain_nonstatus_information 1
  16. is_volatile 0
  17. check_period 24x7
  18. max_check_attempts 3
  19. normal_check_interval 10
  20. retry_check_interval 2
  21. contact_groups admins
  22. notification_options w,u,c,r
  23. notification_interval 60
  24. notification_period 24x7
  25. register 0
  26. }

1.9.2 监控周期模板

查看监控周期timeperiods.cfg文件:

 
  1. define timeperiod{
  2. timeperiod_name 24x7 # 24x7只是一个名字而已,真正的时间在下面定义
  3. alias 24 Hours A Day, 7 Days A Week
  4. sunday 00:00-24:00
  5. monday 00:00-24:00
  6. tuesday 00:00-24:00
  7. wednesday 00:00-24:00
  8. thursday 00:00-24:00
  9. friday 00:00-24:00
  10. saturday 00:00-24:00
  11. }
  12. # 'workhours' timeperiod definition
  13. define timeperiod{
  14. timeperiod_name workhours # 这是工作时间的,老外真爽
  15. alias Normal Work Hours
  16. monday 09:00-17:00
  17. tuesday 09:00-17:00
  18. wednesday 09:00-17:00
  19. thursday 09:00-17:00
  20. friday 09:00-17:00
  21. }

# workhours用于监控磁盘的报警,也就是说磁盘的监控是7x24小时的,但是报警时间一般为8点到23点。

1.9.3 联系人模板

模板文件名为contacts.cfg,记录报警信息发送的对象:

 
  1. [root@nagios objects]# vim contacts.cfg
  2. define contact{
  3. contact_name nagiosadmin # 联系人的名字
  4. use generic-contact # 使用的模板
  5. alias Nagios Admin
  6. email nagios@localhost
  7. }
  8. define contactgroup{ # 联系人组
  9. contactgroup_name admins # 可以向这个组内的所有成员报警
  10. alias Nagios Administrators
  11. members nagiosadmin # 组中的成员
  12. }

# 可以添加一个运维组,将所有运维人员都添加到该组中;还可以添加一个老大的邮箱,当某些报警信息不希望老大收到时就不把他写上去。

1.10 报警的过程

 
  1. templates.cfg:
  2. [root@nagios objects]# cat templates.cfg
  3. define contact{
  4. name generic-contact # 这是联系人的模板
  5. service_notification_period 24x7
  6. host_notification_period 24x7
  7. service_notification_options w,u,c,r,f,s
  8. host_notification_options d,u,r,f,s
  9. service_notification_commands notify-service-by-email # 这是服务发送Email的命令,在command.cfg中定义
  10. host_notification_commands notify-host-by-email # 这是主机报警的命令,同上
  11. register 0
  12. }
  13. contacts.cfg:
  14. [root@nagios objects]# vim contacts.cfg
  15. define contact{
  16. contact_name nagiosadmin
  17. use generic-contact # 联系人模板从这里引用
  18. alias Nagios Admin
  19. email nagios@localhost
  20. }
  21. commands.cfg:
  22. [root@nagios objects]# vim commands.cfg
  23. # 'notify-host-by-email' command definition
  24. define command{
  25. command_name notify-host-by-email # 主机报警的命令,最终调用的是系统的mail命令
  26. command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
  27. }
  28. # 'notify-service-by-email' command definition
  29. define command{
  30. command_name notify-service-by-email # 服务报警的命令
  31. 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" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
  32. }

1.png-210.3kB

1.11 自定义开发插件

自定义插件可以使用任意语言开发,只要能在命令行给出结果,就能够使用Nagios监控。什么是插件呢?我们在前文安装的nagios-plugins-1.4.16.tar.gz,这个软件包就是Nagios的插件安装包,插件都安装…/nagios/libexec目录下。只所以使用自定义插件的原因有: 
(1) Nagios自带的插件满足不了需要; 
(2) 由于插件是我们写的,因此它有哪些缺点和BUG我们都清楚。

写自定义插件的规则: 
(1) 插件要有一个退出状态码,它用于被Nagios主程序作为判断被监控系统服务状态的依据; 
(2) 插件在控制台打印的第一行数据,该数据可以被Nagios主程序作为被监控系统服务状态的补充说明,会显示在管理界面。 
1.png-17.1kB

Nagios主程序可识别的状态码和说明如下: 
OK:退出状态码0,表示服务正常工作; 
WARNING:退出状态码1,表示服务处于警告状态; 
CRITICAL:退出状态码2,表示服务处于严重状态; 
UNKNOWN:退出状态码3,表示服务处于未知状态。 
状态码定义的方法可以在libexec目录下执行head -7 utils.sh:

 
  1. [root@nagios libexec]# head -7 utils.sh
  2. #! /bin/sh
  3. STATE_OK=0
  4. STATE_WARNING=1
  5. STATE_CRITICAL=2
  6. STATE_UNKNOWN=3
  7. STATE_DEPENDENT=4

不同语言的系统退出函数示例如下:

  • Java:System.exit(int status)
  • php:exit(status)
  • python:sys.exit(int status)
  • C/C++:return int status
  • bash:exit int status

不同语言打印第一行数据:

  • Java:System.out.println(String msg)
  • php:echo msg
  • python:printf msg
  • C/C++:printf(“%s”,msg)
  • bash:echo msg(printf)

使用shell开发第一个插件,监控passwd文件的变化: 
# 先写脚本

 
  1. [root@client1 ~]# md5sum /etc/passwd >/etc/oldboy.md5
  2. [root@client1 ~]# cd /usr/local/nagios/libexec/
  3. [root@client1 libexec]# vim check_passwd
  4. #!/bin/bash
  5. char=`md5sum -c /etc/oldboy.md5|grep "OK"|wc -l`
  6. if [ $char -eq 1 ];then
  7. echo "passwd is ok"
  8. exit 0
  9. else
  10. echo "passwd is changed"
  11. exit 2
  12. fi
  13. [root@client1 libexec]# chmod +x check_passwd

# 添加到监控,只能使用被动监控

 
  1. [root@client1 ~]# echo "command[check_passwd]=/usr/local/nagios/libexec/check_passwd" >>/usr/local/nagios/etc/nrpe.cfg
  2. [root@client1 libexec]# pkill nrpe
  3. [root@client1 libexec]# ps aux|grep nrpe
  4. [root@client1 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  5. [root@nagios nagios]# ./libexec/check_nrpe -H 10.0.0.2 -c check_passwd # 在服务器端先测试
  6. [root@nagios nagios]# vim etc/objects/services.cfg # 在服务端配置
  7. define service {
  8. use generic-service
  9. host_name 02-client1
  10. service_description passwd
  11. check_command check_nrpe!check_passwd
  12. }
  13. [root@nagios nagios]# /etc/init.d/nagios checkconfig
  14. [root@nagios nagios]# /etc/init.d/nagios reload

# 最终结果: 
2.png-2.7kB

1.12 图形显示和管理

虽然能显示能报警,但是企业工作中需要一个历史趋势图。Nagios本身只是实现了核心功能,因此它不具备出图的能力,和Nagios配合出图的工具有很多,但是最好的还是pnp。想要使用pnp出图,但是也需要其他软件的支持。我们先安装图形显示管理的依赖库:

 
  1. [root@nagios ~]# yum install -y cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel

1.12.1 安装软件

pnp出图实际上是利用rrdtool(轮询的数据库工具)这个软件,但是安装之前我们先安装rrdtool的依赖库。我们可以使用yum install libart_lgpl libart_lgpl-devel安装,这里我们使用编译安装的方式(软件包中都有):

 
  1. tar xf libart_lgpl-2.3.17.tar.gz
  2. cd libart_lgpl-2.3.17
  3. ./configure
  4. make && make install
  5. cp -r /usr/local/include/libart-2.0/ /usr/include/
  6. cd ..

安装rrdtools,rrdtools是真正画图的软件,这个工具虽然冷门,但是很多软件的画图都是靠它:

 
  1. tar xf rrdtool-1.2.14.tar.gz
  2. cd rrdtool-1.2.14
  3. ./configure --prefix=/usr/local/rrdtool --disable-python --disable-tcl
  4. make && make install
  5. cd ..
  6. ll /usr/local/rrdtool/bin/ # 3个程序

现在才是真正的安装pnp,pnp的作用就是收集数据然后告诉rrdtool,然后rrdtool画完图后还要通过pnp进行展示:

 
  1. tar xf pnp-0.4.14.tar.gz
  2. cd pnp-0.4.14
  3. ./configure \
  4. --with-rrdtool=/usr/local/rrdtool/bin/rrdtool \ # 画图的命令
  5. --with-perfdata-dir=/usr/local/nagios/share/perfdata # 出图的路径
  6. make all
  7. make install
  8. make install-config
  9. make install-init
  10. ll /usr/local/nagios/libexec/|grep process

# configure出现WARNING不用理会

1.12.2 修改配置

软件都准备好了,接下来要做的就是编辑主配置文件:

 
  1. cd /usr/local/nagios/etc/
  2. cp nagios.cfg nagios.cfg.bak
  3. sed -ri 's@(^pro.*=).*@\11@g' nagios.cfg
  4. sed -ri 's@^#(host_perfdata_c.*)@\1@g' nagios.cfg
  5. sed -ri 's@^#(service_perfdata_c.*)@\1@g' nagios.cfg

# 这是修改后的内容

 
  1. process_performance_data=1
  2. host_perfdata_command=process-host-perfdata
  3. service_perfdata_command=process-service-perfdata

修改commands.cfg文件: 
# 将这两段内容删除:

 
  1. # 'process-host-perfdata' command definition
  2. define command{
  3. command_name process-host-perfdata
  4. command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n" >> /usr/local/nagios/var/host-perfdata.out
  5. }
  6. # 'process-service-perfdata' command definition
  7. define command{
  8. command_name process-service-perfdata
  9. command_line /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /usr/local/nagios/var/service-perfdata.out
  10. }

# 替换成:

 
  1. # 'process-host-perfdata' command definition
  2. define command{
  3. command_name process-host-perfdata
  4. command_line /usr/local/nagios/libexec/process_perfdata.pl # 其实就是该这一行
  5. }
  6. # 'process-service-perfdata' command definition
  7. define command{
  8. command_name process-service-perfdata
  9. command_line /usr/local/nagios/libexec/process_perfdata.pl
  10. }

# /usr/local/nagios/libexec可以用$USER1$进行替换 
重载配置文件后访问10.0.0.1/nagios/pnp/index.php,出现以下界面表示安装成功:

 
  1. /etc/init.d/nagios checkconfig
  2. /etc/init.d/nagios reload

3.png-101.5kB

只要等一会,图上就有数据显示了,只所以会有数据出现在图上,是因为模板配置文件中一个参数决定的:

 
  1. [root@nagios etc]# vim objects/templates.cfg
  2. define service{
  3. name generic-service
  4. active_checks_enabled 1
  5. passive_checks_enabled 1
  6. parallelize_check 1
  7. obsess_over_service 1
  8. check_freshness 0
  9. notifications_enabled 1
  10. event_handler_enabled 1
  11. flap_detection_enabled 1
  12. failure_prediction_enabled 1
  13. process_perf_data 1 # 就是这个参数

1.12.3 将二者结合

目前监控和趋势图二者是分离的,我们现在要将它们结合起来。

在主机监控中整合:

 
  1. [root@nagios objects]# vim hosts.cfg
  2. define host{
  3. use linux-server
  4. host_name 02-client1
  5. alia 02-client1
  6. address 10.0.0.2
  7. action_url /nagios/pnp/index.php?host=$HOSTNAME$ # 增加这一行,定义URL
  8. }

# 在模板中添加更省事 
重载配置文件后访问10.0.0.1/nagios,可以看到多了一个按钮,点进去就是该主机所有的趋势图了: 
4.png-11kB

服务出图:

 
  1. [root@nagios objects]# vim services.cfg
  2. define service {
  3. use generic-service
  4. host_name 02-client1,01-nagios
  5. service_description Disk Partition
  6. check_command check_nrpe!check_disk
  7. action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ # 增加一行
  8. }

# 同样可以写在模板中 
5.png-34.7kB

1.13 报警

  1. 邮件报警:这是必须会的,生产环境中应尽量使用自己公司的信箱作为报警信箱,或者建立一个邮箱组(邮件列表)。尽量不用非公司信箱作为报警信箱,如126、qq等,因为这些信箱是免费的,对报警的频率等会有限制,很有可能会拒收或当成垃圾邮件,导致报警延时或无法收到。适用于重要且不紧急的业务报警;
  2. 邮件转短信报警:收到邮件会短信提醒,就相当于短信报警的功能。这是由邮箱提供商提供的一个功能,但报警内容长度有限制;
  3. http短信网关:有专门的公司提供直接发送信息到手机的短信网关,常用的报警就是一个URL地址携带信息,收短信费。这是推荐的报警方式;
  4. 购买短信猫:类似于手机终端一样的客户端硬件设备,早期报警选用的方式,收短信费;
  5. 电话语音报警:先将报警语音录下来,报警时直接打电话到报警负责人,播放报警语音;也可以用语音识别软件,将文字识别为语音;
  6. QQ/微信:模拟QQ,微信发信息的功能,QQ不太稳定;微信实际上就是将微信和邮箱绑定,当邮箱收到邮件时,微信会提醒

在生产环境中,一般会根据业务的紧急程度不同,多个报警策略结合使用。对于不需要紧急处理的业务一般选择邮件报警,如内存、磁盘空间的剩余率;对于重要且紧急的业务,会使用邮件加上短信同时报警。使用邮件报警便于记录故障详细信息,短息报警时及时提醒,优点是及时。短信报警的缺点是报警内容有限,所以在工作中如果接到严重报警时,我们紧急处理之前也会开启邮件系统先查看邮件细节。

其中http短信网关是老男孩老师最优先推荐使用的短信报警方式,原因: 
(1) 简单、易用; 
(2) 稳定、可靠; 
(3) 收费合理,类似个人手机一样,收取发送费用。

老男孩老师的思想:花一定的费用把业务做到最好是正常工作的思维,如果总想着免费,那么如果业务报警收不到,损失可能会更大。

1.13.1 报警分级

A类:磁盘空间、cpu、内存报警等为一般报警,运维内部采取常规处理方式; 
B类:网站域名不能打开为严重报警,需协调技术部相关人员会诊处理。 
若收到A类报警,原则上限制处理时间,但以不影响服务为前提,进行即使处理;或收到B类报警短信,值班人员需在10分钟内邮件周知运维全体同事及相关技术人员,并解决。纯值班人员可能没有处理权限,只能电话及邮件周知运维人员,有的公司根据业务分配好报警的人。

1.13.2 配置报警

配置报警就是配置contacts.cfg文件。可以将公司所有的运维人员都加入到这个文件中,如果有需要还可以分组。

配置报警的步骤: 
(1) 添加联系人及联系组contacts.cfg;

 
  1. define contact{
  2. contact_name oldboy-pager
  3. use generic-contact
  4. alias Nagios users
  5. email 18901398229
  6. }

(2) 添加报警的命令commands.cfg

 
  1. define command {
  2. command_name notify-host-by-pager
  3. command_line $USER1$/sms_send "$HOSTSTATE$ alert for $HOSTNAME$" $CONTACTOAGER$
  4. }
  5. define command {
  6. command_name notify-service-by-pager
  7. command_line $USER1$/sms_send "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTOAGER$
  8. }

(3) 调整联系人模板,添加报警的命令(来自于commands.cfg):

 
  1. define contact{
  2. name generic-contact
  3. service_notification_period 24x7
  4. host_notification_period 24x7
  5. service_notification_options w,u,c,r,f,s
  6. host_notification_options d,u,r,f,s
  7. service_notification_commands notify-service-by-email,notify-service-by-pager
  8. host_notification_commands notify-host-by-email,notify-host-by-pager
  9. register 0
  10. }

(4) 在hosts.cfg和service.cfg配置文件中添加报警联系人及组,或者在模板中添加

 
  1. contact_groups admins,group1,group2,user1

总结:总体来说nagios还是不错的一款监控软件,但是相比zabbix 就没有zabbix那样配置简单。只需要在图形化界面点点就可以完成。

启动与停止Nagios系统运维 Nagios是一种广泛使用的开源网络监控工具,用于监视服务器、网络设备和应用程序的状态。在系统运维过程中,了解如何启动和停止Nagios是非常重要的。在系统运维中,了解如何启动和停止Nagios是非常重要的。通过本文提供的方法,您可以轻松地在Nagios服务器上执行这些操作。您可以按照Nagios官方文档的指引进行安装和配置。打开终端或命令行界面,并使用root或具有sudo权限的用户登录到Nagios服务器。打开终端或命令行界面,并使用root或具有sudo权限的用户登录到Nagios服务器。 阅读详情

相关推荐

监控软件 Nagios 安装与配置

Nagios 与其他类似的包之间的主要区别在于,Nagios 将所有的信息简化为“工作(working)”、“可疑的(questionable)”和“故障(failure)”状态,并且 Nagios 支持由插件组成的非常丰富的“生态系统”。nagios-nrpe-server 的功能是向服务器发送监控数据, 而服务器端通过nagios-nrpe-plugin接收监控数据。-p, --path=PATH, --partition=PARTITION 参数监控路径,可以一次写多个参数。

1773

nagios-plugin-1.4.16 编译备忘

需要额外安装 fping 软件包 http://apt.sw.be/redhat/el6/en/i386/rpmforge/RPMS/fping-3.4-1.el6.rf.i686.rpm   nagios-plugin-1.4.16.tar.gz ./configure --enable-libtap --enable-extra-opts --enable-redhat-pthre

Terry Tsang 2938

88、Nagios 监控配置详解

本文详细解析了 Nagios 的监控配置方法,涵盖主机模板、时间周期、检查命令、通知机制、联系人与联系组、服务配置等内容,并通过示例说明了如何定义主机和服务,以及如何处理状态转换与日志分析。适合系统管理员学习和深入理解 Nagios 监控工具的应用。

q6r7s8t9的博客 75

Nagios安装与配置详解

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。<br />1.默认效果<br /><br />代码<br />Toast.makeText(getApplicationContext(), "默认Toast样式",<br />      Toast.LENGTH_SHORT).show();<br /> <br />2.自定义显示位置效果<br /><br />代码<br />toast = Toast.makeText(getAppli

Java海洋 2983

Class-Accessor-0.31.tar.gz

Class-Accessor-0.31.tar.gz

nagios-plugins-1.4.16.tar.gz

nagios-plugins-1.4.16.tar.gz nagios 插件

运维监控利器Nagios之:nagios配置详解

一、nagios配置过程详解 1、nagios默认配置文件介绍  nagios安装完毕后,默认的配置文件在/usr/local/nagios/etc目录下,每个文件或目录含义如下表所示:   2、配置文件之间的关系  在nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是...

weixin_34133829的博客 558

COLMAP生成MVSNet数据集

COLMAP生成MVSNet数据集

qq_43307074的博客 5635

Nagios监控及配置

Nagios简介 Nagios是一款用来监视系统和网络的开源应用软件。利用其众多的插件实现对本机和远端服务的监控,当被监控对象发生异常时,会及时向管理员告警,提供一批预设好的监控插件,用户可以直接调用,也可以自定义Shell脚本来监控服务,适合各类企业的业务监控,可通过Web页面显示对象状态、日志、告警信息 Nagios与Cacti的区别 Cacti:在监控方面绘图比较不错,在流量与图形展现...

jsut_rick的博客 2579

Halcon形状模板匹配实战:inspect_shape_model参数优化指南

本文深入解析Halcon形状模板匹配中inspect_shape_model参数的优化技巧,涵盖NumLevels和Contrast参数的实战应用。通过具体案例展示如何平衡匹配速度与精度,帮助开发者高效调试形状模型,提升工业视觉检测的准确性和效率。

weixin_30342209的博客 618

乐鑫WIFI芯片开发流程梳理-工具链、编译和下载

最近开始基于乐鑫ESP32的WIFI模组的软件开发,首先要做整套流程的梳理,乐鑫官网以及安信可上已经有了较为系统的资料,但是在实现过程中也遇到了一些问题,花了几天去解决,所以为了总结,也为了给其他人提供一些借鉴,把流程分享一下,官网上相对比较完善的,就不在赘述。 1. 开发方案选择 目前网上有两套较为成熟的方案: (1)基于乐鑫官网推荐的开发方式,原网页:https://docs.espress...

一名嵌入式软件工程师的自我成长 3925

Nagios监控项目配置

1.     check_icmp 简单地来说 默认我们使用的check_ping 插件是获取不到性能数据PING ok – Packet loss = 0%, RTA = 0.80 ms 这个就是默认check_ping得出来的结果check_icmp是check_ping的扩展,加了性能数据,check_icmp得到的结果如下: PING ok – Packet loss = 0%, RT

良玉的博客 3680

《哥谭神话-工程篇》02.Palantir的Ontology层(语义层) -企业操作系统的大脑

Ontology(语义层) 是Foundry 的核心层。Ontology 位于集成到 Foundry 中的数字资产(数据集和模型)之上,并将其连接到现实世界的对应物,从工厂、设备和产品等物理资产到客户订单或金融交易等概念。本篇将详细拆解Ontology(语义层)。

880

centos下配置nagios监控主机及服务的总结

一、    安装nagios准备工作 1、基础支持套件安装:gcc glibc glibc-common gd gd-develxinetd openssl-devel 2、 创建nagios安装目录,并新建nagios用户。 二、    安装nagios框架  nagios-3.4.3.tar.gz 三、    安装nagios插件  nagios-plugins-1.4.16.tar

到底意难平 1620

从零搭建UVM验证环境:新手入门实践指南

在数字芯片验证领域,SystemVerilog和UVM(Universal Verification Methodology)是构建高效验证环境的核心技术。其原理基于面向对象编程和事务级建模,通过组件化架构实现验证的复用性和自动化。这一方法学的技术价值在于显著提升验证效率与质量,广泛应用于处理器、通信芯片等复杂设计的验证场景。本文聚焦于UVM验证环境的搭建实践,通过一个加法器实例,详细解析了uvm_driver、uvm_monitor、uvm_sequence等关键组件的协同工作机制,并提供了基于Quest

weixin_30015371的博客 445

C++碰撞检测库FCL安装(MinGW,CMake)

FCL碰撞检测库、libccd\Eigen库编译配置,全流程问题记录

weixin_45650767的博客 4673

[Nagios Core] struct监控对象 | 配置.cfg加载为内存模型

> - `base/config.c`文件包含读取主配置和执行验证检查的核心功能。 > - 解析和模板化逻辑由`xdata/xodtemplate.c`等文件处理。

No Game No Life 1297

013:无人机航线规划的概念

航线规划是无人机任务规划的核心环节,它决定了无人机在整个任务执行过程中的飞行路径。航线规划需要考虑多种因素,包括飞行时间、飞行距离、能源消耗、飞行安全等。

qq_31762031的博客 2443

Nagios监控配置文件及监控流程

1、安装路径/usr/local/nagios/下存在etc、bin、sbin、share、var 这五个目录。Nagios 各个目录用途说明如下: bin Nagios 可执行程序所在目录 etc Nagios 配置文件所在目录 sbin Nagios CGI 文件所在目录,也就是执行外部命令所需文件所在的目录 share Nagios网页文件所在的目录 ...

weixin_30421809的博客 186

解锁RK平台黑科技:DPDK加速与硬件卸载实战

本文探讨了在RK平台上通过DPDK加速与硬件卸载技术构建高性能网络架构的方法。RK平台凭借强大的硬件性能为高并发网络处理提供基础,而DPDK通过用户态驱动、轮询机制等技术绕过内核瓶颈,显著提升数据包处理效率。硬件卸载则将校验和计算等任务交由专用硬件,释放CPU资源。实践案例显示,该方案可使网络吞吐量提升至理论极限,延迟降至5ms以下,CPU利用率降低40%。文章详细介绍了配置步骤、优化方法和常见问题解决方案,并展望了该技术在5G、车联网等新兴领域的应用前景。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 1275

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Linux监控一之Nagios的安装与配置

一、Nagios简介   Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。   Nagios原名为NetSaint,由Ethan Galstad开发并维护至今。NAGIOS是一个缩写形式: "Na...

weixin_34403693的博客 202

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

nagios监控配置与邮件告警

nagios监控配置与邮件告警 ip 主机名 备注 172.16.121.227 nagios 服务端 172.16.121.228 client 客户端 这里还需要安装nagios服务以及插件可以参考我之前的博客:https://blog.youkuaiyun.com/miss_miss6/article/details/105061694 1.先设置服务端 [root@nagios...

miss_miss6的博客 415

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

nagios配置监控windows配置

  /*<![CDATA[*/if(top.location != self.location){ top.location = self.location;}var myref = encodeURIComponent("http://hi.baidu.com/rhca/blog/item/d1af02234a4f244893580779%2Ehtml");/*]

moonseen的专栏 2069

nagios配置详解与集群监控

1 前言本系列文章主要讲述如何一步一步地监控大数据平台集群状况,接上篇文章nagios安装部署,本文主要阐述Nagios主要配置文件,Nagios运作流程,如何监控一个Zookeeper集群,并以一个该实例贯穿全文。2 Nagios文件结构2.1 监控端文件结构nagios/ ├── bin ├── etc │   └── objects ├── libexec ├── sbin ├── share

Oozie123的博客 2797

nagios的安装及配置实现网络监控

Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、 NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios 和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中(1)NRPE:用来在监控的远程Linux/Unix主机上执行脚本插件以实...

sw_147的专栏 447

[nagios监控] 配置nagios监控的nrpe组件-监控私有信息

1> 服务端配置----监控私有信息       a> 安装nrpe             wget ftp://192.168.68.254/monitor/nagios/nrpe-2.9.tar.gz             tar -zxvf nrpe-2.9.tar.gz              cd nrpe-2.9             ./configure && m

kakane的专栏 1440
上一篇: 服务器安全之iptables iptables
下一篇: SSH 配置
linuxlsq
博客等级 码龄11年 69粉丝 14原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值