
現在、私は自分のウェブサイトの 1 時間ごとのトラフィック (入力リクエストの合計数) を MySQL テーブルに保存しています。過去 90 日間のデータを保持しています。
毎時間、たとえば 6 時間ごとに、トラフィックが過去 7 日間または過去 30 日間の 6 時間ごとのトラフィックと比較して、あるしきい値を超えて増加/減少したかどうかを確認したいと思います。基本的に、トラフィックのパターンを確認します。時間によって値が異なります。
アラートを生成するために、さまざまな統計メトリックを見つけたいです。少し調べたところ、Statsd
この目的に使用できることがわかりました。
このようなアラートを送信するために使用するのは正しいものですか? これに対するより良い/より簡単な解決策はありますか?
ダッシュボードを構築するつもりはありません。
現在のデータは次のようになります:
+---------------------+---------------------+-----------+----------+
| startTime | endTime | component | traffic |
+---------------------+---------------------+-----------+----------+
| 2015-05-01 00:00:00 | 2015-05-01 01:00:00 | rest | 29090345 |
| 2015-05-01 01:00:00 | 2015-05-01 02:00:00 | rest | 32224087 |
| 2015-05-01 02:00:00 | 2015-05-01 03:00:00 | rest | 35165799 |
| 2015-05-01 03:00:00 | 2015-05-01 04:00:00 | rest | 36903464 |
| 2015-05-01 04:00:00 | 2015-05-01 05:00:00 | rest | 40394130 |
| 2015-05-01 05:00:00 | 2015-05-01 06:00:00 | rest | 44874862 |
| 2015-05-01 06:00:00 | 2015-05-01 07:00:00 | rest | 49988600 |
| 2015-05-01 07:00:00 | 2015-05-01 08:00:00 | rest | 52240544 |
| 2015-05-01 08:00:00 | 2015-05-01 09:00:00 | rest | 54517705 |
| 2015-05-01 09:00:00 | 2015-05-01 10:00:00 | rest | 55277967 |
| 2015-05-01 10:00:00 | 2015-05-01 11:00:00 | rest | 55285309 |
| 2015-05-01 11:00:00 | 2015-05-01 12:00:00 | rest | 55572614 |
答え1
答え2
トラフィックを比較するには、次の SQL スクリプトを使用できます。
set @threshold = 50; /*threshold for comparing the traffic*/
set @limit = 30 /*how many days to consider while generating avg value*/
/*calculate the time range, comparison is done for the last hour*/
set @end_time = current_timestamp();
set @end_time = timestamp(date(@end_time), maketime(hour(@end_time), 0, 0));
set @start_time = date_sub(@end_time, interval 1 hour);
/*find out the traffic for the last hour*/
select traffic
from test.traffic_stats
where startTime >= @start_time
and endTime <= @end_time
into @curr_traffic;
/*now find out the avg traffic for the past @limit days*/
select ifnull(avg(traffic), 0)
from test.traffic_stats
where startTime < @start_time
and startTime >= date_sub(@start_time, interval @limit day)
and time(startTime) >= time(@start_time)
and time(endTime) <= time(@end_time)
into @avg_traffic;
/*generate the report*/
select concat(
'Current traffic '
@curr_traffic,
' is '
if(@curr_traffic > @avg_traffic + @threshold,
'more',
if(@curr_traffic < @avg_traffic - @threshold,
'less',
'same'
)
),
' compared to the avg traffic ',
@avg_traffic
) as result;
スクリプトは、テーブルをクエリして、過去 30 日間の平均トラフィックに基づいてレポートを生成しますtest.traffic_stats
。要件に合わせてスクリプトを変更してください。この SQL スクリプトを として保存し、以下に示すように、mysql コマンド ラインを使用して特定の間隔で実行するreport.sql
ことができます。Cron
mysql -hhost -uuser -ppassword -s <report.sql | awk '{print $1}'
これにより結果が抽出され、stdout に出力されます。これで、GNU Mailutils を使用してアラートを電子メール アドレスに送信できます。
mail -s "$(mysql -hhost -uuser -ppassword -s <report.sql | awk '{print $1}')" [email protected]