Hitachi Ops Center​

 View Only

 script to gather storage allocations

Jonathan W. Burelbach's profile image
Jonathan W. Burelbach posted 01-30-2024 16:59

I'm looking for help writing a script (powershell, python, curl, etc.) that will query OpsCenter (or individual arrays) to produce a list of hosts and associated LUNs and sizes.  The ultimate goal is a text file with "hostname", "total allocated capacity", "date run" either as CSV or tab separated.   Part of the powershell code is below and it works to get the "response.data" when talking directly to the array but I'm getting a "User authentication failed" error trying to get the ldev info.  Any assistance is appreciated.

$baseURL = "https://<array IP>/ConfigurationManager"
$creds = New-Object System.Management.Automation.PSCredential ($Username, (ConvertTo-SecureString $Pass -AsPlainText -Force))

# Define the headers for the API request
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")

# Define the endpoints for getting storage system information
$endpoint = "/v1/objects/storages"
$endpoint2 = "/v1/objects/storages/<array SN>/ldevs"

# Send the API request and store the response
$response = Invoke-RestMethod -SkipCertificateCheck -Uri ($baseURL + $endpoint) -Method Get -Credential $creds -Headers $headers
$response.data | Format-Table

$ldevlist = Invoke-RestMethod -SkipCertificateCheck -Uri ($baseURL + $endpoint2) -Method Get -Credential $creds -Headers $headers
$ldevlist.data | Format-Table
Andrew J. Romero's profile image
Andrew J. Romero

Hi Jonathan,

Last year I posted some demo RestAPI code (python ) to the Flash Storage community.

Title:  

Test Drive of Hitachi Platform (PF) RestAPI ( example scripts attached )

The goal of this was to produce a light weight tool to do basic ( but flexible ) storage provisioning.

It work well .  I really like the RestAPI  .    For you use case you will want to:

  • read the notes / readme files
  • You will use the base module ( to authenticate ... etc )
  • create your own mission specific mini-modules that utilize the base module
    use my mini-modules as a reference

  • most of my output is to a log file ( my mission was more provisioning than reporting ); but , you don't need to limit
    yourself to that ..

    have fun ... and get stuff done

Andy 

William Jansen Van Nieuwenhuizen's profile image
William Jansen Van Nieuwenhuizen

Hello Jonathan

You need to get a session token first if you're using PFRest.

The first end point you have needs no session token, that's why it's working:
https://docs.hitachivantara.com/r/en-us/virtual-storage-platform-5100/93-07-2x/mk-98rd9014/common-operations-in-the-rest-api/getting-a-list-of-storage-systems

However getting ldev information requires a session token. I'm no Powershell guru however here is how I believe you'll get it:

You'll need a session token:

https://docs.hitachivantara.com/r/en-us/virtual-storage-platform-5100/93-07-2x/mk-98rd9014/common-operations-in-the-rest-api/generating-sessions 

$username = "maintenance"
$password = ConvertTo-SecureString "raid-maintenance" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
$headers = @{
    "Accept" = "application/json"
}
$response = Invoke-RestMethod -Uri "https://10.0.0.1/ConfigurationManager/v1/objects/sessions/" `
    -Method Post `
    -Credential $credential `
    -Authentication Basic `
    -Headers $headers `
    -ContentType "application/json" `
    -Body "" `
    -SkipCertificateCheck


After than you'll have to update your LDEV query with the token when you get the ldevs(I'm just substituting it here free hand, hope my syntax is correct):

$body = @{
    "headLdevId" = "0"
    "count" = "1000"
}
$headers = @{
    "Accept" = "application/json"
    "Authorization" = "Session $response.token"
}
$response = Invoke-RestMethod -Uri "https://10.0.0.1/ConfigurationManager/v1/objects/ldevs" `
    -Body $body `
    -Headers $headers `
    -ContentType "application/json" `
    -SkipCertificateCheck
Andrew J. Romero's profile image
Andrew J. Romero

To make life easier for you,  I have attached a simpler RestAPI demo to this thread

NOTE:

Do all test and dev work in a safe non-critical environment.
The notes below and the attached code may not be perfect.  Do NOT cavalierly treat
them like a fully tested document / product .... they are for instructional purposes only


[] Create a directory to store and test demo scripts
   c:\demos

   # note: annoying features of new Windows versions may
   # make it difficult for you to create directories ( outside of your profile )
   # I'll assume that you know how to deal with all of these silly msoft annoyances

[] Copy the attached zip file to the  directory you created

[] extract the contents of the zip file 

   You should end up with this directory:

   c:\demos\RestDemo

   In the RestDemo directory there should be:
     - several files
     - a logs sub-directory

[] Run the CMD_Shell bat file 
   ( this opens a Windows shell prompt in the current directory )


[] From the cmd shell prompt, run the script using python 3.x ( latest version )

   # note: annoying features of new Fisher-Price Windows versions may
   # make it difficult for you to run scripts ... 
   # I'll assume that you know how to deal with all of these silly msoft annoyances


   I have both python 2 and python 3 on my workstation, so I use the py launcher
   to specify python 3

   py -3 DpLdevMgr.py

   When run directly from the command line, the module's built-in test main
   function is called.

   I stripped all lines from the test main that modify array elements.
   Only the call to a function that lists DP LDEVs remains.

   Output is to a file in the logs directory.

   For nice reporting you will want to use the response_py element of the
   tuple returned by oDpLdevMgr.ListDpLdevs().  The data structure
   referenced by response_py is in python "format" ( ie. list of dictionaries ...etc. )
   and is easy to deal with.

Andy

Attachment  View in library
RestDemo.zip 11 KB