嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元微信扫码支付:5 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
## 功能介绍
对qps以及接口的平均耗时进行统计
## 特点
(1)轻量: 共219KB(代码量约300行), 无需安装,解压即可使用
(2)低侵入: 基于日志文件进行统计,无需修改原程序
## 使用说明
(1) 二进制包:下载工具包 log_monitor.tar.gz,并解压(http://www.oschina.net/p/log_monitor)
(2) 源码编译:make
之后进行参数配置,可以拷贝现有的,各个参数说明如下:
```
log_file=/home/liao/programs/nginx/logs/access.log
log_reg=\[(.*) \ 0800\]
time_format=%d/%b/%Y:%H:%M:%S
retain_seconds=3600
http_port=3344
log_level=INFO
```
log_file: 需要监控的日志的路径
log_reg: 需要匹配的日期以及耗时信息的正则表达式(需要加括号)
time_format: 日志中日期的格式
retain_seconds: 统计数据保留时间
http_port: 用来通过页面展示统计数据的端口
log_level: log monitor的日志级别
### 启动
./bin/log_monitor [conf_file_path]
### 查看
(1) 在控制台就可以看到系统实时的qps以及每一秒中接口的平均耗时情况
(2) 通过浏览器访问 http://${ip}:{http_port}/show.html
/* * http_monitor.cpp * * Created on: Apr 7, 2015 * Author: liao */ #include <fstream> #include "http_server.h" #include "http_monitor.h" #include "monitor_handler.h" void get_qps_statist_info(Request& request, Response &response) { std::vector<std::string> times; Json::Value root; Json::Value json_data; time_t now = time(NULL); time_t start = now - 10; std::map<time_t, StatInfo> line_stat; LogMonitorHandler &handler = *LogMonitorHandler::get_instance(); handler.get_stat(start, now, line_stat); int max_value = 0; for (size_t i = 0; i < 5; i ) { Json::ArrayIndex index = (Json::ArrayIndex) i; time_t stat_time = start i; struct tm *time_info = localtime(&stat_time); char day_str[80]; bzero(day_str, 80); strftime(day_str, 80, "%Y-%m-%d_%H:%M:%S", time_info); json_data[index]["unit"] = day_str; int pv_value = line_stat.find(start i) == line_stat.end() ? 0 : line_stat[start i].qps; if (pv_value == 0) { LOG_WARN("PV VALUE IS ZERO which stat_time:%d", stat_time); } if (max_value < pv_value) { max_value = pv_value; } json_data[index]["value"] = pv_value; LOG_DEBUG("start_time:%s, pv:%d", day_str, pv_value); } Json::ArrayIndex index = 0; root["JSChart"]["datasets"][index]["type"] = "line"; root["JSChart"]["datasets"][index]["data"] = json_data; response.set_body(root); }; void get_time_statist_info(Request& request, Response &response) { std::vector<std::string> times; Json::Value root; Json::Value json_data; time_t now = time(NULL); time_t start = now - 5; std::map<time_t, StatInfo> line_stat; LogMonitorHandler &handler = *LogMonitorHandler::get_instance(); handler.get_stat(start, now, line_stat); for (size_t i = 0; i < 5; i ) { Json::ArrayIndex index = (Json::ArrayIndex) i; time_t stat_time = start i; struct tm *time_info = localtime(&stat_time); char day_str[80]; bzero(day_str, 80); strftime(day_str, 80, "%Y-%m-%d_%H:%M:%S", time_info); json_data[index]["unit"] = day_str; int pv_value = line_stat.find(start i) == line_stat.end() ? 0 : line_stat[start i].qps; int avg_cost_time = 0; if (pv_value != 0) { avg_cost_time = line_stat[start i].total_time / pv_value; } json_data[index]["value"] = avg_cost_time; LOG_DEBUG("start_time:%s, avg_cost_time:%d", day_str, avg_cost_time); } Json::ArrayIndex index = 0; root["JSChart"]["datasets"][index]["type"] = "line"; root["JSChart"]["datasets"][index]["data"] = json_data; response.set_body(root); }; void static_source_handler(Request& request, Response &res) { Json::Value root; std::string uri = request.get_request_uri(); uri.replace(0, 1, ""); LOG_DEBUG("GET replaced uri:%s", uri.c_str()); std::string file_path = "resources/"; file_path = uri; std::fstream fs(file_path.c_str()); std::stringstream ss; if(fs) { int file_max_size = 500 * 1024; char buffer[file_max_size]; bzero(buffer, file_max_size); fs.read(buffer, file_max_size); ss << std::string(buffer); } res.body = ss.str(); std::string content_type = "text/html"; if (uri.find(".js") != uri.npos) { content_type = "text/javascript;charset=UTF-8"; } res.set_head("Content-Type", content_type); } void *start_http_server(void *ptr) { // start a http server std::map<std::string, std::string> *configs = (std::map<std::string, std::string> *) ptr; HttpServer http_server; http_server.add_mapping("/get_count", get_qps_statist_info); http_server.add_mapping("/get_time", get_time_statist_info); http_server.add_mapping("/show.html", static_source_handler); http_server.add_mapping("/jscharts.js", static_source_handler); http_server.start(atoi((*configs)["http_port"].c_str())); }