3PAR WSAPI via Powershell

Earlier I’ve demonstrated how to use 3PAR CLI with Powershell. In this example I will show how to work with 3PAR’s WSAPI via Powershell and poll last time remote copy group was synchronized (i use Last Sync date from first volume in the remote copy group).

I was asked to create solution to monitor replication via Recovery Manager for SQL as it sometimes fails for whichever reason and we don’t get notification that our SQL server wasn’t synchronizing to DR side for a while. I have special place for this product from HP (read my earlier posts).

If you’re using WSAPI to only read information, i recommend you create brand new account with limited privileges, as oppose to using 3paradm.


First check if WSAIP is enabled. By default it is disabled, but can be easily enabled.
cli% showwsapi
-Service- -State- -HTTP_State- HTTP_Port -HTTPS_State- HTTPS_Port -Version-
Enabled Active Disabled 8008 Enabled 8080 1.4.2

If service shows as Disabled, run the following command:
cli% startwsapi
Optional, you can configure either http or https. I suggest only use https (in my example i use SSL and have code to deal with self signed certs)
cli% setwsapi -http enable
cli% setwsapi -https enable

Now to Powershell example:

# Name: 3PAR_RC_Check.ps1
# Author: Naz Snidanko
# Date Created: Nov 11, 2015
# Date Modified: 
# Version: 0.1
# Description: uses WSAPI to poll last sync of the 3par group. Checks if last sync is older than X days and sends email alert.
# Credit: http://setspn.blogspot.ca/2014/11/3par-connect-to-webapi-using-powershell.html
############# START EDIT ##############
#Credentials  
$username = "uname"  
$password = "****"  
#IP of the 3PAR device  
$IP = "10.10.10.10"
#name of the RC group
$RCGroup = "TEST.r12345"
#Alert when older than X minutes
$oldThanMinutes = 10
#SMTP Server
$smtp = "mail.domain.com"
#Sender of Alerts
$FromEm = "noreply@domaincom"
#Recipient for alerts
$ToEm = "nsnidanko@domain.com"
#API URL  
$APIurl = "https://$($IP):8080/api/v1"  
############# END EDIT ##############

#avoid issues with an invalid (self-signed) certificate, try avoid tabs/spaces as this might mess up the string block  
#http://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error  
add-type @" 
    using System.Net; 
    using System.Security.Cryptography.X509Certificates; 
    public class TrustAllCertsPolicy : ICertificatePolicy { 
        public bool CheckValidationResult( 
            ServicePoint srvPoint, X509Certificate certificate, 
            WebRequest request, int certificateProblem) { 
            return true; 
        } 
    } 
"@  
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#connect to 3PAR WSAPI
$postParams = @{user=$username;password=$password} | ConvertTo-Json  
$headers = @{}  
$headers["Accept"] = "application/json"  
$credentialdata = Invoke-WebRequest -Uri "$APIurl/credentials" -Body $postParams -ContentType "application/json" -Headers $headers -Method POST -UseBasicParsing  
$key = ($credentialdata.Content | ConvertFrom-Json).key

#Poll 3PAR Remote Copy data
$headers = @{} 
$headers["Accept"] = "application/json" 
$headers["Accept-Language"] = "en"
$headers["X-HP3PAR-WSAPI-SessionKey"] = $key
$WSAPIdata = Invoke-WebRequest -Uri "$APIurl/remotecopygroups/$RCGroup" -ContentType "application/json" -Headers $headers -Method GET -UseBasicParsing  

#get last sync time of the first volume in Remote Copy group as string in ISO 8601 and cast it
[DateTime]$volLastSync = ( $WSAPIdata.content | Convertfrom-Json ).volumes[0].remoteVolumes.volumeLastSyncTime

#close 3PAR WSAPI connection
Invoke-WebRequest -Uri "$APIurl/credentials/$key" -ContentType "application/json" -Method DELETE -UseBasicParsing 

# get current date in ISO 8601 Format
$date = Get-Date -format "s"
#compare how much time since last sync
$Diff = new-timespan -Start $volLastSync -end $date

#logic to compare timespan
if ( $diff.TotalMinutes -ge $oldThanMinutes ) {
#send email
Send-MailMessage -From $FromEm -To $ToEm -SmtpServer $smtp -Subject "3PAR Replication Alert for $RCGroup" -Body "Last sync happened at $volLastSync"
}
This entry was posted in 3par, Powershell, Random stuff and tagged , , , , . Bookmark the permalink.

3 Responses to 3PAR WSAPI via Powershell

  1. Bruno Carvalho says:

    Great Man! This is the beggining to manage 3PAR from PS and WSAPI. I’m starting to work with that. Thks for this!

  2. marco says:

    Hi Naz

    great post. Just a question about HP 3par configuration:
    how did you create the user in 3par? Is it necessary to assign to a role?

    • Naz Snidanko says:

      Hi Marco, depending what you’re trying to do via WSAPI. If you’re only reading information (as in the script above) you can create brand new user and assign default “browse” role. You don’t have to create user and can run everything with 3paradm account, but it’s a good security practice to assign least required permissions.

Leave a Reply

Your email address will not be published. Required fields are marked *