| 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!, Num :step_threshold, | ||
| 22 | Str{1,} :from, Str{1,} :to, Int :memory, Num :avg_threshold, | ||
| 23 | Num :stddev_threshold, Num :skew_threshold, Num :z_threshold | ||
| 24 | ) { | ||
| 25 | |||
| 26 | my $core = $self -> core(); | ||
| 27 | my %data = (); | ||
| 28 | |||
| 29 | $from //= '-10min'; | ||
| 30 | $to //= 'now'; | ||
| 31 | $memory //= $self -> default_memory_limit(); | ||
| 32 | |||
| 33 | return sub { | ||
| 34 | |||
| 35 | my $now = time(); | ||
| 36 | my $result = $core -> graphite() -> query( | ||
| 37 | target => $target, | ||
| 38 | from => $from, | ||
| 39 | to => $to, | ||
| 40 | ); | ||
| 41 | |||
| 42 | if( defined $result ) { | ||
| 43 | |||
| 44 | my @warnings = (); | ||
| 45 | |||
| 46 | foreach my $metric ( $result -> all_metrics() ) { | ||
| 47 | |||
| 48 | my $target = $metric -> target(); | ||
| 49 | my $storage = $data{ $target } //= { | ||
| 50 | sum => 0, | ||
| 51 | cnt => 0, | ||
| 52 | avg => 0, | ||
| 53 | sum_of_squared_ranges => 0, | ||
| 54 | sum_of_cubed_ranges => 0, | ||
| 55 | m3 => 0, | ||
| 56 | stddev => 0, | ||
| 57 | skew => 0, | ||
| 58 | values => [], | ||
| 59 | prev_value => undef, | ||
| 60 | step => 0, | ||
| 61 | z => 0, | ||
| 62 | z_value => undef, | ||
| 63 | }; | ||
| 64 | |||
| 65 | my $warn = sub { | ||
| 66 | |||
| 67 | my ( $type, %data ) = @_; | ||
| 68 | |||
| 69 | push( @warnings, { | ||
| 70 | time => $now, | ||
| 71 | type => "graphite/discontinuity/${type}", | ||
| 72 | data => \%data, | ||
| 73 | stat => { map( { $_ => $storage -> { $_ } } ( | ||
| 74 | 'avg', 'stddev', 'skew', | ||
| 75 | ) ) }, | ||
| 76 | target => $target, | ||
| 77 | } ); | ||
| 78 | }; | ||
| 79 | |||
| 80 | my $max_diff = undef; | ||
| 81 | |||
| 82 | foreach my $point ( sort( { | ||
| 83 | ( $a -> [ POINT_IDX_TIME ] // 0 ) | ||
| 84 | <=> ( $b -> [ POINT_IDX_TIME ] // 0 ) } @{ $metric -> datapoints() } ) ) { | ||
| 85 | |||
| 86 | my $value = $point -> [ POINT_IDX_VALUE ]; | ||
| 87 | |||
| 88 | next unless defined $value; | ||
| 89 | |||
| 90 | ++ $storage -> { 'cnt' }; | ||
| 91 | $storage -> { 'sum' } += $value; | ||
| 92 | |||
| 93 | $storage -> { 'avg' } = ( $storage -> { 'sum' } / $storage -> { 'cnt' } ); | ||
| 94 | |||
| 95 | if( defined $storage -> { 'prev_value' } ) { | ||
| 96 | |||
| 97 | my $diff = abs( $value - $storage -> { 'prev_value' } ); | ||
| 98 | |||
| 99 | if( defined $max_diff ) { | ||
| 100 | |||
| 101 | if( $max_diff < $diff ) { | ||
| 102 | |||
| 103 | $max_diff = $diff; | ||
| 104 | } | ||
| 105 | |||
| 106 | } else { | ||
| 107 | |||
| 108 | $max_diff = $diff; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | $storage -> { 'prev_value' } = $value; | ||
| 113 | push( @{ $storage -> { 'values' } }, $value ); | ||
| 114 | |||
| 115 | while( scalar( @{ $storage -> { 'values' } } ) > $memory ) { | ||
| 116 | |||
| 117 | shift( @{ $storage -> { 'values' } } ) | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if( defined $max_diff ) { | ||
| 122 | |||
| 123 | $storage -> { 'step' } = $max_diff; | ||
| 124 | } | ||
| 125 | |||
| 126 | foreach my $value ( @{ $storage -> { 'values' } } ) { | ||
| 127 | |||
| 128 | $storage -> { 'sum_of_squared_ranges' } += ( | ||
| 129 | ( $value - $storage -> { 'avg' } ) ** 2 | ||
| 130 | ); | ||
| 131 | |||
| 132 | $storage -> { 'sum_of_cubed_ranges' } += ( | ||
| 133 | ( $value - $storage -> { 'avg' } ) ** 3 | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | $storage -> { 'm3' } = ( | ||
| 138 | $storage -> { 'sum_of_cubed_ranges' } / $storage -> { 'cnt' } | ||
| 139 | ); | ||
| 140 | |||
| 141 | $storage -> { 'stddev' } = sqrt( | ||
| 142 | $storage -> { 'sum_of_squared_ranges' } / $storage -> { 'cnt' } | ||
| 143 | ); | ||
| 144 | |||
| 145 | $storage -> { 'skew' } = ( | ||
| 146 | $storage -> { 'm3' } / ( $storage -> { 'stddev' } ** 3 ) | ||
| 147 | ); | ||
| 148 | |||
| 149 | if( $storage -> { 'stddev' } > 0 ) { | ||
| 150 | |||
| 151 | my $highest_z = undef; | ||
| 152 | my $most_abnormal_value = undef; | ||
| 153 | |||
| 154 | foreach my $value ( @{ $storage -> { 'values' } } ) { | ||
| 155 | |||
| 156 | my $z = ( ( $value - $storage -> { 'avg' } ) / $storage -> { 'stddev' } ); | ||
| 157 | |||
| 158 | if( defined $highest_z ) { | ||
| 159 | |||
| 160 | if( $z > $highest_z ) { | ||
| 161 | |||
| 162 | $highest_z = $z; | ||
| 163 | $most_abnormal_value = $value; | ||
| 164 | } | ||
| 165 | |||
| 166 | } else { | ||
| 167 | |||
| 168 | $highest_z = $z; | ||
| 169 | $most_abnormal_value = $value; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | $storage -> { 'z' } = $highest_z; | ||
| 174 | $storage -> { 'z_value' } = $most_abnormal_value; | ||
| 175 | |||
| 176 | if( | ||
| 177 | defined $z_threshold | ||
| 178 | && ( $storage -> { 'z' } > $z_threshold ) | ||
| 179 | ) { | ||
| 180 | |||
| 181 | $warn -> ( z_threshold => ( | ||
| 182 | z => $storage -> { 'z' }, | ||
| 183 | value => $storage -> { 'z_value' }, | ||
| 184 | threshold => $z_threshold, | ||
| 185 | ) ); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | foreach my $spec ( | ||
| 190 | [ stddev => $stddev_threshold ], | ||
| 191 | [ skew => $skew_threshold ], | ||
| 192 | [ avg => $avg_threshold ], | ||
| 193 | [ step => $step_threshold ], | ||
| 194 | ) { | ||
| 195 | |||
| 196 | my ( $key, $threshold ) = @$spec; | ||
| 197 | |||
| 198 | if( | ||
| 199 | defined $threshold | ||
| 200 | && ( $storage -> { $key } > $threshold ) | ||
| 201 | ) { | ||
| 202 | |||
| 203 | $warn -> ( "${key}_threshold" => ( | ||
| 204 | $key => $storage -> { $key }, | ||
| 205 | threshold => $threshold, | ||
| 206 | ) ); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | if( scalar( @warnings ) > 0 ) { | ||
| 212 | |||
| 213 | my $bus = $core -> bus(); | ||
| 214 | my $json = JSON | ||
| 215 | -> new() | ||
| 216 | -> allow_blessed() | ||
| 217 | -> allow_bignum() | ||
| 218 | ; | ||
| 219 | |||
| 220 | foreach my $warning ( @warnings ) { | ||
| 221 | |||
| 222 | $bus -> notify( $warning -> { 'type' }, $json -> encode( $warning ) ); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | }; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||
| 230 | |||
| 231 | __END__ |