From e0ba6492be188ce1ccf4c65ee3e1fe9e465cddd3 Mon Sep 17 00:00:00 2001 From: LeonChapman <46498444+lchapman4@users.noreply.github.com> Date: Mon, 21 Nov 2022 05:29:11 -0500 Subject: [PATCH] historical windows uptime calculator (#300) custom powershell script to track historical uptime of windows per 24 hour period. Co-authored-by: Cristian Ciutea --- .../windows/GetUpTimeFromSystemLogPastDay.ps1 | 37 +++++++++++++++++++ .../windows/windows-historical-uptime.yml | 10 +++++ 2 files changed, 47 insertions(+) create mode 100644 examples/windows/GetUpTimeFromSystemLogPastDay.ps1 create mode 100644 examples/windows/windows-historical-uptime.yml diff --git a/examples/windows/GetUpTimeFromSystemLogPastDay.ps1 b/examples/windows/GetUpTimeFromSystemLogPastDay.ps1 new file mode 100644 index 00000000..65eee458 --- /dev/null +++ b/examples/windows/GetUpTimeFromSystemLogPastDay.ps1 @@ -0,0 +1,37 @@ +[timespan]$downTime = New-TimeSpan -start 0 -end 0 +[timespan]$totalDownTime = New-TimeSpan -start 0 -end 0 +Get-EventLog -LogName system | +Where-Object ` +{ $_.eventid -eq 6005 -OR $_.eventID -eq 6006 -AND $_.timegenerated -gt (get-date).adddays(-1) } | +Sort-Object -Property timegenerated | +Foreach-Object ` +{ + if ($_.EventID -eq 6006) + { + $down = $_.TimeGenerated + } #end if eventID + Else + { + $up = $_.TimeGenerated + } #end else + if($down -AND $up) + { + if($down -ge $up) + { + Write-Host -foregroundColor Red "*** Invalid data. Ignoring $($up)" + $up = $down + } #end if down is greater than up + [timespan]$CurrentDownTime = new-TimeSpan -start $down -end $up + [timeSpan]$TotalDownTime = $currentDownTime + $TotalDownTime + $down = $up = $null + } #end if down and up +} #end foreach +#"Total down time on $env:computername is:" +#$TotaldownTime +$minutesInMonth = (24*60)*30 +$minutesInDay = 24*60 +$downTimeMinutes = $TotaldownTime.TotalMinutes +$percentUpTime = "{0:n2}" -f (100 - ($downTimeMinutes/$minutesInDay)*100) +"{ + ""UptimePercent1Day"": $percentUptime +}" \ No newline at end of file diff --git a/examples/windows/windows-historical-uptime.yml b/examples/windows/windows-historical-uptime.yml new file mode 100644 index 00000000..5838e0b3 --- /dev/null +++ b/examples/windows/windows-historical-uptime.yml @@ -0,0 +1,10 @@ +integrations: + - name: nri-flex + config: + name: WindowsUptime + apis: + - event_type: systemUptime + shell: powershell + commands: + - run: "C:/Program` Files/New` Relic/newrelic-infra/integrations.d/GetUpTimeFromSystemLogPastDay.ps1" + timeout: 29000 \ No newline at end of file