пятница, 11 октября 2019 г.

Как мониторить SMART и выводить результат в Telegram

Решая свою задачу обнаружил интересный пример на Реддите.

SMART Info w/ Telegram Notifications [Linux]

Yesterday I asked how to get warnings of failing drives on Debian 8.8 Today I woke up to some very helpful comments that I'd like to stitch together for all of you to easily enable your server "speaking" capabilities. (Easier to learn it to speak than asking it through the terminal "How you doin?")
  • Installed smartmontools
  • Check all disks are SMART enabled:
    df -h
    sudo smartctl --info /dev/ | grep 'SMART support is:'
  • 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.
  • I suggest download and installing Postman. A very powerful free API tool. Postman
  • Initiate a chat with your new bot.
  • Open Postman and send a GET to https://api.telegram.org/bot{YOUR_TOKEN}/getUpdates. You will need the chat id called "id": xxxxxx. This ID-number is shown several times in the return JSON file.
  • Open the file /etc/default/smartmontools
  • uncomment the lines
start_smartd=yes smartd_opts="--interval=1800"
  • 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
  • Create a bash file in the folder /etc/smartmontools/run.d called 10telegram-notify and 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
  • Make the file executable: sudo chmod +x /etc/smartmontools/run.d/10telegram-notify
  • Start the daemon 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
  • 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
  • Finished :)

Комментариев нет:

Отправить комментарий