2 # $Id: machines.cgi,v 1.6 2000/08/19 02:34:11 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 # Modified/created time. TODO: maybe add is the name of the creator/modifier
85 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
86 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
88 # Format email addresses
89 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
90 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
93 my ($sponsor, $url) = undef;
94 $output{sponsor} = undef;
95 foreach $sponsor (@{$data->{sponsor}}) {
96 $sponsor =~ m#((http|ftp)://\S+)#i;
99 $output{sponsor} .= "<br>" if ($output{sponsor});
101 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
103 $output{sponsor} .= $sponsor;
107 $selected = " selected ";
110 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
112 # collect summary info
113 foreach $key (keys(%summaryattrs)) {
114 $summary{$thishost}{$key} = $data->{$key}->[0];
117 $summary{$thishost}{hostname} = undef;
118 foreach my $hostname (@{$data->{hostname}}) {
119 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? ', ' : ''), $summary{$thishost}{host}, $hostname);
124 if ($output{havehostdata}) {
125 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
126 $hostdetails .= "<ul>\n";
127 foreach $key (@attrorder) {
129 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
132 $hostdetails .= "</ul>\n";
134 # display summary info
135 $hostdetails = "<h1>Summary</h1>\n";
136 $hostdetails .= "<table border=1 width=90%>\n<tr>";
137 foreach $key (@summaryorder) {
138 $hostdetails .= "<th>$summaryattrs{$key}</th>";
140 $hostdetails .= "</tr>\n";
142 foreach $host (sort(keys(%summary))) {
143 $hostdetails .= "<tr>";
144 foreach $key (@summaryorder) {
145 $hostdetails .= "<td>$summary{$host}{$key} </td>";
147 $hostdetails .= "</tr>\n";
149 $hostdetails .= "</table>\n";
152 # Finally, we can write the output... yuck...
153 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
155 s/~hostlist~/$hostlist/;
156 s/~hostdetails~/$hostdetails/;