VMware Cloud Community
Contributor

## Custom Alarm based on Snapshot Age- Vm's with snapshots older than 48Hrs

Looking to create Custom alarm for Snapshot Age(Not Size), Requirement is to trigger alarm from vcenter automatically for Vm's with snapshots older than say 48 Hours.

Tried API like "com.vmware.library.vc.virtualmachinesnapshottree" in Custom Alarm Event, but not working.. Any input on how to achieve this.?.  Custom Larm Script or API..?. Thanks..

12 Replies
Enthusiast

PowerCLI Scripting - SnapShot Create Date is older than 3 days

The important part would be

$ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-2)}

(Note: I changed it to 2 days from the orig 3 as per your 48 hour requirement)

Though this is for an emailed report and not a vCenter alert.

On the vCenter you can create alarms that are based on 3 types of triggers: event, metric and state.

The age of a snapshot doesn't fall under any of these triggers I'm afraid.

One of the options left is to run a scheduled task on a Windows box at regular intervals, and in that scheduled task verify the age of all the snapshots.

When a snapshot exceeds your age threshold, you could take an action, just like an Alarm.

The action could be sending a SNMP trap or sending an email.

Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Contributor

Thanks, I tested the one-liner and Scripts.  The use-case i am looking is to automate via Alarms such that we can create incidents/tickets by calling the Latest Alarms. .

Contributor

Oh, To be frank i am actually thinking LucD may have something to help me on this .. Ha ha. I looked all your posts on Alarms/Snapshots to tweak, but no Luck.   Thanks for the response and bummer to know that  We can't do on age Basis. We can achieve by  alternate ways, but that changes the existing nature of  other Alarm's that we are using for generating tickets for the ops Team.

I can create a scheduled task on the vCenter that runs a script, but since I'm using VCSA, that would require a Perl script.

Then from that script I could check the snapshot age, and fire an Alarm.

But I would need to use a generic Alarm.

I'm currently trying something out, but not sure yet that my idea will work.

Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Contributor

oh, Interesting to know. we use VCSA's.  if by any chance, once you tried that please let me know, i can check on that in my appliance Lucd. Thanks again.

Contributor

You can use mine below. I use this all the time and it runs as a sheduled task each night on a windows box. It connects to several Vcenters and nukes any snapshot older than 7 days, via our policy. I have cleaned up all the references to our organization. It also maps a drive and logs the removals. Add to the $vcenters. I have set up new vicredentials for all the vcenters on the machine that is running this script so login is taken care of. Change the$age variable to 2 for your 48 hrs. With such a short time frame, it all depends on how many times you plan to execute this script i suppose.

$drive = New-Object -com wscript.network$drive.MapNetworkDrive("Z:", "\\share\folder")

$LogFilePath = "z:\rm-old-snapshots.log"$Exclusions = ('VMnames to exclude')

$vcenters = @("vc01", "vc02") #connect all vcenters: Foreach ($vcenter in $vcenters) { Connect-VIServer -Server$vcenter -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

# Set the variables used

$vms = get-view -viewtype VirtualMachine -Filter @{"snapshot" = ""} # Select only up to 10 Snaps per cycle, this is to prevent a delete snapshot storm.$vms = $vms | ? name -notin$Exclusions | sort @{e={$_.snapshot.rootsnapshotlist.createtime} } | select -first 10$age = 7

$date = (Get-Date).ToString() foreach ($vm in $vms) { try {$snap = get-snapshot -vm $vm.name -ErrorAction Stop if ($snap.count -gt 1) { $snap =$snap | sort created | select -first 1}

if($snap.Created -lt (Get-Date).Adddays( -$age)){

Remove-Snapshot $snap -RemoveChildren -RunAsync -Confirm:$false -ErrorAction stop

write-output "$($date): $($vm.name) SNAP:$($snap.name) SIZE:$($snap.SizeGB -as [int])Gb CREATED:$($snap.Created)" |

Out-File -Append $LogFilePath } } Catch { Write-output "FAILED:$($vm.name)" | out-file -Append$LogFilePath

} #try-catch

}

Disconnect-VIServer $vcenter -Confirm:$false

}

Contributor

You could remove your user's permission to create snapshots and force them to use an Orchestrator workflow. In the logic you could alert to or deny the request if the number of snapshots is too high. Otherwise you have to use a scheduled task to check it. There isn't an alarm trigger related to snapshot that I could quickly find.

Or you can specify the maximum number of snapshots in the vmx: http://www.virtuallyghetto.com/2010/10/how-to-control-maximum-number-of-vmware.html

Contributor

Or you could set up a trigger that would be called when the task is complete in Orchestrator: Create a Trigger Object

Contributor

I am currently doing Something Like this, but with No Auto delete, But just Email.. The remaining are anyways being taken care by incidents generated using default size Alarm..

Contributor

Because of the fact that snapshots running for excessively long times can create performance and storage issues, it seems to me that an important enhancement to  vCenter alarm capabilities is to add age as a criterion.  Is there some essential reason vCenter can't be enabled for this?