# workerman-report **Repository Path**: owenzhang24/workerman-report ## Basic Information - **Project Name**: workerman-report - **Description**: webman应用监控系统 TransferStatistics https://github.com/hsk99/transfer-statistics - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-05-16 - **Last Updated**: 2023-05-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # TransferStatistics TransferStatistics 使用[webman](https://github.com/walkor/webman)开发的一个应用监控系统,用于查看应用调用记录、请求量、调用耗时、调用分析等。 系统使用 `UDP` 接收上报数据;使用 `Redis` 存储、汇总数据 # 项目管理 ## 本项目采用php8.1版本,cli模式运行 请使用 php81 start.php (restart | start | stop) 命令进行控制 守护模式 -d 帐号:admin 密码:tayun2022 测试环境地址:http://testchongdianreport.xbshouyou.com ## Contact QQ:279387808 E-mail:wolfcode@88.com # Manual https://www.workerman.net/doc/webman # 安装 ## composer安装 创建项目 `composer create-project hsk99/transfer-statistics` ## 下载安装 1、下载 或 `git clone https://github.com/hsk99/transfer-statistics` 2、执行命令 `composer install` ## 配置 1、config/redis.php 设置 Redis 2、config/app.php 设置 登录用户名、密码 3、config/server.php 设置 WebServer 4、config/process.php 设置 采集服务 ## 运行 执行命令 `php start.php start` ## 查看统计 浏览器访问 `http://ip地址:55573` ## nginx ``` server { listen 80; server_name 139.196.153.113; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/chongdian-report/public; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-00.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/chongdian-report.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log /dev/null; access_log /dev/null; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log /dev/null; } location / { proxy_pass http://127.0.0.1:55573; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; } access_log /www/wwwlogs/chongdian-report.com.log; error_log /www/wwwlogs/chongdian-report.com.error.log; } ``` ## 上报数据 使用类库:Client/StatisticClient.php 1、webman 中间件使用 config/app.php 增加 `statisticAddress` 项,设置 上报地址 ``` getRealIp($safe_mode = true); $controller = $request->controller; $action = $request->action; $transfer = $controller . '::' . $action; // 开始计时 $unique = StatisticClient::tick('project', $ip, $transfer); $response = $next($request); $code = $response->getStatusCode(); $success = $code < 400; $details = [ 'ip' => $request->getRealIp($safe_mode = true) ?? '', // 请求客户端IP 'url' => $request->fullUrl() ?? '', // 请求URL 'method' => $request->method() ?? '', // 请求方法 'request_param' => $request->all() ?? [], // 请求参数 'request_header' => $request->header() ?? [], // 请求头 'cookie' => $request->cookie() ?? [], // 请求cookie 'session' => $request->session()->all() ?? [], // 请求session 'response_code' => $response->getStatusCode() ?? '', // 响应码 'response_header' => $response->getHeaders() ?? [], // 响应头 'response_body' => $success ?: (string)$response->rawBody(), // 响应数据(发生异常) ]; // 数据上报 StatisticClient::report($unique, 'project', $ip, $transfer, $success, $code, json_encode($details, 320)); return $response; } } ``` 2、通用使用 ``` $project, 'ip' => $ip, 'transfer' => $transfer, 'code' => $code, 'success' => $success ], 320); // 详情(JSON格式) StatisticClient::report($unique, $project, $ip, $transfer, $success, $code, $details); ``` 3、SQL监控(ThinkORM 示例) ``` \think\facade\Db::listen(function ($sql, $runtime, $master) { switch (true) { case is_numeric($runtime): $transfer = $sql; $cost = $runtime; break; case !is_numeric($runtime) && 'CONNECT' === substr($sql, 0, 7): @preg_match("/UseTime:([0-9]+(\\.[0-9]+)?|[0-9]+(\\.[0-9]+))/", $sql, $result); if (count($result) > 1) { $transfer = substr($sql, strpos($sql, "s ] ") + 4); $cost = $result[1]; } else { $transfer = $sql;; $cost = 0; } break; default: $transfer = $sql;; $cost = 0; break; } StatisticClient::report('', 'projectSql', '127.0.0.1', $transfer, true, 1, json_encode([ 'sql' => $sql, 'runtime' => $cost . 's', 'master' => $master, ], 320), $cost); }); ```