Score:0

Setting up Slack Alerts for Monit monitoring MySQL

fm flag

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?

Score:0
cu flag
#!/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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.