-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsite2ce.pl
More file actions
executable file
·213 lines (189 loc) · 5.25 KB
/
site2ce.pl
File metadata and controls
executable file
·213 lines (189 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/perl -w
######################################################################
#
# site2ce.pl [--bdii <bdii>] --vo <vo> site
#
# Author: Andrea Sciaba' <Andrea.Sciaba@cern.ch>
#
# Version: 1.0.
#
# --bdii <bdii>: <bdii> must be in format <hostname>:<port>. If not
# specified, LCG_GFAL_INFOSYS is used
# --vo <vo>: <vo> must be in format VO:<vo> or VOMS:<fqan>
# <site>: <site> corresponds to GlueSiteName
#
######################################################################
use Getopt::Long;
use Net::LDAP;
use Net::LDAP::Message;
use Net::LDAP::Filter;
use LWP::Simple;
use XML::Parser;
use Pod::Usage;
$NAME = 'site2ce.pl';
# Array with all Sites and tiers
%sites = ();
$vo = "cms";
$compact = 0;
GetOptions(
'bdii=s' => \$bdii,
'vo=s' => \$vo,
'compact' => \$compact
) or die("$NAME: wrong arguments.\n");
# BDII server
if (! $bdii) {
$bdii = $ENV{LCG_GFAL_INFOSYS} or die("$NAME: LCG_GFAL_INFOSYS undefined.\n");
}
@bdiilist = split /,/, $bdii;
if (@ARGV == 0) {
die("$NAME: a site must be specified.\n");
}
my $site = $ARGV[0];
#Get XML file from SiteDB
my $url = "https://cmsweb.cern.ch/sitedb/reports/showXMLReport?reportid=cms_to_sam.ini";
my $doc = get($url) or die "Cannot retrieve XML\n";
# Parse XML
$p = new XML::Parser(Handlers => {Start => \&h_start, Char => \&h_char});
$p->parse($doc) or die "Cannot parse XML\n";
@site_list = ();
foreach my $s (values %sites) {
if ($s->{CMS} eq $site) {
push @site_list, $s->{SAM};
if (! $compact) {
print "CMS name: " . $s->{CMS} . "\n";
print "BDII name: " . $s->{SAM} . "\n";
}
$found = 1;
}
}
if ( ! $found and ! $compact) {
print "BDII name: " . $site . "\n";
}
my $ldap = &bdii_init(@bdiilist) or die "$NAME: cannot contact LDAP server.\n";
for $site (@site_list) {
my $siteid = &siteid($ldap, $site) or die "$NAME: site $site not found.\n";
my @clusters = &clusters($ldap, $siteid) or warn "$NAME: no clusters associated to site $site.\n";
if ($output and $compact) {
print ",";
}
$output = '';
$found = 0;
foreach my $clusterid (@clusters) {
my @ces = &ce($ldap, $clusterid, $vo);
if (! $compact) {
map {$output .= "$_\n"} @ces;
} else {
map {$output .= "$_,"} @ces;
}
}
$output =~ s/,$//;
if ( ! $output ) {
die "No CEs found\n";
}
print $output;
}
sub bdii_init {
my @bdiilist = @_;
my $ldap = 0;
foreach my $bdii (@bdiilist) {
unless ($ldap = Net::LDAP->new($bdii)) {
warn("$NAME: failed to contact BDII $bdii.\n");
next;
}
$mesg = $ldap->bind;
if ($mesg->is_error()) {
warn("$NAME: error in binding the BDII:\n$mesg->error_text().\n");
next;
}
}
return $ldap;
}
sub siteid {
my ($ldap, $site) = @_;
my $siteid;
my $base = 'o=grid';
my $filter = "(&(objectclass=GlueSite)(GlueSiteName=$site))";
my $mesg = $ldap->search(base => $base,
filter => $filter);
if ( $mesg->is_error() ) {
$ldap->unbind();
warn ("$NAME: error searching the BDII:\n$mesg->error()\n");
return $siteid;
}
my $entry = $mesg->entry(0);
if (! $entry) {
$ldap->unbind();
return $siteid;
}
$siteid = $entry->get_value('GlueSiteUniqueID');
return $siteid;
}
sub clusters {
my ($ldap, $siteid) = @_;
my @clusters;
my $base = 'o=grid';
my $filter = "(&(objectclass=GlueCluster)(GlueForeignKey=GlueSiteUniqueID=$siteid))";
my $mesg = $ldap->search(base => $base,
filter => $filter);
if ( $mesg->is_error() ) {
$ldap->unbind();
warn ("$NAME: error searching the BDII:\n$mesg->error()\n");
return @clusters;
}
foreach my $entry ($mesg->entries()) {
push @clusters, $entry->get_value('GlueClusterUniqueID');
}
return @clusters;
}
sub ce {
my ($ldap, $clusterid, $vo) = @_;
my $base = 'o=grid';
my @ces;
my $filter = "(&(objectclass=GlueCE)(|(GlueCEAccessControlBaseRule=VO:$vo)(GlueCEAccessControlBaseRule=VOMS:/$vo/*))(GlueCEStateStatus=Production)(GlueForeignKey=GlueClusterUniqueID=$clusterid))";
my $mesg = $ldap->search(base => $base,
filter => $filter);
if ( $mesg->is_error() ) {
$ldap->unbind();
warn ("$NAME: error searching the BDII:\n$mesg->error()\n");
return @ces;
}
foreach my $entry ($mesg->entries()) {
my $hostname = $entry->get_value('GlueCEInfoHostName');
push @ces, $hostname if (!grep{/$hostname/} @ces);
}
return @ces;
}
# Handler routines
sub h_start {
my $p = shift;
my $el = shift;
my %attr = ();
while (@_) {
my $a = shift;
my $v = shift;
$attr{$a} = $v;
}
if ($el eq 'item') {
$lastid = $attr{id};
my $s = new Site($attr{id});
$sites{$lastid} = $s;
}
}
sub h_char {
my $p = shift;
my $a = shift;
if ($p->in_element('cms_name')) {
my $site = $sites{$lastid};
$site->{CMS} = $a;
} elsif ($p->in_element('sam_name')) {
my $site = $sites{$lastid};
$site->{SAM} = $a;
}
}
package Site;
sub new {
my $class = shift;
my $id = shift;
my $self = {ID => $id, CMS => '', SAM => ''};
bless $self, $class;
}