# Importing Alerts

## Using Kloudfuse Customer Scripts

Kloudfuse provides Python scripts to manage alerts and dashboards in Grafana, to enable you to upload, download, and delete alerts and dashboards in specified folders within Grafana.

This document is your guide to efficiently manage Grafana alerts and dashboards using the `alert.py` and `dashboard.py` scripts.

### Prerequisites

- Python 3.x  
- Required Python packages: `requests`, `loguru`, `argparse` (use the requirements.txt file to install all the packages: `pip3 install -r requirements.txt`)
- Access to a Grafana instance with API authentication
- The Kloudfuse scripts can be found [Kloudfuse Shared Customer Repo](https://github.com/kloudfuse-ext/customer)

### Common Arguments

These arguments apply to both alert and dashboard scripts:

`-f, --folder-name`  
Grafana folder name

`-a, --grafana-address`  
Grafana server URL

`-u, --grafana-username`  
Grafana username (default: `admin`)

`-p, --grafana-password`  
Grafana password (default: `password`)

`-v, --verify-ssl`  
Disable SSL verification (default: enabled). Use this flag to skip SSL verification when connecting to Grafana.

|     |     |
| --- | --- |
|  | - Replace `<grafana-instance>` with the actual Grafana server URL.<br>  <br>- Ensure that the API credentials have the necessary permissions to manage alerts and dashboards.<br>  <br>- The `-f` flag is required. It is a placeholder in some multi-directory operations. |

### Manage Alerts

To migrate (download and upload) or delete existing **alerts**, use the `alert.py` file, hosted for you in our `customer/blob/main/scripts/assets/` directory.

#### Upload Alerts

Upload alerts to a specific Grafana folder.

Upload a Single Alert

```bash
python alert.py upload -s /path/to/alert.json \
    -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```

Upload Multiple Alerts from a Directory

```bash
python alert.py upload -d /path/to/alerts/directory \
    -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```

Upload Alerts from Multiple Directories

```bash
python alert.py upload -m /path/to/root_directory \
    -a https://<grafana-instance>/grafana \
    -u admin -p password \
    -f "placeholder"
```

#### Download Alerts

Retrieve alerts from Grafana, and save them as JSON files.

Download a Single Alert

```bash
python alert.py download -s "Alert Name" -o /path/to/alert.json \
    -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```

Download All Alerts from a Folder

```bash
python alert.py download -d -o /path/to/alerts/download/ \
    -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```

Download Alerts from All Folders

```bash
python alert.py download -m -o /path/to/alerts/download/ \
    -a https://<grafana-instance>/grafana \
    -u admin -p password \
    -f "placeholder"
```

#### Delete Alerts

Remove alerts from Grafana.

Delete a Single Alert

```bash
python alert.py delete -s "Alert Name" \
    -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```

Delete All Alerts in a Folder

```bash
python alert.py delete -d -f "My Alert Folder" \
    -a https://<grafana-instance>/grafana \
    -u admin -p password
```
