> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/sonar/reference/script-variables.md).

# Script variables

The placeholder variables SONAR replaces in script arguments, environment variables, task commands, alert actions and web hooks: device, client, site, custom field, key store and alert values.

## What this is and when to use it

A script variable is a placeholder in double braces, such as `{{agent.hostname}}`, that SONAR replaces with a value from the database just before the script runs on a device. The same script can then receive, for each device, its own hostname, its client's name, a custom field value or a secret kept in the key store, without editing the script.

Variables are resolved on the SONAR server, for the device that runs the script. The script body itself is sent unchanged, except for [snippets](https://wiki.datadike.com/sonar/administration/automation/scripts.md), which use the same braces but insert code.

## Where variables work

| Place                                       | Fields                                                                                                                                                                                               |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Run Script** on a device                  | **Script Arguments**, **Environment vars**                                                                                                                                                           |
| **Tools › Bulk Script**                     | **Script Arguments**, **Environment vars**, resolved separately for each device                                                                                                                      |
| Script library defaults and **Test Script** | **Script Arguments**, **Environment vars**                                                                                                                                                           |
| Automated tasks                             | Script actions: **Script Arguments**, **Environment vars**. Command actions: the **Command** line                                                                                                    |
| Script checks                               | **Policy Arguments**, **Environment vars**                                                                                                                                                           |
| Alert templates                             | **Failure script arguments**, **Resolved script arguments**, **Failure script environment vars**. Alert variables are also available here                                                            |
| URL actions and web hooks                   | URL, body and headers. See [URL actions](https://wiki.datadike.com/sonar/administration/settings/url-actions.md) and [Webhooks](https://wiki.datadike.com/sonar/administration/settings/webhooks.md) |

## Syntax

* Write the object, a dot and the property: `{{agent.hostname}}`, `{{client.name}}`, `{{global.BackupToken}}`. Spaces just inside the braces are ignored.
* Use **one variable per argument** and one per environment variable value (alert actions and web hooks accept several). Text around the variable is kept: `Computer={{agent.hostname}}` becomes `Computer='WS-0001'` in PowerShell (see quoting below).
* Environment variables use `KEY=VALUE`, with the variable in the value: `SITE_NAME={{site.name}}`.
* In script arguments, task commands and alert actions, text values are wrapped in single quotes, so a value with spaces reaches the script as one argument. Batch scripts and **CMD** commands receive the value without quotes. In PowerShell, single quotes inside the value are doubled. Environment variable values are not quoted, except in alert actions, and web hook fields are never quoted.
* Values that are lists, such as a multiple-choice custom field, are joined with commas: `'A,B,C'`.
* Yes/no values, such as a checkbox custom field, become `$True` or `$False` in PowerShell and `1` or `0` in the other shells.
* If a variable cannot be resolved (misspelled, or the custom field or key does not exist), the argument is passed unchanged, braces included.

## Device variables

These read the device that runs the script. The values come from the device's latest inventory.

| Variable                        | Value                                                             |
| ------------------------------- | ----------------------------------------------------------------- |
| `{{agent.hostname}}`            | Device name, for example `WS-0001`                                |
| `{{agent.description}}`         | The device's **Description**                                      |
| `{{agent.operating_system}}`    | Operating system name and version                                 |
| `{{agent.plat}}`                | Platform: `windows`, `linux` or `darwin`                          |
| `{{agent.goarch}}`              | Architecture: `amd64`, `386`, `arm64` or `arm`                    |
| `{{agent.version}}`             | Agent version                                                     |
| `{{agent.monitoring_type}}`     | `server` or `workstation`                                         |
| `{{agent.timezone}}`            | The device's time zone, or the default time zone when none is set |
| `{{agent.public_ip}}`           | Public IP address last reported                                   |
| `{{agent.local_ips}}`           | Local IPv4 addresses, comma-separated                             |
| `{{agent.logged_in_username}}`  | User logged in when the device last reported                      |
| `{{agent.last_logged_in_user}}` | Last user who logged in                                           |
| `{{agent.total_ram}}`           | Total memory, in GB                                               |
| `{{agent.boot_time}}`           | Last boot time, as a Unix timestamp                               |
| `{{agent.needs_reboot}}`        | Whether a reboot is pending (yes/no value)                        |
| `{{agent.make_model}}`          | Manufacturer and model                                            |
| `{{agent.serial_number}}`       | Serial number                                                     |
| `{{agent.cpu_model}}`           | Processor description (list)                                      |
| `{{agent.graphics}}`            | Graphics adapters                                                 |
| `{{agent.physical_disks}}`      | Physical disks (list)                                             |

## Client and site variables

| Variable          | Value                       |
| ----------------- | --------------------------- |
| `{{client.name}}` | Name of the device's client |
| `{{site.name}}`   | Name of the device's site   |

## Custom field variables

Custom fields created in **Admin Center › Settings › Custom Fields** are read by their name, after the object they belong to:

| Variable                  | Value                                                     |
| ------------------------- | --------------------------------------------------------- |
| `{{agent.<Field Name>}}`  | The device's value of a device custom field               |
| `{{site.<Field Name>}}`   | The value of a site custom field on the device's site     |
| `{{client.<Field Name>}}` | The value of a client custom field on the device's client |

The name is written exactly as defined, spaces included: `{{client.Backup Server}}`. When the record has no value, the field's default value is used. A custom field with the same name as a device property above takes precedence over the property. See [Custom fields](https://wiki.datadike.com/sonar/administration/settings/custom-fields.md).

## Key store variables

| Variable                | Value                                                                       |
| ----------------------- | --------------------------------------------------------------------------- |
| `{{global.<Key Name>}}` | The value stored under that name in **Admin Center › Settings › Key Store** |

Use the key store for values that must not be written in scripts, such as service passwords or API tokens, and pass them as arguments or environment variables. See [Key store](https://wiki.datadike.com/sonar/administration/settings/key-store.md).

## Alert variables

In the actions of an [alert template](https://wiki.datadike.com/sonar/administration/automation/alerts.md) and in web hooks sent for an alert, the `alert` object is available as well. Device, client, site, custom field and key store variables resolve through the device that raised the alert.

| Variable                                 | Value                                                              |
| ---------------------------------------- | ------------------------------------------------------------------ |
| `{{alert.message}}`                      | Alert text, for example `WS-0001 in CLIENT-A, SITE-01 is overdue.` |
| `{{alert.severity}}`                     | `info`, `warning` or `error`                                       |
| `{{alert.alert_type}}`                   | `availability`, `check` or `task`                                  |
| `{{alert.alert_time}}`                   | When the alert was raised                                          |
| `{{alert.resolved_on}}`                  | When the alert was resolved (resolution actions)                   |
| `{{alert.get_result.retcode}}`           | Check and task alerts: return code of the last run                 |
| `{{alert.get_result.stdout}}`            | Check and task alerts: standard output of the last run             |
| `{{alert.get_result.stderr}}`            | Check and task alerts: standard error of the last run              |
| `{{alert.get_result.more_info}}`         | Check alerts: the check's latest detail                            |
| `{{alert.assigned_check.readable_desc}}` | Check alerts: the check description                                |
| `{{alert.assigned_task.name}}`           | Task alerts: the task name                                         |

In alert action arguments and environment variables, each value is wrapped in single quotes.

## Examples

| Field                         | Entry                                  | The script receives, on `WS-0001` of `CLIENT-A` |
| ----------------------------- | -------------------------------------- | ----------------------------------------------- |
| Script Arguments (PowerShell) | `-Computer {{agent.hostname}}`         | `-Computer 'WS-0001'`                           |
| Script Arguments (PowerShell) | `{{client.name}}`                      | `'CLIENT-A'`                                    |
| Script Arguments (Batch)      | `{{agent.hostname}}`                   | `WS-0001`                                       |
| Environment vars              | `CLIENT={{client.name}}`               | `CLIENT` set to `CLIENT-A`                      |
| Environment vars              | `API_TOKEN={{global.MonitoringToken}}` | `API_TOKEN` set to the key store value          |
| Task command (Powershell)     | `Write-Output {{site.name}}`           | `Write-Output 'SITE-01'`                        |

## Common errors

| Symptom                                               | Cause                                                                                               | Fix                                                                          |
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| The script receives `{{...}}` literally               | The object or property is misspelled, or the custom field or key does not exist                     | Check the name against the tables above, **Custom Fields** and **Key Store** |
| Two variables in one script argument are not replaced | Script arguments resolve one variable each                                                          | Split them into two arguments                                                |
| The script receives the value with quotes             | Text values are quoted in arguments                                                                 | Use the value as a string parameter, or pass it as an environment variable   |
| A variable in the script body is not replaced         | Variables work in arguments and environment variables; braces in the body are read as snippet names | Pass the value as an argument or environment variable                        |

## Related pages

* [Script library](https://wiki.datadike.com/sonar/administration/automation/scripts.md)
* [Run a command or a script on a device](https://wiki.datadike.com/sonar/administration/automation/run-commands-and-scripts.md)
* [Custom fields](https://wiki.datadike.com/sonar/administration/settings/custom-fields.md)
* [Key store](https://wiki.datadike.com/sonar/administration/settings/key-store.md)
* [Alerts and alert templates](https://wiki.datadike.com/sonar/administration/automation/alerts.md)
