zoukankan      html  css  js  c++  java
  • Linux下日志文件监控系统Logwatch的使用记录

    Linux下日志文件监控系统Logwatch的使用记录

     原文:http://www.cnblogs.com/kevingrace/p/6519504.html

    在维护Linux服务器时,经常需要查看系统中各种服务的日志,以检查服务器的运行状态,如登陆历史、邮件、软件安装等日志。作为运维人员,我们一个个去检查会十分不方便;且大多时候,这会是一种被动的检查,即只有在发现系统运行异常时才会想到去查看日志以获取异常的信息。那么如何主动、集中的分析这些日志,并产生报告,定时发送给管理员就会显得十分重要。对于运维人员来说,发现一款能把原始的日志文件转换成更人性化的记录摘要的工具,将会受益无穷。

    logwatch介绍:
    1)logwatch是一款用Perl 语言编写的开源日志解析分析器。它能对原始的日志文件进行解析并转换成结构化格式的文档,也能根据您的使用情况和需求来定制报告。logwatch的主要目的是生成更易于使用的日志摘要,并不是用来对日志进行实时的处理和监控的。正因为如此,logwatch通常被设定好时间和频率的自动定时任务来调度运行或者是有需要日志处理的时候从命令行里手动运行。一旦日志报告生成,logwatch 可以通过电子邮件把这报告发送给您,您可以把它保存成文件或者直接显示在屏幕上。
    2)Logwatch报告的详细程度和报告覆盖范围是完全可定制化的。Logwatch 的日志处理引擎也是可扩展的,从某种意义上来说,如果您想在一个新的应用程序中使用 logwatch 功能的话,只需要为这个应用程序的日志文件编写一个日志处理脚本(使用 Perl 语言),然后挂接到 logwatch 上就行。
    3)logwatch的缺点就在于:在它生成的报告中没有详细的时间戳信息,而原来的日志文件中是存在的。只能知道被记录下来的一段时间之内的特定事件,如果想要知道精确的时间点的信息,就不得不去查看原日志文件了。

    logwatch安装
    Logwatch能够对Linux 日志文件进行分析,并自动发送mail给相关处理人员,可定制需求。Logwatch的mail功能是借助宿主系统自带的mail server发邮件的,所以系统需安装mail server(如sendmail,postfix等)

    1
    [root@xqsj-beta ~]# yum install -y logwatch    

    查看logwatch帮助

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    [root@xqsj-beta ~]# logwatch --help
     
    Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>]
       [--print] [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
       [--save <filename>] [--help] [--version] [--service <name>]
       [--numeric] [--output <output_type>]
       [--splithosts] [--multiemail] [--no-oldfiles-log]
     
    --detail <level>: Report Detail Level - High, Med, Low or any #.
    --logfile <name>: *Name of a logfile definition to report on.
    --logdir <name>: Name of default directory where logs are stored.
    --service <name>: *Name of a service definition to report on.
    --print: Display report to stdout.
    --mailto <addr>: Mail report to <addr>.
    --archives: Use archived log files too.
    --save <filename>: Save to <filename>.
    --range <range>: Date range: Yesterday, Today, All, Help
                                 where help will describe additional options
    --numeric: Display addresses numerically rather than symbolically and numerically
               (saves  a  nameserver address-to-name lookup).
    --debug <level>: Debug Level - High, Med, Low or any #.
    --splithosts: Create a report for each host in syslog.
    --multiemail: Send each host report in a separate email.  Ignored if
                  not using --splithosts.
    --output <output type>: Report Format - mail, html or unformatted#.
    --encode: Use base64 encoding on output mail.
    --no-oldfiles-log: Suppress the logwatch log, which informs about the
                       old files in logwatch tmpdir.
    --version: Displays current version.
    --help: This message.
    * = Switch can be specified multiple times...

    常用参数选项的解释:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    --detail < level>:   报告的详细度,可选项: High, Med, Low , 数字 0-10
    --logfile < name>:   指日志文件名
    --service < name>:   服务名,有对应的解析脚本,可以在 /usr/share/logwatch/scripts/services中找到
    --print:             打印打标准输出
    --mailto < addr>:    收件人地址
    --archives:          以归档格式发送。使用压缩的文件, 轮转的文件, 例如 messages.1、messages.1.gz
    --save < filename>:  保存到文件
    --range < range>:    日期范围, Yesterday, Today, All
    --debug < level>:    调试级别: High, Med, Low
    --splithosts:        为每个主机创建一份报告
    --multiemail:        将报告发送给多个邮件地址,没有使用--splithosts参数时,这个选项自动忽略
    --output:            配置指令定义了一份 logwatch 报告的格式。有mail邮箱格式和html格式

    安装后的目录文件说明:
    配置文件在/etc/logwatch目录下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@xqsj-beta ~]# ll /etc/logwatch/
    total 8
    drwxr-xr-x 4 root root 4096 Mar  8 11:16 conf
    drwxr-xr-x 3 root root 4096 Mar  8 11:16 scripts
    [root@xqsj-beta ~]# ll /etc/logwatch/conf/
    total 20
    -rw-r--r-- 1 root root   81 Jan 11  2016 ignore.conf
    drwxr-xr-x 2 root root 4096 Jan 11  2016 logfiles
    -rw-r--r-- 1 root root  103 Jan 11  2016 logwatch.conf
    -rw-r--r-- 1 root root   77 Jan 11  2016 override.conf
    drwxr-xr-x 2 root root 4096 Jan 11  2016 services
    [root@xqsj-beta ~]# cat /etc/logwatch/conf/logwatch.conf
    # Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)

    相关配置模板文件是/usr/share/logwatch/default.conf/logwatch.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    /usr/share/logwatch这个目录下的文件结果如下:
        default.conf/     # 配置目录
            logwatch.conf   # 主配置文件,收件人,级别等
            logfiles/       # 定义待分析服务的日志文件组路径,相对于/var/log(*.conf)
            services/       # 自定义需分析日志的Service目录(*.conf)
        scripts/          # 可执行脚本
            logwatch.pl     # 启动分析的perl脚本,/usr/sbin/logwatch的源链接
            logfiles/       # 可包含多个logwatch日志文件组的子目录,对应的日志服务运行的时候,子目录下的脚本会自动被调用
            services/       # logwatch日志服务的过滤脚本,一一对应
            shared/         # 可被多个logwatch日志服务引用的脚本
        dist.conf/
            logfiles/
            services/
        lib/

    默认情况下使用的是/usr/share/logwatch/default.conf/logwatch.conf作为主配置文件,但在/etc/logwatch/conf/logwatch.conf中的存在配置选项会覆盖前一个(/usr/share/logwatch下的logwatch.conf还是会起作用,比如在/etc/logwatch的logwatch.conf中没有的选项)。但优先级最高的是在执行命令行中指定的选项。
    也就是说:如果不配置/etc/logwatch/conf/logwatch.conf,即它是一个空文件,那么默认配置会使用 /usr/share/logwatch/default.conf/logwatch.conf

    在/etc/logwatch下也存在一个与/usr/share/logwatch类似的目录结构,可以在这里添加自定义的监控日志信息。
    比如说:logwatch监控时的选项--service后面跟的是server name,默认情况下/etc/logwatch/scripts/services/目录下为空,可以将/usr/share/logwatch/scripts/services下的服务监控脚本拷贝到/etc/logwatch/scripts/services/下

    logwatch应用
    首先要确保服务器的自带邮箱服务启动了(这里我的是postfix).默认报告的邮件是从logwatch本机发出的,即邮件发件人是“logwatch@$hostname”

    1
    2
    [root@xqsj-beta ~]# /etc/init.d/postfix status
    master (pid  6594) is running...

    接着进行下面安装操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@xqsj-beta ~]# wget http://www.cpan.org/authors/id/M/MS/MSTROUT/YAML-0.84.tar.gz
    [root@xqsj-beta ~]# tar -zvxf YAML-0.84.tar.gz
    [root@xqsj-beta ~]# # cd YAML-0.84
    [root@xqsj-beta YAML-0.84]# perl Makefile.PL
    [root@xqsj-beta YAML-0.84]# make
    [root@xqsj-beta YAML-0.84]# make install
     
    [root@xqsj-beta ~]# wget http://www.cpan.org/authors/id/A/AN/ANDK/CPAN-2.16.tar.gz
    [root@xqsj-beta ~]# tar -zvxf CPAN-2.16.tar.gz
    [root@xqsj-beta ~]# cd CPAN-2.16
    [root@xqsj-beta CPAN-2.16]# perl Makefile.PL
    [root@xqsj-beta CPAN-2.16]# make
    [root@xqsj-beta CPAN-2.16]# make install
     
    [root@xqsj-beta ~]# perl -MCPAN -e 'install HTML::Template'
      
    [root@xqsj-beta ~]# perl -MCPAN -e 'install Sys::CPU'
    [root@xqsj-beta ~]# perl -MCPAN -e 'install Sys::MemInfo'

    1)直接命令行手动监控

    1
    2
    [root@xqsj-beta ~]# logwatch --detail 10 --mailto wangshibo@huanqiu.cn --range all --service all --output mail
    [root@xqsj-beta ~]#

    上面命令中采用的是邮箱格式的报告输出,如下:

    下面采用html格式的报告输出,通常情况下,我都会使用这个格式:

    1
    2
    3
    4
    5
    6
    [root@xqsj-beta ~]# logwatch --detail 10 --mailto wangshibo@huanqiu.cn --range all --service all --output html
    [root@xqsj-beta ~]#
     
    若是想发送给多个邮箱,中间需要用逗号隔开
    [root@xqsj-beta ~]# logwatch --detail 10 --mailto wangshibo@huanqiu.cn,wang_shibo***@163.com --range all --service all --output html
    [root@xqsj-beta ~]#

    收到的邮件中的监控报告如下:

    注意一个细节:
    如上在邮箱里发现报告里的内容太多,有一些无关紧要的服务的监控结果不想打印在报告里,那么就可以在监控时过滤掉这些服务项。
    可以对比邮件里出现的监控列表里的服务去过滤,服务名最后以/etc/logwatch/scripts/services下的名称为准,服务名不正确,会报错说不识别。如下:

    1
    2
    [root@xqsj-beta services]# logwatch --detail 10 --mailto wangshibo@huanqiu.cn --range all --service  All --service -XNTPD --service -Kernel --service -http --service -Postfix --service -pam_unix --service -XNTPD --output html
    [root@xqsj-beta services]#

    再次查看邮件,发现被过滤掉的服务的监控结果就不会出现在报告里了

    这种方式不需要logwatch的配置文件,比较简单,可以将执行命令放到shell脚本中,结合crontab每天定时执行!

    ----------------------------------------------------------------------------------------------
    发送邮件的时候报错如下:
    You have old files in your logwatch tmpdir (/var/cache/logwatch):
    logwatch.A3V8WQ6_
    The directories listed above were most likely created by a
    logwatch run that failed to complete successfully. If so, you
    may delete these directories.

    解决办法:
    # rm -rf /var/cache/logwatch/*
    -------------------------------------------------------------------------------------------

    2)采用配置文件的方式
    需要编辑的文件是/etc/logwatch/conf/logwatch.conf,以下是对配置选项的解释:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    1)LogDir = /var/log     
    定义的日志文件存放路径
     
    2)TmpDir = /var/cache/logwatch
    定义的缓存路径
     
    3)Save = /tmp/logwatch
    开启此项, 会将处理结果保存到本地文件里,如/tmp/logwatch文件, 不会邮寄或显示输出。一般会注释掉此行或不配置此行
     
    4)MailTo = your@mail.com
    发送监控报告的收件人地址,发送给多个邮箱,需要在多个邮箱地址之间用逗号隔开。但是需要在logwatch运行的服务器上配置好本地邮件传输代理(MTA)如sendmail、 Postfix等,这个配置指令项才能起作用。
     
    5)MailFrom = Logwatch
    当你收到邮件时, 显示是谁发给你的。默认是Logwatch
     
    6)Range = <Yesterday|Today|All>
    处理什么时候的日志 , 可选项 All , Yesterday , Today , 即所有, 昨天的 , 今天的。
     
    7)Detail = High
    日志详细度, 可选项 Low , Med , High , 或是 0-10数字
     
    8)Print = No
    可选项, Yes会被打印到系统标准输出, 并且不会以邮件的形式发送到MailTo设定的邮箱里 , No选项则会发到邮箱中。
     
    9)Server = All
    监控的服务项,all表示所有的服务。这个可以到/etc/logwatch/scripts/services/下面去找要监控的服务名称,默认是空的,可以从/usr/share/logwatch/scripts/services下面将服务监控脚本拷贝过来。
    Service选项指定想要监控的一个或多个服务。在 /usr/share/logwatch/scripts/services 目录下列出的服务都能被监控,它们已经涵盖了重要的系统服务(例如:pam,secure,iptables,syslogd 等),也涵盖了一些像 sudo、sshd、http、fail2ban、samba等主流的应用服务。如果您想添加新的服务到列表中,得编写一个相应的日志处理 Perl 脚本,并把它放在这个目录中。
    如果这个选项要用来选择特定的服务话,您需要把文件中的 "Service = All " 这一行注释掉。
     
    另外注意一点:
    可以在不监控的服务前面加 "-" , 如"-httpd"即表示不监控httpd服务,可以写多条。
    一般我们建议的配置是:先监控所有服务,然后在排除不需要监控的服务,比如:
    Service = All
    Service = "-zz-network"
    Service = "-zz-sys"
    Service = "-eximstats"
     
    10)output = <html|mail>
    配置指令定义了一份 logwatch 报告的格式。有mail邮箱格式和html格式。

    下面是自己线上服务器上使用过的一个配置示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    [root@xqsj-beta ~]# cat /etc/logwatch/conf/logwatch.conf
    # Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)
    LogDir = /var/log     
    TmpDir = /var/cache/logwatch
     
    #Save = /tmp/logwatch
     
    MailTo = wangshibo@huanqiu.cn,wang_shibo1987@163.com
    MailFrom = Logwatch
     
    Range = Yesterday
     
    Detail = High
     
    Print = No
     
    Service = All
    Service = "-XNTPD"
    Service = "-Kernel"
    Service = "-http"
    Service = "-Postfix"
    Service = "-pam_unix"
    Service = "-XNTPD"
     
    output = html
     
    然后手动执行下命令:
    [root@xqsj-beta ~]# /usr/sbin/logwatch
    [root@xqsj-beta ~]#
     
    然后去对应的邮箱里查看,发现已有报告邮件。

    另外注意:
    logwatch的工作不是监控日志异常后及时报警的工具,并不具有及时性,logwatch默认每天定时发送一封整合的邮件:
    logwatch默认在crontab定时任务设定目录下存在/etc/cron.daily/0logwatch脚本:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@xqsj-beta ~]# cat /etc/cron.daily/0logwatch
    #!/bin/bash
     
    DailyReport=`grep -e "^[[:space:]]*DailyReport[[:space:]]*=[[:space:]]*" /usr/share/logwatch/default.conf/logwatch.conf | head -n1 | sed -e "s|^s*DailyReports*=s*||"`
     
    if "$DailyReport" != "No" ] && [ "$DailyReport" != "no" ]
    then
        logwatch
    fi

    正是因为这个默认的定时脚本,所以只要如上配置好logwatch.conf文件后,每天都会自动收取一封整合的报告邮件

    如果在logwatch.conf中显式设置了选项DailyReport = No,则会取消logwatch每日执行任务。
    如果要修改logwatch在cron.daily的执行时间,可以删掉/etc/cron.daily/0logwatch这个文件,然后添加到/etc/crontab里去定义执行时间。即:

    1
    2
    3
    4
    5
    [root@xqsj-beta ~]# mv /etc/cron.daily/0logwatch /root/logwatch.sh
     
    比如设定每天早上5点发送邮件
    [root@xqsj-beta ~]# crontab -e
    0 5 * * * /bin/bash -x /root/logwatch.sh

    又或者是删除/etc/cron.daily/0logwatch文件,然后自己在crontab里自定义发送:

    1
    2
    3
    每周1-5的早上8点发送邮件
    [root@xqsj-beta ~]# crontab -e
    0 8 * * 1,2,3,4,5 /usr/sbin/logwatch
  • 相关阅读:
    JS基础类型和引用类型
    ul的margin撑不开想要的距离的办法
    html中的列表
    BEM的命名规则
    意义模糊的函数签名……文档注释
    最基础,新手入门第一段代码
    Cookie的使用
    用servlet校验密码2
    用servlet进行用户名和密码校验
    登录页面
  • 原文地址:https://www.cnblogs.com/liujiacai/p/7798022.html
Copyright © 2011-2022 走看看