AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 24593
Accepted
Robert Munteanu
Robert Munteanu
Asked: 2009-06-13 00:03:23 +0800 CST2009-06-13 00:03:23 +0800 CST 2009-06-13 00:03:23 +0800 CST

在 apache logwatch 报告中包含主机名

  • 772

当使用 apache 托管多个域时,查看包含虚拟主机名的 logwatch apache 输出很有用,但我只得到:

 --------------------- httpd Begin ------------------------

 Requests with error response codes
   400 Bad Request
      /: 1 Time(s)
      /robots.txt: 1 Time(s)

而我想要类似的东西

 --------------------- httpd Begin ------------------------

 Requests with error response codes
   400 Bad Request
      example.com/: 1 Time(s)
      example.org/robots.txt: 1 Time(s)

如何使用 logwatch 实现这一目标?

apache-2.2 logwatch
  • 6 6 个回答
  • 4069 Views

6 个回答

  • Voted
  1. Best Answer
    Syquus
    2011-06-02T11:08:15+08:002011-06-02T11:08:15+08:00

    试试这个(对我有用):在你的httpd.confas中定义 LogFormat

    LogFormat "%h %t [%V] \"%r\" %>s \"%{Referer}i\""

    在这种特殊情况下,您将拥有 remote_address、日期/时间、[根据 UseCanonicalName 设置的服务器名称]、请求、状态代码和Referer(这是我想要的格式),然后放

    $LogFormat "%h %t %V \"%r\" %>s \"%{Referer}i\""

    在您的 services/http.conf LogWatch 文件中。那将

    1. 让 apache 输入主机名(规范与否,这取决于您使用的是 %v 还是 %V)
    2. 强制 LogWatch 了解您的 Apache 访问日志

    以下是使用此特定指令集的日志输出中的 a 行示例:

    172.3.20.11 [01/Jun/2011:21:00:52 +0200] joomla.local "GET /images/tabs_back.png HTTP/1.1" 404 " http://joomla.local/templates/beez_20/css/personal .css "

    如果我们关注错误代码,以及它们在 LogWatch 中的处理方式,您可以对/usr/share/logwatch/scripts/services/http进行以下更改:添加:

    我的 $my_host = ""; 我的 $my_url = "";

    然后,关于第 462 行,添加此行以保存我们的第 4 列(HOST):

    $field{my_host} = $field{$log_fields[3]};

    在第 560 行,在fmt_url缩短 ( if (length($field{url}) > 60) {...}) 之后添加:

    $my_host = $field{$log_fields[3]};
    $my_host = substr($my_host,1);
    $my_url=$my_host . $fmt_url;
    

    最后,改变:

    $needs_exam{$field{http_rc}}{$fmt_url}++;

    经过

    $needs_exam{$field{http_rc}}{$my_url}++;

    这样做,你会在你的 Logwatch 中有这个:

    Requests with error response codes
    404 Not Found
    joomla.local/images/tabs_back.png: 3 Time(s)
    

    希望对大家有所帮助

    • 3
  2. Vassil
    2016-01-14T11:49:39+08:002016-01-14T11:49:39+08:00

    我遇到了同样的问题并通过更改LogFormat(http://httpd.apache.org/docs/2.2/mod/mod_log_config.htmlapache.conf)解决了它

    # LogFormat "%h %l %u %t \"%r\" %>s %O" common
    
    # The default output has no info about the server name (%v).
    # %m %U%q %H is strictly equivalent to %r.
    LogFormat "%h %l %u %t \"%m %v%U%q %H\" %>s %O" common
    

    这会生成与默认相同的输出,添加规范服务器名称作为前缀。例如:

    ... "GET www.example.com/apache_pb.gif HTTP/1.0" 200 2326 ... 
    

    优点是您不需要任何其他自定义(例如在 logwatch 端)。缺点是您会为每个记录的行获得一些额外的字符。

    • 3
  3. sucuri
    2009-08-09T04:27:11+08:002009-08-09T04:27:11+08:00

    如果您将所有虚拟域记录到同一个日志文件中,我认为这是不可能的...... apache 日志不会区分它们。

    我还建议您查看开源OSSEC。我们从 logwatch 转移到它,因为它是实时的并且允许集中关联(将 ssh 登录失败与 apache 400 错误相关联)。

    • 1
  4. dunxd
    2011-01-04T15:54:47+08:002011-01-04T15:54:47+08:00

    我找到了一篇博客文章,详细介绍了这个问题以及有人如何解决它。不知道这将如何影响日志分析,但在这里注意到它,因为我发现它很有用。

    • 1
  5. Fran6co
    2009-10-03T15:54:51+08:002009-10-03T15:54:51+08:00

    您可以使用 apache 配置中的 LogFormat 指令获取主机名。您可以使用以下选项

    %{Host}i
    

    我在这个链接中找到了这个信息。Logwatch 应该能够解析自定义信息。

    • 0
  6. lsh
    2013-10-10T06:57:08+08:002013-10-10T06:57:08+08:00

    非常感谢@Syquus,他让我走上了修改/usr/share/logwatch/scripts/services/http文件的正确道路。

    我的文件和解决方案不同,但我认为我会共享相同的。

    我使用vhost_combinedApache 提供的标准 LogFormat,如下所示:

    LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined

    输出类似:

    example.org:80 1.1.1.1 - - [08/Oct/2013:16:55:01 +0000] "GET / HTTP/1.1" 200 6094 "-" "Opera/9.80 (X11; Linux x86_64; Edition Linux Mint) Presto/2.12.388 Version/12.16"

    我把它放在服务配置覆盖中/etc/logwatch/conf/services/http.conf:

    $logformat = "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\""
    

    在找到对@Syquus 的解决方案进行更改的大致正确位置之后/usr/share/logwatch/scripts/services/http,我认为只需将索引从 [3] 更改为 [0] 就可以了——它没有。我得到了不正确的路径段,即使在遍历整个哈希/数组之后,我也没有找到主机名。调试令人沮丧,因为我是 Perl 的新手,但我的解决方案是添加匹配%v被丢弃的内容,然后进一步修改 url 以包含域名。

    我的解决方案的差异(我还删除了 url 截断),YMMV:

    --- __http.2013-10-09   2013-10-09 13:11:48.000000000 +0000
    +++ http        2013-10-09 14:36:59.000000000 +0000
    @@ -132,6 +132,8 @@
     #  Build tables of the log format to parse it and determine whats what
     #
    
    +my $my_url = "";
    +
     my $detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
     my $ignoreURLs = $ENV{'http_ignore_urls'};
     my $ignoreIPs = $ENV{'http_ignore_ips'};
    @@ -379,7 +381,10 @@
     $logformat =~ s/%[\d,!]*/%/g;
     while ($end_loop) {
    
    -   if ($logformat =~ /\G%h/gc) {
    +   if ($logformat =~ /\G%v/gc) {
    +      $parse_string[$parse_index] .= "(\\S*?)";
    +      $parse_field[$parse_index][$parse_subindex++] = "my_host";
    +   } elsif ($logformat =~ /\G%h/gc) {
           $parse_string[$parse_index] .= "(\\S*?)";
           $parse_field[$parse_index][$parse_subindex++] = "client_ip";
        } elsif ($logformat =~ /\G%l/gc) {
    @@ -437,7 +442,6 @@
     #
     #  Process log file on stdin
     #
    -
     while (my $line = <STDIN>) {
        chomp($line);
    
    @@ -580,11 +584,12 @@
              !((defined $ignoreURLs) && ($field{url} =~ /$ignoreURLs/)) &&
              !((defined $ignoreIPs) && ($field{client_ip} =~ /$ignoreIPs/)) ) {
           my $fmt_url = $field{url};
    -      if (length($field{url}) > 60) {
    -         $fmt_url = substr($field{url},0,42) . " ... " .
    -                    substr($field{url},-15,15);
    -      }
    -      $needs_exam{$field{http_rc}}{$fmt_url}++;
    +      #if (length($field{url}) > 60) {
    +      #   $fmt_url = substr($field{url},0,42) . " ... " .
    +      #              substr($field{url},-15,15);
    +      #}
    +      $my_url = $field{my_host} . $fmt_url;
    +      $needs_exam{$field{http_rc}}{$my_url}++;
        }
        if (defined $field{userid} && $field{userid} ne "-" &&
              (eval $user_display) &&
    

    如果我决定在 :80 以外的端口上提供安全内容或内容,我可能会在将来包含它。现在应该很明显了。

    希望这可以帮助!

    更新

    进行了更多更改,修复了一个错误。您可以在这里找到我的修改,而不是继续编辑这个答案:https ://bitbucket.org/ubiquitypress/logwatch

    • 0

相关问题

  • 在您分发的应用程序中使用 Apache HTTPD 运行 SSL 的最佳方式是什么?

  • 阿帕奇的替代品

  • 如何强制我的网址始终以 www 开头?

  • 在 Linux Xen VPS 上优化 Apache 和 MySQL

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve