API - Posting widget updates via powershell
Hello, Trying to get the below script to work, but hitting a snag. I've tried to repurpose the DataSource Upload Example script from this page to get the desired result but no dice. Param([Parameter(Mandatory=$true)][string]$name,[string]$GetInstance,[string]$GetType,[string]$Computername)<# Use TLS 1.2 #>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $accessId = '###'$accessKey = '###'$company = '###'$httpVerb = 'PUT'$widgetID = "350"$resourcePath = "/dashboard/widgets/$widgetID"$queryParams = ''$boundary = [System.Guid]::NewGuid().ToString()$LF = "\r\n"$data = @" "items":[{"deviceGroupFullPath":"*","deviceDisplayName":"$computername","dataSourceDisplayName":"$name","instanceName":"$Name","dataPointName":"FolderGT60","groupBy":"instance","name":"$getinstance"}]"@$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)$requestVars = $httpVerb + $epoch + $data + $resourcePath$hmac = New-Object System.Security.Cryptography.HMACSHA256$hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)$signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))$signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'$signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"$headers.Add("Authorization",$auth)$headers.Add("Content-Type","multipart/form-data; boundary=----$boundary")$response = Invoke-RestMethod -Uri $url -Method $httpVerb -body $data -Header $headers $status = $response.status$body = $response.dataWrite-Host "Status:$status"Write-Host "Response:$body" I assume that the '$headers.Add("Content-Type","multipart/form-data; boundary=----$boundary")' section, or '$boundary', is incorrect but have only really been looking into anything Logicmonitor-API-Powershell in the last 24 hours. I'm getting HTTP 415 error Unsupported Media Typeh1 which I thought originally was from the $data section but I can't figure out any other way to pass the JSON into the variable. Thanks, Amir.11Views0likes2CommentsGet all devices in all sub device groups via LogicMonitor REST API
Hi, I am trying to use the LogicMonitor REST APIget a list of allof the devices in a given device group, including all devices in all sub groups below in the hierarchy. I have tried using the /device/groups/{id}/devices API but it just seems to return the devices in that immediate device group. This behaviour seems contrary to the /device/groups/{id}/alerts and /device/groups/{id}/sdts methods which do work with any sub groups below. This was previously possiblevia the getHosts RPC API method. I believe it is still possible but we are looking to migrate away from the RPC API. Can someone please advise on a solution for this issue?62Views0likes6CommentsREST API for resources -- inheritedProperties objects should include which hostGroupId that property is inherited from
For /device/devices the inheritedProperties objects should include a key-value pair that identifiesthe hostGroupId that object is inherited from Example { "status": 200, "errmsg": "OK", "data": { "total": 1, "items": [ { "name": "hostname", "inheritedProperties": [ { "name": "keyname1", "value": "value1", "inheritedFromHostGroupId": 2 } ] } ] } } I'm not super tied to the name of the proposed key? Thanks!12Views0likes1CommentHow to use the ~ (includes) filter operator?
Hi, I needto retrieve alerts by a list of alert IDs. I thought maybe the includes ~ operator in the filter would be the way to do this, passing acomma separated list of alert IDs: /alert/alerts?needMessage=true&filter=internalId~LMD2316102,LMD1785737 But this only returns the first alert. Am I using the ~ operator correctly? https://www.logicmonitor.com/support/rest-api-developers-guide/v1/alerts/get-alerts/8Views0likes3CommentsRest API and Powershell
So I have had pretty good luck converting the Python examples to Powershell. The one thing I can not figure out how to do is return volume usage for a specific host. Anyone got any example powershell script using Rest API to pull volume usage on a host?19Views0likes7CommentsSDT scheduling using REST API and PowerShell
Trying to schedule a new SDT for January 1, 2018 1:00 AM to 1:30 AM for a device group, but getting a Status:1007 and a blank response. <# account info #> $accessId = 'SHj6Hub8e63FUwkc5' $accessKey = 'xz37=(][{qb6ANLp}5$-S9Hvn6HV292P' $company = 'api' # stdTYpe (integer) # 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT # we have to use "one time" style values because LM has no concept of day of month $stdTYpe = 1 # type (string) # ServiceGroupSDT, DeviceGroupSDT, CollectorSDT $type = "DeviceGroupSDT" # deviceGroupId (string) # $deviceGroupId = 18 # dataSourceId (integer) # 0 = ALL $dataSourceId = 0 <# request details #> $httpVerb = 'POST' $resourcePath = '/sdt/sdts' #serviceGroupSDTs # data $data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","deviceGroupId":'+ $deviceGroupId +',"dataSourceId":'+ $dataSourceId +',"startDateTime":1514786400,"endDateTime":1514788200}' <# Construct URL #> $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath <# Get current time in milliseconds #> $epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds) <# Concatenate Request Details #> $requestVars = $httpVerb + $epoch + $data + $resourcePath <# Construct Signature #> $hmac = New-Object System.Security.Cryptography.HMACSHA256 $hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey) $signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars)) $signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-' $signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower())) <# Construct Headers #> $auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$auth) $headers.Add("Content-Type",'application/json') <# Make Request #> $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers <# Print status and body of response #> $status = $response.status $body = $response.data| ConvertTo-Json -Depth 5 Write-Host "Status:$status" Write-Host "Response:$body"27Views0likes5CommentsIssues With Creating A Datasource
I took a working groovy script datasource and am now trying to adjust it to some needs we have. This data will end up giving us alert totals for each month so we can build reports. Any ideas? Here is what I have so far. import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import groovy.json.JsonSlurper; //define credentials and url def accessId = hostProps.get('lmaccess.id'); def accessKey = hostProps.get('lmaccess.key'); def account = hostProps.get('lmaccount'); def alertgroup = hostProps.get('lmaccess.group'); def collectionFailures = 0 def failures = [:] def client = new LogicMonitorRestClient(accessId, accessKey, account, this.&println) try { def alerts = client.get("/device/groups/" + alertgroup + "/alerts", fields: "severity", filter: "startEpoch>:1538370000,endEpoch<:1541048399,cleared:*") //warnings = alerts.findAll {it.severity == 2}.size() println "WarningCount: ${alerts.findAll {it.severity == 2}.size()}" println "ErrorCount: ${alerts.findAll { it.severity == 3 }.size()}" println "CriticalCount: ${alerts.findAll { it.severity == 4 }.size()}" println "TotalAlerts: ${alerts.size()}" } catch (Throwable e) { failures["alerts"] = e.toString() collectionFailures += 1 } // Do error reporting println "CollectionFailures:${collectionFailures}" failures.each{ query, exception -> println "Exception while querying $query:" println exception } return 0 ////////////////////// // HELPER FUNCTIONS // ////////////////////// class LogicMonitorRestClient { String userKey String userId String account int maxPages = 20 int itemsPerPage = 1000 def println LogicMonitorRestClient(userId, userKey, account, printFunction) { this.userId = userId this.userKey = userKey this.account = account this.println = printFunction } def generateHeaders(verb, path) { def headers = [:] def epoch = System.currentTimeMillis() def requestVars = verb + epoch + path // Calculate signature def hmac = Mac.getInstance('HmacSHA256') def secret = new SecretKeySpec(userKey.getBytes(), 'HmacSHA256') hmac.init(secret) // Sign the request def hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes())) def signature = hmac_signed.bytes.encodeBase64() headers["Authorization"] = "LMv1 " + userId + ":" + signature + ":" + epoch headers["Content-Type"] = "application/json" return headers } def packParams(params) { def pairs = [] params.each{ k, v -> pairs << ("${k}=${v}")} return pairs.join("&") } // Non paginating, raw version of the get function def _rawGet(path, params) { def baseUrl = 'https://' + account + '.logicmonitor.com' + '/santaba/rest' + path def packedParams = "" if(params) { packedParams = "?"+packParams(params) } def query = baseUrl+packedParams def url = query.toURL() def response = url.getText(useCaches: true, allowUserInteraction: false, requestProperties: generateHeaders("GET", path)) return response } // Public interface for getting stuff. def get(Map args=[:], path) { def itemsReceived = [] def pageReads = 0 // Impose our own paging parameters. args.size = itemsPerPage args.offset = 0 while(true) { // Do da nastieh def response = new JsonSlurper().parseText(_rawGet(path, args)) if (response.errmsg == "OK") { // Catch individual items if (response.data.items == null) { return response.data } itemsReceived += response.data.items // Check if there are more items // if (response.data.total > itemsReceived.size()) // { args.offset = args.size + args.offset // } // else // { // break // we are done // } } else { // Throw an exception with whatever error message we got. throw new Exception(response.errmsg) } pageReads += 1 // Check that we don't exceed max pages. if (pageReads >= maxPages) { break } if (response.data.total > 0) { break } } return itemsReceived } } If I run the URL with the API creds in my test powershell script, it works perfectly. When I test it in LM as a datasource, I get the attached error. Quote Exception while querying alerts: java.io.IOException: Server returned HTTP response code: 400 for URL https://XXX.logicmonitor.com/santaba/rest/device/groups/224/alerts?fields=severity&filter=startEpoch>:1538370000,endEpoch<:1541048399,cleared:*Solved13Views0likes5CommentsSwagger Doc
It would be nice to have a swagger doc to define all the rest endpoints. We are developing a graphql server internally and it would be useful to be able to programmatically define the LogicMonitor portion. I see that LM has a swagger project in githubhttps://github.com/logicmonitor/swagger-codegen not quite sure the scope of that, but an example of a swagger doc that can be read and interpreted comes from connectwise manage:https://marketplace.connectwise.com/docs/openapi/json/Project.txt7Views1like5CommentsCollector REST API Requests
I would like the REST API to support Scheduling a collector version update Applying a one-time collector version update Working with the Collector Custom Properties (recently added I think, but don't see anything in the online documentation about support in REST API).8Views0likes4CommentsUpdating Escalation Chains via REST
I am attempting to adjust an escalation chain via PowerShell and REST. I am currently getting back a 1007 and can't seem to figure out what is wrong. Below is what I am using, I have tried different forms of $data. Our main goal is to change CC. We have tried these two and then the 1 in the script below. Just not sure what is coming up wrong. $data = '"ccDestinations":[{"type":"arbitrary","method":"email","addr":"test@test.com"}]' $data = '"destinations":[{"type":"single","stages":[[{"type":"arbitrary","method":"email","addr":"test@test.com"}]]}]' <# Use TLS 1.2 #> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 <# account info #> $accessId = '' $accessKey = '' $company = '' <# request details #> $httpVerb = 'PUT' $resourcePath = '/setting/alert/chains/4' $data = '"name":"Chain 1","ccDestinations":[{"type":"arbitrary","method":"email","addr":"fakeemail@email.com"}],"destinations":[{"type":"single","stages":[[{"type":"arbitrary","method":"email","addr":"anotherfakeemail@email.com"}]]}]' <# Construct URL #> $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath <# Get current time in milliseconds #> $epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds) <# Concatenate Request Details #> $requestVars = $httpVerb + $epoch + $data + $resourcePath <# Construct Signature #> $hmac = New-Object System.Security.Cryptography.HMACSHA256 $hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey) $signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars)) $signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-' $signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower())) <# Construct Headers #> $auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$auth) $headers.Add("Content-Type",'application/json') <# Make Request #> $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers <# Print status and body of response #> $status = $response.status $status $body = $response.data | ConvertTo-Json -Depth 5 $bodySolved6Views0likes1Comment