来源:http://linux.chinaunix.net/ebook/doc/2009/09/17/1135830.shtml
本文介绍在centos 5.x环境下通过yum源的扩展使用munin、 monit、ntop工具来监管你的应用程序和服务器。题为懒人说说的是简便的安装方式而已,将强大的功能配置简单的应用起来是很重要的,可以节省时间并提高效率。
Monit:http:#www.tildeslash.com/monit/ Munin: http:#munin.projects.linpro.no/ Ntop:http:#www.ntop.org/ |
当你的 Linux 主机投入生产环境以后,想要知道系统或者服务每天或者每周的运行情况, Munin 是最佳选择。在某些情况下,监控 Linux 主机服务、应用程序的运行状态,是非常有必要的,尤其是应用程序或服务会出现错误,并可能开始吞噬你主机的内存和 cpu ,这将导致主机上的应用不能正常的运行。因此,需要其他一些程序来监控你的程序,在程序出错以后重启你的程序。在 linux 下解决方案,如 Nagios 、 ganglia 、 HyperHQ 、 ZABBIX 等, nagios 的插件很多功能也是很强大的,需要详尽的配置实现; HyperHQ 会开启 JAVA 的应用并对 JAVA 的应用服务器会自动设备,想起来 JAVA 对内存的的强烈需求还是会有过估计。这这了推荐使用 Monit 、 Munin 以及 ntop 一个简单的网络流量的监控工具。相对前面列举的强大功能需要复杂配置,本文将描述的 Monit 、 Munin 、 Ntop 只是需要使用 yum 进行安装就可以方便的使用监控主机上的资源并发出警告邮件。
Munin 是一个类似 cacti 的性能展示工具,走的是更可靠的 TCP 协议。不会出现 cacti 基于 snmp 这样的监控方式会出现各种各样的问题。 Munin 是用 Perl 编写的 , 有很多插件功能,支持 Unix 操作系统的多种版本,用到很多不同的 perl 包; Munin 和 MRTG 类似,也可以通过各种插件将服务器的各方面状态以图像的形式表现出来。 munin 画图也是使用 rrdtool ,因此 Net::Server perl 模块和 rrdtool 都已经安装在了服务器上这些必须安装 munin 前安装完毕。
Munin 的主要优点就是它能够同时提供收集信息和分析信息的功能,这样你就无需在每个节点上安装一个 web 服务器,并且详细信息页面底部的附加信息能够帮助你获取一些重要数据的统计信息。
Debian/Ubuntu 和 Fedora 都附有 munin 安装包,不过 RHEL 和 Centos 默认没有包括 munin 的安装包。
Munin 功能主要为收集和分析功能,这可以帮助你收集多个服务器信息并在一个中央服务器上分析所有收集到的信息,通过 apt 源或者 yum 源使其安装和更新都相当简单。
当你访问 http://localhost/munin 时, Munin 会显示一张概览图向你展示所有连接到的节点,包括到节点的具体功能,如磁盘、网络、 NFS 和进程。点击节点名称会显示两行的图表,每一行都会在左边显示每日统计信息,右边显示每周统计信息,点击这些图表能够提供显示某日、某月的详细信息页面。在这项细节信息页面的底部,显示的是关于数据的更加详细的信息,包括不规则活动通知等。
Monit 是一款运行于应用层功能非常丰富的用于对系统中的进程、文件、目录、以及设备等进行监视和管理的工具。 Monit 包含一个内嵌的 HTTPS Web 界面,可以使用浏览器方便地查看 Monit 所监视的服务器。 Monit 对管理员来说可谓神器也。 Monit 可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误。 Nagios 也就是在监控,而 Monit 不但本地监控,远程服务监控。可以做到服务就永远都能“死而复生”,是服务一直在线。 Monit 可以监控的主机信息如下:
* System Services o Cron (program timer) o Gdm (gnome desktop manager) o Inetd (internet service manager) o Syslogd (system logfile daemon) o Xfs (X font server) o YPBind (Yellow page bind daemon) o Net-SNMP (SNMP agent) o NTP (time server) o Nscd (name service caching daemon) * Name Services o Bind (chrooted) * FTP Services o vsftpd * Login Services o SSHD * WWW Services o Apache (web server) o Zope (appication server) o Squid (http/ftp proxy) o Privoxy (spamfilter proxy) * Mail Services o Postfix (mail server) o sendmail (mail server) o Qpopper (pop3 server) o Spamassassin daemon (spam scan daemon) o Amavis-new (mail virus scanner) o Policyd (Postfix access policy delegation daemon) * Virus Scanner o Sophie (virus scan daemon) o Trophie (virus scan daemon) o Clamavd (virus scan daemon) * Printing Services o LPRng (printer daemon) * Database Services o MySQL Server o OpenLDAP Server * File Services o Samba (windows file/domain server) * Sun ONE Services o iPlanetDirectoryServer (Sun ONE) o iPlanetMessagingServer processes (Sun ONE) o iPlanetCalendarServer processes (Sun ONE) * Misc Services o apcupsd (APC ups daemon) o Webmin (remote admin service) o STunnel (SSL tunnel) * Misc Usage o Watch and analyze crashdumps (Solaris) o Watch and analyze crashdumps (Linux) o Start and stop tcpdump based on condition o Rotate tcpdump until condition occures o MySQL event driven process list |
Ntop 跟 top 监视系统活动状况相似, ntop 是一个用来实时监控网络使用情况的工具。由于 ntop 具有 Web 界面模式,因此无论是配置还是使用都很容易在短时间之内快速上手。 Ntop 让网络流量透明化, NTOP 显示网络的使用情况比 MRTG 更加直观、详细, NTOP 甚至可以列出每个节点计算机的网络带宽利用率。 NTOP 是一个灵活的、功能齐全的,用来监控和解决局域网问题的工具。它同时提供命令行输入和 WEB 界面,可应用于嵌入式 WEB 服务。可以通过分析网络流量来确定网络上存在的各种问题;也可以用来判断是否有黑客正在攻击网络系统;还可以很方便地显示出特定的网络协议、占用大量带宽的主机、各次通信的目标主机、数据包的发送时间、传递数据包的延时等详细信息。通过了解这些信息,网管员可以对故障做出及时的响应,对网络进行相应的优化调整,以保证网络运行的效率和安全。
上面介绍的 munin, monit,ntop 工具传统的基于源码的安装方式相对要解决的软件包的依赖关系比较的多,这个也是很多 Linux 的管理员选择监控工具相对比较单一的安装因素。很多人都是习惯于使用 redhat 的发行版本,尽管 yum 是在软件包安装方面比较的强大不过默认的 redhat 的官方的 yum 源和 Centos 的社区源是没有 munin, monit,ntop 这三个软件包源提供的,而在 Ubuntu/Debian 的 apt 的源中默认都是有支持的。本文将告诉大家如何使用第三方的 RPMforge Repository yum 源来快速安装 munin, monit,ntop 工具并简单的配置,实现 Linux 主机网络的监控的应用。
RPMforge Repository 源分为 i386 和 X86_64 位版本
在 i386 版本在中的安装方式
wget http:#packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm rm -f rpmforge-release-0.3.6-1.el5.rf.i386.rpm |
wget http:#packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm rm -f rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm |
安装后在 /etc/yum.repos.d 文件夹中生成 mirrors-rpmforge rpmforge.repo 配置信息。
有了这个 RPMforge Repository 的源就可以轻松的开始 munin, monit,ntop 的监控之旅。
安装和配置 munin
执行 yum 完成安装
yum install munin munin-node httpd |
设置 munin 启动脚步并启动 munin
chkconfig --levels 235 munin-node on /etc/init.d/munin-node start |
vi /etc/munin/munin.conf dbdir /var/lib/munin htmldir /var/www/html/monitoring #此行设置web页面及图形数据的存放位置 logdir /var/log/munin rundir /var/run/munin # Where to look for the HTML templates tmpldir /etc/munin/templates # a simple host tree [localhost] #此部分定义一个监控主机的信息 address 127.0.0.1 use_node_name yes |
新建 /var/www/html/monitoring 文件夹并设置权限
mkdir -p /var/www/html/monitoring chown munin:munin /var/www/html/monitoring /etc/init.d/munin-node restart |
[root@server146 monitoring]# ls -l /var/www/html/monitoring 总计 28 -rw-r--r-- 1 munin munin 2555 07-22 15:15 definitions.html -rw-r--r-- 1 munin munin 1584 07-23 17:00 index.html drwxr-xr-x 2 munin munin 12288 07-22 15:15 localhost -rw-r--r-- 1 munin munin 473 07-22 15:15 logo.png -rw-r--r-- 1 munin munin 3538 07-22 15:15 style.css |
通过 http://ip/monitoring/ 就可以查看到主机的监控信息 http:#192.168.1.146/monitoring/localhost/localhost.html#Network 图: Munin 监控信息
图: Munin 监控信息每天
图: Munin 监控信息每周
安装和配置 monit
执行 yum 完成安装
yum install monit |
chkconfig --levels 235 monit on
默认的 /etc/monit.conf 文件中是注释的示例配置文件, http:#mmonit.com/wiki/Monit/ConfigurationExamples 这里是官方提供的配置文件参考。
http:#mmonit.com/monit/documentation/monit.html 这里是详尽的配置文件的语法格式信息。
大家注意到 /etc/monit.conf 文件末尾的信息。
############################################################################### ## Includes ############################################################################### ## ## It is possible to include additional configuration parts from other files or ## directories. # include /etc/monit.d/* # # |
此处的信息的作用是与 apache 的配置文件类似,在 /etc/monit.d/ 中的每个文件都是一个独立的配置文件,便于配置文件的结构化和维护操作。
创建一个简单的配置文件
vi /etc/monit.d/monitrc set daemon 60 #每2分钟检查一次 set logfile syslog facility log_daemon #monit的日志文件 set mailserver localhost #设置邮件服务器 set mail-format { from: monit@192.168.1.149 } #出现错误的时候发报警邮件到指定的邮箱 set alert lvsheat@qq.com set httpd port 2812 and use address 192.168.1.149 #设置http监控页面的端口和ip SSL ENABLE PEMFILE /var/certs/monit.pem allow admin:test #认证的用户名和密码 #平均负载.内存使用率,cpu使用率 check system 192.168.1.149 if loadavg (1min) > 4 then alert if loadavg (5min) > 2 then alert if memory usage > 75% then alert if cpu usage (user) > 70% then alert if cpu usage (system) > 30% then alert if cpu usage (wait) > 20% then alert # 磁盘空间使用率 check device data with path /dev/sda2 if space usage > 90% then alert if inode usage > 85% then alert check device home with path /dev/sda3 if space usage > 85% then alert if inode usage > 85% then alert #监控vsftpd服务 check process vsftpd with pidfile /var/run/vsftpd.pid start program = "/etc/init.d/vsftpd start" stop program = "/etc/init.d/vsftpd stop" if failed port 21 protocol ftp then restart if 5 restarts within 5 cycles then timeout #监控ssh服务 check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout #监控mysql服务 check process mysql with pidfile /var/run/mysqld/mysqld.pid group database start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout #监控apache服务 check process apache with pidfile /var/run/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host 192.168.1.149 port 80 protocol http and request "/monit/testok" then restart if cpu is greater than 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 500 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout #监控postfix 邮件服务 check process postfix with pidfile /var/spool/postfix/pid/master.pid group mail start program = "/etc/init.d/postfix start" stop program = "/etc/init.d/postfix stop" if failed port 25 protocol smtp then restart if 5 restarts within 5 cycles then timeout #监控自定义服务 check process web_lb with pidfile /opt/server/web_lb/httpd.pid start program = "/opt/bin/lb.sh" #启动脚本 stop program = "/opt/bin/lb_stop.sh" #停止脚本 if failed host 192.168.1.149 port 6101 proto http then restart if failed host 192.168.1.149 port 6101 proto http for 5 times within 5 cycles then exec "/opt/bin/lb_pay.sh" if failed host 192.168.1.149 port 6102 type TCPSSL proto http then restart if failed host 192.168.1.149 port 6102 type TCPSSL proto http for 5 times within 5 cycles then exec "/opt/bin/lb_pay.sh |
这个配置文件监控了 vsftpd, sshd, mysql, apache, postfix 服务,设定 web 监控的端口是 2812 ,设置 lvsheat@qq.com 将接受到警告邮件。
配置 apache 的检查信息
mkdir /var/www/monit echo "hello" > /var/www/monit/token |
mkdir /var/certs cd /var/certs |
编辑一个默认的 OpenSSL 的配置文件
vi /var/certs/monit.cnf # create RSA certs - Server RANDFILE = ./openssl.rnd [ req ] default_bits = 1024 encrypt_key = yes distinguished_name = req_dn x509_extensions = cert_type [ req_dn ] countryName = Country Name (2 letter code) countryName_default = MO stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Monitoria localityName = Locality Name (eg, city) localityName_default = Monittown organizationName = Organization Name (eg, company) organizationName_default = Monit Inc. organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = Dept. of Monitoring Technologies commonName = Common Name (FQDN of your server) commonName_default = server.monit.mo emailAddress = Email Address emailAddress_default = root@monit.mo [ cert_type ] nsCertType = server |
创建 /var/certs/monit.pem 文件
openssl req -new -x509 -days 365 -nodes -config ./monit.cnf -out /var/certs/monit.pem -keyout /var/certs/monit.pem openssl gendh 512 >> /var/certs/monit.pem openssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pem chmod 700 /var/certs/monit.pem |
/etc/init.d/monit start
通过 https://192.168.1.149:2812/ 访问监控页面信息查看邮件可以看到监控的邮件警告信息
图:登陆信息输入admin 密码test
图:监控的信息清单
图:监控的服务的详细信息
图:收到的监控警告邮件
图:监控邮件内容
Ntop的安装和使用
执行yum 安装ntop
yum install ntop |
启动ntop
ntop -P /tmp -u nobody ntop startup - waiting for user response! Please enter the password for the admin user: Please enter the password again: |
第一次執行会要求输入admin的password
启动后查看本机的端口信息,会出现TCP3000的端口
netstat -tlnup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 :::3000 :::* LISTEN 3932/ntop |
以在web浏览器中输入http://ip:3000 就可以访问到监控的信息页面
图:Ntop监控信息
图:Ntop监控信息
现在就可以使用munin monit ntop监控Linux主机的服务及资源。使用yum可以很便捷的安装软件包,达到快速实现系统服务的目的,维护使用Centos的一部分工作就是选择一个很好的yum的源,这样可以很快捷的实现管理。
作者简介:CU网友kns1024wh,目前从事Linux群集方面的具体工作,之前做过多年的IT技术支持、MCT讲师、及REDFLAG的技术合作,技术专长群集、unix主机、AD部署等,您可以通过电子邮件lvsheat@qq.com或者Chinaunix社区与他取得联系。