# Graphite and Grafana – How to calculate Percentage of Total/Percent Distribution

When working with Grafana and Graphite, it is quite common that I need to calculate the percentage of a total from Graphite time series. There are a few variations on this that are solved in different ways.

## SingleStat With the SingleStat panel in Grafana, you need to reduce a time series down to one number. For example, to calculate the available memory percentage for a group of servers we need to sum all available memory for all servers and sum total memory for all servers and then divide the available memory total by the total memory total.

The way to do this in Grafana is to create two queries, #A for the total and #B for the subtotal and then divide #B by #A. Graphite has a function divideSeries that you can use for this. Then hide #A (you can see that is grayed out below) and use #B for the SingleStat value.

The divideSeries function can be used in a Graph panel too, as long as the divisor is a single time series (for example, it will work for the sum of all servers but not when grouped by server). ## Graph Multiple Percentage of Totals Sometimes I want to graph the percentage of total grouped by server/node e.g. disk usage percentage per server. In this case, divideSeries will not work. It cannot take multiple time series and divide them against each other (Prometheus has vector matching but Graphite does not have anything quite as smooth unfortunately). One way to solve this is to use a different graphite function called reduceSeries.

In the example, there are two values, capacity (the total) and usage (the subtotal). First, a groupByNode function is applied, this will return a list with the two values for each server (e.g. minion-2.capacity and minion-2.usage). The mapSeries and reduceSeries take this list and for each server applies the asPercent reduce function to the two values. The result is a list of percentage totals per server.

The reduceSeries function can also apply two other reduce functions: a diff function and a divide function.

### Same result with the AsPercent Function

In the query above, the values (usage and capacity) are in the same namespace if that is not the case then the reduceSeries technique will be difficult or not work. Another function worth checking out is the AsPercent function which might work better in some cases. The example below uses the same two query technique that we used for divideSeries but it works with multiple time series! I learned these three techniques by looking at Grafana dashboards built by some of the Graphite experts that work with me at Raintank/Grafana Labs. I did not know them before so I think they will help others too.

## 2 thoughts on “Graphite and Grafana – How to calculate Percentage of Total/Percent Distribution”

1. Fredrik Persson says:

Thanks for posting this. I am quite new to Grafana but now use it to send monitoring stuff from Nagios (check MK) to Graphite and last Grafana. I have really tried to make my calulation correct but it just wont work. Maybe I am just to stupid. I have the total value and the used value but wont get the percentage used in the graph. Like my post here. https://community.grafana.com/t/disk-usage-as-percentage-of-used-disk/1499

1. Daniel Lee says:

You probably have solved this already but in your case it sounds like you have a total and subtotal so the divideSeries function that I describe at the beginning is probably what want. E.g. if your total is 50 and your used is 30 then dividing 30 by 50 will give you 0.6 (60%).

Important note: some of the functions described here are only in the latest version of Graphite (1.0.x) and might not exist in your Graphite.