View difference between Paste ID: 1bN3NNTf and R6A6TVqA
SHOW: | | - or go back to the newest paste.
1-
#!/usr/bin/env perl
1+
#!/usr/bin/env perl
2-
# We want to know if stuff is going to explode in our face
2+
# We want to know if stuff is going to explode in our face
3-
use warnings;
3+
use warnings;
4-
use strict;
4+
use strict;
5-
5+
6-
# Awesome logging
6+
# Awesome logging
7-
use Log::Log4perl;
7+
use Log::Log4perl;
8-
8+
9-
# LWP is an awesome HTTP client
9+
# LWP is an awesome HTTP client
10-
use LWP::UserAgent;
10+
use LWP::UserAgent;
11-
use HTTP::Request;
11+
use HTTP::Request;
12-
12+
13-
# For interacting with the API
13+
# For interacting with the API
14-
use XML::Simple;
14+
use XML::Simple;
15-
15+
16-
# Mediawiki is too awesome to use unix time
16+
# Mediawiki is too awesome to use unix time
17-
use Date::Parse;
17+
use Date::Parse;
18-
use Time::Local;
18+
use Time::Local;
19-
19+
20-
# Good for debugging
20+
# Good for debugging
21-
use Data::Dumper;
21+
use Data::Dumper;
22-
22+
23-
=head1 NAME
23+
=head1 NAME
24-
check_cluebotng.pl - A script to check cluebotng is running
24+
check_cluebotng.pl - A script to check cluebotng is running
25-
25+
26-
=head1 OVERVIEW
26+
=head1 OVERVIEW
27-
This script checks the last edit time against a threashold.
27+
This script checks the last edit time against a threashold.
28-
28+
29-
=head1 AUTHOR
29+
=head1 AUTHOR
30-
Damian Zaremba <[email protected]>
30+
Damian Zaremba <[email protected]>
31-
31+
32-
=head1 LICENSE
32+
=head1 LICENSE
33-
This program is free software: you can redistribute it and/or modify
33+
This program is free software: you can redistribute it and/or modify
34-
it under the terms of the GNU General Public License as published by
34+
it under the terms of the GNU General Public License as published by
35-
the Free Software Foundation, either version 3 of the License, or
35+
the Free Software Foundation, either version 3 of the License, or
36-
(at your option) any later version.
36+
(at your option) any later version.
37-
37+
38-
This program is distributed in the hope that it will be useful,
38+
This program is distributed in the hope that it will be useful,
39-
but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
but WITHOUT ANY WARRANTY; without even the implied warranty of
40-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41-
GNU General Public License for more details.
41+
GNU General Public License for more details.
42-
42+
43-
You should have received a copy of the GNU General Public License
43+
You should have received a copy of the GNU General Public License
44-
along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
along with this program. If not, see <http://www.gnu.org/licenses/>.
45-
45+
46-
=head1 CONFIG
46+
=head1 CONFIG
47-
47+
48-
Hash of our config values
48+
Hash of our config values
49-
49+
50-
=head2 Required options
50+
=head2 Required options
51-
51+
52-
wiki_url - URL to the wiki api.php script
52+
wiki_url - URL to the wiki api.php script
53-
threashold - Time threashold
53+
threshold - Time threshold
54-
admins - Array of people to notify
54+
admins - Array of people to notify
55-
55+
56-
=cut
56+
=cut
57-
57+
58-
# Config stuff
58+
# Config stuff
59-
my $config = {
59+
my $config = {
60-
	wiki_url => "http://en.wikipedia.org/w/api.php",
60+
	wiki_url => "http://en.wikipedia.org/w/api.php",
61-
	threashold => 1800, # 30min
61+
	# grawity: it's spelled 'threshold'
62-
	admins => [
62+
	threshold => 1800, # 30min
63-
		'[email protected]',
63+
	admins => [
64-
		'[email protected]',
64+
		'[email protected]',
65-
	],
65+
		'[email protected]',
66-
};
66+
	],
67-
67+
};
68-
my $VERSION = "0.1";
68+
69-
69+
my $VERSION = "0.1";
70-
# Stuff we need everywhere
70+
71-
our($logger);
71+
# Stuff we need everywhere
72-
72+
our($logger);
73-
=head1 METHODS
73+
74-
74+
=head1 METHODS
75-
=head2 run
75+
76-
Sets up everything and kicks off the process.
76+
=head2 run
77-
77+
Sets up everything and kicks off the process.
78-
=head3 Arguments
78+
79-
Takes no arguments.
79+
=head3 Arguments
80-
80+
Takes no arguments.
81-
=head3 Returns
81+
82-
Returns nothing.
82+
=head3 Returns
83-
83+
Returns nothing.
84-
=cut
84+
85-
85+
=cut
86-
sub run {
86+
87-
	# Setup the logger object
87+
sub run {
88-
	Log::Log4perl->easy_init();
88+
	# Setup the logger object
89-
	$logger = Log::Log4perl->get_logger();
89+
	Log::Log4perl->easy_init();
90-
90+
	$logger = Log::Log4perl->get_logger();
91-
	# Error if we couldn't initialize the logger oject
91+
92-
	if( ! defined( $logger ) ) {
92+
	# Error if we couldn't initialize the logger oject
93-
		print "!!! Could not init logger !!!\n";
93+
	if( ! defined( $logger ) ) {
94-
		exit(1);
94+
		# grawity: use stderr
95-
	}
95+
		warn "!!! Could not init logger !!!\n";
96-
96+
		exit(1);
97-
	$logger->info("Getting edit info");
97+
	}
98-
	my $edit = get_last_edit();
98+
99-
99+
	$logger->info("Getting edit info");
100-
	if( get_last_edit() ) {
100+
	my $edit = get_last_edit();
101-
		$logger->info("Got edit info");
101+
102-
		my $editUNIXTime = str2time($edit->{'timestamp'});
102+
	# grawity: function was being called twice
103-
		my $time = time();
103+
	if( $edit ) {
104-
		my $difference = $time-$editUNIXTime;
104+
		$logger->info("Got edit info");
105-
105+
		my $editUNIXTime = str2time($edit->{'timestamp'});
106-
		$logger->info("Checking edit time");
106+
		my $time = time();
107-
		if( $difference > $config->{'threashold'} ) {
107+
		my $difference = $time-$editUNIXTime;
108-
			$logger->info("Bot not running!");
108+
109-
			notify_admins("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
109+
		$logger->info("Checking edit time");
110-
			notify_wiki("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
110+
		if( $difference > $config->{'threshold'} ) {
111-
		} else {
111+
			$logger->info("Bot not running!");
112-
			$logger->info("Bot running!");
112+
			notify_admins("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
113-
			notify_wiki("Running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
113+
			notify_wiki("Not running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
114-
		}
114+
		} else {
115-
	}
115+
			$logger->info("Bot running!");
116-
}
116+
			notify_wiki("Running - last edit was " . $edit->{'title'} . " " . $difference . "s ago");
117-
117+
		}
118-
=head2 get_last_edit
118+
	}
119-
Gets the lsat bot edit
119+
}
120-
120+
121-
=head3 Arguments
121+
=head2 get_last_edit
122-
Takes no arguments
122+
Gets the lsat bot edit
123-
123+
124-
=head3 Returns
124+
=head3 Arguments
125-
Hash of edit data.
125+
Takes no arguments
126-
126+
127-
=cut
127+
=head3 Returns
128-
128+
Hash of edit data.
129-
sub get_last_edit {
129+
130-
	# user_agent to make the request with
130+
=cut
131-
	my $user_agent = LWP::UserAgent->new(
131+
132-
		timeout => 5,
132+
sub get_last_edit {
133-
		agent => "ClueBotNGChecker/v" . $VERSION,
133+
	# user_agent to make the request with
134-
	);
134+
	my $user_agent = LWP::UserAgent->new(
135-
135+
		timeout => 5,
136-
	# URL to request
136+
		agent => "ClueBotNGChecker/v" . $VERSION,
137-
	my $url = $config->{"wiki_url"} . "?format=xml&action=query&list=usercontribs&ucuser=ClueBot_NG&uclimit=1";
137+
	);
138-
138+
139-
	# Request object
139+
	# URL to request
140-
	my $request_object = HTTP::Request->new(
140+
	my $url = $config->{"wiki_url"} . "?format=xml&action=query&list=usercontribs&ucuser=ClueBot_NG&uclimit=1";
141-
		GET => $url,
141+
142-
	);
142+
	# Request object
143-
143+
	my $request_object = HTTP::Request->new(
144-
	# Make the request and store the response object
144+
		GET => $url,
145-
	my $response = $user_agent->request($request_object);
145+
	);
146-
146+
147-
	# Check if we didn't get a 200OK back
147+
	# Make the request and store the response object
148-
	if ( ! $response->is_success ) {
148+
	my $response = $user_agent->request($request_object);
149-
		$logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
149+
	# grawity: you can use $user_agent->get($url) here
150-
		return 0;
150+
151-
	} else {
151+
	# Check if we didn't get a 200OK back
152-
		# Everything was good, get the content
152+
	if ( ! $response->is_success ) {
153-
		my $raw_data = $response->decoded_content;
153+
		$logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
154-
154+
		return 0;
155-
		# Data is where we will stick the config
155+
	} else {
156-
		my $data;
156+
		# Everything was good, get the content
157-
157+
		my $raw_data = $response->decoded_content;
158-
		# Try and load the returned XML
158+
159-
		eval {
159+
		# Data is where we will stick the config
160-
			my $xml = new XML::Simple;
160+
		my $data;
161-
			$data = $xml->XMLin($raw_data);
161+
162-
		};
162+
		# Try and load the returned XML
163-
163+
		eval {
164-
		# If the XML was bad then error
164+
			# grawity: removed indirect method usage for consistency
165-
		if( $@ ) {
165+
			my $xml = XML::Simple->new;
166-
			$logger->error("Could not process the api data: " . $@);
166+
			$data = $xml->XMLin($raw_data);
167-
			return 0;
167+
		};
168-
		} else {
168+
169-
			my $contributions = $data->{'query'}->{'usercontribs'};
169+
		# If the XML was bad then error
170-
			if(
170+
		if( $@ ) {
171-
				$contributions->{'item'} &&
171+
			$logger->error("Could not process the api data: " . $@);
172-
				$contributions->{'item'}->{'timestamp'} &&
172+
			return 0;
173-
				$contributions->{'item'}->{'title'}
173+
		} else {
174-
			) {
174+
			my $contributions = $data->{'query'}->{'usercontribs'};
175-
				return $contributions->{'item'};
175+
			if(
176-
			}
176+
				$contributions->{'item'} &&
177-
		}
177+
				$contributions->{'item'}->{'timestamp'} &&
178-
	}
178+
				$contributions->{'item'}->{'title'}
179-
}
179+
			) {
180-
180+
				return $contributions->{'item'};
181-
=head2 notify_wiki
181+
			}
182-
Updates the wiki check page
182+
		}
183-
183+
	}
184-
=head3 Arguments
184+
}
185-
message - Message to update with
185+
186-
186+
=head2 notify_wiki
187-
=head3 Returns
187+
Updates the wiki check page
188-
Returns nothing.
188+
189-
189+
=head3 Arguments
190-
=cut
190+
message - Message to update with
191-
191+
192-
sub notify_wiki {
192+
=head3 Returns
193-
	my $message = shift;
193+
Returns nothing.
194-
194+
195-
	# user_agent to make the request with
195+
=cut
196-
	my $user_agent = LWP::UserAgent->new(
196+
197-
		timeout => 5,
197+
sub notify_wiki {
198-
		agent => "ClueBotNGChecker/v" . $VERSION,
198+
	my $message = shift;
199-
	);
199+
200-
200+
	# user_agent to make the request with
201-
	# URL to request
201+
	my $user_agent = LWP::UserAgent->new(
202-
	my $url = $config->{"wiki_url"} . "?format=xml&action=edit&title=User:ClueBot_NG/running";
202+
		timeout => 5,
203-
	$url .= "&summary=Uploading running status&token=%2B%5C" . "&text=" . $message;
203+
		agent => "ClueBotNGChecker/v" . $VERSION,
204-
204+
	);
205-
	# Request object
205+
206-
	my $request_object = HTTP::Request->new(
206+
	# URL to request
207-
		POST => $url,
207+
	my $url = $config->{"wiki_url"} . "?format=xml&action=edit&title=User:ClueBot_NG/running";
208-
	);
208+
	$url .= "&summary=Uploading running status&token=%2B%5C" . "&text=" . $message;
209-
209+
210-
	# Make the request and store the response object
210+
	# Request object
211-
	my $response = $user_agent->request($request_object);
211+
	my $request_object = HTTP::Request->new(
212-
212+
		POST => $url,
213-
	# Check if we didn't get a 200OK back
213+
	);
214-
	if ( ! $response->is_success ) {
214+
215-
		$logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
215+
	# Make the request and store the response object
216-
		return 0;
216+
	my $response = $user_agent->request($request_object);
217-
	} else {
217+
218-
		# Everything was good, get the content
218+
	# Check if we didn't get a 200OK back
219-
		my $raw_data = $response->decoded_content;
219+
	if ( ! $response->is_success ) {
220-
220+
		$logger->error("Could not get " . $url . ", server returned: " . $response->status_line);
221-
		# Data is where we will stick the config
221+
		return 0;
222-
		my $data;
222+
	} else {
223-
223+
		# Everything was good, get the content
224-
		# Try and load the returned XML
224+
		my $raw_data = $response->decoded_content;
225-
		eval {
225+
226-
			my $xml = new XML::Simple;
226+
		# Data is where we will stick the config
227-
			$data = $xml->XMLin($raw_data);
227+
		my $data;
228-
		};
228+
229-
229+
		# Try and load the returned XML
230-
		# If the XML was bad then error
230+
		eval {
231-
		if( $@ ) {
231+
			my $xml = XML::Simple->new;
232-
			$logger->error("Could not process the api data: " . $@);
232+
			$data = $xml->XMLin($raw_data);
233-
			return 0;
233+
		};
234-
		} else {
234+
235-
			if( $data->{'edit'} && $data->{'edit'}->{'result'} eq "Success" ) {
235+
		# If the XML was bad then error
236-
				$logger->info("Wiki updted");
236+
		if( $@ ) {
237-
			} else {
237+
			$logger->error("Could not process the api data: " . $@);
238-
				$logger->error("Could not update wiki");
238+
			return 0;
239-
			}
239+
		} else {
240-
		}
240+
			if( $data->{'edit'} && $data->{'edit'}->{'result'} eq "Success" ) {
241-
	}
241+
				$logger->info("Wiki updted");
242-
242+
			} else {
243-
}
243+
				$logger->error("Could not update wiki");
244-
244+
			}
245-
=head2 notify_admins
245+
		}
246-
Emails the cluebotng admins if it doesn't appear to be editing
246+
	}
247-
247+
248-
=head3 Arguments
248+
}
249-
message - Message to send
249+
250-
250+
=head2 notify_admins
251-
=head3 Returns
251+
Emails the cluebotng admins if it doesn't appear to be editing
252-
Returns nothing
252+
253-
253+
=head3 Arguments
254-
=cut
254+
message - Message to send
255-
255+
256-
sub notify_admins {
256+
=head3 Returns
257-
	my $message = shift;
257+
Returns nothing
258-
	my $admins = join(", ", @{ $config->{"admins"} });
258+
259-
259+
=cut
260-
	$logger->info("Notifying admins: " . $admins);
260+
261-
	my $MAIL;
261+
sub notify_admins {
262-
 	open($MAIL, "|/usr/lib/sendmail -oi -t");
262+
	my $message = shift;
263-
	print $MAIL "From: watcher\@cluebotng\n";
263+
	my $admins = join(", ", @{ $config->{"admins"} });
264-
	print $MAIL "To: " . $admins . "\n";
264+
265-
	print $MAIL "Subject: Bot not running\n\n";
265+
	$logger->info("Notifying admins: " . $admins);
266-
	print $MAIL "$message\n";
266+
	my $MAIL;
267-
	close($MAIL)
267+
 	open($MAIL, "|/usr/lib/sendmail -oi -t");
268-
}
268+
	print $MAIL "From: watcher\@cluebotng\n";
269-
269+
	print $MAIL "To: " . $admins . "\n";
270
	print $MAIL "Subject: Bot not running\n\n";
271
	print $MAIL "$message\n";
272
	close($MAIL)
273
}
274
275
run();