updated with some new features: (copy&pasted from Culus' email)
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.6 2000/08/19 02:34:11 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     # 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});
87     
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'});
91     
92     # URL
93     my ($sponsor, $url) = undef;
94     $output{sponsor} = undef;
95     foreach $sponsor (@{$data->{sponsor}}) {
96       $sponsor =~ m#((http|ftp)://\S+)#i;
97       $url = $1;
98       $sponsor =~ s/$url//;
99       $output{sponsor} .= "<br>" if ($output{sponsor});
100       if ($url) {
101         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
102       } else {
103         $output{sponsor} .= $sponsor;
104       }
105     }
106     
107     $selected = " selected ";    
108   }
109   
110   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
111   
112   # collect summary info
113   foreach $key (keys(%summaryattrs)) {
114     $summary{$thishost}{$key} = $data->{$key}->[0];
115   }
116   
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);
120   }
121 }
122 $ldap->unbind;
123
124 if ($output{havehostdata}) {
125   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
126   $hostdetails .= "<ul>\n";
127   foreach $key (@attrorder) {
128     if ($output{$key}) {
129       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
130     }
131   }
132   $hostdetails .= "</ul>\n";
133 } else {
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>";
139   }
140   $hostdetails .= "</tr>\n";
141   
142   foreach $host (sort(keys(%summary))) {
143     $hostdetails .= "<tr>";
144     foreach $key (@summaryorder) {
145       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
146     }
147     $hostdetails .= "</tr>\n";
148   }
149   $hostdetails .= "</table>\n";
150 }
151
152 # Finally, we can write the output... yuck...
153 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
154 while (<F>) {
155   s/~hostlist~/$hostlist/;
156   s/~hostdetails~/$hostdetails/;
157   print;
158 }
159 close F;