2 # $Id: machines.cgi,v 1.8 2000/08/20 02:32:46 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 'description' => 'Description',
31 'createtimestamp' => 'Entry created',
32 'modifytimestamp' => 'Entry modified'
35 # This defines what fields are displayed, and in what order
36 @attrorder = ('hostname', 'admin', 'architecture', 'distribution', 'access',
37 'sponsor', 'sponsor-admin', 'location', 'machine', 'memory',
38 'disk', 'bandwidth', 'status', 'notes', 'description',
39 'createtimestamp', 'modifytimestamp');
42 %summaryattrs = ('hostname' => 'Host name',
43 'host' => 'just for a link',
44 'architecture' => 'Architecture',
45 'distribution' => 'Distribution',
47 'access' => 'Access');
49 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
52 my %config = &Util::ReadConfigFile;
54 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
56 $ldap->unbind if (defined($ldap));
59 $SIG{__DIE__} = \&DieHandler;
62 my $host = lc($query->param('host'));
64 &Util::HTMLSendHeader;
65 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
69 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
70 $mesg->code && &Util::HTMLError($mesg->error);
71 $entries = $mesg->as_struct;
73 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
74 $data = $entries->{$dn};
76 my $thishost = $data->{host}->[0];
79 if (lc($thishost) eq $host) {
80 $output{havehostdata} = 1;
82 foreach $key (keys(%attrs)) {
83 $output{$key} = $data->{$key}->[0];
86 $output{hostname} = undef;
87 foreach my $hostname (@{$data->{hostname}}) {
88 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
91 # Modified/created time. TODO: maybe add is the name of the creator/modifier
92 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
93 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
95 # Format email addresses
96 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
97 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
100 my ($sponsor, $url) = undef;
101 $output{sponsor} = undef;
102 foreach $sponsor (@{$data->{sponsor}}) {
103 $sponsor =~ m#((http|ftp)://\S+)#i;
105 $sponsor =~ s/$url//;
106 $output{sponsor} .= "<br>" if ($output{sponsor});
108 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
110 $output{sponsor} .= $sponsor;
114 $selected = " selected ";
117 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
119 # collect summary info
120 foreach $key (keys(%summaryattrs)) {
121 $summary{$thishost}{$key} = $data->{$key}->[0];
124 $summary{$thishost}{hostname} = undef;
125 foreach my $hostname (@{$data->{hostname}}) {
126 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
131 if ($output{havehostdata}) {
132 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
133 $hostdetails .= "<ul>\n";
134 foreach $key (@attrorder) {
136 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
139 $hostdetails .= "</ul>\n";
141 # display summary info
142 $hostdetails = "<h1>Summary</h1>\n";
143 $hostdetails .= "<table border=1 width=90%>\n<tr>";
144 foreach $key (@summaryorder) {
145 $hostdetails .= "<th>$summaryattrs{$key}</th>";
147 $hostdetails .= "</tr>\n";
149 foreach $host (sort(keys(%summary))) {
150 $hostdetails .= "<tr>";
151 foreach $key (@summaryorder) {
152 $hostdetails .= "<td>$summary{$host}{$key} </td>";
154 $hostdetails .= "</tr>\n";
156 $hostdetails .= "</table>\n";
159 # Finally, we can write the output... yuck...
160 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
162 s/~hostlist~/$hostlist/;
163 s/~hostdetails~/$hostdetails/;