免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6638 | 回复: 0
打印 上一主题 下一主题

独辟蹊径之Linux主机监控工具munin monit ntop[第十一期] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-17 07:41 |只看该作者 |倒序浏览
独辟蹊径之Linux主机监控工具munin monit ntop
ChinaUnix网友:kns1024wh
  本文介绍在centos 5.x环境下通过yum源的扩展使用muninmonitntop工具来监管你的应用程序和服务器。题为懒人说说的是简便的安装方式而已,将强大的功能配置简单的应用起来是很重要的,可以节省时间并提高效率。
Monithttp:#www.tildeslash.com/monit/
Munin: http:#munin.projects.linpro.no/
Ntophttp:#www.ntop.org/

  当你的Linux主机投入生产环境以后,想要知道系统或者服务每天或者每周的运行情况,Munin是最佳选择。在某些情况下,监控Linux主机服务、应用程序的运行状态,是非常有必要的,尤其是应用程序或服务会出现错误,并可能开始吞噬你主机的内存和cpu,这将导致主机上的应用不能正常的运行。因此,需要其他一些程序来监控你的程序,在程序出错以后重启你的程序。在linux下解决方案,如NagiosgangliaHyperHQZABBIX等,nagios的插件很多功能也是很强大的,需要详尽的配置实现;HyperHQ会开启JAVA的应用并对JAVA的应用服务器会自动设备,想起来JAVA对内存的的强烈需求还是会有过估计。这这了推荐使用 MonitMunin以及ntop一个简单的网络流量的监控工具。相对前面列举的强大功能需要复杂配置,本文将描述的MonitMuninNtop只是需要使用yum 进行安装就可以方便的使用监控主机上的资源并发出警告邮件。
  Munin是一个类似cacti的性能展示工具,走的是更可靠的TCP协议。不会出现cacti基于snmp这样的监控方式会出现各种各样的问题。Munin是用Perl编写的,有很多插件功能,支持Unix操作系统的多种版本,用到很多不同的perl包;MuninMRTG类似,也可以通过各种插件将服务器的各方面状态以图像的形式表现出来。munin画图也是使用rrdtool,因此Net::Server perl模块和rrdtool都已经安装在了服务器上这些必须安装munin前安装完毕。
  Munin的主要优点就是它能够同时提供收集信息和分析信息的功能,这样你就无需在每个节点上安装一个web服务器,并且详细信息页面底部的附加信息能够帮助你获取一些重要数据的统计信息。
  Debian/UbuntuFedora都附有munin安装包,不过RHELCentos默认没有包括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

  Ntoptop 监视系统活动状况相似,ntop 是一个用来实时监控网络使用情况的工具。由于 ntop 具有 Web 界面模式,因此无论是配置还是使用都很容易在短时间之内快速上手。Ntop让网络流量透明化,NTOP显示网络的使用情况比MRTG更加直观、详细,NTOP甚至可以列出每个节点计算机的网络带宽利用率。NTOP是一个灵活的、功能齐全的,用来监控和解决局域网问题的工具。它同时提供命令行输入和WEB界面,可应用于嵌入式WEB服务。可以通过分析网络流量来确定网络上存在的各种问题;也可以用来判断是否有黑客正在攻击网络系统;还可以很方便地显示出特定的网络协议、占用大量带宽的主机、各次通信的目标主机、数据包的发送时间、传递数据包的延时等详细信息。通过了解这些信息,网管员可以对故障做出及时的响应,对网络进行相应的优化调整,以保证网络运行的效率和安全。
  上面介绍的munin, monit,ntop工具传统的基于源码的安装方式相对要解决的软件包的依赖关系比较的多,这个也是很多Linux的管理员选择监控工具相对比较单一的安装因素。很多人都是习惯于使用redhat的发行版本,尽管yum是在软件包安装方面比较的强大不过默认的redhat的官方的yum源和Centos的社区源是没有munin, monit,ntop这三个软件包源提供的,而在Ubuntu/Debianapt的源中默认都是有支持的。本文将告诉大家如何使用第三方的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
x86_64 版本中的安装方式
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
(https:#rpmrepo.org/RPMforge/Using 最新版本信息).
  安装后在/etc/yum.repos.d文件夹中生成mirrors-rpmforge rpmforge.repo 配置信息。
有了这个RPMforge Repository的源就可以轻松的开始munin, monit,ntop的监控之旅。
  安装和配置 munin
  执行yum 完成安装
yum install munin munin-node httpd
  如果没有安装apahce 需要添加上httpd参数
  设置 munin启动脚步并启动 munin
chkconfig --levels 235 munin-node on
/etc/init.d/munin-node start
  简单设置 munin的配置文件/etc/munin/munin.conf
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
观察/var/www/html/monitoring的文件夹的信息如下
[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
设置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
创建/var/certs/monit.pem信息,设置httpsweb监控访问
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
启动Monit服务
/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:

第一次執行会要求输入adminpassword
启动后查看本机的端口信息,会出现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社区与他取得联系。

[ 本帖最后由 八重樱 于 2009-9-17 07:46 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP