Microsoft Virtualization Discussions

Highlighted

Powershell Lag Time

So, i'm new to the whole powershell on Netapp, but I'm looking for a way to query a bunch of filers and report on snapmirror lag time.    I'm a Vbscript guy, so go easy on me as im just starting to learn powershell.

32 REPLIES 32
Highlighted

Re: Powershell Lag Time

Shouldn't be difficult.  You can get a flavor of this by connecting to a controller (Connect-NaController) and listing the snapmirror relationships:

PS C:\> Get-NaSnapmirror | ft -AutoSize

Source          Destination                         Status State         LagTimeTS

------          -----------                         ------ -----         ---------

dunn:vol2       benson:dunn_vol2_mirror             idle   source        13:05:57

dunn:testvol3   benson:testvol3                     idle   source     188.07:48:27

benson:testvol3 dunn:testvol3                       idle   broken-off 188.06:48:25

dunn:AS_5_12_1  fas2050b:AS_5_12_1_NODELETE         idle   source     434.02:47:27

dunn:SC_3_3_0   fas2050b:SnapCreator_3_3_0_NODELETE idle   source     434.02:47:00

Then it's just a matter of looping through multiple controllers and formatting the output however you want.  The LagTimeTS field is a .NET TimeSpan object, so it's easy to convert that to different units.

Highlighted

Re: Powershell Lag Time

Clinton

Thanks for the quick response.. I am a poweshell newbie, so i'm just kickin around the tires on this. I have reviewed the "getting started document and plan on reviewing your latest document "Making the most out of powershell"

The goal of the script would be to loop through a set of determined filers and anything over lets' say 50hours lag time be emailed to us.  The issue is that the environment i'm thinking about is has 3800+ daily snapsmirrors and snapvaults.  Right now we don't have a good way to determine where the issue is. 

Highlighted

Re: Powershell Lag Time

That sounds like a great application for a PowerShell script.  You might also review a few other links that may be relevant:

https://communities.netapp.com/docs/DOC-6293

https://communities.netapp.com/docs/DOC-6196

http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php

http://blog.usepowershell.com/2009/03/how-to-send-e-mail-from-powershell/

Once you get the script working, I'd invite you to post it here in the Documents section.

Highlighted

Re: Powershell Lag Time

Clinton,

Thanks for the linkage... Seems like I got LOTS of reading in order.. I will start to review it.  If I get to the point where the script is working 100%, I will definetly post it.  I will post back if i get stuck..

Highlighted

Re: Powershell Lag Time

I've written a similar type of script and I want the LagTimeTS to be presented as actual hours:minutes:seconds and not 118.07:23:01. You wrote that "The LagTimeTS field is a .NET TimeSpan object, so it's easy to convert that to different units." I've done a bit of googeling, but I can't figure out how I can convert it. I'm new to powershell, so some pointers to how to do this would be really nice.

Highlighted

Re: Powershell Lag Time

fmc - you want to share what you already have?

Highlighted

Re: Powershell Lag Time

Sure... I'm not very good at powershell, but this works for me. Although there are probably better ways of doing it.

We got multiple filers with different user/pass so I made one of these for each filer. I don't know ho to use the foreach cmdlet when I have to deal with multiple filers, user and password.

# Import the proper powershell module

Import-module DataONTAP.psd1

# Set your alert time here, this is in seconds. Lagtime higher than 24 hrs 

$LagTimeSeconds = "86400"

# Set your root password

$filerpassw = "p@ssword"

Set your Filer IP or FQDN

$filername1 = "netapp.example.com"

#SMTP server

$PSEmailServer = "smtp.example.com"

#Mail body array

$body = @()

################# FILER1 #################

# Create an encrypted string which the API will use — Change password below

$password = ConvertTo-SecureString $filerpassw -AsPlainText –Force

# Create a credential object in powershell against the root user – Change root user if you use a different admin user

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "root",$password

# Connect to the Filer using the credential created above – You can change the FQDN or IP below

$connection =  Connect-NaController $filername1 -Credential $cred

$body += Get-NaSnapmirror | where-object {$_.LagTime -gt $LagTimeSeconds}

$body = $body | out-string

send-mailmessage -to "User01 <user1@example.com>" -from "User02 <user2@example.com>" -subject "Snapmirror status" -body "$body"

Highlighted

Re: Powershell Lag Time

Yeah, i'm just getting rockin with it as well.. I will kick the tires on your script as well. 

Highlighted

Re: Powershell Lag Time

"118.07:23:01" is days.hours:minutes:seconds, which isn't much different.

You can use the various TimeSpan properties to get whatever you want:

PS C:\> $ts = (Get-NaSnapmirror -Location dunn:AS_5_12_1).LagTimeTS

PS C:\> $ts

Days              : 450

Hours             : 20

Minutes           : 26

Seconds           : 29

Milliseconds      : 0

Ticks             : 389535890000000

TotalDays         : 450.851724537037

TotalHours        : 10820.4413888889

TotalMinutes      : 649226.483333333

TotalSeconds      : 38953589

TotalMilliseconds : 38953589000

PS C:\> $ts.ToString()

450.20:26:29


PS C:\> ([int] $ts.TotalHours).ToString() + ":" + $ts.Minutes + ":" + $ts.Seconds

10820:26:29

http://msdn.microsoft.com/en-us/library/system.timespan.aspx

Highlighted

Re: Powershell Lag Time

Just thought I'd add a pedantic note about PowerShell's weird integer rounding rules.  You might want to use [Math]::Truncate() in this case to make sure you don't accidentally round up an extra hour:

PS C:\> [Math]::Truncate($ts.TotalHours).ToString() + ":" + $ts.Minutes + ":" + $ts.Seconds

10820:26:29

Highlighted

Re: Powershell Lag Time

It would be cool if i knew a way to convert 3.05:14:59 to days, hours, minutes, seconds.  So, like 3days,5hours,15minutes,59seconds  Is that a powershell expression that needs to happen?

Highlighted

Re: Powershell Lag Time

Trivial:

PS C:\> $ts

Days              : 450

Hours             : 20

Minutes           : 26

Seconds           : 29

Milliseconds      : 0

Ticks             : 389535890000000

TotalDays         : 450.851724537037

TotalHours        : 10820.4413888889

TotalMinutes      : 649226.483333333

TotalSeconds      : 38953589

TotalMilliseconds : 38953589000

PS C:\> [String]::Format("{0} days, {1} hours, {2} minutes, {3} seconds", $ts.days, $ts.Hours, $ts.Minutes, $ts.Seconds)

450 days, 20 hours, 26 minutes, 29 seconds

Highlighted

Re: Powershell Lag Time

Clinton,

Now i'm sort of getting somewhere

I took your advice from the other thread and have come up with this

"

gc $hostfile | % { $c = Connect-NaController -credential $cred $_; Write-Host "`nController: $c`n"; Get-NaSnapmirror | where-object {$_.LagTime -gt $LagTimeSeconds}  }

"

A few things i'm noticing.  As the script loops through it strips out the headers on each loop.  so for the first example it only lists

Controller: filera

Source                               Destination                          Status         State                LagTimeTS

------                               -----------                          ------         -----                ---------

Also, what is the best way to write the host name like we are and then if it doesn't meet the criteria to write "no snapmirror lag issue"

I was thinking along the lines of

$lag = Get-NaSnapmirror | where-object {$_.LagTime -gt $LagTimeSeconds

Am i totally off base?

I'm also noticing the built in formatting is not big enough for some of our volume names.  I am going to work on that with the expression statements like you did in the other post.  I will re-post when im done.

Highlighted

Re: Powershell Lag Time

Clinton,

Got my heads back with the following code.. I took your advince from the previous thread.. Still needs work but it's a start

$lagtimeseconds = "86400"

gc $hostfile | % {

     $c = Connect-NaController $_ -credential $cred

     Get-NaSnapmirror | where-object {$_.LagTime -gt $LagTimeSeconds}  | Select SourceLocation,DestinationLocation,Status,State,LagtimeTS |  ft @{expression={$c};Label="Filer name";width=20},@{expression={$_.SourceLocation};Label="Source";Width=40},@{Expression={$_.DestinationLocation};Label="Destination";Width=40},@{expression={$_.status};Label="status";width=20},@{Expression={$_.state};Label="state";width=20},@{Expression={$_.lagtimets};label="lag time";width=20}

     }

I had to increase the widths b/c our volume are quite long.    I'm not a true fan of how i did filer name, but it's a start. 

I would more like

*** Filer Name ****

blah blah

That's for tomorrow.. And then i will do the same for snapvault status.. This will really helps us out.  I knew you converted lagtimTS into days and hours.  I'm thinking how I would do this in VB.  I would create a function that does that that I can whip a variable through to convert.  is it something similiar? 

Highlighted

Re: Powershell Lag Time

Josh, you're on the right track.  Try the -GroupBy parameter to Format-Table.  That can save you a little horizontal space.  'Fraid I can't help you with VB, though!

PS C:\> gc .\filers.txt | % { $c = Connect-NaController $_; Get-NaSnapmirror | where-object {$_.LagTime -gt $LagTimeSeconds}  | Select SourceLocation,DestinationLocation,Status,State,LagtimeTS |  ft @{expression={$_.SourceLocation};Label="Source";Width=40},@{Expression={$_.DestinationLocation};Label="Destination";Width=40},@{expression={$_.status};Label="status";width=20},@{Expression={$_.state};Label="state";width=20},@{Expression={$_.lagtimets};label="lag time";width=20} -GroupBy @{expression={$c};Label="Filer name"} }

   Filer name: dunn

Source                                   Destination                              status               state                lag time

------                                   -----------                              ------               -----                --------

dunn:testvol3                            benson:testvol3                          idle                 source               209.08:02:47

benson:testvol3                          dunn:testvol3                            idle                 broken-off           209.07:02:45

dunn:AS_5_12_1                           fas2050b:AS_5_12_1_NODELETE              idle                 source               455.03:01:47

dunn:SC_3_3_0                            fas2050b:SnapCreator_3_3_0_NODELETE      idle                 source               455.03:01:20

   Filer name: benson

Source                                   Destination                              status               state                lag time

------                                   -----------                              ------               -----                --------

dunn:testvol3                            benson:testvol3                          idle                 broken-off           209.08:02:48

benson:testvol3                          dunn:testvol3                            idle                 source               209.07:02:46

Check out the KB!
Knowledge Base
All Community Forums