
현재 나는 내 웹사이트의 시간당 트래픽(총 입력 요청 수)을 MySQL 테이블에 저장하고 있습니다. 지난 90일간의 데이터를 보관하고 있습니다.
트래픽이 지난 7일 또는 지난 30일의 6시간 트래픽보다 특정 임계값을 초과하여 증가/감소했는지 매 시간(예: 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;
스크립트는 test.traffic_stats
테이블을 쿼리하여 지난 30일 동안의 평균 트래픽을 기반으로 보고서를 생성합니다. 요구 사항에 맞게 스크립트를 수정하십시오. 이제 이 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]