最近开发团队配置了下服务器监控,用的是Munin。作为开发人员,我觉得有必要了解服务器的运行状态,特别是那些部署了自己写的程序的服务器。当面对出现 的问题,能立刻获取服务器各项指标状态的第一手资料,对问题的彻底解决会有很大的帮助。 dbanotes冯大辉说“笔者一向认为,即使网站有一台服务器,也应该搭建监控工具,这是保障网站能持续改进的基石。” 于是借鉴了一些web2.0网站的经验,使用可以绘图的服务器监控工具,将服务器的运行数据记录下来,并绘制成直观的图形。
对比了几款工具,Cacti、Munin,与Cacti要使用php和MySQL相比,Munin生成静态页面的方式更好些,虽然功能简单,但也够 用,被监控的服务器配置简单,plugin编写方便灵活。Cacti需要在被监控的服务器安装SNMP,配置复杂,所以坚决弃之。
Munin 工作方式简介
Munin分为master和node
master只需安装在一台服务器上即可,它每5分钟去各个node采集信息,并使用RRD格式记录数据,绘制成图表
node安装在各个被监控的服务器上,node上可以配置许多plugin,每个plugin有不同的职责,负责获取服务器各种不同的信息。
Installation on Red Hat Enterprise 4 (RHEL4)
我们社区目前使用的的服务器基本上都是Red Hat Enterprise 4,此文档也只针对这一种环境来写。(为啥不用CentOS,有yum,安装一些没必要自己编译的东西方便啊,像我这种用惯了Debian的APT功能的人...)
安装很简单,安装程序请使用我放在文章后面的压缩包,下载munin-1.2.6.tar.gz包
Munin node 安装
压缩包中munin-node文件夹下面是所有需要的rpm,解压缩后,进入munin-node,直接运行
rpm -Uvh *
即可安装完毕。
Munin master 安装
同munin-node,解压缩后,进入munin-master,运行
rpm -Uvh *
即可安装完毕。
Configuration
配置文件的说明还是参考一下官方文档吧,我在这里只给出一些实例和说明
Configuration of Munin master
后续更新......
master的配置文件中主要是记录各个node所在服务器的IP地址
Configuration of Munin node
修改 /etc/munin/munin-node.conf
只需在配置文件增加一行即可
allow ^192/.168/.xx/.xx$
Configuration of Munin node plugins
这一部分是munin安装中稍显复杂的一部分
jvm plugin
该附件是调用jstat -gc pid来获取jvm信息,要求JDK 1.5以上
- 下载: jstat_jdk5_munin.tar.gz
- 解压到 /usr/share/munin/plugins 中
- 在 /etc/munin/plugins 建立link,这是munin node plugin安装的特点
ln -s /usr/share/munin/plugins/jstat__* /etc/munin/plugins
- 修改 /etc/munin/plugin-conf.d/munin-node,增加
[jstat__*]
user root #使用此插件的系统用户
env.resinfolder resin3-bbs #假如你的应用在/usr/local/resin3-bbs,这里就填写 resin3-bbs
env.javahome /usr/local/jdk #JAVA_HOME路径 - 执行 service munin-node start
- 验证安装
[root@localhost ~]#telnet localhost 4949
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
# munin node at xxxx.com这时候进入了telnet交互界面,输入 fetch jstat__heap,能看到如下显示,表示插件安装完毕
fetch jstat__heap
Eden_Used.value 343109120
Eden_Free.value 101224960
Survivor0_Used.value 0
Survivor0_Free.value 11993088
Survivor1_Used.value 0
Survivor1_Free.value 12451840
Old_Used.value 8.36026e+07
Old_Free.value 5.20377e+08
Permanent_Used.value 4.3694e+07
Permanent_Free.value 2.34149e+07
. - 如果一台服务器有多个resin的jvm需要监控怎么办,比如有 resin3-footprint,resin3-bbs,resin3-album
第3步中创建link时,在link的名字上做文章,只是link的创建没法偷懒了
ln -s /usr/share/munin/plugins/jstat__gccount /etc/munin/plugins/jstat_footprint_gccount
ln -s /usr/share/munin/plugins/jstat__gccount /etc/munin/plugins/jstat_bbs_gccount
ln -s /usr/share/munin/plugins/jstat__gccount /etc/munin/plugins/jstat_album_gccount
ln -s /usr/share/munin/plugins/jstat__gctime /etc/munin/plugins/jstat_footprint_gctime
ln -s /usr/share/munin/plugins/jstat__gctime /etc/munin/plugins/jstat_bbs_gctime
ln -s /usr/share/munin/plugins/jstat__gctime /etc/munin/plugins/jstat_album_gctime
ln -s /usr/share/munin/plugins/jstat__heap /etc/munin/plugins/jstat_footprint_heap
ln -s /usr/share/munin/plugins/jstat__heap /etc/munin/plugins/jstat_bbs_heap
ln -s /usr/share/munin/plugins/jstat__heap /etc/munin/plugins/jstat_album_heap然后,第4步,/etc/munin/plugin-conf.d/munin-node的配置稍微修改一下
[jstat_footprint_*]
user root #使用此插件的系统用户
env.resinfolder resin3-footprint #假如你的应用在/usr/local/resin3-footprint,这里就填写 resin3-footprint
env.javahome /usr/local/jdk #JAVA_HOME路径
[jstat_bbs_*]
user root #使用此插件的系统用户
env.resinfolder resin3-bbs #假如你的应用在/usr/local/resin3-bbs,这里就填写 resin3-bbs
env.javahome /usr/local/jdk #JAVA_HOME路径
[jstat_album_*]
user root #使用此插件的系统用户
env.resinfolder resin3-album #假如你的应用在/usr/local/resin3-album,这里就填写 resin3-album
env.javahome /usr/local/jdk #JAVA_HOME路径
apache plugin
首先,补充一个munin没有的plugin,下载: apache_activity_munin.tar.gz
- 将apache_activity_munin.tar.gz解压缩到 /usr/share/munin/plugins
- 在 /etc/munin/plugins 建立link
ln -s /usr/share/munin/plugins/apache_* /etc/munin/plugins
- 修改 Apache 配置文件, httpd.conf,添加如下配置,并重启 Apache
<IfModule status_module>
ExtendedStatus On
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from localhost.localdomain
Allow from 127.0.0.1
Allow from localhost
</Location>
</IfModule>然后 wget http://localhost/server-status?auto ,看看打开的页面内容对不对,参考一下http://httpd.apache.org/server-status?auto
- 无需修改 /etc/munin/plugin-conf.d/munin-node,apache plugin的默认配置就可以了
- service munin-node restart
- 同安装jvm_plugin一样的验证安装,telnet localhost 4499 之后,执行 fetch apache_processes
mysql plugin
首先,补充一个munin没有的plugin,下载: mysql_munin.tar.gz
- 将 mysql_munin.tar.gz 解压缩到 /usr/share/munin/plugins
- 在 /etc/munin/plugins 建立link
ln -s /usr/share/munin/plugins/mysql_* /etc/munin/plugins
rm -f /etc/munin/plugins/mysql_isam_space_ - 修改 /etc/munin/plugin-conf.d/munin-node,增加
[mysql*]
user root # 使用此插件的系统用户
env.mysqladmin /usr/local/mysql/bin/mysqladmin # mysqladmin命令的完整路径
env.mysqlopts -uroot -pfnm_2008_fnm # mysql的用户名密码 - service munin-node restart
- 验证安装,telnet localhost 4499 之后,执行 fetch mysql_queries
memcached plugin
首先,补充一个munin没有的plugin,下载: memcached_munin.tar.gz
- 将 memcached_munin.tar.gz 解压缩到 /usr/share/munin/plugins
- 在 /etc/munin/plugins 建立link,注意,这里的link名字很重要
这个memcached plugin要求link名字格式是: memcached_connections_127_0_0_1_11211,IP和端口是在link名字中配置的
ln -s /usr/share/munin/plugins/memcached_bytes_ /etc/munin/plugins/memcached_bytes_127_0_0_1_11211
ln -s /usr/share/munin/plugins/memcached_connections_ /etc/munin/plugins/memcached_connections_127_0_0_1_11211
ln -s /usr/share/munin/plugins/memcached_hits_ /etc/munin/plugins/memcached_hits_127_0_0_1_11211
ln -s /usr/share/munin/plugins/memcached_items_ /etc/munin/plugins/memcached_items_127_0_0_1_11211
ln -s /usr/share/munin/plugins/memcached_requests_ /etc/munin/plugins/memcached_requests_127_0_0_1_11211
ln -s /usr/share/munin/plugins/memcached_traffic_ /etc/munin/plugins/memcached_traffic_127_0_0_1_11211- service munin-node restart
- 验证安装,telnet localhost 4499 之后,执行 fetch memcached_requests_127_0_0_1_11211
补充
补充一些验证plugin正常工作的方法
符合要求的plugin都能够在命令行接受一个autoconf参数,来做自身检验,如果有问题,会输出信息,告诉你不能运行的原因,安装plugin的过程中也应该执行一下。