Search code examples
prometheusgrafanaprometheus-alertmanagerpromql

how to create query to monitoring how many minutes docker containers ran for a day


I am trying to monitor docker containers, but I have problems when making a query to monitor how many minutes the container runs in a day

count(rate(container_last_seen{id=~"/docker/.*",instance=~"$node"}[1d]))

this is my query to show how many container run in a day, but i don't have idea for monitoring how many minutes container runs in a day


Solution

  • If you know interval between data points for container_last_seen metric (it is known as scrape_interval and is usually configured in Prometheus config file), then the following query could be used for calculating the duration in seconds when container was running during the last day:

    count_over_time(
      (changes(container_last_seen{id=~"/docker/.*",instance=~"$node"}[<3*scrape_interval>])>0)
      [1d:<3*scrape_interval>]
    )*<3*scrape_interval>
    

    For example, if scrape_interval equals to 10s, then the query will look like:

    count_over_time(
      (changes(container_last_seen{id=~"/docker/.*",instance=~"$node"}[30s])>0)
      [1d:30s]
    )*30
    

    This query uses PromQL subqueries for calculating the number 30s intervals when container_last_seen time series had at least a single change.