Remember to place the policy before the catch-all block in the configuration file.
Installing and Configuring Graphite on Ubuntu 20.04
- Graphite
- Ubuntu
- -
- kubernetes
- -
- instances
- -
- elastic-metal
In this tutorial, you will find a comprehensive guide to understanding and using Graphite, a widely-used monitoring tool. Graphite serves two primary functions:
- Efficient Time-Series Data Storage: Graphite excels in storing time-related data efficiently, ensuring minimal resource consumption while retaining data integrity.
- Flexible Visualization and Data Manipulation: It provides a robust interface for visualizing stored data and offers the flexibility to perform mathematical operations such as summation, grouping, and scaling in real time.
Throughout this tutorial, we will dive into the technical aspects of Graphite, allowing you to discover its capabilities effectively for your monitoring needs.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- An Instance running Ubuntu 20.04 LTS
- A valid API key
sudo
privileges or access to the root user
Installing Graphite
- Graphite and the required tools can be installed easily via apt. Update the apt cache and upgrade the packages already installed on the system:
apt-get update && apt-get upgrade -y
- Install Graphite:
apt-get install graphite-web graphite-carbon
Configuring the Graphite web application
- Open the configuration file in which you have to make the modifications explained afterward:
nano /etc/graphite/local_settings.py
- Set a
SECRET_KEY
that will be used as a salt when creating hashes. Uncomment the line and set it to secure key:SECRET_KEY = 'a_salty_string' - Uncomment the
TIME_ZONE
parameter and configure it to your local time-zone:TIME_ZONE = 'Europe/Paris' - Save the file and quit the editor.
- Synchronize the database to create the required database layout:
graphite-manage migrate
Configuring Carbon
As the database is ready now, continue with the configuration of Carbon, the storage backend of Graphite.
- Open the service configuration file with a text editor:
nano /etc/default/graphite-carbon
- There is only one configuration parameter in the file, to make sure Carbon starts at boot, change the value to
true
.CARBON_CACHE_ENABLED=true - Save the file and quit the Editor once you have changed the value.
- Open the Carbon configuration file:
nano /etc/carbon/carbon.conf
- Enable log rotation by setting the following value to true:
ENABLE_LOGROTATION = True
- Save the file and quit the Editor once you have changed the value.
Configuring Storage Schemas
-
Open the storage schemas file. It contains information about how long and how detailed values should be stored:
nano /etc/carbon/storage-schemas.conf -
The content of the file will look like this example:
[carbon]pattern = ^carbon\.retentions = 60:90d[default_1min_for_1day]pattern = .*retentions = 60s:1dBy default two sections are defined in the file:
- The first one is to decide what to do with data coming from Carbon itself, as it is configured by default to store some of its own performance metrics.
- The second one is a catch-all section that applies to any data that hasn’t been matched by any other section. It has to remain always the last section of the file.
Each section is defined by section headers, namely the words in the brackets. Below each section header, a pattern definition and retention policy are defined.
The pattern definition is a regular expression, used to match any information sent to Carbon. All information sent to Carbon includes a metric name, which is checked by the pattern definition. In the first section, all metrics in question start with the string “carbon.”.
The retention policy is defined by sets of numbers, consisting of a metric interval (defining how often a metric is recorded), followed by a colon and the storage duration of these values. It is possible to define multiple sets of retention policies, separated by commas.
-
Define a new retention policy that is triggered by a “test.” pattern and will be used later:
[test]pattern = ^test\.retentions = 10s:10m,1m:1h,10m:1dNoteThis section will store the data it collects three times with different levels of detail. The first collection
10s:10m
will create a data point every ten seconds and data is stored for only 10 minutes.The second collection
1m:1h
will create a data point every minute by gathering all the data from the past minute from the first collection. The information in the data point is aggregated by averaging the points (six points, as the first collection creates a point every 10 seconds). Data will be stored in this collection within one hour.The last collection
10m:1d
will make a data point every 10 minutes by aggregating the information gathered from the second collection in the same way. Data will be stored in this collection for one day.Graphite returns the data from the most detailed collection that measures the requested time frame when asking for information. This means:
- If metrics for the past 5 minutes will be requested, Graphite will return information from the first collection.
- If metrics for the past 50 minutes will be requested, Graphite will return information from the second collection.
-
Save and close the file when you have finished editing it.
Defining storage aggregation methods
To gather accurate metrics, it is essential to understand the way Carbon decides when it crunches detailed information into a generalized number. This happens whenever Graphite converts more detailed metrics into less detailed ones, like in the second and third collections in the test schema above.
The default behavior is to get the average value when aggregating. This means unlike the most detailed information in the first collection, information is less accurate in the second and third collections.
This may not always be useful though. For example, to count up the total number of times an event occurred over different periods of time it is not useful to average them but to count each event.
It is possible to define the way Graphite aggregates metrics with a file called storage-aggregation.conf
.
-
Copy the file from the examples directory to the actual configuration directory:
cp /usr/share/doc/graphite-carbon/examples/storage-aggregation.conf.example /etc/carbon/storage-aggregation.conf -
Open the file in a text editor:
nano /etc/carbon/storage-aggregation.conf -
You will see a file similar to this content:
[min]pattern = \.min$xFilesFactor = 0.1aggregationMethod = minIt looks similar to the previous file, and the section name and pattern are exactly the same as in the storage-schemas file.
The
xFilesFactor
is what we will take a closer look at. It allows specifying the minimum percentage of values that Carbon must have to create an aggregated data-point. For example, if the value is set to 0.5, it requires that 50% of the more detailed data points be available to create an aggregated point.This can be useful to avoid creating data-points misrepresenting the actual situation.
aggregationMethod
defines the way data is recorded. Possible values areaverage
,sum
,last
,max
, andmin
. It is important to choose the correct value to avoid that your data will be recorded inaccurately. The correct selection depends on the kind of metrics that you are tracking. -
Once you have edited the file to your needs, save and close the file.
-
Start Carbon by typing:
service carbon-cache start
Installing and configuring Apache
To use the web interface of Graphite, a web server is required. The software comes with a pre-defined configuration for Apache, making configuration pretty easy.
- Install the Apache web server and the required module:
apt-get install apache2 libapache2-mod-wsgi-py3
- Disable the default site of Apache, as it won’t be needed:
a2dissite 000-default
- Copy the Apache configuration file of Graphite into the available sites directory of Apache:
cp /usr/share/graphite-web/apache2-graphite.conf /etc/apache2/sites-available
- Enable the Graphite site:
a2ensite apache2-graphite
- Reload the configuration of Apache:
service apache2 reload
- Check if the web interface is working by pointing your browser to
http://YOUR_SERVERS_IP
. The following interface will be visible:
Discovering the web interface
- Login by clicking on the Login-Button on the top of the page. Enter the username and password that you have set when you have synchronized the database. In case you do not remember these credentials or if you want to add another superuser, run the following command to create a new user:
graphite-manage createsuperuser
- You will notice a menu on the left of the Screen. Click carbon to see the metrics that are collected by Graphite. Currently, you will only see the data the application is gathering about its performance:
- It is also possible to create dashboards from the data Graphite collects to have an overview of the different metrics:
Feeding data to graphite
Graphite is very flexible when it comes to the origin of data. There are three main methods for sending data to Graphite: Plaintext, Pickle, and AMQP.
-
Previously a
test
block has been created in the storage-schemas file. It will be used now to send some data to Graphite from a terminal. -
Type the following command in a terminal. You can replace the value
42
with some different numbers to see what it does:echo "test.count 42 `date +%s`" | nc -q0 localhost 2003Metric messages need to contain a metric name, a value, and a timestamp.
The example feeds the storage schema
test
with the metriccount
and the value42
. To get the timestamp, thedate
command is used. -
Go back to the Graphite Web Interface and reload it. The new storage scheme test will appear in the menu.
-
Set the time range to a few minutes, by clicking on the clock-icon:
-
The values that you have sent will appear in the graph:
-
Wait for 15 minutes and refresh the graph by setting the time frame to the past 15 minutes. The graph will look different:
This is because our first collection does not store data for 15 minutes and Graphite will look into the second collection for rendering the graph.
As the data was sent with a count metric, Graphite adds up the values in the larger intervals instead of averaging them. It is therefore essential to choose the right metric for each use case.
- Pushing Content from a terminal is not the usual way to send data to Graphite. Instead, you will use a tool to automatize the collection of data.
- A complete list of tools that work with Graphite is available in the official documentation.