source: subversion/applications/utils/serverStatus/ServerStatusToWiki.pl @ 34714

Last change on this file since 34714 was 2175, checked in by ojw, 13 years ago

Script to test connectivity to a set of HTTP servers, and put the
results on a wiki page

File size: 3.0 KB
Line 
1#!/usr/bin/perl
2#-----------------------------------------------------------------------
3# Tests HTTP connectivity to a load of servers, and displays the result
4# on a wiki page.
5#
6# Usage: need to supply wiki username/password, and set the wiki page
7# Check list of servers, then just run the program occasionally.
8# See the results on wiki.openstreetmap.org recent changes
9#
10# OJW, 2007. GNU GPL v2 or later
11#----------------------------------------------------------------------
12use MediaWiki;
13use LWP::UserAgent;
14use strict;
15
16TestServers("www","dev","wiki","tile","tilegen");
17
18
19sub TestServers{
20  # Given a list of OSM servers to check, put their current status on the wiki
21  # TODO: cache results and only upload on a change in status
22 
23  # Wiki header
24  my $Result = "==Server status==\n"; 
25  $Result .= "{| border=1 cellspacing=0\n";
26 
27  foreach my $Server(@_){ 
28 
29    # Test connectivity to the server
30    # TODO: consider more appropriate ways of testing the servers' responses
31    my ($OK, $Status) = testServer("http://$Server.openstreetmap.org/");
32   
33    # Format the wikitext (red/green backgrounds etc.)
34    my $Colour = $OK ? "#BFB":"#FBB";
35    my $Style = "background-color:$Colour;";
36    $Style .= "font-weight:bold;" if(!$OK);
37    $Style = "style=\"$Style\"";
38   
39    # Link to dev stats
40    my $Link = "http://openstreetmap.org/munin/openstreetmap/$Server.openstreetmap.html";
41   
42    # Add to the wikitable
43    $Result .= "|-\n";
44    foreach my $Data("[$Link $Server]", $OK ? "OK" : "DOWN", $Status){
45      $Result .= "| $Style | $Data\n";
46    }
47  }
48 
49  # Wiki footer
50  $Result .= "|}\n";
51 
52  # Debug: print result to screen
53  print $Result; 
54 
55  # Optional: exit here to just test the program and not upload the results
56  exit if(0);
57
58  # Upload to the wiki 
59  # Config: which page to upload to
60  UploadToWiki("Sandbox/statusBotTest", $Result);
61}
62
63sub UploadToWiki{
64  # Given a page name and some wikitext, update an OSM wiki page
65  my ($Pagename, $Data) = @_;
66 
67  my $c = MediaWiki->new;
68 
69  # Config: Username, password
70  my $is_ok = $c->setup({
71          'bot' => { 'user' => 'WikiUsername', 'pass' => 'WikiPassword' },
72          'wiki' => {
73                  'host' => 'wiki.openstreetmap.org',
74                  'path' => '/'
75          }});
76
77  # Check whether connected OK to wiki
78  die("Can't connect to wiki - error: ". $c->{error} . "\n(see MediaWiki module on CPAN)\n") if(!$is_ok);
79  printf "Status %s, user %s\n",  $is_ok, $c->user();
80 
81  # Update the wiki page
82  print $c->text($Pagename, $Data);
83}
84
85sub testServer{
86  # Given a webserver name, test whether it can be connected to
87  my $Browser = LWP::UserAgent->new;
88 
89  # Browser options (see LWP pages on CPAN)
90  $Browser->timeout(20);
91 
92  # Name to supply as the user-agent
93  $Browser->agent("ServerStatusToWiki/0.7");
94 
95  # Try to fetch a page over HTTP
96  my $Response = $Browser->get(shift());
97 
98  # Returns: boolean success, and the "200 OK"-style status line
99  return($Response->is_success, $Response->status_line);
100}
Note: See TracBrowser for help on using the repository browser.