forked from CMSCompOps/MonitoringScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_avail_nagios.pl
executable file
·166 lines (137 loc) · 3.79 KB
/
site_avail_nagios.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
#!/usr/bin/perl -w
# Program to generate a text file with the site availabilities for the SSB
#
# Input: none
# Writes the lists to files in the current directory
#
use LWP::Simple;
use XML::Parser;
use File::Temp("tempfile");
# Small class for Sites
package Site;
sub new {
my $class = shift;
my $id = shift;
my $self = {ID => $id, CMS => '', SAM => ''};
bless $self, $class;
}
sub tier {
my $self = shift;
return substr $self->{CMS}, 1, 1;
}
# Main program
package main;
# Array with all Sites and tiers
%sites = ();
#Get XML file from SiteDB
my $url = "https://cmsweb.cern.ch/sitedb/sitedb/reports/showXMLReport/?reportid=naming_convention.ini";
my $doc = get($url) or die "Cannot retrieve XML\n";
my $filepath = "/afs/cern.ch/cms/LCG/SiteComm/site_avail_nagios.txt";
($fh, $tmpfile) = tempfile(UNLINK => 1) or die "Cannot create temporary file\n";
# Parse XML
$p = new XML::Parser(Handlers => {Start => \&h_start, Char => \&h_char});
$p->parse($doc) or die "Cannot parse XML\n";
# Exit if no sites are found
if (! %sites) {
die "No sites found!\n";
}
%seen = ();
foreach my $s ( values %sites ) {
my $cms = $s->{CMS};
next if $seen{$cms}++;
my $t = $s->tier;
next if ( $t eq 'X' );
# Skip T1_CH_CERN
next if ($s->{CMS} eq 'T1_CH_CERN');
my $timestamp = ×tamp;
my $avail = &get_avail($s->{CMS});
die "Cannot get XML\n" if ($avail eq 'error');
next if ( $avail eq 'NA' );
my $colour = 'green';
if ( $t == 0 or $t == 1 ) {
$colour = 'red' if ( $avail ne 'NA' and $avail < 90 );
} elsif ( $t == 2 ) {
$colour = 'red' if ( $avail ne 'NA' and $avail < 80 );
}
$colour = 'red' if ( $avail eq 'NA' );
my $comm_url = &avail_url($s->{CMS});
printf $fh "%s\t%s\t%s\t%s\t%s\n", $timestamp, $s->{CMS}, "${avail}",
$colour, $comm_url;
# Use T0_CH_CERN for T1_CH_CERN
if ( $s->{CMS} eq 'T0_CH_CERN' ) {
printf $fh "%s\t%s\t%s\t%s\t%s\n", $timestamp, 'T1_CH_CERN',
"${avail}", $colour, $comm_url;
}
}
close $fh;
system("/bin/cp -f $tmpfile $filepath");
# 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')) {
my $site = $sites{$lastid};
$site->{CMS} = $a;
}
if ($p->in_element('sam')) {
my $site = $sites{$lastid};
$site->{SAM} = $a;
}
}
sub timestamp {
my @time = gmtime(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 ptime {
my @time = @_;
my $t = sprintf("%s-%02d-%02d",
1900 + $time[5], 1 + $time[4], $time[3]);
return $t;
}
sub get_avail {
my $site = shift;
my $start = &ptime(gmtime(time));
my $end = &ptime(gmtime(time+86400));
my $avail = 'NA';
my $url = "http://dashb-nagios-cms.cern.ch/dashboard/request.py/historicalsiteavailabilityranking?siteSelect3=All%20Sites&sites=${site}&timeRange=individual&start=${start}&end=${end}";
my $cmd = "curl -H \'Accept: text/xml\' \'$url\' 2> /dev/null";
my $output = `$cmd`;
if ( defined $output ) {
if ($output =~ /<num>(.+)<\/num>/) {
$avail = sprintf "%.0f", $1;
}
} else {
return 'error';
}
return $avail;
}
sub avail_url {
my $site = shift;
my $start = &ptime(gmtime(time));
my $end = &ptime(gmtime(time+86400));
my $url = "http://dashb-nagios-cms.cern.ch/dashboard/request.py/historicalsiteavailability?siteSelect3=All%20Sites&sites=${site}&timeRange=individual&start=${start}&end=${end}";
return $url;
}