@sww4718168
2015-06-14T06:47:29.000000Z
字数 10322
阅读 2842
nagios的报警功能虽强,但是缺失绘图,cactai绘制的图可定制性却太差,所以传统nagios+cactai的组合被我放弃。从网上看到grafana的图表非常漂亮,而且可以通过graphite与nagios结合,所以决定使用nagios作为后端实现报警及数据支持并使用grafana进行图表展现。
各个组件的用途:
- grafana从graphite获取数据,并呈现(可以直接使用graphite的图)
- graphite对数据进行处理并存储(他本身的绘图功能也很强大)
- graphios将nagios获取的数据推送给graphite
- nagios获取数据及报警
不喜欢apache的笨重,所以使用nginx来server nagios。
具体安装过程可以参考官方文档,这里只写nginx相关部分。
直接上命令:
sudo yum install epel-release.noarchsudo yum install nginx php-fpm gcc glibc glibc-common gd gd-devel
nagios的安装路径为/opt/nagios/
配置nginx:
# 此配置文件还需要优化,准备用uwsgi来替换其他cgi解析程序。cat > /etc/nginx/conf.d/nagios.conf <<EOFserver {listen 8080;server_name monitor.flexops.org;# root /var/www/html;charset utf-8; # 我这里出现了乱码,所以加上了这行index index.php index.html index.htm;# 增加认证,如果没有认证,nagios的监控条目是看不到的auth_basic "Nagios Access";auth_basic_user_file /opt/nagios/etc/htpasswd; # 由htpasswd(apache的工具)生成,可以在安装有apache的机器上生成,然后copy过来location / {root /opt/nagios/share/; # 静态文件目录}# 分离php,由php-fpm解析php文件。location ~ \.php$ {root /opt/nagios/share/;include fastcgi_params;# fastcgi_params里面没有下面这条magic parameter,加上才能让fcgi找到具体php文件。fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_pass unix:/var/run/php-fpm.sock; # socket比ip:port方式效率要高}# 分离nagios调用的perl cgi。nginx默认不支持直接处理cgi文件,可以使用fcgiwrap。因CentOS7的源里没有该包,所以这里使用其他处理方式,127.0.0.1:8999就是其端口。location ~ ^/nagios/cgi-bin/(.*)\.cgi$ {#auth_basic "Nagios Access";#auth_basic_user_file /opt/nagios/etc/htpasswd;root /opt/nagios/sbin/;rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;# 将认证信息传递给cgifastcgi_param AUTH_USER $remote_user;fastcgi_param REMOTE_USER $remote_user;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;fastcgi_pass 127.0.0.1:8999;}# 开始未配置下面部分,nagios主页获取不到nagios daemon的信息,通过开发者工具发现调用地址不一样。所以加入下面的locationlocation ~ ^/cgi-bin/(.*)\.cgi$ {#auth_basic "Nagios Access";#auth_basic_user_file /opt/nagios/etc/htpasswd;root /opt/nagios/sbin/;rewrite ^/cgi-bin/(.*)\.cgi /$1.cgi break;fastcgi_param AUTH_USER $remote_user;fastcgi_param REMOTE_USER $remote_user;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;fastcgi_pass 127.0.0.1:8999;}}EOF# 配置cgi的解析程序sudo wget flexops.org/download/nginx/perl/fastcgi-wrapper.pl -O /usr/bin/fastcgi-wrapper# fastcgi-wrapper的开机启动脚本sudo wget flexops.org/download/nginx/perl/perl-fcgi.init-scripts -O /etc/init.d/perl-fcgisudo chkconfig --add perl-fcgisudo systemctl enable perl-fcgisudo systemctl start perl-fcgi# 启动nginxsudo systemctl enable nginxsudo systemctl start nginx# 启动nagiossudo systemctl enable nagiossudo systemctl start nagios
现在可以打开浏览器查看下nagios是否已经正常运行了
http://127.0.0.1:8080/
这里不讨论关于nagios监控其他机器的配置方法。
关于graphite的更多信息可以参阅官方文档
InfoQ有篇关于graphite的详细介绍,中文的,对各个组件介绍很详细。还有更详细的配置和测试方法。点这里查看
继续上命令:
sudo yum install python-pip python-devel# pip 命令由 python-pip 提供,python部分扩展是由C写的。# 使用pip安装扩展是从pypi下载源码包进行然后编译安装,所以确保python-devel和gcc等工具和库齐全# 使用pip的原因是便于管理,且不受CentOS源的影响。sudo pip install https://github.com/graphite-project/ceres/tarball/mastersudo pip install whispersudo pip install carbon # carbon 依赖python-devel和gccsudo pip install graphite-web# 默认安装到/opt/graphite下,可以使用-t选项指定其他目录# 例: sudo pip install -t /usr/local/graphite graphite-web# 关于更多定制化选项请参考grahite[官方文档](https://graphite.readthedocs.org/en/latest/install-pip.html)# graphite是由django框架写的,安装django# sudo pip install django# graphite依赖cairocffi库来进行绘图sudo yum install python-cairocffi # 因其依赖一些二进制库所以使用yum安装
更改graphite的配置
sed -i "/#TIME_ZONE/a TIME_ZONE = 'Asia/Shanghai'" /opt/graphite/webapp/graphite/local_settings.py
使用nginx来server graphite
# 亦可使用pip安装uwsgi,不过如果想通过systemd管理其启动,会比较麻烦# 关于uwsgi的更多信息可以参阅uwsgi[官方文档](http://uwsgi-docs.readthedocs.org/en/latest/)sudo yum install uwsgi-plugin-python.x86_64 uwsgi.x86_64sudo cp /opt/graphite/conf/example/graphite.wsgi.example /opt/graphite/conf/graphite.wsgicat > /etc/uwsgi.ini <<EOF"[uwsgi]#uid = uwsgi#gid = uwsgipidfile = /run/uwsgi/uwsgi.pid#emperor = /etc/uwsgi.d#stats = /run/uwsgi/stats.sock#emperor-tyrant = true#cap = setgid,setuidlog-date=truechmod-socket = 660processes = 2uid = nginxgid = nginx# socket = /run/uwsgi/graphite.sockhttp-socket = :7000# 如果通过pip安装uwsgi,下面两行需要删除plugins-dir = /usr/lib64/uwsgiplugins = python#chdir = /opt/graphite/conf#module = wsgi:applicationwsgi-file = /opt/graphite/conf/graphite.wsgimaster = truelogto=/var/log/uwsgi_graphite.logEOF"sudo systemctl enable uwsgisudo systemctl start uwsgi# nginx 的配置文件尚有问题,不能正常转发,现在可以直接通过127.0.0.1:7000来访问graphite# uwsgi的坑比较多,不过功能非常强大。# 下面的nginx配置部分暂时可以忽略,待更新## TODO: 使用nginx来增加并发能力cat > /etc/nginx/conf.d/graphite.conf <<EOF"server {listen 8081;server_name monitor.tedu.cn;root /opt/graphite/webapp;index index.html index.htm;access_log /var/log/nginx/graphite.access.log;error_log /var/log/nginx/graphite.error.log;location / {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' "origin, authorization, accept";include uwsgi_params;uwsgi_pass 127.0.0.1:7000;}location /content/ {alias /opt/graphite/webapp/content;}}EOF"sudo cp /opt/graphite/conf/example/storage-aggregation.conf.example /opt/graphite/conf/storage-aggregation.confsudo cp /opt/graphite/conf/example/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.confsudo cp /opt/graphite/conf/example/carbon.conf.example /opt/graphite/conf/carbon.conf# 这里我使用了默认配置,没有进行改动。其监听端口、更新时间间隔等在此文件,可以根据需要进行更改。# 启动carbonsudo /opt/graphite/bin/carbon-cache.py startss -tulanp | grep :2003 # 检查是否成功启动
graphite本身的图表绘制功能也是很强大的,而且创建好的dashboard可以直接导入grafana。
有兴趣的同学可以自行探索,这里不做介绍
具体可以参阅官方文档或者InfoQ的文章
graphios的源码托管于github上,有兴趣的可以去看看。这里也有不甚详细配置介绍 ^_^ .
本插件亦使用从pip安装的方式。使用pip或者yum省去了很多,比如更改nagios的配置文件,比如增加自启动脚本
sudo pip install graphios
# 更改为nagios获取的数据存放的位置,nagios的配置文件里也要进行相应更改# 使用pip安装的可以忽略以下配置文件修改的操作,这些在安装时会自己检测并修改sed -i '/^spool_directory/c spool_directory = /opt/nagios/var/graphios/' /etc/graphios/graphios.cfgsed -i '/^log_file/c log_file = /opt/nagios/var/graphios.log' /etc/graphios/graphios.cfg # 更改日志的存放位置
# 如果没有更改carbon的端口,这步可以忽略sed -i '/^carbon_servers/c carbon_servers = 127.0.0.1:2004' /etc/graphios/graphios.cfg
检查/opt/nagios/etc/nagios.cfg文件中增加了如下内容,如果没有请手动添加:
process_performance_data=1
service_perfdata_file=/opt/nagios/var/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$\tMETRICTYPE::$_SERVICEMETRICTYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=graphios_perf_service
host_perfdata_file=/opt/nagios/var/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$\tMETRICTYPE::$_HOSTMETRICTYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=graphios_perf_host
cfg_dir=/opt/nagios/etc/objects
将/opt/nagios/etc/nagios.cfg文件中其他cfg_dir配置行注释掉
检查是否存在/opt/nagios/etc/objects/graphios_commands.cfg文件,并有如下内容:
define command {
command_name graphios_perf_host
command_line /bin/mv /opt/nagios/var/graphios/host-perfdata /opt/nagios/var/graphios/host-perfdata.$TIMET$
}
define command {
command_name graphios_perf_service
command_line /bin/mv /opt/nagios/var/graphios/service-perfdata /opt/nagios/var/graphios/service-perfdata.$TIMET$
}
# 将nagios所有配置文件中的/var/spool/nagios/graphios更改为/opt/nagios/var/graphiossed -i 's;/var/spool/nagios/graphios;/opt/nagios/var/graphios;' /opt/nagios/etc/*sed -i 's;/var/spool/nagios/graphios;/opt/nagios/var/graphios;' /opt/nagios/etc/objects/*
在需要出图的主机或者命令定义中增加_graphiteprefix变量
例如:
define host {
name myhost
check_command check_host_alive
_graphiteprefix monitoring.nagios01.pingto
}
在graphite中就会表现为/monitoring/nagios01/pingto/myhost/{rta,rtmin,rtmax,pl}
define service {
service_description MySQL threads connected
host_name myhost
check_command check_mysql_health_threshold!threads-connected!3306!1600!1800
_graphiteprefix monitoring.nagios01.mysql
}
在graphite中就会表现为/monitoring/nagios01/mysql/myhost/threads_connected的形式
也可以定义_graphitepostfix变量
例如:
define service {
service_description Load
host_name myhost
_graphiteprefix datacenter01.webservers
_graphitepostfix nrdp.load
}
在graphite中就会呈现为
/datacenter01/webservers/myhost/nrdp/load/{load1 8.41 nagios_timet, load5 6.06 nagios_timet, load15 5.58 nagios_timet}
如果觉得自定义变量很麻烦,可以使用graphios的自动命名功能。当然也是有前提的,就是服务描述需要一致,不过这样会对所有监控信息都进行处理,你可能需要更大些的磁盘?
# 开启很简单,只需要在graphios.cfg中更改use_service_desc = False为use_service_desc = True。# 如果没有请手动添加sed -i '/^use_service_desc/s/False/True/' /opt/nagios/graphios.cfg
图片引用自InfoQ
sudo systemctl restart nagios
# 测试命令sudo graphios.py --spool-directory /opt/nagios/var/graphios --log-file /tmp/graphios.log --backend carbon --server 127.0.0.1:7002 --verbose --test# 启动sudo systemctl enable graphiossudo systemctl start graphios
# 我的在启动的时候启动脚本出现问题# 将/etc/init.d/graphios中 /lib/lsb/functions 修改为 /etc/init.d/functions# 并添加如下内容log_daemon_msg () {# Dummy function to be replaced by LSB library.echo $@}log_end_msg () {# Dummy function to be replaced by LSB library.if test "$1" != "0"; thenecho "Error with $DESCRIPTION: $NAME"fireturn $1}
稍等几分钟,你就可以去graphite中查看,数据已经获得,并可以将这些数据展现在graphite的dashboard上
graphite获取数据之后的效果:
图片引用自InfoQ
这个很简单,具体按照请参阅官方文档
# 我的配置文件内容,根据需要更改,没有必改项cat /etc/grafana/grafana.ini
[paths]data = /opt/grafana/datalogs = /opt/grafana/log[server][database]type = sqlite3path = grafana.db[session][analytics][security]admin_user = adminadmin_password = admin[users]allow_sign_up = trueallow_org_create = trueauto_assign_org = trueauto_assign_org_role = Viewer[auth.anonymous][auth.github][auth.google][log][log.console][log.file]log_rotate = truedaily_rotate = true[event_publisher]
启动grafana
sudo systemctl enable grafana-serversudo systemctl start grafana-server
grafana使用的是3000端口
现在可以去浏览器中测试啦~~ http://$IP:3000
以下图片引用自grafana官网,包含grafana与graphite整合的一些其它信息。
关于grafana定制的详细信息请参阅官方文档
1. 点击左上角grafana的图标
2. 点击弹出的边栏菜单中的Data Sources
3. 点击上方导航栏中的Add new,就会出现数据源添加页面
4. Name栏自己定义一个名字;Type栏选graphite;Url写graphite的地址,如果不是80端口则需要带端口,如http://127.0.0.1:7000;Access栏选proxy
5. 点击Add添加
6. 回到主页,点击左上角grafana图标右边的New dashboard
7. 左上角grafana图标下方有个绿色小条,鼠标移上去会弹出,点击
8. 选择Add Panel -> Graph
9. 在新出现的图表的标题部分no title (click here)点击,选择edit
10. 可以使用他定义的一些函数来处理从graphite获得的数据:
11. 自定义的函数有些可选参数:
