# Agent

<https://hub.docker.com/repository/docker/datasentinel/datasentinel-agent/general>

{% hint style="info" %}
A single agent has the capability to monitor multiple PostgreSQL clusters
{% endhint %}

### Howto

{% hint style="warning" %}
Because the agent registers the server name and listening port to the platform, you need to expose them externally and pass the values when running a new agent.
{% endhint %}

This allows the platform to communicate with the agent.

```bash
// download, install and run the agent

export DATASENTINEL_AGENT_HOST=172.16.250.199
export DATASENTINEL_AGENT_PORT=8383

docker run -d -p $DATASENTINEL_AGENT_PORT:$DATASENTINEL_AGENT_PORT -e DATASENTINEL_AGENT_HOST=$DATASENTINEL_AGENT_HOST -e DATASENTINEL_AGENT_PORT=$DATASENTINEL_AGENT_PORT datasentinel/datasentinel-agent
```

{% hint style="info" %}
You can use the [agent CLI](https://docs.datasentinel.io/manual/implementation/agent-usage/cli) by attaching a shell (`docker exec -it <mycontainer> bash`)
{% endhint %}

```bash
// Example agent CLI command
/agent/datasentinel/datasentinel status agent
```

Subsequently, you can use the [API](https://docs.datasentinel.io/manual/implementation/agent-usage/api-reference) to configure the agent. For detailed information, please refer to the Agent API documentation.\
\
Configuration files are stored within the volume **/agent/config**

```bash
// Check agent status
curl -k https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/agent/status
```

Update token with valid license

```bash
export TOKEN="Valid license key"

cat <<EOF >body.json
{
    "value" : "$TOKEN"
}
EOF

curl -k --header 'Content-Type: application/json' --request PUT "https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/server/token" -d @body.json
```

Set Datasentinel upload platform

```bash
cat <<EOF >upload_server.json
{
  "host": "datasentinel_server",
  "port": 443
}
EOF

curl -k --header "api-token: $TOKEN" --header 'Content-Type: application/json' -X PUT https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/server -d @upload_server.json
# Check upload server
curl -k -X GET https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/server
```

Test the communication between the agent and the platform

```bash
curl -k --header "api-token: $TOKEN" --request POST https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/server/test-upload
```

Add a PostgreSQL connection

```bash
cat > body.json <<EOF
{
  "host": "172.16.250.199",
  "port": 5432,
  "user": "datasentinel",
  "password": "myPassword",
  "tags": "application=application_name,environment=docker,datacenter=datacenter"
}
EOF

curl -k --header "api-token: $TOKEN" --header 'Content-Type: application/json' --request POST "https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/connections/pg_docker_example" -d @body.json
```

Check connection

```bash
curl -k --header "api-token: $TOKEN" -X GET https://${DATASENTINEL_AGENT_HOST}:${DATASENTINEL_AGENT_PORT}/api/connections
```

### Dockerfile

{% @github-files/github-code-block url="<https://github.com/datasentinel/datasentinel_toolkit/blob/master/docker/agent/Dockerfile>" %}

### Docker-compose

```bash
wget https://raw.githubusercontent.com/datasentinel/datasentinel_toolkit/master/docker/agent/docker-compose.yml
docker-compose up -d
```

{% @github-files/github-code-block url="<https://github.com/datasentinel/datasentinel_toolkit/blob/master/docker/agent/docker-compose.yml>" %}
