Search code examples
prometheusgrafanapromql

report on website with grafana using blackbox exporter http_status_code


I am in an environment where one can use blackbox exporter to gather specific metrics. One of them is http_probe_status_code, it will probe a website, return the http_status_code. It will return a number, 200, 500, 301 etc. So far so good.

However i want to use the collection of metrics to give an availability percentage over the time of 30 days. It turns out this site does not have much downtime, so it is possible to end up with no data when searching the metrics for a non 200 status code.

I have tried multiple queries(kind of a novice when it comes to promQL voodoo.)

What is the best way to calculate a percentage (gauge bar in grafana) based on the number of statuscode 200 and non 200, where the query is capable of handeling 0(zero) times of non 200 returned status code.

Thank in advance for sharing your thoughts on this


Solution

  • You can achive this with query like this:

    count_over_time((probe_http_status_code==200)[30d:]) / count_over_time(probe_http_status_code[30d:]) * 100
    

    Here we count number of successful probes over 30 days, and divide by total number of probes over same range.

    Demo of this query can be seen here.