content/influxdb3/cloud-serverless/process-data/send-alerts.md
Query, analyze, and send alerts using time series data stored in InfluxDB.
This guide uses Python, the InfluxDB 3 Python client library, and the Python Slack SDK to demonstrate how to query data from InfluxDB and send alerts to Slack, but you can use your runtime and alerting platform of choice with any of the available InfluxDB 3 client libraries. Whatever clients and platforms you choose the use, the process is the same:
To send alerts to Slack, first create a Slack app and gather the required connection credentials to interact with your app. More information is provided in the Slack basic app setup documentation.
[!Note] This guide assumes you have already setup your Python project and virtual environment.
Use pip to install the following dependencies:
influxdb_client_3pandasslack_sdkpip install influxdb3-python pandas slack_sdk
Use the InfluxDBClient3 function in the influxdb_client_3 module to
instantiate an InfluxDB client.
Provide the following credentials:
{{% code-placeholders "(API|BUCKET|ORG)_(NAME|TOKEN)" %}}
from influxdb_client_3 import InfluxDBClient3
import pandas
# Instantiate an InfluxDBClient3 client configured for your bucket
influxdb = InfluxDBClient3(
host='{{< influxdb/host >}}',
org='ORG_NAME',
token='API_TOKEN',
database='BUCKET_NAME'
)
{{% /code-placeholders %}}
Import the WebClient function from the slack.sdk module and the SlackApiError
function from the slack_sdk.errors module.
Use the WebClient function to instantiate a Slack client.
Provide the following credentials:
{{% code-placeholders "SLACK_BOT_TOKEN" %}}
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
slack = WebClient(token='SLACK_BOT_TOKEN')
{{% /code-placeholders %}}
Define either a SQL or InfluxQL query to retrieve data to alert on. Depending on what data you want to alert on, you can:
The example query below only returns values above a threshold that should trigger alerts.
{{< code-tabs-wrapper >}} {{% code-tabs %}} SQL InfluxQL {{% /code-tabs %}}
<!--------------------------------- BEGIN SQL --------------------------------->{{% code-tab-content %}}
SELECT
selector_last(co, time)['time'] AS time,
selector_last(co, time)['value'] AS co,
room
FROM home
WHERE co > 10
GROUP BY room
{{% /code-tab-content %}}
<!---------------------------------- END SQL ----------------------------------> <!------------------------------- BEGIN INFLUXQL ------------------------------>{{% code-tab-content %}}
SELECT
LAST(co) AS co,
room
FROM home
WHERE co > 10
GROUP BY room
{{% /code-tab-content %}}
<!-------------------------------- END INFLUXQL ------------------------------->{{< /code-tabs-wrapper >}}
Assign the query string to a variable.
Use the query method of your instantiated client
to query raw data from InfluxDB. Provide the following arguments.
sql or influxqlUse the to_pandas method to convert the returned Arrow table to a Pandas DataFrame.
{{< code-tabs-wrapper >}} {{% code-tabs %}} SQL InfluxQL {{% /code-tabs %}}
<!--------------------------------- BEGIN SQL --------------------------------->{{% code-tab-content %}}
# ...
query = '''
SELECT
selector_last(co, time)['time'] AS time,
selector_last(co, time)['value'] AS co,
room
FROM home
WHERE co > 10
GROUP BY room
'''
table = influxdb_raw.query(query=query, language="sql")
data_frame = table.to_pandas()
{{% /code-tab-content %}}
<!---------------------------------- END SQL ----------------------------------> <!------------------------------- BEGIN INFLUXQL ------------------------------>{{% code-tab-content %}}
# ...
query = '''
SELECT
LAST(co) AS co,
room
FROM home
WHERE co > 10
GROUP BY room
'''
table = influxdb_raw.query(query=query, language="influxql")
data_frame = table.to_pandas()
{{% /code-tab-content %}}
<!-------------------------------- END INFLUXQL ------------------------------->{{< /code-tabs-wrapper >}}
Iterate through the DataFrame and send an alert to Slack for each row.
Use the reset_index function on the data frame to ensure indexes align
with the number of rows in the DataFrame.
Iterate through each row and use the chat_postMessage method of your
Slack client to send a message (per row) to Slack.
Provide the following arguments:
{{% code-placeholders "SLACK_CHANNEL" %}}
# ...
data_frame = data_frame.reset_index()
for index, row in data_frame.iterrows():
slack.chat_postMessage(
channel="#SLACK_CHANNEL",
text=f'Carbon monoxide (co) high in {row.room}: {row.co} ppm at {row.time}'
)
{{% /code-placeholders %}}
{{< code-tabs-wrapper >}} {{% code-tabs %}} SQL InfluxQL {{% /code-tabs %}}
<!--------------------------------- BEGIN SQL --------------------------------->{{% code-tab-content %}}
{{% code-placeholders "(API|BUCKET|ORG|SLACK(BOT)*)(NAME|TOKEN|CHANNEL)" %}}
from influxdb_client_3 import InfluxDBClient3
import pandas
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
influxdb = InfluxDBClient3(
host='{{< influxdb/host >}}',
org='ORG_NAME',
token='API_TOKEN',
database='BUCKET_NAME'
)
slack = WebClient(token='SLACK_BOT_TOKEN')
query = '''
SELECT
selector_last(co, time)['time'] AS time,
selector_last(co, time)['value'] AS co,
room
FROM home
WHERE co > 10
GROUP BY room
'''
table = influxdb_raw.query(query=query, language="sql")
data_frame = table.to_pandas()
data_frame = data_frame.reset_index()
for index, row in data_frame.iterrows():
slack.chat_postMessage(
channel="#SLACK_CHANNEL",
text=f'Carbon monoxide (co) high in {row.room}: {row.co} ppm at {row.time}'
)
{{% /code-placeholders %}}
{{% /code-tab-content %}}
<!---------------------------------- END SQL ----------------------------------> <!------------------------------- BEGIN INFLUXQL ------------------------------>{{% code-tab-content %}}
{{% code-placeholders "(API|BUCKET|ORG|SLACK(BOT)*)(NAME|TOKEN|CHANNEL)" %}}
from influxdb_client_3 import InfluxDBClient3
import pandas
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
influxdb = InfluxDBClient3(
host='{{< influxdb/host >}}',
org='ORG_NAME',
token='API_TOKEN',
database='BUCKET_NAME'
)
slack = WebClient(token='SLACK_BOT_TOKEN')
query = '''
SELECT
LAST(co) AS co,
room
FROM home
WHERE co > 10
GROUP BY room
'''
table = influxdb_raw.query(query=query, language="influxql")
data_frame = table.to_pandas()
data_frame = data_frame.reset_index()
for index, row in data_frame.iterrows():
slack.chat_postMessage(
channel="#SLACK_CHANNEL",
text=f'Carbon monoxide (co) high in {row.room}: {row.co} ppm at {row.time}'
)
{{% /code-placeholders %}}
{{% /code-tab-content %}}
<!-------------------------------- END INFLUXQL ------------------------------->{{< /code-tabs-wrapper >}}