使用pnp4nagios虽然可以出图了,但感觉还是不太好看,后来看到grafana绘图不错,捣鼓了下,便选择了 nagios + graphios + graphite + grafana 的绘图方案
grafana有个在线demo,可以体验下效果,地址:
ttp://play.grafana.org/
一.nagios安装
网上教程很多,官方的文档也很详尽,这里就不表了
二.graphite安装
graphite使用Django框架,python版本需要2.6及以上
1.准备环境
yum install -y bitmap bitmap-fonts-compat Django diagno django-tagging MySQL-python fontconfig cairo python-devel python-memcached python-twisted pycairo mod_python python-ldap python-simplejson memcached python-zope-interface mod_wsgi python-sqlite2
2.获取安装包并安装
git clone https://github.com/graphite-project/graphite-web
git clone https://github.com/graphite-project/carbon
git clone https://github.com/graphite-project/whisper
解压软件包,进行软件包目录,然后执行以下命令:
python setup.py install
Graphite-web, Carbon, and Whisper安装方法相同
除了通过git获取源码安装外,也可以用pip安装
# pip install whisper
# pip install carbon
# pip install graphite-web
3.添加配置文件
cd /opt/graphite/conf/
cp carbon.conf.example carbon.conf
cp storage-schemas.conf.example storage-schemas.conf
cp graphite.wsgi.example graphite.wsgi
配置apache
cat /opt/graphite/examples/example-graphite-vhost.conf >> /etc/httpd/conf/httpd.conf
vi /etc/httpd/conf/httpd.conf
修改端口为8080
<VirtualHost *:8080>
修改WSGISocketPrefix 为
WSGISocketPrefix /var/run/httpd/wsgi
增加密码验证
<Location />
AuthName "graphite"
AuthType Basic
AuthUserFile /opt/graphite/htpasswd
<LimitExcept OPTIONS>
require user graphite
</LimitExcept>
</Location>
4.初始化数据库
cd /opt/graphite/webapp/graphite/
cp local_settings.py.example local_settings.py
python manage.py syncdb
chown -R apache:apache /opt/graphite
5.启动graphite
设置登陆graphite的用户名及密码
htpasswd -c /opt/graphite/htpasswd graphite
/opt/graphite/bin/carbon-cache.py start
service httpd start
访问http://ip 即可进入graphite
三.grafana安装
1.apache添加grafana配置
<VirtualHost *:8081>
ServerName grafana
DocumentRoot "/opt/grafana/src/"
<Location />
AuthName "grafana restricted"
AuthType Basic
AuthUserFile /opt/grafana/htpasswd
<LimitExcept OPTIONS>
require user grafana
</LimitExcept>
</Location>
</VirtualHost>
设置登陆grafana的用户名及密码
htpasswd -c /opt/grafana/htpasswd grafana
2.apache下graphite添加CORS支持
<VirtualHost *:8080>
Header set Access-Control-Allow-Origin "http://ip地址:8081"
Header set Access-Control-Allow-Methods "GET, OPTIONS"
Header set Access-Control-Allow-Headers "origin, authorization, accept"
Header set Access-Control-Allow-Credentials true
3.安装elasticsearch
# 增加elasticsearch的repo:
rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch
vi /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-1.0]
name=Elasticsearch repository for 1.0.x packages
baseurl=http://packages.elasticsearch.org/elasticsearch/1.0/centos
gpgcheck=1
gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch
enabled=1
#直接yum安装
yum install elasticsearch -y
yum install npm -y
#启动elasticsearch
service elasticsearch start
由于默认通过9200端口可以直接访问并操作elasticsearch,使得系统的安全性降低,需屏蔽外网访问9200端口,通过apache的代理来实现内部调用
#elasticsearch proxy
Listen 8082
<VirtualHost *:8082>
ProxyRequests Off
ProxyPass / http://127.0.0.1:9200/
ProxyPassReverse / https://127.0.0.1:9200/
<Location />
AuthName "es"
AuthType Basic
AuthUserFile /opt/grafana/eshtpasswd
<LimitExcept OPTIONS>
require user elase
</LimitExcept>
</Location>
Header set Access-Control-Allow-Methods "GET, POST, DELETE, OPTIONS, PUT"
Header set Access-Control-Allow-Headers "Content-Type, X-Requested-With, X-HTTP-Method-Override, Origin, Accept, Authorization"
Header set Access-Control-Allow-Credentials "true"
Header set Cache-Control "max-age=0"
Header set Access-Control-Allow-Origin "http://ip地址:8081"
</VirtualHost>
4.配置grafana
获取源码
wget http://grafanarel.s3.amazonaws.com/grafana-1.9.1.tar.gz
tar zxvf grafana-1.9.1.tar.gz
mv grafana-1.9.1 /opt/grafana
cd /opt/grafana
cp config.sample.js config.js
vi config.js
修改
elasticsearch:"http://"+window.location.hostname+":9200",
为
elasticsearch: "http://username:password@ip地址:8082", #8082端口的用户名和密码
修改
graphiteUrl:"http://"+window.location.hostname+":8080",
为
graphiteUrl:"http://"username:password@ip地址:8080", #graphite的用户名和密码
设置登陆的用户及密码
htpasswd -c /opt/grafana/htpasswd grafana
到这一步就配置完成了,打开浏览器,输入http://ip地址:8081 即可访问grafana。
但现在graphite还获取不到nagios的数据,无法给nagios绘图,google了下,发现有个脚本graphios可以把nagios数据发送给graphite
四.安装graphios
1.配置nagios
vi /usr/local/nagios/etc/nagios.cfg
#添加以下内容
service_perfdata_file=/opt/spool/nagios/graphios/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$\tGRAPHITEPREFIX::$_SERVICEGRAPHITEPREFIX$\tGRAPHITEPOSTFIX::$_SERVICEGRAPHITEPOSTFIX$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=graphite_perf_service
host_perfdata_file=/opt/spool/nagios/graphios/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tGRAPHITEPREFIX::$_HOSTGRAPHITEPREFIX$\tGRAPHITEPOSTFIX::$_HOSTGRAPHITEPOSTFIX$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=graphite_perf_host
注意:
1`process_performance_data=1 此处的值要改为1,默认是0
2`host_perfdata_file 和service_perfdata_file的路径
3`以下目录的权限
chown -R apache:apache /opt/spool/nagios/graphios/
2.配置commands.cfg
vi /usr/local/nagios/etc/objects/commands.cfg
#添加以下内容
define command {
command_name graphite_perf_host
command_line /bin/mv /var/spool/nagios/graphios/host-perfdata /var/spool/nagios/graphios/host-perfdata.$TIMET$
}
define command {
command_name graphite_perf_service
command_line /bin/mv /var/spool/nagios/graphios/service-perfdata /var/spool/nagios/graphios/service-perfdata.$TIMET$
}
3.获取安装包
github上进行了更新,未做测试,这里放测试过可用的版本
http://download.csdn.net/detail/u012375924/8353521
下载后解压即可
tar xvf graphios.tar
mv graphios /opt/
cp graphios.init /etc/init.d/graphios
chown root:root /etc/init.d/graphios
chmod 750 /etc/init.d/graphios
vi /opt/graphios/graphios.py
carbon_server = 'ip' #改为服务器的ip
spool_directory = '/opt/spool/nagios/graphios' #spool文件路径
log_file = '/var/log/graphios/graphios.log' #日志存放路径
#注意文件的权限
chown nagios:nagios /opt/graphios/graphios.py
chown nagios:nagios /var/log/graphios/graphios.log
#测试graphios运行状况
/opt/graphios/graphios.py -v
看到如下信息
Processed 2 files in /opt/spool/nagios/graphios
查看/opt/spool/nagios/graphios是否有文件,如有文件,即是配置正确了。
#启动graphios
service graphios start
4.nagios使用graphios
#在nagios的每台服务器配置文件中增加graphios的定义
格式如下:
_graphiteprefix 服务名称
例如
define host {
name myhost
check_command check_host_alive
_graphiteprefix monitoring.nagios01.pingto
}
以上的配置都完成后,重启nagios,等待几分钟,访问http://ip地址:8081 ,进入grafana,添加模板。即可看到grafana的绘图效果了