# Watch another folder on your devices

> Add a folder of your own to file integrity for every device in the realm, through the shared configuration, and see a change in it reported.

Your billing service keeps its settings in `/opt/billing/config` on each server, and a change there that nobody
recorded is something you want to hear about. File integrity watches the system's own folders out of the box; you
add yours once, in the configuration every sensor in the realm shares, and each device picks it up by itself.

## Before you start

- You are an administrator of the realm, in a group holding `RealmOwner`, with your session in `$CVYUH_SESSION`
  ([Call the API](../../../develop/api/)), and `jq` to edit the configuration.
- The realm has enrolled devices, with the folder on them (see [Device records](../../../endpoint-management/devices/device-records/)).
  A device starts watching a folder in real time only if the folder is there when the change arrives.

## Add the folder

<Steps>

1. **Read the shared configuration**, and keep it as it is, to put back later if you need to:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/endpoint/dar/v1/group/configuration \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" > held.json
   ```

   It holds the configuration's text, `xml`, and the `revision` you read it at.

2. **Add a block for the folder** after what's there. The text can hold any number of `<agent_config>` blocks, so
   what was there stays as it was:

   ```sh
   jq --arg dir /opt/billing/config '{revision, xml: (.xml + "\n<agent_config>\n  <syscheck>\n    <directories realtime=\"yes\" check_all=\"yes\">" + $dir + "</directories>\n  </syscheck>\n</agent_config>\n")}' \
     held.json > written.json
   ```

   `realtime="yes"` reports a change as it happens, and `check_all="yes"` records every checksum, the owner and the
   permissions. Every setting you can use is in the [Configuration reference](../../sensors/configuration-reference/).

3. **Write it back**, with the revision you read:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/endpoint/dar/v1/group/configuration \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d @written.json
   ```

   The answer is the configuration's new `revision`. It goes out to every device in the realm over each device's
   own connection, and each sensor reloads with it, usually within two minutes. A device that's offline gets it when
   it reconnects.

</Steps>

<Aside type="caution" title="If the write is refused">
  A configuration that would run a command on your devices, or holds a setting the sensor doesn't take, is refused
  with `400` and the reason before it goes anywhere. If someone changed the configuration since you read it, the
  write is refused with `412`: read it again and redo your change.
</Aside>

## Check it worked

Ask one device's sensor (by its sensor id, from the sensors list), which folders its file monitor watches now:

```sh
curl https://foobar.cvyuh.com/api/v0/endpoint/dar/v1/agents/<sensor id>/configuration/files \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

`files.directories` lists `/opt/billing/config` once the device has reloaded. If it doesn't after two minutes, restart
the sensor ([Sensor actions](../../sensors/sensor-actions/)); if the folder didn't exist when the sensor started,
create it first. Change a file in the folder on that
device, and the change appears in the device's file change alerts, as
[Security events](../../security-log/events/) with `preset=fim`.

## Undo it

Read the configuration again for its new revision, and write back the text you kept in `held.json` with it:

```sh
curl https://foobar.cvyuh.com/api/v0/endpoint/dar/v1/group/configuration \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" > now.json
jq --slurpfile was held.json '{revision, xml: $was[0].xml}' now.json > undone.json
curl -X PUT https://foobar.cvyuh.com/api/v0/endpoint/dar/v1/group/configuration \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d @undone.json
```

## Related

- [Group configuration](../../sensors/group-configuration/): what the shared configuration is, and its limits.
- [File integrity](../../detection/file-integrity/): what the sensor watches out of the box, and how changes show.
- [Sensor actions](../../sensors/sensor-actions/): restart sensors, if one doesn't pick the change up.
