Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # We want to know if stuff is going to explode in our face
- use warnings;
- use strict;
- # Awesome logging
- use Log::Log4perl;
- # LWP is an awesome HTTP client
- use LWP::UserAgent;
- use HTTP::Request;
- # For interacting with the API
- use XML::Simple;
- # Mediawiki is too awesome to use unix time
- use Date::Parse;
- use Time::Local;
- # Good for debugging
- use Data::Dumper;
- =head1 NAME
- check_cluebotng.pl - A script to check cluebotng is running
- =head1 OVERVIEW
- This script checks the last edit time against a threashold.
- =head1 AUTHOR
- Damian Zaremba <[email protected]>
- =head1 LICENSE
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- =head1 CONFIG
- Hash of our config values
- =head2 Required options
- wiki_url - URL to the wiki api.php script
- threashold - Time threashold
- admins - Array of people to notify
- =cut
- # Config stuff
- my $config = {
- wiki_url => "http://en.wikipedia.org/w/api.php",
- threashold => 1800, # 30min
- admins => [
- ],
- };
- my $VERSION = "0.1";
- # Stuff we need everywhere
- our($logger);
- =head1 METHODS
- =head2 run
- Sets up everything and kicks off the process.
- =head3 Arguments
- Takes no arguments.
- =head3 Returns
- Returns nothing.
- =cut
- sub run {
- # Setup the logger object
- Log::Log4perl->easy_init();
- $logger = Log::Log4perl->get_logger();
- # Error if we couldn't initialize the logger oject
- if( ! defined( $logger ) ) {
- print "!!! Could not init logger !!!\n";
- exit(1);
- }
- $logger->info("Getting edit info");
- my $edit = get_last_edit();
- if( get_last_edit() ) {
- $logger->info("Got edit info");
- my $editUNIXTime = str2time($edit->{'timestamp'});
- my $time = time();
- my $difference = $time-$editUNIXTime;
- $logger->info("Checking edit time");
- if( $difference > $config->{'threashold'} ) {
- $logger->info("Bot not running!");
- notify_admins("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
- notify_wiki("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
- } else {
- $logger->info("Bot running!");
- notify_wiki("Running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
- }
- }
- }
- =head2 get_last_edit
- Gets the lsat bot edit
- =head3 Arguments
- Takes no arguments
- =head3 Returns
- Hash of edit data.
- =cut
- sub get_last_edit {
- # user_agent to make the request with
- my $user_agent = LWP::UserAgent->new(
- timeout => 5,
- agent => "ClueBotNGChecker/v" . $VERSION,
- );
- # URL to request
- my $url = $config->{"wiki_url"} . "?format=xml&action=query&list=usercontribs&ucuser=ClueBot_NG&uclimit=1";
- # Request object
- my $request_object = HTTP::Request->new(
- GET => $url,
- );
- # Make the request and store the response object
- my $response = $user_agent->request($request_object);
- # Check if we didn't get a 200OK back
- if ( ! $response->is_success ) {
- $logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
- return 0;
- } else {
- # Everything was good, get the content
- my $raw_data = $response->decoded_content;
- # Data is where we will stick the config
- my $data;
- # Try and load the returned XML
- eval {
- my $xml = new XML::Simple;
- $data = $xml->XMLin($raw_data);
- };
- # If the XML was bad then error
- if( $@ ) {
- $logger->error("Could not process the api data: " . $@);
- return 0;
- } else {
- my $contributions = $data->{'query'}->{'usercontribs'};
- if(
- $contributions->{'item'} &&
- $contributions->{'item'}->{'timestamp'} &&
- $contributions->{'item'}->{'title'}
- ) {
- return $contributions->{'item'};
- }
- }
- }
- }
- =head2 notify_wiki
- Updates the wiki check page
- =head3 Arguments
- message - Message to update with
- =head3 Returns
- Returns nothing.
- =cut
- sub notify_wiki {
- my $message = shift;
- # user_agent to make the request with
- my $user_agent = LWP::UserAgent->new(
- timeout => 5,
- agent => "ClueBotNGChecker/v" . $VERSION,
- );
- # URL to request
- my $url = $config->{"wiki_url"} . "?format=xml&action=edit&title=User:ClueBot_NG/running";
- $url .= "&summary=Uploading running status&token=%2B%5C" . "&text=" . $message;
- # Request object
- my $request_object = HTTP::Request->new(
- POST => $url,
- );
- # Make the request and store the response object
- my $response = $user_agent->request($request_object);
- # Check if we didn't get a 200OK back
- if ( ! $response->is_success ) {
- $logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
- return 0;
- } else {
- # Everything was good, get the content
- my $raw_data = $response->decoded_content;
- # Data is where we will stick the config
- my $data;
- # Try and load the returned XML
- eval {
- my $xml = new XML::Simple;
- $data = $xml->XMLin($raw_data);
- };
- # If the XML was bad then error
- if( $@ ) {
- $logger->error("Could not process the api data: " . $@);
- return 0;
- } else {
- if( $data->{'edit'} && $data->{'edit'}->{'result'} eq "Success" ) {
- $logger->info("Wiki updted");
- } else {
- $logger->error("Could not update wiki");
- }
- }
- }
- }
- =head2 notify_admins
- Emails the cluebotng admins if it doesn't appear to be editing
- =head3 Arguments
- message - Message to send
- =head3 Returns
- Returns nothing
- =cut
- sub notify_admins {
- my $message = shift;
- my $admins = join(", ", @{ $config->{"admins"} });
- $logger->info("Notifying admins: " . $admins);
- my $MAIL;
- open($MAIL, "|/usr/lib/sendmail -oi -t");
- print $MAIL "From: watcher\@cluebotng\n";
- print $MAIL "To: " . $admins . "\n";
- print $MAIL "Subject: Bot not running\n\n";
- print $MAIL "$message\n";
- close($MAIL)
- }
- run();
Advertisement
Add Comment
Please, Sign In to add comment