Update Graylog dashboard widgets

Applies to: Graylog 3.0.1

What you will need:

  • old and new stream IDs
  • PowerShell Core (6+)

Manual way (for small updates)

Access your Graylog instance’s swagger UI, e.g.: https://log.example.com/api/api-browser/. Enter your credentials at the top, then under Dashboards : Manage dashboards open the “GET /dashboards” element.

A DashboadList object similar to the following will be returned (parts from the example below have been omitted):

{
"total": 3,
"dashboards": [
  {
    "id": "5d0c9b15d6889b1039d3427c",
    "title": "Customer App",
    "widgets": [
      {
        "creator_user_id": "john.doe",
        "cache_time": 10,
        "description": "ExampleService Image Errors",
        "id": "7f4c12a8-32be-4cac-afa7-e57dee2f269b",
        "type": "SEARCH_RESULT_CHART",
        "config": {
          "timerange": {
            "type": "relative",
            "range": 1209600
          },
          "interval": "day",
          "stream_id": "5baf54e1d6889b211ebd3231",
          "query": "source:example-prod-app1a AND _exists_:msg_error AND message: \"getting image for item\""
        }
      }
    ]
  }
]
}

Store that output as “$HOME/Downloads/dashboards.json”

Next open your trusty PowerShell and replace the stream IDs from the code below with your own and run it, it’ll print the information that you’ll need to update your widgets:

$json = cat $HOME/Downloads/dashboards.json | ConvertFrom-Json

$OldStreamID = "000000000000000000000001"
$NewStreamID = "5baf54e1d6889b211ebd3231"

$json.dashboards.foreach({
    $dashboard_id = $_.id
    foreach($widget in $_.widgets){
        $widget_id = $widget.id
        $updateObject = $widget | select-object -property cache_time,description,type,config

        if($updateObject.config.stream_id -eq $OldStreamID) {
            $updateObject.config.stream_id = $NewStreamID
            write-host "db_id: $dashboard_id"
            write-host "wi_id: $widget_id"
            write-host "$($updateObject | convertto-json)"
            write-host ""
        }
    }
})
db_id: 5d0c9b15d6889b1039d3427c
wi_id: 7f4c12a8-32be-4cac-afa7-e57dee2f269b
{
  "cache_time": 10,
  "description": "ExampleService Image Errors",
  "type": "SEARCH_RESULT_CHART",
  "config": {
    "timerange": {
      "type": "relative",
      "range": 1209600
    },
    "interval": "day",
    "stream_id": "5baf54e1d6889b211ebd3231",
    "query": "source:example-prod-app1a AND _exists_:msg_error AND message: \"getting image for item\""
  }
}

Next, in the section Dashboards/Widgets : Manage widgets of an existing dashboard unfold PUT /dashboards/{dashboardId}/widgets/{widgetId}.

For dashboardId enter the value of db_id and for widgetId that of wi_id. Paste the json into the text area titled JSON body. Finally, clicking the button Try it out! should return the following if you followed the instructions:

Response Body
  no content
Response Code
  204

Automated way (for many edits)

First create an access token: (see https://docs.graylog.org/en/3.0/pages/configuration/rest_api.html#creating-and-using-access-token) and then create a PowerShell credential object. The received access token can now be used as username in a request to the Graylog REST API using Basic Auth together with the literal password token.

# username: your actual token
# password: token
$cred = Get-Credential
$baseurl = "https://log.example.com/api"
$OldStreamID = "000000000000000000000001"
$NewStreamID = "5baf54e1d6889b211ebd3231"

$json = Invoke-RestMethod -Method Get -Uri "$Baseurl/dashboards" -Credential $Cred

$json.dashboards.foreach({
    $dashboard_id = $_.id
    foreach($widget in $_.widgets){
        $widget_id = $widget.id
        $updateObject = $widget | select-object -property cache_time,description,type,config

        if($updateObject.config.stream_id -eq $OldStreamID) {
            $updateObject.config.stream_id = $NewStreamID
            try {
                Invoke-RestMethod @{
                    Method = "PUT"
                    Uri = "$Baseurl/dashboards/$dashboard_id/widgets/$widget_id"
                    Credential = $Cred
                    Body = "$($updateObject | ConvertTo-Json)"
                } -ErrorAction Stop -ErrorVariable UpdateError
            }
            catch {
                $ErrorList += $UpdateError
            }
        }
    }
})