| 1 | package Salvation::AnyNotify::Plugin::Graphite::Monitor::Discontinuity; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use bignum; | ||
| 6 | |||
| 7 | use base 'Salvation::AnyNotify::Plugin::Graphite::Monitor'; | ||
| 8 | |||
| 9 | use JSON (); | ||
| 10 | use Salvation::Method::Signatures; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | |||
| 14 | POINT_IDX_TIME => 1, | ||
| 15 | POINT_IDX_VALUE => 0, | ||
| 16 | }; | ||
| 17 | |||
| 18 | sub default_memory_limit { 100 } | ||
| 19 | |||
| 20 | method add( | ||
| 21 | Str{1,}|ArrayRef[Str{1,}]{1,} :target!, Int :threshold, | ||
| 22 | Str{1,} :from, Str{1,} :to, Int :memory | ||
| 23 | ) { | ||
| 24 | |||
| 25 | my $core = $self -> core(); | ||
| 26 | my %data = (); | ||
| 27 | |||
| 28 | $from //= '-10min'; | ||
| 29 | $to //= 'now'; | ||
| 30 | $memory //= $self -> default_memory_limit(); | ||
| 31 | |||
| 32 | return sub { | ||
| 33 | |||
| 34 | my $now = time(); | ||
| 35 | my $result = $core -> graphite() -> query( | ||
| 36 | target => $target, | ||
| 37 | from => $from, | ||
| 38 | to => $to, | ||
| 39 | ); | ||
| 40 | |||
| 41 | if( defined $result ) { | ||
| 42 | |||
| 43 | my @warnings = (); | ||
| 44 | |||
| 45 | foreach my $metric ( $result -> all_metrics() ) { | ||
| 46 | |||
| 47 | my $target = $metric -> target(); | ||
| 48 | my $storage = $data{ $target } //= { | ||
| 49 | sum => 0, | ||
| 50 | cnt => 0, | ||
| 51 | avg => 0, | ||
| 52 | sum_of_squared_ranges => 0, | ||
| 53 | sum_of_cubed_ranges => 0, | ||
| 54 | m3 => 0, | ||
| 55 | stddev => 0, | ||
| 56 | skew => 0, | ||
| 57 | ( defined $threshold ? ( threshold => $threshold ) : () ), | ||
| 58 | values => [], | ||
| 59 | prev_value => undef, | ||
| 60 | diff => 0, | ||
| 61 | }; | ||
| 62 | |||
| 63 | my $warn = sub { | ||
| 64 | |||
| 65 | my ( $type, %data ) = @_; | ||
| 66 | |||
| 67 | push( @warnings, { | ||
| 68 | time => $now, | ||
| 69 | type => "graphite/discontinuity/${type}", | ||
| 70 | data => \%data, | ||
| 71 | stat => { map( { $_ => $storage -> { $_ } } ( | ||
| 72 | 'avg', 'stddev', 'skew', | ||
| 73 | ) ) }, | ||
| 74 | target => $target, | ||
| 75 | } ); | ||
| 76 | }; | ||
| 77 | |||
| 78 | foreach my $point ( sort( { | ||
| 79 | ( $a -> [ POINT_IDX_TIME ] // 0 ) | ||
| 80 | <=> ( $b -> [ POINT_IDX_TIME ] // 0 ) } @{ $metric -> datapoints() } ) ) { | ||
| 81 | |||
| 82 | my $value = $point -> [ POINT_IDX_VALUE ]; | ||
| 83 | |||
| 84 | next unless defined $value; | ||
| 85 | |||
| 86 | ++ $storage -> { 'cnt' }; | ||
| 87 | $storage -> { 'sum' } += $value; | ||
| 88 | |||
| 89 | $storage -> { 'avg' } = ( $storage -> { 'sum' } / $storage -> { 'cnt' } ); | ||
| 90 | |||
| 91 | if( defined $storage -> { 'prev_value' } ) { | ||
| 92 | |||
| 93 | $storage -> { 'diff' } = abs( $value - $storage -> { 'prev_value' } ); | ||
| 94 | |||
| 95 | if( | ||
| 96 | exists $storage -> { 'threshold' } | ||
| 97 | && ( $storage -> { 'diff' } >= $storage -> { 'threshold' } ) | ||
| 98 | ) { | ||
| 99 | |||
| 100 | $warn -> ( threshold => ( | ||
| 101 | diff => $storage -> { 'diff' }, | ||
| 102 | threshold => $storage -> { 'threshold' }, | ||
| 103 | ) ); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | $storage -> { 'prev_value' } = $value; | ||
| 108 | push( @{ $storage -> { 'values' } }, $value ); | ||
| 109 | |||
| 110 | while( scalar( @{ $storage -> { 'values' } } ) > $memory ) { | ||
| 111 | |||
| 112 | shift( @{ $storage -> { 'values' } } ) | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | foreach my $value ( @{ $storage -> { 'values' } } ) { | ||
| 117 | |||
| 118 | $storage -> { 'sum_of_squared_ranges' } += ( | ||
| 119 | ( $value - $storage -> { 'avg' } ) ** 2 | ||
| 120 | ); | ||
| 121 | |||
| 122 | $storage -> { 'sum_of_cubed_ranges' } += ( | ||
| 123 | ( $value - $storage -> { 'avg' } ) ** 3 | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | $storage -> { 'm3' } = ( | ||
| 128 | $storage -> { 'sum_of_cubed_ranges' } / $storage -> { 'cnt' } | ||
| 129 | ); | ||
| 130 | |||
| 131 | $storage -> { 'stddev' } = sqrt( | ||
| 132 | $storage -> { 'sum_of_squared_ranges' } / $storage -> { 'cnt' } | ||
| 133 | ); | ||
| 134 | |||
| 135 | $storage -> { 'skew' } = ( | ||
| 136 | $storage -> { 'm3' } / ( $storage -> { 'stddev' } ** 3 ) | ||
| 137 | ); | ||
| 138 | |||
| 139 | if( $storage -> { 'stddev' } > 0 ) { | ||
| 140 | |||
| 141 | my $highest_z = undef; | ||
| 142 | my $most_abnormal_value = undef; | ||
| 143 | |||
| 144 | foreach my $value ( @{ $storage -> { 'values' } } ) { | ||
| 145 | |||
| 146 | my $z = ( ( $value - $storage -> { 'avg' } ) / $storage -> { 'stddev' } ); | ||
| 147 | |||
| 148 | if( defined $highest_z ) { | ||
| 149 | |||
| 150 | if( $z > $highest_z ) { | ||
| 151 | |||
| 152 | $highest_z = $z; | ||
| 153 | $most_abnormal_value = $value; | ||
| 154 | } | ||
| 155 | |||
| 156 | } else { | ||
| 157 | |||
| 158 | $highest_z = $z; | ||
| 159 | $most_abnormal_value = $value; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | if( defined $highest_z && ( $highest_z > 2 ) ) { | ||
| 164 | |||
| 165 | $warn -> ( abnormal => ( | ||
| 166 | z => $highest_z, | ||
| 167 | most_abnormal_value => $most_abnormal_value, | ||
| 168 | ) ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | if( scalar( @warnings ) > 0 ) { | ||
| 174 | |||
| 175 | my $bus = $core -> bus(); | ||
| 176 | my $json = JSON | ||
| 177 | -> new() | ||
| 178 | -> allow_blessed() | ||
| 179 | -> allow_bignum() | ||
| 180 | ; | ||
| 181 | |||
| 182 | foreach my $warning ( @warnings ) { | ||
| 183 | |||
| 184 | $bus -> notify( $warning -> { 'type' }, $json -> encode( $warning ) ); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | }; | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | |||
| 193 | __END__ |