AI and VoIP Blog

VOIP | AI | Cloud | Kamailio | Open Source


Adding slack notifications to kamailio


Introduction

The current image has no alternative text. The file name is: image-1.png

Integrating Kamailio with Slack allows you to send notifications directly to a Slack channel whenever specific events occur in your VoIP infrastructure. This can be particularly useful for monitoring, alerting, or simply keeping your team informed. In this guide, we’ll walk you through the process of setting up a Slack webhook and configuring Kamailio to send messages to Slack using the Slack module.

Setting Up the Slack Webhook

To send messages to Slack, you first need to create a Slack webhook that will receive the messages. Follow the steps provided in the official Slack documentation here to generate your webhook URL.

Configuring Kamailio to Send Slack Notifications

Once you have the webhook URL, you can configure Kamailio to send messages to Slack. This is done using the Slack module, which requires the http_client module to function.

Step 1: Load the http_client Module

If you’re not already using the http_client module in Kamailio, add these lines to your configuration:

# ----- http_client
loadmodule "http_client.so"
modparam("http_client", "connection_timeout", 1)

Step 2: Load the Slack Module

Next, load the Slack module and configure it with your webhook URL and channel name:

# ----- slack
loadmodule "slack.so"
modparam("slack", "slack_url", "<webhook>")
modparam("slack", "channel", "<slack_channel_name>")

Note: The slack channel name must be added with a # like “#monitoring”.

Step 3: Sending Slack Notifications

You can now use the slack_send function to send messages to Slack. Here’s an example:

slack_send("message");

Example: Blocking and Unblocking IP Addresses with Notifications

In this example, we’ll configure Kamailio to block IP addresses that trigger a Pike check and automatically unblock them after a set period. Additionally, we’ll send Slack notifications whenever an IP is blocked or unblocked. More about this code here.

loadmodule "http_client.so"
loadmodule "htable.so"
loadmodule "pike.so"
... 
modparam("htable", "htable", "ipban=>size=8;autoexpire=600;")
... 
if (!pike_check_req()) {
  xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
slack_send("ALERT: Blocking IP $si due to suspicious activity.");
  $sht(ipban=>$si) = 1;
  http_client_query("http://localhost:8082/addip/$si", "$var(apinfo)");
  exit;
}
... 
event_route[htable:expired:ipban] {
  xlog("mytable record expired $shtrecord(key) => $shtrecord(value)\n");
slack_send("INFO: Unblocking IP $shtrecord(key) after block period.");
  http_client_query("http://localhost:8082/removeip/$shtrecord(key)", "$var(apinfo)");

Join 753 other subscribers

Leave a comment

Akash Gupta
Senior VoIP Engineer and AI Enthusiast



Discover more from AI and VoIP Blog

Subscribe to get the latest posts sent to your email.



Leave a comment