
Maxime Palmieri (Customer) asked a question.
Problem : Having to import numerous machines at once from a CSV File
We once were given by a user a CSV file (with these headers Name;fqdn;ip) containing a list of more than 50 machines to monitor and to add to WhatsUp. A collegue of mine add the task to create all the entries based on that CSV file. Since we didn’t have credentials or snmp (we only had to add Ping Monitor), we could’t use the WhatsUp ability to scan the network and have the machine names. I didn’t wanted him to create everything manually…
To solve his issue, we proceeded like this :
- Creation of one entry manually in WhatsUp web interface
- Retrieve the ID of the machine in the URL http://localhost/NmConsole/#v=Wug_view_device_deviceproperties_DeviceProperties/p=%7B%22deviceId%22%3A%22IDOFTHEDEVICE%22%7D
- Used GET request /devices/ID/config/template to copy all the configurations we did
- Then in a loop we created all the devices using the information retrieved from the previous request and changing the name, fqdn and IP. We used the PATCH request /devices/-/config/template to achieve that
Since the code does not contain any customers or sensitive informations, I can share it here. Everything was written in PowerShell since this is my favorite scripting language 😋
It's a simple use case but it was usefull to us.
- #Documentation API http://localhost:9644/swagger/ui/
- $url = "http://localhost:9644/api/v1"
- $c = get-credential
- $filepath = "C:\file.csv"
- $tocopy = 9295 #id of the machine we wan't to copy
-
- #region AUTHENTIFICATION_TOKEN
- $req_auth = @{
- Method = "POST"
- Uri = "$url/token"
- Body = "grant_type=password&username=$($c.Username)&password=$($c.GetNetworkCredential().Password)"
- }
- $token = Invoke-RestMethod @req_auth
- $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
- $headers.Add("Authorization", "Bearer $($token.access_token)")
- #endregion
-
- #region RERIEVE_TEMPLATE
- $req_device = @{
- Method = "GET"
- Uri = "$url/devices/$tocopy/config/template "
- Headers = $headers
- }
- $device = Invoke-RestMethod @req_device
- $template = $device.data.templates
- #endregion
-
- #region RETRIEVE_CSV
- $machines = Get-Content $filepath | ConvertFrom-Csv -Delimiter ";"
- #endregion
-
- #region CREATION
- # We exclude the name of the machine that we used has a template
- ForEach ($machine in $machines.Where( { $_.'Name' -ne $template.displayName })) {
- #the convertto-json is used to convert the object, if not we have readonly properties
- $new_device = $template | ConvertTo-Json -Depth 4 | ConvertFrom-Json
- $new_device.PSObject.Properties.Remove("Templateid")
- $new_device.displayName = $machine.'Name'
- $new_device.interfaces[0].networkName = $machine.'fqdn'
- $new_device.interfaces[0].networkAddress = $machine.'IP'
-
- $body = @"
- {
- "options":["all"],
- "templates":[
- $($new_device | ConvertTo-Json -Depth 4)]
- }
- "@
-
- $req_new = @{
- Method = 'Patch'
- Uri = "$url/devices/-/config/template"
- Headers = $headers
- ContentType = 'application/json'
- Body = $body
- }
- Invoke-RestMethod @req_new
- }
- #endregion
Thank you for entering! You should have received a participation badge for your effort, and it will show on your profile page.
Good luck, and have a wonderful day!