應用程式流量監控和警報

應用程式流量監控和警報

目前,我將網站每小時的流量(輸入請求總數)放入 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

也許InfluxDB您可能會感興趣。 InfluxDB 是一個時間序列資料庫。

您可以透過以下方式將資料直接推送到 InfluxDB

  • 統計數據
  • 收集二進位介面
  • 石墨協定
  • 休息API

您可以透過 REST-API 查詢 InfluxDB,不需要圖形介面。但格拉法納配合它效果很好。

答案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 腳本儲存為report.sql,您可以使用Cronmysql 命令列按特定時間間隔運行它,如下所示。

mysql -hhost -uuser -ppassword -s <report.sql | awk '{print $1}'

這將提取結果並列印到標準輸出。現在您可以使用 GNU Mailutils 將警報傳送到您的電子郵件地址。

mail -s "$(mysql -hhost -uuser -ppassword -s <report.sql | awk '{print $1}')" [email protected]

相關內容