Skip to content

Instantly share code, notes, and snippets.

@ialiendeg
Last active October 5, 2020 07:26
Show Gist options
  • Save ialiendeg/12ec6fdef243cf48bfbf41cc18bce50f to your computer and use it in GitHub Desktop.
Save ialiendeg/12ec6fdef243cf48bfbf41cc18bce50f to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/https'
require 'json'
require 'yaml'
slack_cfg = YAML.load_file("/root/.slackrc.yml")
#uri = URI.parse("https://#{slack_cfg['team']}.slack.com/services/hooks/incoming-webhook?token=#{slack_cfg['token']}")
uri = URI.parse(slack_cfg['hook_url'])
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" => "#monit",
"username" => "[MONIT]",
"text" => "[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}",
"mrkdwn" => true
}.to_json
response = http.request(request)
puts response.body
@ialiendeg
Copy link
Author

ialiendeg commented Aug 2, 2020

Hi!. You can do it like this (in monit config file):

...
# Check swap usage
if swap usage > 60% then exec /root/slack_monit_alert.rb
...
# Check device free space
check device root with path /
    if SPACE FREE < 1 GB then exec /root/slack_monit_alert.rb
...
# Check service availability
check process nginx matching nginx
    start program = "/bin/systemctl start nginx"
    stop program = "/bin/systemctl stop nginx"
    if failed port 80 protocol http with timeout 20 seconds then exec /root/slack_monit_alert.rb
    else if succeeded for 2 cycle then exec /root/slack_monit_alert.rb

@iamjenechka
Copy link

Hi there! Thanks for answering me. I suppose I can use a different web hook, for example for teams..

@iamjenechka
Copy link

iamjenechka commented Oct 4, 2020

it's the beauty indeed - simple and efficient one.

@ialiendeg
Copy link
Author

Hi there! Thanks for answering me. I suppose I can use a different web hook, for example for teams..

I'm sorry, I lost the notification for this message. I suppose it is possible to use a different webhook, but I haven't tried myself. Maybe you had success... :)

@ialiendeg
Copy link
Author

it's the beauty indeed - simple and efficient one.

Yes, simplicity is the best.

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