*** empty log message ***
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 #$Id: machines.cgi,v 1.2 1999/09/30 05:38:29 tausq Exp $
3 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
4
5 use lib '.';
6 use strict vars;
7 #use Apache::Registry;
8 use CGI;
9 use Util;
10 use Net::LDAP qw(:all);
11
12 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
13
14 # This defines the description of the fields, and which fields are retrieved
15 %attrs = ('hostname' => 'Host name',
16           'admin' => 'Admin contact',
17           'architecture' => 'Architecture',
18           'distribution' => 'Distribution',
19           'access' => 'Access',
20           'sponsor' => 'Sponsor',
21           'sponsorurl' => 'Sponsor URL',
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     $output{sponsor} = sprintf("<a href=\"%s\">%s</a>", $output{sponsorurl}, $output{sponsor});   
91     
92     $selected = " selected ";    
93   }
94   
95   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
96   
97   # collect summary info
98   foreach $key (keys(%summaryattrs)) {
99     $summary{$thishost}{$key} = $data->{$key}->[0];
100   }
101   
102   $summary{$thishost}{hostname} = sprintf("<a href=\"machines.cgi?host=%s\">%s</a>",
103                                           $summary{$thishost}{host}, $summary{$thishost}{hostname});
104 }
105 $ldap->unbind;
106
107 if ($output{havehostdata}) {
108   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
109   $hostdetails .= "<ul>\n";
110   foreach $key (@attrorder) {
111     if ($output{$key}) {
112       $hostdetails .= "<li><b>$key:</b> $output{$key}\n";
113     }
114   }
115   $hostdetails .= "</ul>\n";
116 } else {
117   # display summary info
118   $hostdetails = "<h1>Summary</h1>\n";
119   $hostdetails .= "<table border=1 width=90%>\n<th>";
120   foreach $key (@summaryorder) {
121     $hostdetails .= "<td>$summaryattrs{$key}</td>";
122   }
123   $hostdetails .= "</th>\n";
124   
125   foreach $host (sort(keys(%summary))) {
126     $hostdetails .= "<tr>";
127     foreach $key (@summaryorder) {
128       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
129     }
130     $hostdetails .= "</tr>\n";
131   }
132   $hostdetails .= "</table>\n";
133 }
134
135 # Finally, we can write the output... yuck...
136 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
137 while (<F>) {
138   s/~hostlist~/$hostlist/;
139   s/~hostdetails~/$hostdetails/;
140   print;
141 }
142 close F;