Created
November 6, 2011 19:24
-
-
Save voodoojello/1343344 to your computer and use it in GitHub Desktop.
This is a simple script fired from the 'exec' function in monit [http://mmonit.com/monit/] that will scrape the monit log and tweet (DM) a message to the specified recipient(s).
This file contains 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/perl -w | |
# | |
# File: monit-tweet.pl | |
# App Version: 0.0.2.2a | |
# Created: Sat Nov 22 23:48:18 CDT 2010 | |
# Modified: Thu Nov 17 20:38:06 CST 2011 | |
# Authored: mark page [[email protected]] | |
# | |
# This is a simple script fired from the 'exec' function | |
# in monit [http://mmonit.com/monit/] that will scrape | |
# the monit log and tweet (DM) a message to the specified | |
# recipient(s). The monitrc entry should look something | |
# like this (assumes script is in /etc/monit): | |
# | |
# if match "[match text]" then exec "/usr/bin/perl /etc/monit/monit-tweet.pl [to,to] [from] [id] [match text]" | |
# | |
# This script requires Monit, Net::Twitter, and Data::Dumper (for debugging only). | |
# | |
use strict; | |
use warnings; | |
my $args = { | |
'to' => shift(@ARGV), # Twitter 'to' handle(s) (comma seperate on multiple) | |
'from' => shift(@ARGV), # Twitter 'from' handle' | |
'id' => shift(@ARGV), # Application identifier (prepended to msg) | |
'match' => join(' ',@ARGV), # Text match string used in monit | |
}; | |
my $config = { | |
'monit_log' => '/var/log/monit.log', # /path/to/monit/log | |
'debug_log' => '/var/tmp/mt-debug.log', # /path/to/debug/log | |
'debug' => 0, # 0 = off, 1 = con, 2 = con/log, 3 = log | |
}; | |
print &main::tweet(&main::monit()); | |
exit; | |
sub monit() { | |
# | |
# We're simply grabbing the monit log, reversing it, stepping through the enries | |
# looking for a match based in the $args->{'match'} string passed from monit. | |
# We'll then split the matched line on 'content match ', (a standard entry made | |
# by monit) and take everyting on the right to shorten the message and provide | |
# some measure of readability. | |
# | |
open(MONIT_LOG,'<',$config->{'monit_log'}); | |
chomp(my @monit = <MONIT_LOG>); my @reversed = reverse(@monit); | |
close(MONIT_LOG); | |
for my $l (0 .. (scalar(@reversed) - 1)) { | |
if ($reversed[$l] =~ m/$args->{'match'}/i) { | |
return $args->{'id'} . ': ' . (split('content match ',$reversed[$l]))[-1]; | |
} | |
} | |
} | |
sub tweet() { | |
use Net::Twitter; | |
my $msg = substr(shift @_,0,140); # Trim message length to 140 chars | |
my $tokens = { | |
# | |
# You may specify multiple 'from' accounts here. Of course, one may be used per DM :) | |
# The hash name must match (case-sensitive) the twitter handle. | |
# | |
# See https://dev.twitter.com/ for more information | |
# | |
'some_name_here' => { | |
'consumer_key' => 'xxxxxxxxxxxxxxxxxxxx', | |
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'access_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
}, | |
'some_other_name_here' => { | |
'consumer_key' => 'xxxxxxxxxxxxxxxxxxxx', | |
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'access_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
}, | |
}; | |
if ($args->{'to'} and $args->{'from'} and $msg) { | |
my $tweet = Net::Twitter->new( | |
'traits' => [qw/OAuth API::REST/], | |
'consumer_key' => $tokens->{$args->{'from'}}->{'consumer_key'}, | |
'consumer_secret' => $tokens->{$args->{'from'}}->{'consumer_secret'}, | |
'access_token' => $tokens->{$args->{'from'}}->{'access_token'}, | |
'access_token_secret' => $tokens->{$args->{'from'}}->{'access_token_secret'}, | |
); | |
my ($result); chomp(my @tos = split(',',$args->{'to'})); | |
for my $t (0 .. (scalar(@tos) - 1)) { | |
$result .= $tweet->new_direct_message("$tos[$t]","$msg"); | |
} | |
if ($config->{'debug'}) { return &main::debug($result); } | |
if ($result->{'id_str'}) { | |
return $result->{'id_str'}; | |
} | |
else { | |
return 'an unknown error occurred'; | |
} | |
} | |
else { | |
return 'invalid argument'; | |
} | |
} | |
sub debug() { | |
# | |
# Debugging routine with console and/or log output | |
# set options in 'config' hash. | |
# | |
use Data::Dumper; $Data::Dumper::Terse = 1; | |
my $result = shift @_; | |
if ($config->{'debug'} > 1) { | |
open(DEBUG_LOG,'>>',$config->{'debug_log'}); | |
print DEBUG_LOG Dumper($result) . "\n\n"; | |
close(DEBUG_LOG); | |
} | |
if ($config->{'debug'} < 3) { | |
return Dumper($result) . "\n\n"; | |
} | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment