forked from CMSCompOps/MonitoringScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpledged.pl
executable file
·186 lines (158 loc) · 4.6 KB
/
pledged.pl
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
#!/usr/bin/perl -w
#use strict;
use LWP::Simple;
use XML::Parser;
package Site;
sub new {
my $class = shift;
my $id = shift;
my $self = {ID => $id,
NAME => '',
QUARTER => '',
DISK => '',
TAPE => '',
SLOTS => ''};
bless $self, $class;
}
package main;
$ssbdir = "/afs/cern.ch/cms/LCG/SiteComm/";
$file_pledged_slots = "$ssbdir/pledged_slots.txt";
$file_pledged_disk = "$ssbdir/pledged_disk.txt";
$file_pledged_tape = "$ssbdir/pledged_tape.txt";
# Array with all Sites and tiers
%sites = ();
#Get XML file from SiteDB
my $url_p = "https://cmsweb.cern.ch/sitedb/reports/showXMLReport?reportid=quarterly_pledges.ini";
my $url_s = "https://cmsweb.cern.ch/sitedb/reports/showXMLReport?reportid=naming_convention.ini";
my $doc_p = get($url_p) or die "Cannot retrieve XML\n";
my $doc_s = get($url_s) or die "Cannot retrieve XML\n";
# Parse XML
$p = new XML::Parser(Handlers => {Start => \&h_start, Char => \&h_char_pledges});
$p->parse($doc_p) or die "Cannot parse XML\n";
$p = new XML::Parser(Handlers => {Char => \&h_char_names});
$p->parse($doc_s) or die "Cannot parse XML\n";
%data = ();
foreach (keys %sites) {
my $name = $sites{$_}->{NAME};
my $cms = $site2cms{$name};
my $id = $site2id{$name};
my $slots = $sites{$_} ->{SLOTS};
my $quarter = $sites{$_} ->{QUARTER};
my $disk = $sites{$_} ->{DISK};
my $tape = $sites{$_} ->{TAPE};
next if (>($quarter, &curr_quarter()));
$data{$cms} = [$quarter, $slots, $disk, $tape, $id] if (!defined $data{$cms} or
>($quarter, ${$data{$cms}}[0]));
}
open(SLOTS, "> $file_pledged_slots") or
die "Cannot create $file_pledged_slots\n";
open(DISK, "> $file_pledged_disk") or
die "Cannot create $file_pledged_disk\n";
open(TAPE, "> $file_pledged_tape") or
die "Cannot create $file_pledged_tape\n";
foreach my $cms (sort keys %data){
my $timestamp = ×tamp();
my $quarter = ${$data{$cms}}[0];
my $slots = ${$data{$cms}}[1];
my $disk = ${$data{$cms}}[2];
my $tape = ${$data{$cms}}[3];
my $id = ${$data{$cms}}[4];
my $colour = 'green';
$colour = "yellow" if (>(&curr_quarter(), $quarter));
$colour = "red" if (&diff(&curr_quarter(), $quarter) > 4);
my ($y, $q) = split /\./, $quarter;
my $pledge_url = "https://cmsweb.cern.ch/sitedb/resources/?site=$id&quarter=$q&year=$y";
printf SLOTS "%s\t%s\t%s\t%s\t%s\n", $timestamp, $cms, $slots,
$colour, $pledge_url;
printf DISK "%s\t%s\t%s\t%s\t%s\n", $timestamp, $cms, $disk,
$colour, $pledge_url;
printf TAPE "%s\t%s\t%s\t%s\t%s\n", $timestamp, $cms, $tape,
$colour, $pledge_url;
}
close SLOTS;
close DISK;
close TAPE;
# 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_pledges {
my $p = shift;
my $a = shift;
if ($p->in_element('NAME')) {
my $site = $sites{$lastid};
$site->{NAME} = $a;
} elsif ($p->in_element('PLEDGEQUARTER')) {
my $site = $sites{$lastid};
$site->{QUARTER} = $a;
} elsif ($p->in_element('JOB_SLOTS')) {
my $site = $sites{$lastid};
$site->{SLOTS} = $a;
} elsif ($p->in_element('DISK_STORE')) {
my $site = $sites{$lastid};
$site->{DISK} = $a;
} elsif ($p->in_element('TAPE_STORE')) {
my $site = $sites{$lastid};
$site->{TAPE} = $a;
}
}
sub h_char_names {
my $p = shift;
my $a = shift;
if ($p->in_element('id')) {
$id2 = $a;
} elsif ($p->in_element('site')) {
$sitename = $a;
} elsif ($p->in_element('cms') and $sitename) {
$site2cms{$sitename} = $a;
$site2id{$sitename} = $id2;
}
}
sub timestamp {
my @time = localtime(time);
my $timestamp = sprintf("%s-%02d-%02d %02d:%02d:%02d",
1900 + $time[5],
1 + $time[4],
$time[3],
$time[2],
$time[1],
$time[0]
);
return $timestamp;
}
sub curr_quarter {
my @time = gmtime(time);
my $year = 1900 + $time[5];
my $q = int(($time[4] / 3) + 1);
return "$year.$q";
}
sub gt {
my ($q1, $q2) = @_;
my @a = split /\./, $q1;
my $a = $a[0]*10 + $a[1];
@a = split /\./, $q2;
my $b = $a[0]*10 + $a[1];
if ($a > $b) {
return 1;
} else {
return 0;
}
}
sub diff {
my ($a1, $a2) = @_;
my ($y1, $q1) = split /\./, $a1;
my ($y2, $q2) = split /\./, $a2;
return ($y1 - $y2 ) * 4 + ($q1 - $q2);
}