-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher_sla.pl
executable file
·392 lines (315 loc) · 12.9 KB
/
watcher_sla.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Switch;
use lib '/usr/local/bin/opsadmin/perl/';
use DBI;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case bundling);
use XML::Simple;
use XML::XPath;
require "ctime.pl";
our $AUTHOR = 'John_Vossler@McAfee.com';
our $VERSION = 0.9;
our %flags = ();
$ENV{'PGPASSWORD'} = 'dbG0d'; # Export the postgres password
sub get_opts();
sub usage();
&get_opts();
if($flags{'debug'})
{
print "\nflags\n";
print Dumper(\%flags);
}
##
## Constants
##
my @tspans = (30,60,90,180);
##
## variables
##
my @record = ();
my @records = ();
my $line = ();
my $seg = ();
my @segs = ();
my @ssegs = ();
my @lsegs = ();
my @list = ();
my @solrips1 = ();
my @solrips2 = ();
my $site = ();
my @sites = ();
my @classes = ();
my %seen = ();
my $ip = ();
my $table = ();
my $dest = ();
my $destsrv = ();
my $index = ();
my $nindex = ();
my $timestamp = ();
my $tspan = ();
my $sth = ();
my $dbh = ();
my $psqlcmd = ();
my @locs = ();
my $loc1 = ();
my $loc2 = ();
##
## Get config information for which sites to draw pretty pictures for from table names in DB
##
#
# Get each type of class of SLA data
#
@classes = `/usr/bin/psql -At -U postgres watcher -c "select relname from pg_class where relname ~ '_sla_'" | grep -v pkey | grep -v lastrun |awk -F\_ '{print \$3}'`;
chomp(@sites);
#
# Make the list of sites unique
#
undef %seen;
@classes = grep(!$seen{$_}++, @classes);
chomp(@classes);
if ( $flags{'verbose'} )
{
print "\nlist of classes\n";
print Dumper \@classes;
}
#
# Get each domain for each class of SLA data
#
@sites = `/usr/bin/psql -At -U postgres watcher -c "select relname from pg_class where relname ~ '_sla_'" | awk -F\_ '{print \$1}'`;
chomp(@sites);
#
# Make the list of sites unique
#
undef %seen;
@sites = grep(!$seen{$_}++, @sites);
chomp(@sites);
if ( $flags{'verbose'} )
{
print "\nlist of sites\n";
print Dumper \@sites;
}
##
## Now generate the graphics for the web site
##
##
## Create all the functions we will need for graph creation
##
#
# define graphs for the SLA plots for a given US site
#
$dbh = DBI->connect("dbi:Pg:dbname=watcher;host=localhost","postgres","") || die $DBI::errstr;
if ( $flags{'verbose'} )
{
print "\nconnect status in dbh\n";
print Dumper $dbh;
}
$psqlcmd = "create or replace function sla_plot_us(varchar, integer) returns void as \$\$
filename<-paste(\"\",arg1,\"-sla-\",arg2,\".png\",sep=\"\");
ffilename<-paste(\"\",\"var\",\"www\",\"html\",\"watcher\",\"sla\",filename,sep=\"/\");
png(ffilename,width = 1200,height = 1600,units = \"px\",pointsize = 16);
par(mfrow=c(5,2),oma=c(0,0,3,0),cex.main=2);
# 1,1
query<<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'inbound' \",sep=\"\");
segplot<<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Inbound mail uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 1,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'inbound' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Inbound Latency\");
lines(segplot,col=\"blue\",type=\"l\");
# 2,1
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'outbound' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Outbound mail uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 2,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'outbound' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Outbound mail Latency\");
lines(segplot,col=\"blue\",type=\"l\");
# 3,1
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'web' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Web uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 3,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'web' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Web Latency\");
lines(segplot,col=\"blue\",type=\"l\");
# 4,1
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and ( dnslabel ~ 'portal' or dnslabel ~ 'console' ) \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Customer Interface Uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 4,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and ( dnslabel ~ 'portal' or dnslabel ~ 'console' ) \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Customer Interface Latency\");
lines(segplot,col=\"blue\",type=\"l\");
# 5,2
### arc uptime
# 5,2
### arc latency
title(paste(\"SLA data for \",arg1,\" \",sep=\"\"),outer=TRUE);
dev.off();
system(paste(\"chmod 644 \",ffilename,sep=\"\"));
par(mfrow=c(1,1));
\$\$ language plr;
" ;
$sth = $dbh->prepare($psqlcmd);
if ( $flags{'verbose'} )
{
print "\nsth - after prepare\n";
print Dumper $sth;
}
$sth->execute() || die $DBI::errstr;
if ( $flags{'verbose'} )
{
print "\nsth - after execute\n";
print Dumper $sth;
}
$sth->finish();
$dbh->disconnect();
#
# define graphs for the SLA plots for a given NON-US site
#
$dbh = DBI->connect("dbi:Pg:dbname=watcher;host=localhost","postgres","") || die $DBI::errstr;
if ( $flags{'verbose'} )
{
print "\nconnect status in dbh\n";
print Dumper $dbh;
}
$psqlcmd = "create or replace function sla_plot(varchar, integer) returns void as \$\$
filename<-paste(\"\",arg1,\"-sla-\",arg2,\".png\",sep=\"\");
ffilename<-paste(\"\",\"var\",\"www\",\"html\",\"watcher\",\"sla\",filename,sep=\"/\");
png(ffilename,width = 1200,height = 800,units = \"px\",pointsize = 16);
par(mfrow=c(2,2),oma=c(0,0,3,0),cex.main=2);
###pg.thrownotice(arg1);
###pg.thrownotice(arg2);
# 1,1
query<<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'inbound' \",sep=\"\");
segplot<<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Inbound mail uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 1,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'inbound' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Inbound Latency\");
lines(segplot,col=\"blue\",type=\"l\");
# 2,1
######query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'outbound' \",sep=\"\");
######segplot<-pg.spi.exec(query);
######segplot\$sla_date<-as.Date(segplot\$sla_date);
######plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Outbound mail uptime\");
######lines(segplot,col=\"blue\",type=\"l\");
# 2,2
######query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'outbound' \",sep=\"\");
######segplot<-pg.spi.exec(query);
######segplot\$sla_date<-as.Date(segplot\$sla_date);
######plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Outbound mail Latency\");
######lines(segplot,col=\"blue\",type=\"l\");
# 3,1
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(case when polls=0 then 0 else ((successes*100.00)/polls) end ) as uptime from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'web' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",usr=c(0,105),xlab=\"date\",ylab=\"% uptime\",main=\"Web uptime\");
lines(segplot,col=\"blue\",type=\"l\");
# 3,2
query<-paste(\"select date_trunc('day',epochtime)::date as sla_date,(latency*1000.00) as msec from \",arg1,\"_sla_tesla where epochtime between (now() - '\",arg2,\" days'::interval) and (now() - '1 day'::interval) and dnslabel ~ 'web' \",sep=\"\");
segplot<-pg.spi.exec(query);
segplot\$sla_date<-as.Date(segplot\$sla_date);
plot(segplot,col=\"red\",pch=20,type=\"p\",xlab=\"date\",ylab=\"Latency ms\",main=\"Web Latency\");
lines(segplot,col=\"blue\",type=\"l\");
title(paste(\"SLA data for \",arg1,\" \",sep=\"\"),outer=TRUE);
dev.off();
system(paste(\"chmod 644 \",ffilename,sep=\"\"));
par(mfrow=c(1,1));
\$\$ language plr;
" ;
$sth = $dbh->prepare($psqlcmd);
if ( $flags{'verbose'} )
{
print "\nsth - after prepare\n";
print Dumper $sth;
}
$sth->execute() || die $DBI::errstr;
if ( $flags{'verbose'} )
{
print "\nsth - after execute\n";
print Dumper $sth;
}
$sth->finish();
$dbh->disconnect();
##
## Now call the various functions with all appropriate data for each picture
##
foreach $site (@sites)
{
foreach $tspan (@tspans)
{
#
# Produce the plot for this site and tspan
#
if (( $site eq "us1" ) || ( $site eq "us2" ))
{
print "\nPrinting SLA plots for $site and $tspan days \n";
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"select sla_plot_us('$site', $tspan)\"") == 0 or die "FATAL ERROR: Could not run function sla_plot_us \n";
}
else
{
print "\nPrinting SLA plots for $site and $tspan days \n";
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"select sla_plot('$site', $tspan)\"") == 0 or die "FATAL ERROR: Could not run function sla_plot \n";
}
}
}
##
## End main - Begin subroutines
##
#
# Get all command line options into flags hash
#
sub get_opts()
{
use Getopt::Long qw(:config no_ignore_case bundling);
Getopt::Long::Configure("bundling");
GetOptions(
'debug|d' => \$flags{'debug'},
'verbose|v' => \$flags{'verbose'},
'graph|g' => \$flags{'graph'},
'sync|s' => \$flags{'sync'},
'help|usage|h' => sub {warn &usage; exit 1;})
or die &usage;
defined($flags{'debug'}) || ($flags{'debug'} = 0);
defined($flags{'verbose'}) || ($flags{'verbose'} = 0);
}
# Subroutine: usage
# Args: <void>
# Return Value: <void>
# Purpose: Write the appropriate usage to STDOUT.
sub usage()
{
my $usage = <<EOF;
Usage: $0 [OPTIONS]
-v, --verbose Verbose Mode
-d, --debug Debug Mode (No DB pull)
-g, --graph Only print out graphs
-h, --help Print this help
EOF
print $usage;
}