Tuesday, April 21, 2020

Using powershell with UptimeRobots API

I got a chance to test Uptimerobots API with Powershell and I used it with a csv-list of urls with keywords to update my uptimerobot account.
Pretty good stuff where you also can output all your monitors with uptimelogs directly to PowerBI or equivalent.

Code:
$APIKeyWrite = "<insertYourWriteAPIKeyHere" #API Write Key from uptimerobot
$scriptPath = split-path $MyInvocation.MyCommand.Definition -Parent
$csvPath = "$($scriptPath)\MonitorList.csv"
$monitorList = import-csv -Path $csvPath -Delimiter ";"
$APIUrl = "https://api.uptimerobot.com/v2/"
$contentType = "application/x-www-form-urlencoded"
$headers = @{"cache-control"="no-cache"}
$getMonitorParams = @{"api_key"=$APIKeyWrite;"format"="json";}
$existingMonitors = ((Invoke-WebRequest -Uri $($apiUrl+"getMonitors") -Method POST -Headers $headers -body $getMonitorParams).content |convertFrom-json)|select -ExpandProperty monitors |select friendly_name,url,keyword_value,id
foreach ($monitor in $monitorList) {
Remove-Variable currentMonitor -ErrorAction SilentlyContinue
$currentMonitor = $existingMonitors | where-object {$_.url -like "https://$($monitor.url)"}
if ($existingMOnitors.url -notcontains "https://$($monitor.url)") {
write-output "Doesn't exists, creating $($monitor.url)..."
$params = @{"api_key"=$APIKeyWrite;"format"="json";"type"="2";"url"="https://$($monitor.url)";"friendly_name"="$($monitor.friendlyName)";"keyword_type"="2";"keyword_value"="$($monitor.keyword)"}
$contentWrite = (Invoke-WebRequest -Uri $($apiUrl+"newMonitor") -Method POST -Headers $headers -body $params).content |convertFrom-json
$contentWrite
}
else {
if ($currentMonitor.keyword_value -notlike $monitor.Keyword) {
write-warning "$($monitor.url): Updating keyword..."
$editParams = @{"api_key"=$APIKeyWrite;"format"="json";"type"="2";"id"=$currentMonitor.id;"keyword_value"="$($monitor.keyword)"}
$contentWrite = (Invoke-WebRequest -Uri $($apiUrl+"editMonitor") -Method POST -Headers $headers -body $editParams).content |convertFrom-json
$contentWrite
}
else {
write-output "$($monitor.url) settings is matched. Skipping..."
}
}
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
url;friendlyName;Keyword
site1.company.com;site1;Hello
site2.company.com;site2;Hello
view raw monitorList.csv hosted with ❤ by GitHub


To get UptimeRobot status from PowerBI the following queries can be used:

Example 1 - Simple query

let
   body = Text.ToBinary("api_key=#myAPIKey#&format=json&logs=1"),
   Options = [
   Headers=[#"Content-type"="application/x-www-form-urlencoded", #"cache-control"="no-cache"],
   Content=body
   ],
   result = Web.Contents(url, Options)
in
    result


Example 2 - Query using records


let
   content= [
      #"api_key"="#myAPIKey#",
      #"format" = "json",
      #"logs" = "1"
   ],
   query = Text.ToBinary(Uri.BuildQueryString(content)),
   Options = [
   Headers=[
      #"Content-type"="application/x-www-form-urlencoded",
      #"cache-control"="no-cache"],
      Content=query
   ],
   result = Web.Contents(Url, Options)
in
    result
 
Example 3 - Query using json
 
let
   content = "{
  ""api_key"": ""#myAPIKey#"",
  ""format"": ""json"",
      ""logs"": ""1""        
    }",
   query = Text.ToBinary(Uri.BuildQueryString(Json.Document(content))),
   Options = [
   Headers=[
      #"Content-type"="application/x-www-form-urlencoded",
      #"cache-control"="no-cache"],
      Content=query
   ],
   result = Web.Contents(Url, Options)  
in
    result

No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...