2 # $Id: machines.cgi,v 1.4 1999/10/01 03:44:20 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',
29 'createtimestamp' => 'Entry created',
30 'modifytimestamp' => 'Entry modified'
33 # This defines what fields are displayed, and in what order
34 @attrorder = ('hostname', 'admin', 'architecture', 'distribution', 'access',
35 'sponsor', 'sponsor-admin', 'location', 'machine', 'memory',
36 'disk', 'bandwidth', 'notes', 'createtimestamp', 'modifytimestamp');
39 %summaryattrs = ('hostname' => 'Host name',
40 'host' => 'just for a link',
41 'architecture' => 'Architecture',
42 'access' => 'Access');
44 @summaryorder = ('hostname', 'architecture', 'access');
47 my %config = &Util::ReadConfigFile;
49 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
51 $ldap->unbind if (defined($ldap));
54 $SIG{__DIE__} = \&DieHandler;
57 my $host = lc($query->param('host'));
59 &Util::HTMLSendHeader;
60 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
64 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
65 $mesg->code && &Util::HTMLError($mesg->error);
66 $entries = $mesg->as_struct;
68 foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]} keys(%$entries)) {
69 $data = $entries->{$dn};
71 my $thishost = $data->{host}->[0];
74 if (lc($thishost) eq $host) {
75 $output{havehostdata} = 1;
77 foreach $key (keys(%attrs)) {
78 $output{$key} = $data->{$key}->[0];
81 # Modified/created time. TODO: maybe add is the name of the creator/modifier
82 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
83 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
85 # Format email addresses
86 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
87 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
90 my ($sponsor, $url) = undef;
91 $output{sponsor} = undef;
92 foreach $sponsor (@{$data->{sponsor}}) {
93 $sponsor =~ m#((http|ftp)://\S+)#i;
96 $output{sponsor} .= "<br>" if ($output{sponsor});
98 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
100 $output{sponsor} .= $sponsor;
104 $selected = " selected ";
107 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
109 # collect summary info
110 foreach $key (keys(%summaryattrs)) {
111 $summary{$thishost}{$key} = $data->{$key}->[0];
114 $summary{$thishost}{hostname} = sprintf("<a href=\"machines.cgi?host=%s\">%s</a>",
115 $summary{$thishost}{host}, $summary{$thishost}{hostname});
119 if ($output{havehostdata}) {
120 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
121 $hostdetails .= "<ul>\n";
122 foreach $key (@attrorder) {
124 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
127 $hostdetails .= "</ul>\n";
129 # display summary info
130 $hostdetails = "<h1>Summary</h1>\n";
131 $hostdetails .= "<table border=1 width=90%>\n<th>";
132 foreach $key (@summaryorder) {
133 $hostdetails .= "<td>$summaryattrs{$key}</td>";
135 $hostdetails .= "</th>\n";
137 foreach $host (sort(keys(%summary))) {
138 $hostdetails .= "<tr>";
139 foreach $key (@summaryorder) {
140 $hostdetails .= "<td>$summary{$host}{$key} </td>";
142 $hostdetails .= "</tr>\n";
144 $hostdetails .= "</table>\n";
147 # Finally, we can write the output... yuck...
148 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
150 s/~hostlist~/$hostlist/;
151 s/~hostdetails~/$hostdetails/;