added description field
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.8 2000/08/20 02:32:46 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           'description' => 'Description',
31           'createtimestamp' => 'Entry created',
32           'modifytimestamp' => 'Entry modified'
33          );
34
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');
40
41 # ditto for summary
42 %summaryattrs = ('hostname' => 'Host name',
43                  'host'     => 'just for a link',
44                  'architecture' => 'Architecture',
45                  'distribution' => 'Distribution',
46                  'status' => 'Status',
47                  'access' => 'Access');
48                  
49 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
50
51 # Global settings...
52 my %config = &Util::ReadConfigFile;
53
54 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
55 sub DieHandler {
56   $ldap->unbind if (defined($ldap));
57 }
58
59 $SIG{__DIE__} = \&DieHandler;
60
61 my $query = new CGI;
62 my $host = lc($query->param('host'));
63
64 &Util::HTMLSendHeader;
65 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
66 $mesg;
67 $ldap->bind;
68
69 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
70 $mesg->code && &Util::HTMLError($mesg->error);
71 $entries = $mesg->as_struct;
72
73 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
74   $data = $entries->{$dn};
75
76   my $thishost = $data->{host}->[0];
77   $selected = "";
78   
79   if (lc($thishost) eq $host) {
80     $output{havehostdata} = 1;
81
82     foreach $key (keys(%attrs)) {
83       $output{$key} = $data->{$key}->[0];
84     }
85   
86     $output{hostname} = undef;
87     foreach my $hostname (@{$data->{hostname}}) {
88       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
89     }
90
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});
94     
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'});
98     
99     # URL
100     my ($sponsor, $url) = undef;
101     $output{sponsor} = undef;
102     foreach $sponsor (@{$data->{sponsor}}) {
103       $sponsor =~ m#((http|ftp)://\S+)#i;
104       $url = $1;
105       $sponsor =~ s/$url//;
106       $output{sponsor} .= "<br>" if ($output{sponsor});
107       if ($url) {
108         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
109       } else {
110         $output{sponsor} .= $sponsor;
111       }
112     }
113     
114     $selected = " selected ";    
115   }
116   
117   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
118   
119   # collect summary info
120   foreach $key (keys(%summaryattrs)) {
121     $summary{$thishost}{$key} = $data->{$key}->[0];
122   }
123   
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);
127   }
128 }
129 $ldap->unbind;
130
131 if ($output{havehostdata}) {
132   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
133   $hostdetails .= "<ul>\n";
134   foreach $key (@attrorder) {
135     if ($output{$key}) {
136       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
137     }
138   }
139   $hostdetails .= "</ul>\n";
140 } else {
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>";
146   }
147   $hostdetails .= "</tr>\n";
148   
149   foreach $host (sort(keys(%summary))) {
150     $hostdetails .= "<tr>";
151     foreach $key (@summaryorder) {
152       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
153     }
154     $hostdetails .= "</tr>\n";
155   }
156   $hostdetails .= "</table>\n";
157 }
158
159 # Finally, we can write the output... yuck...
160 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
161 while (<F>) {
162   s/~hostlist~/$hostlist/;
163   s/~hostdetails~/$hostdetails/;
164   print;
165 }
166 close F;