2 # $Id: machines.cgi,v 1.10 2001/12/03 05:02:13 rmurray Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
11 use Net::LDAP qw(:all);
13 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
15 # This defines the description of the fields, and which fields are retrieved
16 %attrs = ('hostname' => 'Host name',
17 'admin' => 'Admin contact',
18 'architecture' => 'Architecture',
19 'distribution' => 'Distribution',
21 'sponsor' => 'Sponsor',
22 'sponsor-admin' => 'Sponsor admin',
23 'location' => 'Location',
24 'machine' => 'Processor',
26 'disk' => 'Disk space',
27 'bandwidth' => 'Bandwidth',
30 'sshrsahostkey' => 'SSH host key',
31 'description' => 'Description',
32 'createtimestamp' => 'Entry created',
33 'modifytimestamp' => 'Entry modified'
36 # This defines what fields are displayed, and in what order
37 @attrorder = qw(hostname admin architecture distribution access
38 sponsor sponsor-admin location machine memory
39 disk bandwidth status notes sshrsahostkey
40 description createtimestamp modifytimestamp);
43 %summaryattrs = ('hostname' => 'Host name',
44 'host' => 'just for a link',
45 'architecture' => 'Architecture',
46 'distribution' => 'Distribution',
48 'access' => 'Access');
50 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
53 my %config = &Util::ReadConfigFile;
55 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
57 $ldap->unbind if (defined($ldap));
60 $SIG{__DIE__} = \&DieHandler;
63 my $host = lc($query->param('host'));
65 &Util::HTMLSendHeader;
66 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
70 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
71 $mesg->code && &Util::HTMLError($mesg->error);
72 $entries = $mesg->as_struct;
74 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
75 $data = $entries->{$dn};
77 my $thishost = $data->{host}->[0];
80 if (lc($thishost) eq $host) {
81 $output{havehostdata} = 1;
83 foreach $key (keys(%attrs)) {
84 $output{$key} = $data->{$key}->[0];
87 $output{hostname} = undef;
88 foreach my $hostname (@{$data->{hostname}}) {
89 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
92 # Modified/created time. TODO: maybe add is the name of the creator/modifier
93 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
94 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
96 # Format email addresses
97 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
98 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
100 $output{sshrsahostkey} = undef;
101 foreach $key (@{$data->{sshrsahostkey}}) {
102 $output{sshrsahostkey} .= $key . "<br>";
106 my ($sponsor, $url) = undef;
107 $output{sponsor} = undef;
108 foreach $sponsor (@{$data->{sponsor}}) {
109 $sponsor =~ m#((http|ftp)://\S+)#i;
111 $sponsor =~ s/$url//;
112 $output{sponsor} .= "<br>" if ($output{sponsor});
114 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
116 $output{sponsor} .= $sponsor;
120 $selected = " selected ";
123 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
125 # collect summary info
126 foreach $key (keys(%summaryattrs)) {
127 $summary{$thishost}{$key} = $data->{$key}->[0];
130 $summary{$thishost}{hostname} = undef;
131 foreach my $hostname (@{$data->{hostname}}) {
132 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
137 if ($output{havehostdata}) {
138 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
139 $hostdetails .= "<ul>\n";
140 foreach $key (@attrorder) {
142 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
145 $hostdetails .= "</ul>\n";
147 # display summary info
148 $hostdetails = "<h1>Summary</h1>\n";
149 $hostdetails .= "<table border=1 width=90%>\n<tr>";
150 foreach $key (@summaryorder) {
151 $hostdetails .= "<th>$summaryattrs{$key}</th>";
153 $hostdetails .= "</tr>\n";
155 foreach $host (sort(keys(%summary))) {
156 $hostdetails .= "<tr>";
157 foreach $key (@summaryorder) {
158 $hostdetails .= "<td>$summary{$host}{$key} </td>";
160 $hostdetails .= "</tr>\n";
162 $hostdetails .= "</table>\n";
165 # Finally, we can write the output... yuck...
166 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
168 s/~hostlist~/$hostlist/;
169 s/~hostdetails~/$hostdetails/;