Graphite是一款企业级的监控工具,其采用Django框架,可以运行在廉价的硬件资源之上。Graphite采用简单的文本协议和绘图功能可以方便地使用在任何操作系统上。本文将以Ubuntu 14.04为系统环境,就Graphite与Grafana的安装与协作做详细介绍。
Before You Begin / 热身准备
1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
本文原文来自Linode官网,所以会看到Linode字样,不用理会,把它当成你的主机的就可以了
2. This guide will use sudo wherever possible from an example account named graphite. Complete the sections of our Securing Your Server guide to create the graphite user, harden SSH access, remove unnecessary network services and set up a firewall. You may need to create additional firewall rules for your specific application.
本文默认使用普通用户进行安装部署
3. Update your system:
开始前,系统需要做一下更新(非必须)
1 | sudo apt-get update && sudo apt-get upgrade |
Install Apache, Python Tools and Graphite
Install the system packages required for working with Graphite:
安装Graphite本身及其依赖的一些工具
1 | sudo apt-get install build-essential graphite-web graphite-carbon python-dev apache2 libapache2-mod-wsgi libpq-dev python-psycopg2 |
During the installation of graphite-carbon
, you will be asked if you want to remove the whisper database files should you ever uninstall Graphite. Answer No to this prompt. You can always remove the files later (which are located in /var/lib/graphite/whisper
).
安装graphite-carbon
的过程中,会提示你是要删除whisper数据库文件,回答No就好,我们可以后面随时手动删除这些文件;如果是新主机,回答Yes也没有问题。
Configure Carbon
1. Configure the retention rate for test metrics by adding a [test]
block to Carbon’s storage-schemas.conf
file. This step is given for testing purposes only and can be safely skipped if you have no use to generate test metrics.
The retention times given below will save data every 5 seconds for 3 hours, and a separate set of data from that aggregated sample every 1 minute for 1 day.
这个文件设置了Graphie接收数据的频率和保存时间。我们向其中添加[test]
字段用于测试。
File excerpt: /etc/carbon/storage-schemas.conf
1 | [carbon] |
For more information on how to configure Carbon storage, see the section torage-schemas.conf in Graphite’s documentation.
2. Copy the default aggregation configuration to /etc/carbon
so we can configure our own settings:
产生文件storage-aggregation.conf
:
1 | sudo cp /usr/share/doc/graphite-carbon/examples/storage-aggregation.conf.example /etc/carbon/storage-aggregation.conf |
storage-aggregation.conf
describes aggregation policies Carbon uses to produce less detailed metrics, such as the 1m:1d retention in the [test] block added above. By default, only the average metric value is taken, which will result in data loss when, for example, you need maximum and minimum values. For this reason, [min]
,[max]
and [sum]
sections are found in the configuration file.
文件storage-aggregation.conf
定义了Carbon聚合数据的策略
**3.**Third, Enable Carbon’s cache to run on boot:
设置Carbon-cahce开机启动
File excerpt: /etc/default/graphite-carbon
1 | CARBON_CACHE_ENABLED=true |
Start the Carbon cache service:
启动:Carbon cache
1 | sudo service carbon-cache start |
Install and Configure PostgreSQL
grephit-web依赖PosetgreSQL数据库
1. Install PostgreSQL for the graphite-web application:
1 | sudo apt-get install postgresql |
2. Change to the postgres
user and create a database user for Graphite:
为Graphite创建数据库用户
1 | su - postgres |
You will be asked to provide a password for the new database user. After that, create the databases graphite
and grafana
with the system’s graphite
user as the owner:
使用刚创建的用户graphite
创建两个数据库:graphite
和grafana
1 | createdb -O graphite graphite |
3. When finished configuring the PostgreSQL databases, change back to the graphite user:
1 | su - graphite |
Configure Graphite
1. Update Graphite’s DATABASES dictionary definition with the settings for the PostgreSQL database created earlier:
配置Graphite使用刚才创建的数据库:
File excerpt: /etc/graphite/local_settings.py
1 | DATABASES = { |
2. Also add the following lines to the end of the file:
在文件尾部添加下面几项配置:
File excerpt: /etc/graphite/local_settings.py
1 | USE_REMOTE_USER_AUTHENTICATION = True |
- TIME_ZONE is your Linode’s time zone, which will be used in graphs. For possible values, run
timedatectl
or see the TZ column in Wikipedia’s timezone database.timedatectl
命令可以用来获取当前主机的时区信息 - SECRET_KEY is a long and unique string used as a salt when hashing passwords.一个用来哈希加密密码的随机字符串,越随机越好。
3. Initialize the database with:
初始化数据库:
1 | sudo graphite-manage syncdb |
4. Then answer the prompts to create a superuser account which will be used to access Graphite’s web interface.
Configure Apache for Graphite
配置Apache用于web访问Graphite
1. Copy Graphite’s Apache config template into Apache’s sites-available directory:
1 | sudo cp /usr/share/graphite-web/apache2-graphite.conf /etc/apache2/sites-available |
2. Change Graphite’s port from 80 to 8080 (port 80 will be used for Grafana later).
使用8080端口来访问Graphite
File excerpt: /etc/apache2/sites-available/apache2-graphite.conf
1 | <VirtualHost *:8080> |
3. Make sure Apache is listening on port 8080. Add Listen 8080
after Listen 80
in ports.conf
:
配置Apache监听8080端口:
File excerpt: /etc/apache2/ports.conf
1 | Listen 80 |
4. Disable the default Apache site to avoid conflicts:
关闭默认页面
1 | sudo a2dissite 000-default |
5. Enable Graphite’s virtual site:
开启Graphite虚拟站点
1 | sudo a2ensite apache2-graphite |
6. Reload Apache to apply the changes:
Reload Apache
1 | sudo service apache2 reload |
Now you should be able to access Graphite by going to your Linode’s hostname or IP address using port 8080 in a web browser (ex: example_domain.com:8080
). You’ll see the Graphite landing page as shown below:
现在可以通过访问所在主机的8080端口来查看Graphite页面了
Create Sample Data
生成测试数据
1. Generate some test metrics with the following command:
1 | for i in 4 6 8 16 2; do echo "test.count $i `date +%s`" | nc -q0 127.0.0.1 2003; sleep 6; done |
2. Wait for the command prompt to be returned. Refresh the page and you should see a new test.count
metric in the tree on the left:
This guide is published under a CC BY-ND 4.0 license.
The pure English Version: https://www.linode.com/docs/uptime/monitoring/deploy-graphite-with-grafana-on-ubuntu-14-04