OceanBase Sql Plan Monitor 对于大查询性能诊断非常好用,之前为了推广,整理了一个离线版。但最终还是觉得差点意思。
能不能类似阿里的“扁鹊”服务,将其服务化呢?使用者只需要登录网页,填写一些基本信息就能一键获得报告?稍微折腾了一下,居然是很简单的。
背景
这个 WebService 最终是通过 lighttpd+php+python 完成
- sql plan monitor 是用 python 写的;
- python 做Web动态脚本非常费事
- PHP 做Web动态脚本非常方便
- nginx 太麻烦,lighttpd 安装部署很省事
方案
lighttpd 作为 http 服务器,下载安装 lighttpd-fastcgi 模块,安装 php-cgi(一般默认已经有)。稍微配置一下即可。配置文件如下:
[xiaochu.yh /ob/xiaochu.yh/share/monitor_web] $cat lighttpd.php.conf
server.modules += ( "mod_fastcgi" )
server.document-root = "/ob/xiaochu.yh/share/monitor_web/"
server.port = 8080
server.dir-listing = "disable"
mimetype.assign = (
".html" => "text/html",
".htm" => "text/html",
".js" => "application/javascript",
".css" => "text/css",
".txt" => "text/plain",
".log" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png",
".ico" => "image/x-icon"
)
fastcgi.server = ( ".php" =>
((
"socket" => "/tmp/php.socket",
"bin-path" => "/usr/bin/php-cgi",
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"min-procs" => 1,
"max-procs" => 1,
"idle-timeout" => 100
))
)
重启 lighttpd 服务即可
lighttpd -f lighttpd.php.conf
服务化脚本
<?php
$IP = $_GET['info-ip'];
$PORT = $_GET['info-port'];
$USER = $_GET['info-user'];
$PWD = $_GET['info-pass'];
$DB = $_GET['info-db'];
$TRACEID = $_GET['info-traceid'];
if (empty($PWD)) {
$cmd = "sh mon.sh -h $IP -P $PORT -u $USER -D $DB --trace-id='$TRACEID' -o report.$TRACEID.html";
} else {
$cmd = "sh mon.sh -h $IP -P $PORT -u $USER -D $DB -p $PWD --trace-id='$TRACEID' -o report.$TRACEID.html";
}
$result = shell_exec($cmd);
$rule = "/Report File Generate OK: (report.*\.html)/i";
preg_match($rule, $result, $m);
if (count($m) == 2) {
header('location:' . $m[1]);
} else {
echo "<p><a href='javascript:history.go(-1)'>Wrong Parameters. Go Back!</a></p>";
}
?>
总结
理论上,任何内部的小工具都可以基于这种方式做简单的服务化。