2 # $Id: machines.cgi,v 1.9 2001/01/08 07:03:23 tausq 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 (RSA)',
31 'sshdsahostkey' => 'SSH host key (DSA)',
32 'description' => 'Description',
33 'createtimestamp' => 'Entry created',
34 'modifytimestamp' => 'Entry modified'
37 # This defines what fields are displayed, and in what order
38 @attrorder = qw(hostname admin architecture distribution access
39 sponsor sponsor-admin location machine memory
40 disk bandwidth status notes sshrsahostkey sshdsahostkey
41 description createtimestamp modifytimestamp);
44 %summaryattrs = ('hostname' => 'Host name',
45 'host' => 'just for a link',
46 'architecture' => 'Architecture',
47 'distribution' => 'Distribution',
49 'access' => 'Access');
51 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
54 my %config = &Util::ReadConfigFile;
56 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
58 $ldap->unbind if (defined($ldap));
61 $SIG{__DIE__} = \&DieHandler;
64 my $host = lc($query->param('host'));
66 &Util::HTMLSendHeader;
67 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
71 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
72 $mesg->code && &Util::HTMLError($mesg->error);
73 $entries = $mesg->as_struct;
75 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
76 $data = $entries->{$dn};
78 my $thishost = $data->{host}->[0];
81 if (lc($thishost) eq $host) {
82 $output{havehostdata} = 1;
84 foreach $key (keys(%attrs)) {
85 $output{$key} = $data->{$key}->[0];
88 $output{hostname} = undef;
89 foreach my $hostname (@{$data->{hostname}}) {
90 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
93 # Modified/created time. TODO: maybe add is the name of the creator/modifier
94 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
95 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
97 # Format email addresses
98 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
99 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
102 my ($sponsor, $url) = undef;
103 $output{sponsor} = undef;
104 foreach $sponsor (@{$data->{sponsor}}) {
105 $sponsor =~ m#((http|ftp)://\S+)#i;
107 $sponsor =~ s/$url//;
108 $output{sponsor} .= "<br>" if ($output{sponsor});
110 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
112 $output{sponsor} .= $sponsor;
116 $selected = " selected ";
119 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
121 # collect summary info
122 foreach $key (keys(%summaryattrs)) {
123 $summary{$thishost}{$key} = $data->{$key}->[0];
126 $summary{$thishost}{hostname} = undef;
127 foreach my $hostname (@{$data->{hostname}}) {
128 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
133 if ($output{havehostdata}) {
134 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
135 $hostdetails .= "<ul>\n";
136 foreach $key (@attrorder) {
138 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
141 $hostdetails .= "</ul>\n";
143 # display summary info
144 $hostdetails = "<h1>Summary</h1>\n";
145 $hostdetails .= "<table border=1 width=90%>\n<tr>";
146 foreach $key (@summaryorder) {
147 $hostdetails .= "<th>$summaryattrs{$key}</th>";
149 $hostdetails .= "</tr>\n";
151 foreach $host (sort(keys(%summary))) {
152 $hostdetails .= "<tr>";
153 foreach $key (@summaryorder) {
154 $hostdetails .= "<td>$summary{$host}{$key} </td>";
156 $hostdetails .= "</tr>\n";
158 $hostdetails .= "</table>\n";
161 # Finally, we can write the output... yuck...
162 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
164 s/~hostlist~/$hostlist/;
165 s/~hostdetails~/$hostdetails/;