Apache 服务器状态页面是否有任何过滤器可用?

Rap*_*tor 1 apache-2.2

我按mod_status模块启用了 Apache 状态页面。进程列表很长,大部分都是OPTIONS * HTTP/1.0,我想过滤掉。

是否有任何调整、选项或标志来隐藏这些OPTIONS进程?

Dam*_*lli 10

除了重新编译mod_status以满足您的需要(这听起来有点矫枉过正,但....它仍然可行),mod_status提供了一个专为机器可读处理设计的选项。根据官方文档

通过访问页面http://your.server.name/server-status?auto可以获得状态文件的机器可读版本。这在自动运行时很有用 [....]

因此,捕获 mod_status 的输出就像调用wgetcurl或任何其他可以在您的应用程序中启动/包含的 http 客户端库一样简单,以满足您的需要。

不幸的是,我刚刚发现使用“?auto”格式时,ExtendedStatus 指令提供的大多数附加信息都没有显示!这意味着使用“?auto”选项,您无法访问进程列表。

由于听起来有点奇怪,我检查了mod_status模块的源代码。除了额外的和未记录的“?notable”选项,“ apache2-2.2.22/modules/generators/mod_status.c ”(我的 Ubuntu 12.04 LTS 笔记本)中的源代码包括:

 * /server-status - Returns page using tables
 * /server-status?notable - Returns page for browsers without table support
 * /server-status?refresh - Returns page with 1 second refresh
 * /server-status?refresh=6 - Returns page with refresh every 6 seconds
 * /server-status?auto - Returns page with data for automatic parsing
Run Code Online (Sandbox Code Playgroud)

(顺便说一句:我发现阅读“?值得注意 - 没有表格支持的浏览器的返回页面”既有趣又好奇,因为我太老了/太古老了,以至于我记得网络的早期,表格支持是可用浏览器的一个功能!)

我还检查了“?auto”格式中缺少的进程列表是一个设计特性:

#define STAT_OPT_AUTO     2
[...]
static const struct stat_opt status_options[] =
{
    {STAT_OPT_REFRESH, "refresh", "Refresh"},
    {STAT_OPT_NOTABLE, "notable", NULL},
    {STAT_OPT_AUTO, "auto", NULL},
    {STAT_OPT_END, NULL, NULL}
};
[...]
if (r->args) {
[...]
     case STAT_OPT_AUTO:
        ap_set_content_type(r, "text/plain; charset=ISO-8859-1");
        short_report = 1;
        break;
[...] 
if (short_report)
    ap_rputs("\n", r);
else {
    ap_rputs("</pre>\n", r);
    ap_rputs("<p>Scoreboard Key:<br />\n", r);
    [...lots of other things, including "processlist"...]
}
[...]
Run Code Online (Sandbox Code Playgroud)

如您所见,您需要的是最后一个“if”的“else”部分。因此,它不包含在“?auto”格式中,因为在这种情况下我们属于“short_report”情况。

因此,在完成上述所有操作并回到您的问题:“是否有任何调整、选项或标志来隐藏这些 OPTIONS 进程? ”,我的答案是您唯一的选择是“调整”一个小应用程序:

  1. 就像一个 HTTP 客户端,而不是/server-status标准 URL;
  2. 解析结果以从 processlist HTML 表中提取数据;
  3. 跳过与 OPTION 请求相关的表行;
  4. 对其他行做任何你需要的事情。

由于我对 PERL 感到满意并且对HTML::TableExtract模块有一些运气,因此 您可以使用以下内容:

#!/usr/bin/perl

use strict;

use HTML::TableExtract;

# PATH to "curl" utility
my $CURL = "/usr/bin/curl";

# URL of the server-status we want to process
my $STATUS_URL = "http://localhost/server-status";

# those are the headers in the first row of the table we want to extract
# Used by HTML::TableExtract to search for our table, within the whole HTML output
my $headers =['Srv','PID','Acc','M','CPU','SS','Req','Conn','Child','Slot','Client','VHost','Request'];


# Let's fetch the status page...
my $output = `$CURL -s $STATUS_URL`;

# Let's search for our table within the HTML...
my $tables = HTML::TableExtract->new( headers => $headers );

# We found it (hopefully), so let's parse it...
$tables->parse($output);

# ...and let's stick to the first one
my $status_table = $tables->first_table_found;

# Now let's loop allover the rows...
foreach my $row_ref ($status_table->rows) {
      # Let's de-reference the ARRAY reference, so to better manager
      # the various elements...
      my @row = @$row_ref;

      # Let's check for an OPTIONS row...
      if ($row[12]=~/OPTIONS/) {
         # simply skip to next row in the loop
         next;
      }

      # Let's choose whatever columns we want (first column has index "0")
      # So here we have Srv, PID, Client and Request
      foreach my $column (0,1,10,12) {
        print $row[$column]."|";
      }
      print "\n";
}
Run Code Online (Sandbox Code Playgroud)

就我而言,上面的脚本产生以下输出:

verzulli@tablet-damiano:~$ perl elab.pl 
0-1|9183|127.0.0.1|GET /server-status HTTP/1.1|
1-1|9184|127.0.0.1|GET /server-status HTTP/1.1|
2-1|9185|127.0.0.1|GET /server-status HTTP/1.1|
3-1|9186|127.0.0.1|GET /server-status HTTP/1.1|
4-1|9187|127.0.0.1|GET /server-status HTTP/1.1|
5-1|9188|127.0.0.1|GET /server-status HTTP/1.1|
Run Code Online (Sandbox Code Playgroud)

你可以看到,跳过 OPTIONS 行。

请注意,上面的应用程序缺乏基本的错误处理,所以......如果出现问题请不要怪我:-)

  • 这是一篇经过充分研究、记录和措辞的文章,但您已经完成了所有工作,只是询问如何在 OP 的服务器上实现它。我觉得他们应该在这样的答案之前提供更多的信息或背景。仍然,我+1! (2认同)