Send alerts using data in InfluxDB
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:
Alerting process
- Use an external runtime and InfluxDB client to query data from InfluxDB.
- Use the queried data and tools available in your runtime to send alerts.
- Create a Slack app
- Install dependencies
- Create an InfluxDB client
- Create a Slack client
- Query InfluxDB
- Send alerts
- Full alerting script
Create a Slack app
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.
Install dependencies
This guide assumes you have already setup your Python project and virtual environment.
Use pip to install the following dependencies:
influxdb_client_3pandasslack_sdk
pip install influxdb3-python pandas slack_sdkCreate an InfluxDB client
Use the InfluxDBClient3 function in the influxdb_client_3 module to
instantiate an InfluxDB client.
Provide the following credentials:
- host: InfluxDB Cloud Serverless region URL (without the protocol)
- org: InfluxDB organization name
- token: InfluxDB API token with read permissions on the bucket you want to query
- database: InfluxDB bucket name
from influxdb_client_3 import InfluxDBClient3
import pandas
# Instantiate an InfluxDBClient3 client configured for your bucket
influxdb = InfluxDBClient3(
host='cloud2.influxdata.com',
org='ORG_NAME',
token='API_TOKEN',
database='BUCKET_NAME'
)Create a Slack client
Import the
WebClientfunction from theslack.sdkmodule and theSlackApiErrorfunction from theslack_sdk.errorsmodule.Use the
WebClientfunction to instantiate a Slack client. Provide the following credentials:- token: Slack bot token
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
slack = WebClient(token='SLACK_BOT_TOKEN')Query InfluxDB
Define either a SQL or InfluxQL query to retrieve data to alert on. Depending on what data you want to alert on, you can:
- Include logic in the query so it only returns results that should be alerted on.
- Query data necessary for further processing and then send alerts based on processing performed in your runtime.
The example query below only returns values above a threshold that should trigger alerts.
SELECT
selector_last(co, time)['time'] AS time,
selector_last(co, time)['value'] AS co,
room
FROM home
WHERE co > 10
GROUP BY roomSELECT
LAST(co) AS co,
room
FROM home
WHERE co > 10
GROUP BY roomExecute the query
Assign the query string to a variable.
Use the
querymethod of your instantiated client to query raw data from InfluxDB. Provide the following arguments.- query: Query string to execute
- language:
sqlorinfluxql
Use the
to_pandasmethod to convert the returned Arrow table to a Pandas DataFrame.
# ...
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()# ...
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()Send alerts
Iterate through the DataFrame and send an alert to Slack for each row.
Use the
reset_indexfunction on the data frame to ensure indexes align with the number of rows in the DataFrame.Iterate through each row and use the
chat_postMessagemethod of your Slack client to send a message (per row) to Slack. Provide the following arguments:- channel: Slack channel to send the alert to.
- text: Message text to send. Use string interpolation to insert column values from each row into the message text.
# ...
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}'
)Full alerting script
from influxdb_client_3 import InfluxDBClient3
import pandas
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
influxdb = InfluxDBClient3(
host='cloud2.influxdata.com',
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}'
)from influxdb_client_3 import InfluxDBClient3
import pandas
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
influxdb = InfluxDBClient3(
host='cloud2.influxdata.com',
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}'
)Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB Cloud Serverless and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.