0

Trying to set up Slack alerts for Monit and MySQL/MariaDB but stuck in writing a MySQL check script. I have the monit successfully working if MySQL is stopped and receive email alerts - but want the slack alert.

Slack alert script

#!/usr/bin/ruby

require 'net/https'
require 'json'

uri = URI.parse("https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
request.body = {
    "channel"  => "#general",
    "username" => "mmonit",
    "text"     => "[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}"
}.to_json
response = http.request(request)
puts response.body

My mysql conf which is called in by monitrc

 check process mysqld with pidfile /var/run/mysqld/mysqld.pid
   group database
   group mysql
   start program = "/etc/init.d/mariadb start"
   stop  program = "/etc/init.d/mariadb stop"
   if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart
   if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart
   if 5 restarts with 5 cycles then timeout
   depend mysql_bin
   depend mysql_rc

 check file mysql_bin with path /usr/sbin/mysqld
   group mysql
   include /etc/monit/templates/rootbin

 check file mysql_rc with path /etc/init.d/mariadb
   group mysql
   include /etc/monit/templates/rootbin

Included in monitrc script to check mysql and then execute a script to alert slack

check program check-mysql with path "/opt/monit/check_mysql.sh"
 if status != 0 then exec /path/to/slack.rb

So I have to add a check-mysql.sh script - but what content do I use in this script?

neilgee
  • 109
  • 2

1 Answers1

0
#!/bin/bash

if ! pgrep mysqld >/dev/null 2>&1; then
    /path/to/slack.rb
fi

Replace /path/to/slack.rb with the actual path to your Slack alert script.
Next, make it executable using chmod +x /opt/monit/check_mysql.sh:

check program check-mysql with path "/opt/monit/check_mysql.sh"
 if status != 0 then exec /path/to/slack.rb
VSYS Host
  • 11
  • 1