利用 collectd 和 Graphite 监控 KVM 服务器

1.collectd 主要是在被监控主机上收集数据,Graphite 利用 carbon,whisper 存储收集过来的数据

2.在ubuntu 12.04 上安装 Graphite

$ sudo apt-get install apache2 libapache2-mod-wsgi python-django \
python-twisted python-cairo python-pip python-django-tagging
$ sudo pip install whisper
$ sudo pip install carbon
$ sudo pip install graphite-web

$ cd /opt/graphite/conf/
$ sudo cp carbon.conf.example carbon.conf
$ sudo cp storage-schemas.conf.example storage-schemas.conf
$ sudo cp graphite.wsgi.example graphite.wsgi

$ wget -c https://launchpad.net/graphite/0.9/0.9.10/+download/graphite-web-0.9.10.tar.gz
$ tar -zxf graphite-web-0.9.10.tar.gz
$ cd graphite-web-0.9.10
$ sudo cp example-graphite-vhost.conf /etc/apache2/sites-available/graphite
$cd /etc/apache2/site-enabled/;sudo ln -s ../sites-available/graphite
$sudo mkdir /etc/apache2/run
$ sudo /etc/init.d/apache2 reload

$ cd /opt/graphite/webapp/graphite/
$ sudo python manage.py syncdb
$ sudo chown -R www-data:www-data /opt/graphite/storage/
$ sudo cp local_settings.py.example local_settings.py
$sudo sed -i  -e '/TIME_ZONE/s/America\/Chicago/Asia\/Shanghai/' local_settings.py
$ sudo /etc/init.d/apache2 restart

$ cd /opt/graphite/
$ sudo ./bin/carbon-cache.py start
$ sudo iptables -A INPUT -p tcp -m state --state NEW -m tcp --syn --dport 2003 -j ACCEPT

浏览器访问ubuntu IP地址后就可以看到 graphite web 界面:

3.在被监控主机centos 6.2 安装 collectd

$ sudo rpm -ivh http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-7.noarch.rpm
$ sudo yum update && sudo yum install collectd-virt collectd
$ sudo yum install git
$ sudo git clone https://github.com/indygreg/collectd-carbon.git /opt/collectd-plugins/

$ sudo vim /etc/collectd.d/graphite.conf #加入以下配置
<LoadPlugin "python">
Globals true
</LoadPlugin>
<Plugin "python">
ModulePath "/opt/collectd-plugins/"
Import "carbon_writer"
<Module "carbon_writer">
LineReceiverHost "XXX.XXX.XXX.XXX" #Graphite 主机地址
LineReceiverPort 2003
DifferentiateCountersOverTime true
LowercaseMetricNames true
TypesDB "/usr/share/collectd/types.db"
</Module>
</Plugin>

$ sudo service /etc/init.d/collectd start

查看graphite web 界面中kvm主机的数据

参考:vpsee.com 1. 2.

             collectd.org graphite  libvirt.org