-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.cgi
118 lines (94 loc) · 2.8 KB
/
servers.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# A challenge: Develop a web application to run on httpd, tomcat or
# rails (using your language of choice) to list a cluster of servers
# which come from a file or database, and each server with running/down
# status which has been cached in a file or database.
require CGI;
my $page = CGI->new;
# File with the list of servers
my $serverlist = "serverlist";
# File with the status of the servers
my $serverstatus = "serverstatus";
# This can accumulate multiple error messages. I like to let my apps run
# to show multiple error conditions, rather than just stop on the first
# error.
my $erroraccumulator = "";
#
# Handle problems with the two input files.
#
sub reportfileerror() {
my $filename = $_[0];
if (!( $filename )) {
print "Internal error in $0: reportfileerror\n";
}
else {
print "Unable to open file $filename\n";
}
print $page->end_html;
exit;
}
# Start putting up the page
print $page->header();
print $page->start_html("Server Status");
# Is our data good? ie. Two files should exist
open (SERVERLIST,"serverlist") || &reportfileerror("serverlist");
if (!(open (SERVERSTATUS,"serverstatus"))) {
close SERVERLIST;
&reportfileerror("serverstatus");
}
# Since this is a small project, statuses get loaded into a hash.
my %serverstatus = ();
my $n = 0;
while (<SERVERSTATUS>) {
$n++;
chomp;
(my $name,my $status) = split(/\s+/);
if (( $name ) && ( $status ))
{
$serverstatus{$name} = $status;
}
else
{
$erroraccumulator .= "ERROR in server status report at line $n:$_\n";
}
}
close SERVERSTATUS;
# The simple list of servers goes in an array.
# NOTE: If there are repeats in a given list of files, then this would have
# to go into a hash and then the keys would be put into an array.
@serverlist = ();
while (<SERVERLIST>)
{
chomp;
($_) && (push (@serverlist,$_));
}
close SERVERLIST;
# Files can be opened. Set up the status report.
(@serverlist) ||
print "<BR>ERROR: No server list from $serverlist.";
(%serverstatus) ||
print "<BR>ERROR: No server list from $serverstatus.";
# Start writing the table.
print $page->start_table();
print "<TR><TH>Server</TH><TH>Status</TH></TR>\n";
foreach my $server (@serverlist)
{
my $status = $serverstatus{$server};
( $status ) || ( $status = "UNKNOWN" );
print "<TR><TD> $server </TD><TD>".$status."</TD></TR>\n";
$status = '';
}
print $page->end_table();
#
# If any errors accumulated during the run, print them here at the bottom.
#
if ( $erroraccumulator ) {
print "<BR>ERRORS:<BR>";
print $erroraccumulator;
}
print "<BR><BR> The source for this page can be seen ";
print "<a href=\"./servers.txt\">here.</a><BR>";
print "The server list file can be seen ";
print "<a href=\"./serverlist\">here.</a><BR>";
print "The server status file can be seen ";
print "<a href=\"./serverstatus\">here.</a><BR>";
print $page->end_html;