Percentage of downtime

Hi folks,

I'm using scripted fields to calculate my host downtime over a month statically using 43800 which is the number of minutes per month: 100 - ((doc['downtime'].value / 43800) *100)

Now this is static and not accurate as the number of minutes is a variable for each month so is there another way to replace the number of minutes by casting the date type cause I have @timestamp field so any ideas how to solve this?

Thx,

Hello,
I gave it a try and this should help you take everything in account to calculate the downtime. You need to switch the strings in the return statements to the results of your calculations, or to set the variable number_of_total_minutes_per_month to the specific value in each branch and then just use it after the whole if/else block to calculate your downtime.


if ([ 1, 3, 5, 7, 8, 10, 12 ].contains( doc['@timestamp'].date.monthOfYear ) )
	{
		return "long month"
}
else
	{ 
		if ( [4, 6, 9, 11 ].contains( doc['@timestamp'].date.monthOfYear) ) 
			{
				return "short month"
		}
		else
			{
				if ( doc['@timestamp'].date.isLeap )
					{
						return "longFebruary"
				}
				else
					{
						return "shortFebruary"
				}
		}
}

There is also a date.dayOfMonth.getMaximumValue()which should return the maximum number of days in the current month. in Joda.Time.MutableDateTime (which is used here), but I can't get it to work due to some conflict between java.Time and Joda.Time.

Thanks for the suggestion and indeed it is not returning the correct percentage of each month for some reason:

int monthly_minutes = 0;
int total = 0;


if ([ 1, 3, 5, 7, 8, 10, 12 ].contains( doc['@timestamp'].date.monthOfYear ) )
	{
		monthly_minutes = 44640;
}
else
	{ 
		if ( [4, 6, 9, 11 ].contains( doc['@timestamp'].date.monthOfYear) ) 
			{
				monthly_minutes = 43200;
		}
		else
			{
				if ( doc['@timestamp'].date.isLeap )
					{
				monthly_minutes = 41760;
				}
				else
					{
				monthly_minutes = 40320;
				}
		}
}
total += (100 - (( doc["downtime"].value / monthly_minutes) *100));
return total;

And by the way I wanted to have something more dynamic similar to this which also throwing an exception:

100 - ((doc['downtime'].value / ( Instant.ofEpochSecond(doc['@timestamp'].value).minusMonths(1).toEpochSecond() ) ) *100)

Any ideas about possible time manipulations here?

Well, it depends on how you have your data structured. Is it a monthly document that contains the downtime? is it a daily document (in that case you would have to do a Sum aggregation on this scripted field)?
Also, no need to use the operations with 100, just leave it as the result of the division and set the field formatting to Percentage.

As for your second script, I have a hard time following what everything there does, can you break it down a little more?

well here's the index I'm working on and you understand it right it's a monthly report based on documents run each 3 minute to check whether my URL is 200 or not, and gets a downtime field containing the downtime in minutes when the HTTP status is different than 200

{
  "template": "whatever-index-*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
      "whatever-index-": {
      "_source": {
      "enabled": true
      },
      "properties": {
        "@timestamp": {
          "type": "date",
          "format" : "epoch_second"
        },
        "uri": {
          "type": "keyword"
        },
        "http_status": {
          "type": "integer"
        },
        "downtime": {
          "type": "integer"
        }
      }
    }
  }
}

So for the first script u sent me the logic is there but when I add the total function, painless somehow doesn't return the correct number: simply my first function 100 - ((doc['downtime'].value / 43800) *100) is run on lang:expression but the then when I try to run same function with lang:painless the result is different and I don't get the problem yet.

for the second script it's the same logic as for 100 - ((doc['downtime'].value / 43800) *100) but here I replace 43800 by ( Instant.ofEpochSecond(doc['@timestamp'].value).minusMonths(1).toEpochSecond() ) which calculate minus one month in minute from the actual time which is also throwing an exception..

Ideas? suggestions?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.