PageSpeed Insights Automation with Zabbix

PageSpeed Insights Automation with Zabbix

The Google PageSpeed Insights Score is an important metric to measure. It not only gives you a score and some recommendation to improve the speed of your side, it is also directly affects your google search ranking. You can use the website to get a point-in-time score or you can use the API to regularly query the score and save it for later analytics. In this post I will describe how you can leverage Zabbix to query the API, save the metrics and create graphs for the relevant scores.

When using the PageSpeed website you get one score for mobile and one for desktop and under Lab Data you get six metrics: First Contentful Paint, Speed Index, Largest Contentful Paint, Time to Interactive, Total Blocking Time, and Cumulative Layout Shift. If your site is bigger than mine, you also get some real world data from your visitors under Field Data.

These six metrics all have their own score and are weighted and then combined to the single score your see above. The score of the six metrics are not visible when using the website but are available when using the API.

The API is very simple to use, you just need to send a GET request to https://www.googleapis.com/pagespeedonline/v5/runPagespeed and add the URL you want to test as a query parameter: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.sherbers.de

In response you get a JSON which includes all kinds of metrics about your site. When using the API you will not get a cached response, instead Google will start a Lighthouse test on your site and then return the results of this test. This means that the API request can take between 10-20 seconds to complete.

The Zabbix template

You can find the Zabbix template that I will describe in detail below, here:

stephan13360/zabbix-templates
Contribute to stephan13360/zabbix-templates development by creating an account on GitHub.

The template requires Zabbix 5.2 or higher, to import directly, even so all functions have been available since at least Zabbix 4.

Before using the template you need to create a PageSpeed API key. You can create one here: https://developers.google.com/speed/docs/insights/v5/get-started. The free keys allows 20.000 PageSpeed tests per day. That should be enough for most people.

Then head the my GitHub Repository and download the YAML template file. Inside Zabbix go to Configuration > Templates > Import (top right) and import the template. The template shows up under the name "Template PageSpeed" in the host group Templates/Custom. After importing, open the template, go to macros and add you API key to the {$GOOGLE_API_KEY} macro. Leave {$PAGESPEED_NAME} and {$PAGESPEED_URL} empty.

Under items you will find two items with type "HTTP agent" and a lot with type "Dependent item". The two HTTP agent items are the ones querying the API (When using the API, you need to query the website once for the mobile scores and once for the desktop scores) and the dependent items parse the JSON response and save the corresponding value.

Both HTTP agent items query the API with four query parameters attached. The url of the website you want to measure, your Google PageSpeed API key, the strategy which is either desktop or mobile and a category which is set to performance, this reduces the JSON response size, since we don't need the other test results.

The timeout is set to 30s but if you website is slow, you might have to increase this. The "History storage period" is set to not keep a history. DO NOT CHANGE THIS. When you set it to keep a history, Zabbix will truncate the JSON because it is longer than the allowed text data limit. This truncation happens before the dependent items are evaluated and will break them.

The dependent items use the JSONPath Preprocessor and sometimes a custom multiplier to get the data we need. The dependent items cover all metrics that are used by the current PageSpeed Insights version to calculate the Score and also the metrics from the previously version, since these can also still be interesting.

The template also includes four triggers that warn you about short term and long term decrease of your mobile and desktop scores. The short term trigger will alert you, if one of your scores decreased by more than 10 points in the last four hours, compared to the average of the last two days. Useful if an update caused a performance degradation. The long term trigger will alert you, if one of your scores decreased by more than 5 points in the last day, compared to the average of the last four weeks. Scores can vary from measurement to measurement by a few points, so setting the threshold for the short term trigger lower than 10 will cause false positives.

Lastly the templates includes four graphs showing the relevant scores over time and also their absolute values.

Using the template

I told you above that you should leave the two macros {$PAGESPEED_NAME} and {$PAGESPEED_URL} empty. Because if you would fill them in, in the template, you could only monitor one website. Instead create a new host group (mine is called PageSpeed), then create a new host in this group and call it something like "PageSpeed www.domain.tld" and attach the imported template to it. And then fill out the two missing macros inside the host.