# Percentage Uptime per month / Count

**URL:** <https://community.influxdata.com/t/percentage-uptime-per-month-count/24928>\
**Category:** Systems\
**Tags:** influxdb, grafana, influxql, flux\
**Created:** [May 6, 2022, 7:30am UTC](https://community.influxdata.com/t/percentage-uptime-per-month-count/24928 "2022-05-06T07:30:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnodb](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arnodb](https://community.influxdata.com/u/Arnodb)\
**Post date:** [May 6, 2022, 7:30am UTC](https://community.influxdata.com/t/percentage-uptime-per-month-count/24928/1 "2022-05-06T07:30:55Z")

</div>

Hi,

My data in Influx comes from LibreNMS, which is polling devices for uptime every 5 minutes. The uptime is stored in seconds. I want to get an uptime percentage per month.  
E.g.

- Device is up for 30 days (or goes down and comes back up immediately), percentage=100%.

The way I currently have it setup, it will go down to 0% everytime the device goes down and start counting again.

Current config using InfluxQL:

```auto
SELECT mean("uptime") / 2592000 
FROM "uptime" 
WHERE ("hostname" = '10.237.150.20') AND $timeFilter 
GROUP BY time($__interval) fill(null)

```

An idea I’ve had to fix this;

- Get a count of everytime the uptime goes to 0, the count increases every 300s (5min) and minuses from the total seconds per month (2592000)
- I dont know if that will work.
- I’m not very familiar with Flux but am happy to have a go.

I am making these graphs in Grafana but I can test in InfluxQL/Flux and make changes to suite Grafana.  
Do you have any advice or queries I should try for this?

Thanks,  
Arno.

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 10, 2022, 7:36pm UTC](https://community.influxdata.com/t/percentage-uptime-per-month-count/24928/2 "2022-05-10T19:36:22Z")

</div>

Hello @Arnodb,  
I’m sorry. I’m a little confused about what you’re trying to do here.  
Can you provide a numerical example?

These functions might be interesting:

> **[stateCount() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/statecount/)**
>
> stateCount() returns the number of consecutive rows in a given state.

> **[stateDuration() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/stateduration/)**
>
> stateDuration() returns the cumulative duration of a given state.

> **[map() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/map/)**
>
> map() iterates over and applies a function to input rows.

---

<div class="post-metadata">

**Author:** ![Arnodb](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arnodb](https://community.influxdata.com/u/Arnodb)\
**Post date:** [May 11, 2022, 12:27am UTC](https://community.influxdata.com/t/percentage-uptime-per-month-count/24928/3 "2022-05-11T00:27:17Z")

</div>

Hi Anaisdg,

Thanks for your reply.

I can’t really think of a numerical value but I can show a graph example.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/6/6e7be9c07602e123dabcd3c1eb8fdc502696d265.png)  
In that image, it shows the graph on the right side going down and instantly counting again, so the uptime should be minimally impacted, not 20.2%.  
The query I described in my original post was a Grafana query which gets the uptime (displayed in days in the second graph) and I made graphical changes in Grafana to display it as a percentage.  
The query is set to select the last non null value and the default display is in days.

I’ve done the following yesterday:

- Threshold is amount of seconds in a day, so it will not count if a device is down for a day.

```auto
threshold = 86400
from(bucket: "librenms")
  |> range(start: -30d)
  |> filter(fn: (r) => r.hostname == "au-brockman4-ves-sw-01" and r._field == "uptime")
  |> aggregateWindow(
        every: 5m,
        fn: (column, tables =<-) => tables
            |> reduce(
                identity: {above_threshold_count: 0.0},
                fn: (r, accumulator) => ({
                    above_threshold_count: if r._value >= threshold then
                        accumulator.above_threshold_count + 1.0
                    else
                        accumulator.above_threshold_count + 0.0,
                }),
            ),
    )

```

This somewhat does what I need it to but still seems to drop to 0 (\_time=2022-04-11T00:20:00.000Z OR \_time=2022-04-11T00:30:00.000Z)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/5/5580bda86675ceed3443ad6d5f8bdec852d9bf5a.png)

I think this may be because there isnt an entry for that time?  
See the below picture of this query which shows no entry for the two times mentioned above.

```auto
from(bucket: "librenms")
  |> range(start: -30d)
  |> filter(fn: (r) => r.hostname == "au-brockman4-ves-sw-01" and r._field == "uptime")

```

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/11df9ce81d377e9a119e75cb0287c3ab25364306.png)

I tried the state duration and state count however I think I need a threshold for it to work properly?

```auto
from(bucket: "librenms")
  |> range(start: -10d)
  |> filter(fn: (r) => r.hostname == "au-brockman4-ves-sw-01" and r._field == "uptime")
    |> stateDuration(fn: (r) => r._measurement == "uptime", column: "stateDuration", unit: 5m)

```

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/ed4b7a0c5f65676c417cd232cc97645ec9af1d5d.png)

How would I use the map function? Again I’m not very experienced in Flux…

Sorry for such a long reply…Thank you for your help so far!
