Skip to content

Commit 46991e4

Browse files
author
Todd Bruner
committed
report for david and LB memorialization
1 parent 54dac6b commit 46991e4

File tree

4 files changed

+250
-5
lines changed

4 files changed

+250
-5
lines changed

bin/david.pl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use v5.18;
6+
7+
use lib '../lib';
8+
use Scot::Env;
9+
use Scot::App::Report;
10+
use Data::Dumper;
11+
12+
my $config = $ENV{'scot_monthly_report_config_file'} //
13+
'/opt/scot/etc/david.cfg.pl';
14+
15+
my $env = Scot::Env->new( config_file => $config );
16+
my $app = Scot::App::Report->new( env => $env );
17+
18+
my $href = $app->alertgroup_counts;
19+
20+
say "Alertgroups";
21+
say "date\tcount";
22+
foreach my $date (sort keys %$href) {
23+
say "$date\t".$href->{$date};
24+
}
25+
26+
$href = $app->event_counts;
27+
say "\nEvents";
28+
say "date\tcount";
29+
foreach my $date (sort keys %$href) {
30+
say "$date\t".$href->{$date};
31+
}
32+
33+
$href = $app->incident_counts;
34+
say "\nIncidents";
35+
say "date\tcount";
36+
foreach my $date (sort keys %$href) {
37+
say "$date\t".$href->{$date};
38+
}
39+
40+
$href = $app->response_times;
41+
say "\nResponse Times";
42+
printf "%8s\t%8s\t%9s\t%20s\n","date","category","avg","human readable avg";
43+
foreach my $date (sort keys %$href) {
44+
foreach my $category (sort keys %{$href->{$date}}) {
45+
printf "%8s\t%8s\t%6.2f\t%20s\n", $date, $category, $href->{$date}->{$category}->{avg}, $href->{$date}->{$category}->{humanavg} // '';
46+
}
47+
print "\n";
48+
}
49+

lib/Scot/App/Report.pm

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package Scot::App::Report;
2+
3+
use lib '../../../lib';
4+
use strict;
5+
use warnings;
6+
use v5.18;
7+
8+
use DateTime;
9+
use Time::Duration;
10+
use Data::Dumper;
11+
12+
use Moose;
13+
extends 'Scot::App';
14+
15+
has months_ago => (
16+
is => 'ro',
17+
isa => 'Int',
18+
required => 1,
19+
default => 6,
20+
);
21+
22+
has nowdt => (
23+
is => 'rw',
24+
isa => 'DateTime',
25+
lazy => 1,
26+
required => 1,
27+
default => sub { DateTime->now; },
28+
);
29+
30+
has thendt => (
31+
is => 'rw',
32+
isa => 'DateTime',
33+
required => 1,
34+
lazy => 1,
35+
builder => '_build_thendt',
36+
);
37+
38+
sub _build_thendt {
39+
my $self = shift;
40+
my $nowdt = $self->nowdt;
41+
my $thendt = $nowdt->clone();
42+
$thendt->subtract( months => $self->months_ago );
43+
return $thendt;
44+
}
45+
46+
sub alertgroup_counts {
47+
my $self = shift;
48+
my $env = $self->env;
49+
my $mongo = $env->mongo;
50+
my $log = $env->log;
51+
my $nowdt = $self->nowdt;
52+
my $thendt = $self->thendt;
53+
54+
$log->debug("counting alertgroups");
55+
56+
my $query = {
57+
metric => 'alertgroups created',
58+
epoch => {
59+
'$gte' => $thendt->epoch,
60+
'$lte' => $nowdt->epoch,
61+
},
62+
};
63+
64+
$log->debug("query is ",{filter=>\&Dumper, value=>$query});
65+
66+
my $cursor = $mongo->collection('Stat')->find($query);
67+
my %result;
68+
69+
while ( my $stat = $cursor->next ) {
70+
my $key = $stat->year . '/' . $stat->month;
71+
$result{$key} += $stat->value;
72+
}
73+
return wantarray ? %result : \%result;
74+
}
75+
76+
sub event_counts {
77+
my $self = shift;
78+
my $env = $self->env;
79+
my $mongo = $env->mongo;
80+
my $log = $env->log;
81+
my $nowdt = $self->nowdt;
82+
my $thendt = $self->thendt;
83+
84+
$log->debug("counting events");
85+
86+
my $query = {
87+
metric => 'event created',
88+
epoch => {
89+
'$gte' => $thendt->epoch,
90+
'$lte' => $nowdt->epoch,
91+
},
92+
};
93+
94+
$log->debug("query is ",{filter=>\&Dumper, value=>$query});
95+
96+
my $cursor = $mongo->collection('Stat')->find($query);
97+
my %result;
98+
99+
while ( my $stat = $cursor->next ) {
100+
my $key = $stat->year . '/' . $stat->month;
101+
$result{$key} += $stat->value;
102+
}
103+
return wantarray ? %result : \%result;
104+
}
105+
106+
sub incident_counts {
107+
my $self = shift;
108+
my $env = $self->env;
109+
my $mongo = $env->mongo;
110+
my $log = $env->log;
111+
my $nowdt = $self->nowdt;
112+
my $thendt = $self->thendt;
113+
114+
$log->debug("counting incidents");
115+
116+
my $query = {
117+
metric => 'incident created',
118+
epoch => {
119+
'$gte' => $thendt->epoch,
120+
'$lte' => $nowdt->epoch,
121+
},
122+
};
123+
124+
$log->debug("query is ",{filter=>\&Dumper, value=>$query});
125+
126+
my $cursor = $mongo->collection('Stat')->find($query);
127+
my %result;
128+
129+
while ( my $stat = $cursor->next ) {
130+
my $key = $stat->year . '/' . $stat->month;
131+
$result{$key} += $stat->value;
132+
}
133+
return wantarray ? %result : \%result;
134+
}
135+
136+
sub response_times {
137+
my $self = shift;
138+
my $env = $self->env;;
139+
my $mongo = $env->mongo;
140+
my $log = $env->log;
141+
my $nowdt = $self->nowdt;
142+
my $thendt = $self->thendt;
143+
144+
my $query = {
145+
metric => qr/Alertgroup Response Times/i,
146+
epoch => {
147+
'$gte' => $thendt->epoch,
148+
'$lte' => $nowdt->epoch,
149+
},
150+
};
151+
152+
my $cursor = $mongo->collection('Stat')->find($query);
153+
my %result;
154+
155+
while ( my $stat = $cursor->next ) {
156+
my $key = $stat->year . '/' . $stat->month;
157+
my $metric = $stat->metric;
158+
my @words = split(/ /,$metric);
159+
my $type = lc($words[0]);
160+
my $category = lc($words[2]);
161+
my $value = $stat->value;
162+
$result{$key}{$category}{$type} += $value;
163+
}
164+
165+
foreach my $key (keys %result) {
166+
$log->debug("$key");
167+
foreach my $category (keys %{$result{$key}}) {
168+
$log->debug(" $category");
169+
my $seconds = $result{$key}{$category}{sum};
170+
my $count = $result{$key}{$category}{count};
171+
$log->debug({filter=>\&Dumper, value=> $result{$key}{$category}});
172+
if ( !defined $count or $count == 0 ) {
173+
$log->error("count zero or undef!");
174+
if ($seconds == 0) {
175+
$result{$key}{$category}{avg} = 0;
176+
}
177+
else {
178+
$result{$key}{$category}{avg} = 'na';
179+
}
180+
next;
181+
}
182+
183+
$result{$key}{$category}{avg} = $seconds / $count;
184+
$result{$key}{$category}{humanavg} = duration($seconds / $count);
185+
}
186+
}
187+
188+
return wantarray ? %result : \%result;
189+
}
190+
191+
192+
193+
1;

lib/Scot/Controller/Api.pm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -959,11 +959,12 @@ sub promote {
959959
});
960960

961961
## use this to memorialize the likaboss object
962-
## if ( $env->meta->has_attribute('lbwebservice') ) {
963-
## my $rootuid = $object->data->{rootUID};
964-
## $log->debug("memorializing $rootuid");
965-
## $self->env->lbwebservice->memorialize($rootuid);
966-
## }
962+
if ( $env->meta->has_attribute('lbwebservice') ) {
963+
foreach my $rootuid (@{$object->data->{rootUID}}) {
964+
$log->debug("memorializing $rootuid");
965+
$self->env->lbwebservice->memorialize($rootuid);
966+
}
967+
}
967968

968969
}
969970

lib/Scot/Env.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ sub BUILD {
122122
my $log = $self->build_logger($self->log_config);
123123
$meta->add_attribute( log => ( is => 'rw', isa => 'Log::Log4perl::Logger'));
124124
$self->log($log);
125+
126+
$log->debug("\@INC = ",{filter=>\&Dumper, value => \@INC});
125127

126128
# now build the modules
127129
foreach my $href (@{ $modules_aref }) {

0 commit comments

Comments
 (0)