目前我把我的网站的每小时stream量(input请求总数)放在MySQL表中。 我保留了过去90天的数据。
我想每小时检查一下,比如说第6个小时,交通是否比过去7天或最近30天6小时的交通量增加/减less了一些门槛。 基本上,我看到一种交通模式。 不同的时间有不同的价值。
要生成警报,我想find各种统计指标。 稍微阅读后,我发现Statsd可以用于这个目的。
用这种方式发送警报是否正确? 有没有更好/更简单的解决scheme呢?
我不打算build立任何仪表板。
我目前的数据如下所示:
+---------------------+---------------------+-----------+----------+ | 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 |
也许InfluxDB可能会让你感兴趣。 InfluxDB是一个时间序列数据库。
你可以直接将数据推送到InfluxDB
您可以通过REST-API查询InfluxDB,并且不需要graphics界面。 但格拉法纳对此很好。
您可以使用以下SQL脚本来比较stream量。
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天内基于avgstream量的报告。 请修改脚本以符合您的要求。 现在将这个SQL脚本保存为report.sql并且可以使用Cron以特定的时间间隔使用mysql命令行运行它,如下所示。
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]