Configure XBMC / Kodi Notifications on Linux

kodi-notification

There are many reasons to set up notifications on certain events such as SMART error. Kodi / XBMC has a simple notification system that can be invoked programmatically or via a HTTP request. Before notifications can be received you will first need to install Kodi on Arch Linux and know the IP address and port on which it Kodi is reachable. It would be helpful to configure static IP on Arch Linux or setup static IP on Ubuntu Linux since then the IP would stay constant.

 

Enable XBMC / Kodi Webserver

By default the Webserver is disabled. To enable it navigate to Settings -> Services -> Webserver and select Allow control of Kodi via HTTP. The port is also displayed below and can be modified there as well. The username and password are both optional.

 

Build XBMC / Kodi Notification URL

The simplest way to issue a notification is probably via a HTTP URL. This can be issued via either GET or POST methods. For simplicity the GET method is preferred in this case as it allows issuing of notifications directly from a browser.

 

Test a sample URL in a browser.

http://192.168.0.100:8080/jsonrpc?request={“jsonrpc”:”2.0″,”method”:”GUI.ShowNotification”,”params”:{“title”:”Sample Title”,”message”:”Sample Message”,”image”:””,”displaytime”:5000},”id”:1}

The IP address and port should be replaced with values specific to your configuration. Apart from notifications other functions can be executed by changing the method to an API call other than GUI.ShowNotification. Parameters for a given function call are passed with params object. Required arguments for a notification function call are the title and the message. Optional arguments are the image and displaytime. The image can be specified as an absolute or relative local path or a URL. How long the notification appears on screen can be modified from the default by specifying time in milliseconds as a value for displaytime key. Final object is required and consists of the id key and any integer such as 1 as it’s value.

 

URL Encoding

The URL needs to be encoded before it is submitted. In most modern browsers this should happen automatically but if the URL request is made programmatically it must be encoded prior to submission. Most programming languages will have a function dedicated to URL encoding but it can sometimes be simpler to do it manually by replacing certain reserved characters with their respective percent encoded values. For example spaces are replaced with %20 while double quotes are replaced by %22.

 

Use encoded URL if unencoded URL does not work.

http://192.168.0.100:8080/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22GUI.ShowNotification%22,%22params%22:{%22title%22:%22Sample%20Title%22,%22message%22:%22Sample%20Message%22,%22image%22:%22%22,%22displaytime%22:5000},%22id%22:1}

 

Smartd XBMC / Kodi Notification Script

Before proceeding make sure to configure automated SMART checks on Arch Linux.

 

Create email notification script.

sudo nano /etc/smartmontools/run.d/kodi

 

Copy the script modifying variable values as needed.

#! /bin/bash

LOCAL_HOST=$(ip addr show | awk '/inet / {print $2}' | cut -d/ -f1 | awk 'NR==2{print $1}')
KODI_HOSTS=("127.0.0.1")
KODI_PORT="8080"
KODI_IMAGE="warning"
KODI_DISPLAYTIME=10000

for KODI_HOST in "${KODI_HOSTS[@]}"
do
   KODI_TITLE="Smartd error detected"
   KODI_MESSAGE="$SMARTD_FAILTYPE Error on $SMARTD_DEVICE."
   if [ "$KODI_HOST" == "$LOCAL_HOST" ] ||
      [ "$KODI_HOST" == "127.0.0.1" ] ||
      [ "$KODI_HOST" == "localhost" ]
   then
      KODI_TITLE="$KODI_TITLE!"
   else
      KODI_TITLE="$KODI_TITLE on $LOCAL_HOST!"
   fi
   
   curl -fs -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"'"$KODI_TITLE"'","message":"'"$KODI_MESSAGE"'","image":"'"$KODI_IMAGE"'","displaytime":'"$KODI_DISPLAYTIME"'},"id":1}' http://$KODI_HOST:$KODI_PORT/jsonrpc
   
   sleep $(( KODI_DISPLAYTIME / 1000 ) / 5)
done

The local IP address of the machine running smartd is stored in $LOCAL_HOST variable. The IP addresses of Kodi hosts that are to be notified are defined in $KODI_HOSTS variable array. Kodi port is assumed to be the same for all hosts and is located in $KODI_PORT variable. Notification title and message are stored in $KODI_TITLE and $KODI_MESSAGE variables respectively. Optionally notification image can be defined with $KODI_IMAGE. Time the notification will appear for is set in milliseconds with $KODI_DISPLAYTIME variable.

Type of error smartd has detected is saved in $SMARTD_FAILTYPE variable. Device node of the device with an error is saved in $SMARTD_DEVICE variable while $SMARTD_DEVICESTRING variable also appends the device type.

 

Set execute permissions on the script.

sudo chmod +x /etc/smartmontools/run.d/kodi

 

Quick Reference:

Install Kodi on Arch Linux

Configure static IP on Arch Linux or setup static IP on Ubuntu Linux

Configure automated SMART checks on Arch Linux

sudo nano /etc/smartmontools/run.d/kodi
sudo chmod +x /etc/smartmontools/run.d/kodi

dom