Request: script for last run alerts

PowerShell script exchange

Request: script for last run alerts

Veeam Logoby PatrickBerger » Mon Dec 12, 2016 2:31 pm

Dear Veeam community,

So far ive been using a script to give out alerts (by either giving a certain text in the powershell output or write it in the event logs) when backups have failed in the last 24 hours.
However now a colleague of mine made a smart remark .. what if for some weird reason or error, the backup hasnt run in the last 24 hours. And i honestly came to the conclusion that we wouldnt notice if that was the case.

Now hereby the question. Does someone have a script or a start up to a script that takes the date from the PC/Server and looks if there are jobs that have NOT ran in the last 24 hours (or x hours/days for that matter) so that the script may give me the option to output a certain code or write into an eventlog when this is the case.
I kinda would prefer it to be totally seperate from the already used script but just to be sure i will simply post the script we run atm here to.

Code: Select all
#Datamex Remote Monitoring als source aanmaken in application eventlog:
New-EventLog -LogName Application -Source "Datamex Remote Monitoring" > $null

#requires -Version 1
Function Get-VeeamHistory
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [System.Int32]
        $Days
    )

    Add-PSSnapin -Name VeeamPSSnapin
   
    $colJobHistory = @()

    $JobHistory = Get-VBRBackupSession |
    Where-Object -FilterScript {
        $_.CreationTime -ge (Get-Date).AddDays(-$Days)
    } |
    Sort-Object -Property CreationTime -Descending

    ForEach ($Job In $JobHistory)
    {
        $objJobHistory = New-Object -TypeName System.Object
        $objJobHistory | Add-Member -MemberType NoteProperty -Name Name -Value $Job.JobName
        $objJobHistory | Add-Member -MemberType NoteProperty -Name StartTime -Value ($Job.CreationTime -f [datetime])
        $objJobHistory | Add-Member -MemberType NoteProperty -Name EndTime -Value ($Job.EndTime -f [datetime])

        $Duration = New-TimeSpan -Start $Job.CreationTime -End $Job.EndTime
        $Duration = '{0:00}:{1:00}:{2:00}' -f $Duration.Hours, $Duration.Minutes, $Duration.Seconds

        $objJobHistory | Add-Member -MemberType NoteProperty -Name Duration -Value $Duration
       
        $AuxData = [xml] $Job.AuxData
        $DataSizeB = $AuxData.AuxData.CBackupStats.BackupSize
        $DataSizeGB = [math]::round($DataSizeB / 1GB,2)

        $objJobHistory | Add-Member -MemberType NoteProperty -Name DataSizeGB -Value $DataSizeGB       
        $objJobHistory | Add-Member -MemberType NoteProperty -Name Result -Value $Job.Result
        #$objJobHistory | Add-Member -MemberType NoteProperty -Name ErrorMessage -Value $Job.ErrorMessage

        Switch ($Job.Result)
        {
            'Success'
            {
                $AEMResult = 0
                Break
            }
            'Warning'
            {
                $AEMResult = 1
                Break
            }
            'Failed'
            {
                $AEMResult = 2
                Break
            }
            default
            {
                $AEMResult = 1
                Break
            }
        }

        $objJobHistory | Add-Member -MemberType NoteProperty -Name AEMResult -Value $AEMResult

        $colJobHistory += $objJobHistory
    }

    Return $colJobHistory
}

Function Exit-AEMResult
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [System.Object]
        $AEMResult
    )

    Begin
    {
        $AEMResultCode = 0
    }

    Process
    {
        Write-Output -InputObject $AEMResult | Select-Object -Property * -ExcludeProperty AEMResult

        If ($AEMResult.AEMResult -gt $AEMResultCode)
        {
            $AEMResultCode = $AEMResult.AEMResult
        }
    }

    End
    {

        Write-Host -NoNewline -Object 'Backup Check Result: '
        Switch ( $AEMResultCode )
        {
            0
            {
                Write-Host -Object 'Success'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Information -EventId 7777 -Message "Veeam backups afgelopen 24 uur gelukt."
                Exit 0
            }
            1
            {
                Write-Host -Object 'Warning'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Error -EventId 6666 -Message "Veeam warnings gedetecteerd!"
                Exit 0
            }
            2
            {
                Write-Host -Object 'Failure'
Write-EventLog -logname Application -Source "Datamex Remote Monitoring" -EntryType Error -EventId 6666 -Message "Veeam Failure(s) gedetecteerd!"
                Exit 1
            }
        }
    }
}

Get-VeeamHistory -Days $env:Days | Exit-AEMResult
PatrickBerger
Lurker
 
Posts: 2
Liked: never
Joined: Mon Dec 12, 2016 2:21 pm
Full Name: Patrick Berger

Re: Request: script for last run alerts

Veeam Logoby Vitaliy S. » Mon Dec 12, 2016 3:44 pm 1 person likes this post

Hi Patrick,

I cannot comment on the PowerShell piece, but you can try to use these solutions (available in Veeam ONE) to address your scenario:

1. Run Protected VMs, which does show VMs which didn't have a backup within the defined RPO
2. Use "VM with no backups" alert, that would can trigger a backup job, once it identifies that there were no backups for the given VM. Check out this topic for more info on the alert > ONEbelievable tricks: Veeam ONE and Automation Services

Thanks!
Vitaliy S.
Veeam Software
 
Posts: 19773
Liked: 1120 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Request: script for last run alerts

Veeam Logoby v.Eremin » Tue Dec 13, 2016 10:23 am 1 person likes this post

The following script should list all jobs that haven't been executed within last 24 hours:

Code: Select all
asnp VeeamPSSnapin
$Day=(Get-Date).adddays(-1)
Get-VBRJob | where {$_.FindLastSession().CreationTime -lt $Day} | select name


Thanks.
v.Eremin
Veeam Software
 
Posts: 13558
Liked: 1006 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Request: script for last run alerts

Veeam Logoby PatrickBerger » Fri Dec 16, 2016 2:21 pm

Thanks alot for both replies! Will keep those features in mind.
Also i am sure i can make something happen using the powershell command.
PatrickBerger
Lurker
 
Posts: 2
Liked: never
Joined: Mon Dec 12, 2016 2:21 pm
Full Name: Patrick Berger

Re: Request: script for last run alerts

Veeam Logoby efd121 » Sun Dec 18, 2016 3:51 am

Take a look at the script I posted https://forums.veeam.com/powershell-f26/get-vbrrestorepoint-extremely-slow-t39613.html.

The script looks at a VMware tag to determine if the RPO time has been met for each VM. I have some jobs that only run once a week and this script takes that into account before reporting a failure. With some help from tsightler it now runs in about 10 minutes checking almost 600 VMs.

Dave
efd121
Enthusiast
 
Posts: 25
Liked: never
Joined: Fri Aug 07, 2015 8:45 pm
Full Name: David Engler

Re: Request: script for last run alerts

Veeam Logoby v.Eremin » Mon Dec 19, 2016 10:41 am

Nice script, indeed. And agree with Tom that usage of hash table should result in even better performance. Thanks.
v.Eremin
Veeam Software
 
Posts: 13558
Liked: 1006 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 5 guests