Fear perl scripts with $Id$ :-)
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.3 1999/09/30 06:21:28 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           'notes' => 'Notes',
29           'createtimestamp' => 'Entry created',
30           'modifytimestamp' => 'Entry modified'
31          );
32
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');
37
38 # ditto for summary
39 %summaryattrs = ('hostname' => 'Host name',
40                  'host'     => 'just for a link',
41                  'architecture' => 'Architecture',
42                  'access' => 'Access');
43                  
44 @summaryorder = ('hostname', 'architecture', 'access');          
45
46 # Global settings...
47 my %config = &Util::ReadConfigFile;
48
49 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
50 sub DieHandler {
51   $ldap->unbind if (defined($ldap));
52 }
53
54 $SIG{__DIE__} = \&DieHandler;
55
56 my $query = new CGI;
57 my $host = lc($query->param('host'));
58
59 &Util::HTMLSendHeader;
60 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
61 $mesg;
62 $ldap->bind;
63
64 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
65 $mesg->code && &Util::HTMLError($mesg->error);
66 $entries = $mesg->as_struct;
67
68 foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]} keys(%$entries)) {
69   $data = $entries->{$dn};
70
71   my $thishost = $data->{host}->[0];
72   $selected = "";
73   
74   if (lc($thishost) eq $host) {
75     $output{havehostdata} = 1;
76
77     foreach $key (keys(%attrs)) {
78       $output{$key} = $data->{$key}->[0];
79     }
80
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});
84     
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'});
88     
89     # URL
90     my $sponsor;
91     $output{sponsor} = undef;
92     foreach $sponsor (@{$data->{sponsor}}) {
93       print "<!-- $sponsor -->\n";
94       $sponsor =~ /(.*)\s*(http.*)?/i;
95       $output{sponsor} .= "<br>" if ($output{sponsor});
96       if ($2) {
97         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $1, $2);
98       } else {
99         $output{sponsor} .= $1;
100       }
101     }
102     
103     $selected = " selected ";    
104   }
105   
106   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
107   
108   # collect summary info
109   foreach $key (keys(%summaryattrs)) {
110     $summary{$thishost}{$key} = $data->{$key}->[0];
111   }
112   
113   $summary{$thishost}{hostname} = sprintf("<a href=\"machines.cgi?host=%s\">%s</a>",
114                                           $summary{$thishost}{host}, $summary{$thishost}{hostname});
115 }
116 $ldap->unbind;
117
118 if ($output{havehostdata}) {
119   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
120   $hostdetails .= "<ul>\n";
121   foreach $key (@attrorder) {
122     if ($output{$key}) {
123       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
124     }
125   }
126   $hostdetails .= "</ul>\n";
127 } else {
128   # display summary info
129   $hostdetails = "<h1>Summary</h1>\n";
130   $hostdetails .= "<table border=1 width=90%>\n<th>";
131   foreach $key (@summaryorder) {
132     $hostdetails .= "<td>$summaryattrs{$key}</td>";
133   }
134   $hostdetails .= "</th>\n";
135   
136   foreach $host (sort(keys(%summary))) {
137     $hostdetails .= "<tr>";
138     foreach $key (@summaryorder) {
139       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
140     }
141     $hostdetails .= "</tr>\n";
142   }
143   $hostdetails .= "</table>\n";
144 }
145
146 # Finally, we can write the output... yuck...
147 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
148 while (<F>) {
149   s/~hostlist~/$hostlist/;
150   s/~hostdetails~/$hostdetails/;
151   print;
152 }
153 close F;