[关闭]
@sww4718168 2015-06-14T06:47:29.000000Z 字数 10322 阅读 2630

nagios的报警功能虽强,但是缺失绘图,cactai绘制的图可定制性却太差,所以传统nagios+cactai的组合被我放弃。从网上看到grafana的图表非常漂亮,而且可以通过graphitenagios结合,所以决定使用nagios作为后端实现报警及数据支持并使用grafana进行图表展现。

各个组件的用途:

  • grafanagraphite获取数据,并呈现(可以直接使用graphite的图)
  • graphite对数据进行处理并存储(他本身的绘图功能也很强大)
  • graphiosnagios获取的数据推送给graphite
  • nagios获取数据及报警

安装nagios

不喜欢apache的笨重,所以使用nginx来server nagios
具体安装过程可以参考官方文档,这里只写nginx相关部分。

安装nginx、php及相关依赖

直接上命令:

  1. sudo yum install epel-release.noarch
  2. sudo yum install nginx php-fpm gcc glibc glibc-common gd gd-devel

nagios的安装路径为/opt/nagios/

配置nginx

  1. # 此配置文件还需要优化,准备用uwsgi来替换其他cgi解析程序。
  2. cat > /etc/nginx/conf.d/nagios.conf <<EOF
  3. server {
  4. listen 8080;
  5. server_name monitor.flexops.org;
  6. # root /var/www/html;
  7. charset utf-8; # 我这里出现了乱码,所以加上了这行
  8. index index.php index.html index.htm;
  9. # 增加认证,如果没有认证,nagios的监控条目是看不到的
  10. auth_basic "Nagios Access";
  11. auth_basic_user_file /opt/nagios/etc/htpasswd; # 由htpasswd(apache的工具)生成,可以在安装有apache的机器上生成,然后copy过来
  12. location / {
  13. root /opt/nagios/share/; # 静态文件目录
  14. }
  15. # 分离php,由php-fpm解析php文件。
  16. location ~ \.php$ {
  17. root /opt/nagios/share/;
  18. include fastcgi_params;
  19. # fastcgi_params里面没有下面这条magic parameter,加上才能让fcgi找到具体php文件。
  20. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  21. fastcgi_pass unix:/var/run/php-fpm.sock; # socket比ip:port方式效率要高
  22. }
  23. # 分离nagios调用的perl cgi。nginx默认不支持直接处理cgi文件,可以使用fcgiwrap。因CentOS7的源里没有该包,所以这里使用其他处理方式,127.0.0.1:8999就是其端口。
  24. location ~ ^/nagios/cgi-bin/(.*)\.cgi$ {
  25. #auth_basic "Nagios Access";
  26. #auth_basic_user_file /opt/nagios/etc/htpasswd;
  27. root /opt/nagios/sbin/;
  28. rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
  29. # 将认证信息传递给cgi
  30. fastcgi_param AUTH_USER $remote_user;
  31. fastcgi_param REMOTE_USER $remote_user;
  32. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  33. include fastcgi_params;
  34. fastcgi_pass 127.0.0.1:8999;
  35. }
  36. # 开始未配置下面部分,nagios主页获取不到nagios daemon的信息,通过开发者工具发现调用地址不一样。所以加入下面的location
  37. location ~ ^/cgi-bin/(.*)\.cgi$ {
  38. #auth_basic "Nagios Access";
  39. #auth_basic_user_file /opt/nagios/etc/htpasswd;
  40. root /opt/nagios/sbin/;
  41. rewrite ^/cgi-bin/(.*)\.cgi /$1.cgi break;
  42. fastcgi_param AUTH_USER $remote_user;
  43. fastcgi_param REMOTE_USER $remote_user;
  44. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  45. include fastcgi_params;
  46. fastcgi_pass 127.0.0.1:8999;
  47. }
  48. }
  49. EOF
  50. # 配置cgi的解析程序
  51. sudo wget flexops.org/download/nginx/perl/fastcgi-wrapper.pl -O /usr/bin/fastcgi-wrapper
  52. # fastcgi-wrapper的开机启动脚本
  53. sudo wget flexops.org/download/nginx/perl/perl-fcgi.init-scripts -O /etc/init.d/perl-fcgi
  54. sudo chkconfig --add perl-fcgi
  55. sudo systemctl enable perl-fcgi
  56. sudo systemctl start perl-fcgi
  57. # 启动nginx
  58. sudo systemctl enable nginx
  59. sudo systemctl start nginx
  60. # 启动nagios
  61. sudo systemctl enable nagios
  62. sudo systemctl start nagios

现在可以打开浏览器查看下nagios是否已经正常运行了
http://127.0.0.1:8080/

这里不讨论关于nagios监控其他机器的配置方法。

安装graphite

关于graphite的更多信息可以参阅官方文档
InfoQ有篇关于graphite的详细介绍,中文的,对各个组件介绍很详细。还有更详细的配置和测试方法。点这里查看
继续上命令:

  1. sudo yum install python-pip python-devel
  2. # pip 命令由 python-pip 提供,python部分扩展是由C写的。
  3. # 使用pip安装扩展是从pypi下载源码包进行然后编译安装,所以确保python-devel和gcc等工具和库齐全
  4. # 使用pip的原因是便于管理,且不受CentOS源的影响。
  5. sudo pip install https://github.com/graphite-project/ceres/tarball/master
  6. sudo pip install whisper
  7. sudo pip install carbon # carbon 依赖python-devel和gcc
  8. sudo pip install graphite-web
  9. # 默认安装到/opt/graphite下,可以使用-t选项指定其他目录
  10. # 例: sudo pip install -t /usr/local/graphite graphite-web
  11. # 关于更多定制化选项请参考grahite[官方文档](https://graphite.readthedocs.org/en/latest/install-pip.html)
  12. # graphite是由django框架写的,安装django
  13. # sudo pip install django
  14. # graphite依赖cairocffi库来进行绘图
  15. sudo yum install python-cairocffi # 因其依赖一些二进制库所以使用yum安装

更改graphite的配置

  1. sed -i "/#TIME_ZONE/a TIME_ZONE = 'Asia/Shanghai'" /opt/graphite/webapp/graphite/local_settings.py

使用nginx来server graphite

  1. # 亦可使用pip安装uwsgi,不过如果想通过systemd管理其启动,会比较麻烦
  2. # 关于uwsgi的更多信息可以参阅uwsgi[官方文档](http://uwsgi-docs.readthedocs.org/en/latest/)
  3. sudo yum install uwsgi-plugin-python.x86_64 uwsgi.x86_64
  4. sudo cp /opt/graphite/conf/example/graphite.wsgi.example /opt/graphite/conf/graphite.wsgi
  5. cat > /etc/uwsgi.ini <<EOF"
  6. [uwsgi]
  7. #uid = uwsgi
  8. #gid = uwsgi
  9. pidfile = /run/uwsgi/uwsgi.pid
  10. #emperor = /etc/uwsgi.d
  11. #stats = /run/uwsgi/stats.sock
  12. #emperor-tyrant = true
  13. #cap = setgid,setuid
  14. log-date=true
  15. chmod-socket = 660
  16. processes = 2
  17. uid = nginx
  18. gid = nginx
  19. # socket = /run/uwsgi/graphite.sock
  20. http-socket = :7000
  21. # 如果通过pip安装uwsgi,下面两行需要删除
  22. plugins-dir = /usr/lib64/uwsgi
  23. plugins = python
  24. #chdir = /opt/graphite/conf
  25. #module = wsgi:application
  26. wsgi-file = /opt/graphite/conf/graphite.wsgi
  27. master = true
  28. logto=/var/log/uwsgi_graphite.log
  29. EOF"
  30. sudo systemctl enable uwsgi
  31. sudo systemctl start uwsgi
  32. # nginx 的配置文件尚有问题,不能正常转发,现在可以直接通过127.0.0.1:7000来访问graphite
  33. # uwsgi的坑比较多,不过功能非常强大。
  34. # 下面的nginx配置部分暂时可以忽略,待更新
  35. ## TODO: 使用nginx来增加并发能力
  36. cat > /etc/nginx/conf.d/graphite.conf <<EOF"
  37. server {
  38. listen 8081;
  39. server_name monitor.tedu.cn;
  40. root /opt/graphite/webapp;
  41. index index.html index.htm;
  42. access_log /var/log/nginx/graphite.access.log;
  43. error_log /var/log/nginx/graphite.error.log;
  44. location / {
  45. add_header 'Access-Control-Allow-Origin' '*';
  46. add_header 'Access-Control-Allow-Credentials' 'true';
  47. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  48. add_header 'Access-Control-Allow-Headers' "origin, authorization, accept";
  49. include uwsgi_params;
  50. uwsgi_pass 127.0.0.1:7000;
  51. }
  52. location /content/ {
  53. alias /opt/graphite/webapp/content;
  54. }
  55. }
  56. EOF"
  57. sudo cp /opt/graphite/conf/example/storage-aggregation.conf.example /opt/graphite/conf/storage-aggregation.conf
  58. sudo cp /opt/graphite/conf/example/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf
  59. sudo cp /opt/graphite/conf/example/carbon.conf.example /opt/graphite/conf/carbon.conf
  60. # 这里我使用了默认配置,没有进行改动。其监听端口、更新时间间隔等在此文件,可以根据需要进行更改。
  61. # 启动carbon
  62. sudo /opt/graphite/bin/carbon-cache.py start
  63. ss -tulanp | grep :2003 # 检查是否成功启动

graphite本身的图表绘制功能也是很强大的,而且创建好的dashboard可以直接导入grafana。
有兴趣的同学可以自行探索,这里不做介绍
具体可以参阅官方文档或者InfoQ的文章

安装graphios

使用pip安装

graphios的源码托管于github上,有兴趣的可以去看看。这里也有不甚详细配置介绍 ^_^ .

本插件亦使用从pip安装的方式。使用pip或者yum省去了很多,比如更改nagios的配置文件,比如增加自启动脚本

  1. sudo pip install graphios

配置graphios

  1. # 更改为nagios获取的数据存放的位置,nagios的配置文件里也要进行相应更改
  2. # 使用pip安装的可以忽略以下配置文件修改的操作,这些在安装时会自己检测并修改
  3. sed -i '/^spool_directory/c spool_directory = /opt/nagios/var/graphios/' /etc/graphios/graphios.cfg
  4. sed -i '/^log_file/c log_file = /opt/nagios/var/graphios.log' /etc/graphios/graphios.cfg # 更改日志的存放位置
  1. # 如果没有更改carbon的端口,这步可以忽略
  2. 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

  1. # 将nagios所有配置文件中的/var/spool/nagios/graphios更改为/opt/nagios/var/graphios
  2. sed -i 's;/var/spool/nagios/graphios;/opt/nagios/var/graphios;' /opt/nagios/etc/*
  3. 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的自动命名功能。当然也是有前提的,就是服务描述需要一致,不过这样会对所有监控信息都进行处理,你可能需要更大些的磁盘?

  1. # 开启很简单,只需要在graphios.cfg中更改use_service_desc = False为use_service_desc = True。
  2. # 如果没有请手动添加
  3. sed -i '/^use_service_desc/s/False/True/' /opt/nagios/graphios.cfg

grphite中的效果呈现
图片引用自InfoQ

重启nagios

  1. sudo systemctl restart nagios

启动grphios

  1. # 测试命令
  2. sudo graphios.py --spool-directory /opt/nagios/var/graphios --log-file /tmp/graphios.log --backend carbon --server 127.0.0.1:7002 --verbose --test
  3. # 启动
  4. sudo systemctl enable graphios
  5. sudo systemctl start graphios
  1. # 我的在启动的时候启动脚本出现问题
  2. # 将/etc/init.d/graphios中 /lib/lsb/functions 修改为 /etc/init.d/functions
  3. # 并添加如下内容
  4. log_daemon_msg () {
  5. # Dummy function to be replaced by LSB library.
  6. echo $@
  7. }
  8. log_end_msg () {
  9. # Dummy function to be replaced by LSB library.
  10. if test "$1" != "0"; then
  11. echo "Error with $DESCRIPTION: $NAME"
  12. fi
  13. return $1
  14. }

稍等几分钟,你就可以去graphite中查看,数据已经获得,并可以将这些数据展现在graphitedashboard
graphite获取数据之后的效果:
graphite获取数据之后的效果
图片引用自InfoQ

安装grafana

这个很简单,具体按照请参阅官方文档

  1. # 我的配置文件内容,根据需要更改,没有必改项
  2. cat /etc/grafana/grafana.ini
  1. [paths]
  2. data = /opt/grafana/data
  3. logs = /opt/grafana/log
  4. [server]
  5. [database]
  6. type = sqlite3
  7. path = grafana.db
  8. [session]
  9. [analytics]
  10. [security]
  11. admin_user = admin
  12. admin_password = admin
  13. [users]
  14. allow_sign_up = true
  15. allow_org_create = true
  16. auto_assign_org = true
  17. auto_assign_org_role = Viewer
  18. [auth.anonymous]
  19. [auth.github]
  20. [auth.google]
  21. [log]
  22. [log.console]
  23. [log.file]
  24. log_rotate = true
  25. daily_rotate = true
  26. [event_publisher]

启动grafana

  1. sudo systemctl enable grafana-server
  2. sudo systemctl start grafana-server

grafana使用的是3000端口
现在可以去浏览器中测试啦~~ http://$IP:3000

grafanagraphite的整合

以下图片引用自grafana官网,包含grafanagraphite整合的一些其它信息。
关于grafana定制的详细信息请参阅官方文档

增加Data Sources
1. 点击左上角grafana的图标
2. 点击弹出的边栏菜单中的Data Sources
3. 点击上方导航栏中的Add new,就会出现数据源添加页面
4. Name栏自己定义一个名字;Type栏选graphite;Urlgraphite的地址,如果不是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
增加dashboard
10. 可以使用他定义的一些函数来处理从graphite获得的数据:
使用函数
11. 自定义的函数有些可选参数:
可选参数

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注