some cosmetic fixes
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.7 2000/08/19 02:53:56 tausq Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '.';
7 use strict vars;
8 #use Apache::Registry;
9 use CGI;
10 use Util;
11 use Net::LDAP qw(:all);
12
13 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
14
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',
20           'access' => 'Access',
21           'sponsor' => 'Sponsor',
22           'sponsor-admin' => 'Sponsor admin',
23           'location' => 'Location',
24           'machine' => 'Processor',
25           'memory' => 'Memory',
26           'disk' => 'Disk space',
27           'bandwidth' => 'Bandwidth',
28           'status' => 'Status',
29           'notes' => 'Notes',
30           'createtimestamp' => 'Entry created',
31           'modifytimestamp' => 'Entry modified'
32          );
33
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');
38
39 # ditto for summary
40 %summaryattrs = ('hostname' => 'Host name',
41                  'host'     => 'just for a link',
42                  'architecture' => 'Architecture',
43                  'distribution' => 'Distribution',
44                  'status' => 'Status',
45                  'access' => 'Access');
46                  
47 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
48
49 # Global settings...
50 my %config = &Util::ReadConfigFile;
51
52 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
53 sub DieHandler {
54   $ldap->unbind if (defined($ldap));
55 }
56
57 $SIG{__DIE__} = \&DieHandler;
58
59 my $query = new CGI;
60 my $host = lc($query->param('host'));
61
62 &Util::HTMLSendHeader;
63 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
64 $mesg;
65 $ldap->bind;
66
67 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
68 $mesg->code && &Util::HTMLError($mesg->error);
69 $entries = $mesg->as_struct;
70
71 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
72   $data = $entries->{$dn};
73
74   my $thishost = $data->{host}->[0];
75   $selected = "";
76   
77   if (lc($thishost) eq $host) {
78     $output{havehostdata} = 1;
79
80     foreach $key (keys(%attrs)) {
81       $output{$key} = $data->{$key}->[0];
82     }
83   
84     $output{hostname} = undef;
85     foreach my $hostname (@{$data->{hostname}}) {
86       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
87     }
88
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});
92     
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'});
96     
97     # URL
98     my ($sponsor, $url) = undef;
99     $output{sponsor} = undef;
100     foreach $sponsor (@{$data->{sponsor}}) {
101       $sponsor =~ m#((http|ftp)://\S+)#i;
102       $url = $1;
103       $sponsor =~ s/$url//;
104       $output{sponsor} .= "<br>" if ($output{sponsor});
105       if ($url) {
106         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
107       } else {
108         $output{sponsor} .= $sponsor;
109       }
110     }
111     
112     $selected = " selected ";    
113   }
114   
115   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
116   
117   # collect summary info
118   foreach $key (keys(%summaryattrs)) {
119     $summary{$thishost}{$key} = $data->{$key}->[0];
120   }
121   
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);
125   }
126 }
127 $ldap->unbind;
128
129 if ($output{havehostdata}) {
130   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
131   $hostdetails .= "<ul>\n";
132   foreach $key (@attrorder) {
133     if ($output{$key}) {
134       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
135     }
136   }
137   $hostdetails .= "</ul>\n";
138 } else {
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>";
144   }
145   $hostdetails .= "</tr>\n";
146   
147   foreach $host (sort(keys(%summary))) {
148     $hostdetails .= "<tr>";
149     foreach $key (@summaryorder) {
150       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
151     }
152     $hostdetails .= "</tr>\n";
153   }
154   $hostdetails .= "</table>\n";
155 }
156
157 # Finally, we can write the output... yuck...
158 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
159 while (<F>) {
160   s/~hostlist~/$hostlist/;
161   s/~hostdetails~/$hostdetails/;
162   print;
163 }
164 close F;