在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例常规C/C++编程 → http 请求日志监控(c++)

http 请求日志监控(c++)

常规C/C++编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.35M
  • 下载次数:21
  • 浏览次数:489
  • 发布时间:2018-06-19
  • 实例类别:常规C/C++编程
  • 发 布 人:ssxin
  • 文件格式:.gz
  • 所需积分:5
 相关标签: 日志 监控

实例介绍

【实例简介】

## 功能介绍
对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

【实例截图】

from clipboard

【核心代码】

/*
 * 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()));
}

标签: 日志 监控

实例下载地址

http 请求日志监控(c++)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警