Skip to content

Instantly share code, notes, and snippets.

@te-online
Last active January 4, 2024 15:27
Show Gist options
  • Save te-online/37fc034c28fff38963b6cdbead5e8622 to your computer and use it in GitHub Desktop.
Save te-online/37fc034c28fff38963b6cdbead5e8622 to your computer and use it in GitHub Desktop.
How to make smartmontools send errors to you via Telegram messenger.

###Step by step

Installing smartmontools

  1. Install smartmontools with sudo apt-get install smartmontools

Create your new bot

  1. Create a new telegram bot via the botfather (see https://core.telegram.org/bots#3-how-do-i-create-a-bot). You will need the token of the bot from botfather.
  2. Initiate a chat with your new bot.
  3. Send a GET to https://api.telegram.org/bot{YOUR_TOKEN}/getUpdates. You will need the chat id of your chat with the bot.

Configure smartd to use Telegram

  1. On your machine open the file /etc/default/smartmontools and uncomment the line start_smartd=yes.
  2. In the file /etc/smartd.conf use the first example that might be uncommented already and change it to DEVICESCAN -d removable -n standby -m root -M test -M exec /usr/share/smartmontools/smartd-runner
  3. Create a bash file in the folder /etc/smartmontools/run.d called 10telegram-notify.
  4. Fill in this content (replace token and chat id):
#!/bin/bash -e

chatid="YOUR_CHAT_ID"
error=$(cat $1)
apikey="YOUR_BOT_TOKEN"

curl -G --data-urlencode "chat_id=${chatid}" --data-urlencode "text=${error}" https://api.telegram.org/bot${apikey}/sendMessage
  1. Make the file executable with chmod +x FILENAME

Prime time

  1. Start the daemon with sudo smartd. You should be receiving a test message in the chat with your bot. If not, you can investigate the error with sudo cat /var/log/syslog.
  2. Remove the -M test from your /etc/smartd.conf file, so that the line looks like this now: DEVICESCAN -d removable -n standby -m root -M exec /usr/share/smartmontools/smartd-runner.
  3. Done.
@fliker09
Copy link

fliker09 commented Dec 3, 2017

One suggestion - replace:

sudo apt-get install smartmontools

with

sudo apt-get install smartmontools curl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment