Index ¦ Archives ¦ Categories ¦ Tags

安装和配置 graphite, grafana和collectD (基于nginx, uwsgi)

配置环境: Ubuntu 12.04.4 LTS on AWS EC2, python2.7.3


安装和配置 graphite

下载源码包

git clone https://github.com/graphite-project/graphite-web.git # graphite-web
git clone https://github.com/graphite-project/carbon.git # carbon
git clone https://github.com/graphite-project/whisper.git # whisper
git clone https://github.com/graphite-project/ceres.git # ceres
# 分别进入到每个文件夹进行安装
python setup.py install
# 默认会装到/opt/graphite/下

可以通过--prefix, --install-lib, --install-data, --install-scripts指定安装地址, 例如:

python setup.py install --prefix=/srv/graphite --install-lib=/srv/graphite/lib

安装依赖包

graphtie的依赖包很多,基本要求的有:

  • Python 2.6 or greater
  • Pycairo
  • Django 1.4 or greater
  • django-tagging 0.3.1 or greater
  • Twisted 8.0 or greater (10.0+ recommended)
  • zope-interface (often included in Twisted package dependency)
  • fontconfig and at least one font package (a system package usually)
  • nginx with uwsgi or apache

我们可以通过graphite-web源码包下的check-dependencies.py来检查需要安装的依赖包:

python /path/to/graphite-web/check-dependencies.py
# required的是必须安装的, optional可以通过自己的需要安装

注意: 安装cairo需要一下步骤:

sudo apt-get install libffi-dev
sudo apt-get install libcairo2-dev
sudo pip install cairocffi

安装后台数据库

graphite因为是基于django的,其默认的后台数据库是sqlite,我这里后台用MySQL,安装及配置:

sudo apt-get install libmysqlclient-dev
sudo apt-get install mysql-server
sudo pip install mysql-python

注意: 如果需要配置MySQL的字符集为utf8, 则需要修改my.conf的配置, 参考这里

配置graphite

创建MySQL用户, 我这里用root. 配置local_settings.py

cp /opt/graphite/webapp/graphite/local_setings.py.example /opt/graphite/webapp/graphite/local_settings.py
vim /opt/graphite/webapp/graphite/local_settings.py

修改配置

# 去掉注释并修改
# 在MySQL创建一个名为graphite的database
DATABASES = {
    'default': {
            'NAME': 'graphite', # 数据库名
            'ENGINE': 'django.db.backends.mysql',
            'USER': 'root',
            'PASSWORD': '密码',
            'HOST': '127.0.0.1',
            'PORT': '3306'
        }
    }
    ......
# 解注释最后一行
from graphite.app_settings import *

安装django-cores-headers

如果grafana和graphite运行在不同端口, grafana访问graphtie因为同源安全策略被拒绝, 我们需要设置CORS(同源资源共享) 安装django-coreos-headers

sudo pip install django-cores-headers

修改/opt/graphite/webapp/graphite/app_settings.py

MIDDLEWARE_CLASSES = (
        ......
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        ......
)
INSTALLED_APPS = (
        ......
        'corsheaders',
        ......
)

运行syncdb

需要将manage.py拷到/opt/graphite/webapp/下

cp /usr/local/lib/python2.7/dist-packages/django/conf/project_template/manage.py /opt/graphite/webapp/

运行syncdb

cd /opt/graphite/webapp/
python manage.py syncdb

安装和配置nginx

安装nginx

sudo apt-get install nginx

配置nginx

这里的配置是给graphite原生web界面单开一个端口, 如果只用grafana做前端则不需要这些配置

# 创建graphite配置
sudo vim /etc/nginx/sites-enabled/graphite

我的配置文件如下:

server {
        listen        8000;
        server_name   your.server.name;
        charset       utf-8;
        access_log    /var/log/nginx/graphite.access.log;
        error_log     /var/log/nginx/graphite.error.log;


        location / {
            add_header Access-Control-Allow-Origin $http_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'origin, authorization, accept';
            uwsgi_pass 127.0.0.1:3031;
            include uwsgi_params;
        }
        location /static {
            alias /opt/graphite/webapp/content;
        }
        location /content {
            alias /opt/graphite/webapp/content;
            gzip off;
            include uwsgi_params;
            uwsgi_pass      127.0.0.1:3031;
        }
}

载入新配置并运行nginx

sudo nginx -t #检查配置
sudo /etc/ini.d/nginx reload

安装uwsgi

sudo apt-get install uwsgi
sudo pip install uwsgi

配置uwsgi

# 在/opt/graphite/webapp/graphite.ini
sudo vim /opt/graphite/webapp/graphite.ini

我的配置如下:

[uwsgi]
processes = 2
socket = 127.0.0.1:3031
gid = www-data
uid = www-data
chdir = /opt/graphite/webapp/graphite
module = wsgi
buffer-size = 65536
pidfile = /tmp/uwsgi.pid
chmod-socket = 666
pythonpath = /opt/graphite/webapp
daemonize = /var/log/graphite.log

运行uwsgi

uwsgi --ini /opt/graphite/webapp/graphite.ini

uwsgi在后台运行, 通过访问nignx配置的server_name:8000就可以看到graphite界面, 现在graphite并没有数据.


安装和配置 grafana

下载grafana源码包

git clone https://github.com/grafana/grafana.git

安装nodejs和npm

sudo apt-get install python-software-properties
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

检查一下nodejs和npm

nodejs -v # 版本要v0.10 +
npm -v    # 检查npm是否被安装

如果npm没有被安装

sudo apt-get install npm

安装grafana

npm install # 在grafana文件夹里
npm install -g grunt-cli
grunt (runt default task that will generate css files)
grunt build (creates optimized & minified release)

配置grafana

拷贝grafana配置

cp /path/to/grafana/src/config.example.js /path/to/grafana/src/config.js

配置config.js

//去掉注释并修改为
    datasources: {
      graphite: {
        type: 'graphite',
        url: "http://server_name:8000",
      },
      elasticsearch: {
        type: 'elasticsearch',
        url: "http://server_name:9200",
        index: 'grafana-dash',
        grafanaDB: true,
      }
    },

安装elasticsearch

如果没有java, 安装java

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

加入elasticsearch源, 并安装

wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | apt-key add -
sudo echo "deb http://packages.elasticsearch.org/elasticsearch/1.1/debian stable main" >> /etc/apt/sources.list
sudo apt-get update
sudo apt-get install elasticsearch

启动elasticsearch服务, 并测试

sudo /etc/init.d/elasticsearch start
curl -X GET 'http://localhost:9200'
    {
        "status" : 200,
        "name" : "Carl \"Crusher\" Creel",
        "version" : {
        "number" : "1.3.2",
        "build_hash" : "dee175dbe2f254f3f26992f5d7591939aaefd12f",
        "build_timestamp" : "2014-08-13T14:29:30Z",
        "build_snapshot" : false,
        "lucene_version" : "4.9"
        },
        "tagline" : "You Know, for Search"
    }

配置ningx

创建grafana配置

sudo vim /etc/nginx/sites-enabled/grafana

我的配置如下 参考了dongwm这篇博客的配置

    server {
        listen      *:80 ;

        server_name     your.server.name;
        access_log      /var/log/nginx/grafana.org.access.log;

        location / {
            add_header 'Access-Control-Allow-Origin' "$http_origin";
            add_header 'Access-Control-Allow-Credentials' 'true';
            root  /home/tian.li/grafana/src;
            index  index.html  index.htm;
        }
        location ~ ^/_aliases$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
        }
        location ~ ^/_nodes$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
        }
        location ~ ^/.*/_search$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
        }
        location ~ ^/.*/_mapping$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
        }
        # Password protected end points
        location ~ ^/kibana-int/dashboard/.*$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
            limit_except GET {
                proxy_pass http://127.0.0.1:9200;
                auth_basic "Restricted";
                #添加你自己的basic auth
                auth_basic_user_file /etc/nginx/conf.d/grafana.htpasswd;
        }
        location ~ ^/kibana-int/temp.*$ {
            proxy_pass http://127.0.0.1:9200;
            proxy_read_timeout 90;
            limit_except GET {
                proxy_pass http://127.0.0.1:9200;
                auth_basic "Restricted";
                auth_basic_user_file /etc/nginx/conf.d/grafana.htpasswd;
            }
        }
    }

注意 这里用到了Basic Authentication, 关于NginX的Basic Authentication可以参考这里. 当然你也可以添加SSL认证.

检查配置和reload Nginx

sudo nginx -t
sudo /etc/ini.d/nginx reload

安装和配置CollectD

下载源码包

git clone https://github.com/collectd/collectd.git

编译和安装

cd /path/to/collectd
./configure
sudo make
sudo make install

修改配置(默认会在/opt/collectd/etc/collectd.conf)

    # 添加
    Include     "/opt/collectd/etc/collectd.d"

创建配置文件

sudo mkdir mkdir /opt/collectd/etc/collectd.d
sudo vim /opt/collectd/etc/collectd.d/graphite.conf

加入以下配置, 修改你的graphite server

<LoadPlugin "python">
        Globals true
</LoadPlugin>
<Plugin "python">
        ModulePath "/opt/collectd-plugins/"
        Import "carbon_writer"
        <Module "carbon_writer">
            LineReceiverHost "your.graphite.server.name"
            LineReceiverPort 2003
            DifferentiateCountersOverTime true
            LowercaseMetricNames true
            TypesDB "/opt/collectd/share/collectd/types.db"
        </Module>
</Plugin>

安装collectd-carbon插件

sudo git clone https://github.com/indygreg/collectd-carbon.git /opt/collectd-plugins

默认会开启cpu, load, memory, interface的监控. 如果开启更多监控plugins, 参考官方wiki

运行各组件

在graphite server

sudo /opt/graphite/bin/carbon-cache

在监控的server

sudo /opt/collectd/sbin/collectd

至此就基本配置就完成了, 在grafana上可以添加dashboard, graph, row来得到监控数据的可视化图.

© Tian Li. Built using Pelican. Theme by Giulio Fidente on github. .