Created
March 5, 2018 17:41
-
-
Save msaladna/f4dbf9b8cc12c33efa8e954e19cb9893 to your computer and use it in GitHub Desktop.
Monit -> Pushover bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'net/https' | |
re = Regexp.new('\bService\s*(?<svc>[^$]+)^$^\s*Date:\s*(?<date>.*?)$[\r\n]{1,2} | |
^\s*Action:\s*(?<action>.*?)$[\r\n]{1,2} | |
^\s*Host:\s*(?<host>(?<node>[^.]+).*?)$[\r\n]{1,2} | |
^\s*Description:\s*(?<desc>.*?)$', Regexp::MULTILINE|Regexp::EXTENDED) | |
# Take mail straight from STDIN | |
msg = ARGF.read | |
m = re.match(msg) || exit | |
uri = URI.parse("https://api.pushover.net/1/messages.json") | |
http = Net::HTTP.new(uri.host, uri.port) | |
# Oh our root certs are so old :( | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http.use_ssl = uri.to_s[4] == 's' | |
priority = 0 | |
if /Does not exist mysql/ =~ msg | |
priority = 2 | |
end | |
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'}) | |
request.set_form_data({ | |
"priority" => priority, | |
"expire" => 3600, | |
"retry" => 120, | |
"token" => "INSERT YOUR TOKEN", | |
"user" => "INSERT YOUR USER", | |
"message" => "[#{m[:node]}] #{m[:svc]} #{m[:action]} - #{m[:desc]}" | |
}) | |
response = http.request(request) | |
puts response.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment