2 # $Id: machines.cgi,v 1.7 2000/08/19 02:53:56 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 'createtimestamp' => 'Entry created',
31 'modifytimestamp' => 'Entry modified'
34 # This defines what fields are displayed, and in what order
35 @attrorder = ('hostname', 'admin', 'architecture', 'distribution', 'access',
36 'sponsor', 'sponsor-admin', 'location', 'machine', 'memory',
37 'disk', 'bandwidth', 'status', 'notes', 'createtimestamp', 'modifytimestamp');
40 %summaryattrs = ('hostname' => 'Host name',
41 'host' => 'just for a link',
42 'architecture' => 'Architecture',
43 'distribution' => 'Distribution',
45 'access' => 'Access');
47 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
50 my %config = &Util::ReadConfigFile;
52 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
54 $ldap->unbind if (defined($ldap));
57 $SIG{__DIE__} = \&DieHandler;
60 my $host = lc($query->param('host'));
62 &Util::HTMLSendHeader;
63 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
67 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
68 $mesg->code && &Util::HTMLError($mesg->error);
69 $entries = $mesg->as_struct;
71 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
72 $data = $entries->{$dn};
74 my $thishost = $data->{host}->[0];
77 if (lc($thishost) eq $host) {
78 $output{havehostdata} = 1;
80 foreach $key (keys(%attrs)) {
81 $output{$key} = $data->{$key}->[0];
84 $output{hostname} = undef;
85 foreach my $hostname (@{$data->{hostname}}) {
86 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
89 # Modified/created time. TODO: maybe add is the name of the creator/modifier
90 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
91 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
93 # Format email addresses
94 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
95 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
98 my ($sponsor, $url) = undef;
99 $output{sponsor} = undef;
100 foreach $sponsor (@{$data->{sponsor}}) {
101 $sponsor =~ m#((http|ftp)://\S+)#i;
103 $sponsor =~ s/$url//;
104 $output{sponsor} .= "<br>" if ($output{sponsor});
106 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
108 $output{sponsor} .= $sponsor;
112 $selected = " selected ";
115 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
117 # collect summary info
118 foreach $key (keys(%summaryattrs)) {
119 $summary{$thishost}{$key} = $data->{$key}->[0];
122 $summary{$thishost}{hostname} = undef;
123 foreach my $hostname (@{$data->{hostname}}) {
124 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
129 if ($output{havehostdata}) {
130 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
131 $hostdetails .= "<ul>\n";
132 foreach $key (@attrorder) {
134 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
137 $hostdetails .= "</ul>\n";
139 # display summary info
140 $hostdetails = "<h1>Summary</h1>\n";
141 $hostdetails .= "<table border=1 width=90%>\n<tr>";
142 foreach $key (@summaryorder) {
143 $hostdetails .= "<th>$summaryattrs{$key}</th>";
145 $hostdetails .= "</tr>\n";
147 foreach $host (sort(keys(%summary))) {
148 $hostdetails .= "<tr>";
149 foreach $key (@summaryorder) {
150 $hostdetails .= "<td>$summary{$host}{$key} </td>";
152 $hostdetails .= "</tr>\n";
154 $hostdetails .= "</table>\n";
157 # Finally, we can write the output... yuck...
158 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
160 s/~hostlist~/$hostlist/;
161 s/~hostdetails~/$hostdetails/;