如何在Ubuntu 16.04 LTS上安装和使用Lnav Log Viewer

如何在Ubuntu 16.04 LTS上安装和使用Lnav Log Viewer

LNAV也被称为“横向导航”,它是一款免费且开源的增强型日志文件查看器,可用于查找正在查看的文件信息以及时间戳和日志级别。 LNAV是为小型解决方案构建的命令行日志文件查看器。 它很简单,易于使用和定制。 LNAV是一款完全免费的应用程序,可在Linux和Mac操作系统上运行。 它使用户能够在单个窗口中显示来自多个文件的日志,并且还可以看到这些日志的实时更新。

LNAV具有很多功能,其中一些列在下面:

  • 允许您通过SQL查询日志。
  • 能够检测和读取多种日志格式的日志。
  • 它具有着色亮起功能,可帮助用户轻松识别信息,警告和错误之间的差异。
  • 自动从压缩文件中提取数据,如gzip和bzip2。
  • 能够在您输入时搜索日志。 新的日志行会在添加时自动加载和搜索。

在本教程中,我们将解释如何在Ubuntu 16.04服务器上安装和使用lnav。

要求

  • 运行Ubuntu 16.04的服务器。
  • 具有sudo权限的非root用户。

安装Lnav

默认情况下,lnav在Ubuntu 16.04默认存储库中可用。 所以你可以通过运行以下命令来安装它:

sudo apt-get install lnav -y

安装lnav后,您可以通过运行以下命令来查看lnav的版本:

lnav -V

您应该看到以下输出:

lnav 0.6.2

与Lnav合作

您可以使用以下命令查看lnav的所有选项:

lnav -h

您应该看到以下输出:

usage: lnav [-hVsar] [logfile1 logfile2 ...]

A curses-based log file viewer that indexes log messages by type
and time to make it easier to navigate through files quickly.

Key bindings:
  ?     View/leave the online help text.
  q     Quit the program.

Options:
  -h         Print this message, then exit.
  -C         Check configuration and then exit.
  -d file    Write debug messages to the given file.
  -V         Print version information.
  -s         Load the most recent syslog messages file.
  -a         Load all of the most recent log file types.
  -r         Load older rotated log files as well.
  -t         Prepend timestamps to the lines of data being read in
             on the standard input.
  -w file    Write the contents of the standard input to this file.

Optional arguments:
  logfile1          The log files or directories to view.  If a
                    directory is given, all of the files in the
                    directory will be loaded.

Examples:
  To load and follow the syslog file:
    $ lnav -s

  To load all of the files in /var/log:
    $ lnav /var/log

  To watch the output of make with timestamps prepended:
    $ make 2>&1 | lnav -t

Version: lnav 0.6.2

现在,让我们开始运行lnav命令,根据所有日志文件中最新的时间戳来查看实时信息。 该命令将从/ var / log目录内的所有文件中收集日志。

sudo lnav

您应该看到以下输出:

Jan 28 16:56:01 icingaclient wpa_supplicant[926]: wlan0: CTRL-EVENT-SCAN-STARTED
Jan 28 16:59:46 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e)
Jan 28 16:59:46 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1
Jan 28 16:59:46 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 273 seconds.
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
  
    (wlan0): DHCPv4 state changed reboot -> renew
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
   
       address 192.168.137.101
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
    
        prefix 24 (255.255.255.0)
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
     
         gateway 192.168.137.1
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
      
          nameserver '192.168.137.1'
Jan 28 16:59:46 icingaclient NetworkManager[865]: 
       
         domain name 'mshome.net' Jan 28 16:59:46 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 16:59:46 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' Jan 28 17:04:19 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e) Jan 28 17:04:19 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1 Jan 28 17:04:20 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 226 seconds. Jan 28 17:04:20 icingaclient NetworkManager[865]: 
        
          (wlan0): DHCPv4 state changed renew -> renew Jan 28 17:04:20 icingaclient NetworkManager[865]: 
         
           address 192.168.137.101 Jan 28 17:04:20 icingaclient NetworkManager[865]: 
          
            prefix 24 (255.255.255.0) Jan 28 17:04:20 icingaclient NetworkManager[865]: 
           
             gateway 192.168.137.1 Jan 28 17:04:20 icingaclient NetworkManager[865]: 
            
              nameserver '192.168.137.1' Jan 28 17:04:20 icingaclient NetworkManager[865]: 
             
               domain name 'mshome.net' Jan 28 17:04:20 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:04:20 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' Jan 28 17:05:01 icingaclient CRON[3964]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Jan 28 17:08:06 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e) Jan 28 17:08:06 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1 Jan 28 17:08:06 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 297 seconds. Jan 28 17:08:06 icingaclient NetworkManager[865]: 
              
                (wlan0): DHCPv4 state changed renew -> renew Jan 28 17:08:06 icingaclient NetworkManager[865]: 
               
                 address 192.168.137.101 Jan 28 17:08:06 icingaclient NetworkManager[865]: 
                
                  prefix 24 (255.255.255.0) Jan 28 17:08:06 icingaclient NetworkManager[865]: 
                 
                   gateway 192.168.137.1 Jan 28 17:08:06 icingaclient NetworkManager[865]: 
                  
                    nameserver '192.168.137.1' Jan 28 17:08:06 icingaclient NetworkManager[865]: 
                   
                     domain name 'mshome.net' Jan 28 17:08:06 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:08:06 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' 
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  

如果要查看/ var / log / apache2目录中的日志,请运行以下命令:

sudo lnav /var/log/apache2

有时,最新的信息在最近的文件中不可用。 因此,您可以使用-r选项显示旧日志文件中的信息:

sudo lnav -r

您应该看到以下输出:

Jan 28 17:04:20 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 226 seconds.
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
  
    (wlan0): DHCPv4 state changed renew -> renew
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
   
       address 192.168.137.101
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
    
        prefix 24 (255.255.255.0)
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
     
         gateway 192.168.137.1
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
      
          nameserver '192.168.137.1'
Jan 28 17:04:20 icingaclient NetworkManager[865]: 
       
         domain name 'mshome.net' Jan 28 17:04:20 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:04:20 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' Jan 28 17:05:01 icingaclient CRON[3964]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Jan 28 17:08:06 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e) Jan 28 17:08:06 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1 Jan 28 17:08:06 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 297 seconds. Jan 28 17:08:06 icingaclient NetworkManager[865]: 
        
          (wlan0): DHCPv4 state changed renew -> renew Jan 28 17:08:06 icingaclient NetworkManager[865]: 
         
           address 192.168.137.101 Jan 28 17:08:06 icingaclient NetworkManager[865]: 
          
            prefix 24 (255.255.255.0) Jan 28 17:08:06 icingaclient NetworkManager[865]: 
           
             gateway 192.168.137.1 Jan 28 17:08:06 icingaclient NetworkManager[865]: 
            
              nameserver '192.168.137.1' Jan 28 17:08:06 icingaclient NetworkManager[865]: 
             
               domain name 'mshome.net' Jan 28 17:08:06 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:08:06 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' Jan 28 17:12:47 icingaclient dnsmasq[1174]: nameserver 192.168.137.1 refused to do a recursive query Jan 28 17:13:03 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e) Jan 28 17:13:03 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1 Jan 28 17:13:03 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 291 seconds. 
             
            
           
          
         
        
       
      
     
    
   
  

您还可以使用-t选项查看带有时间戳选项的日志文件:

sudo lnav -t

您应该看到以下输出:

Jan 28 17:08:06 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1
Jan 28 17:08:06 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 297 seconds.
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
  
    (wlan0): DHCPv4 state changed renew -> renew
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
   
       address 192.168.137.101
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
    
        prefix 24 (255.255.255.0)
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
     
         gateway 192.168.137.1
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
      
          nameserver '192.168.137.1'
Jan 28 17:08:06 icingaclient NetworkManager[865]: 
       
         domain name 'mshome.net' Jan 28 17:08:06 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:08:06 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' Jan 28 17:12:47 icingaclient dnsmasq[1174]: nameserver 192.168.137.1 refused to do a recursive query Jan 28 17:13:03 icingaclient dhclient: DHCPREQUEST of 192.168.137.101 on wlan0 to 192.168.137.1 port 67 (xid=0xcd468e) Jan 28 17:13:03 icingaclient dhclient: DHCPACK of 192.168.137.101 from 192.168.137.1 Jan 28 17:13:03 icingaclient dhclient: bound to 192.168.137.101 -- renewal in 291 seconds. Jan 28 17:13:03 icingaclient NetworkManager[865]: 
        
          (wlan0): DHCPv4 state changed renew -> renew Jan 28 17:13:03 icingaclient NetworkManager[865]: 
         
           address 192.168.137.101 Jan 28 17:13:03 icingaclient NetworkManager[865]: 
          
            prefix 24 (255.255.255.0) Jan 28 17:13:03 icingaclient NetworkManager[865]: 
           
             gateway 192.168.137.1 Jan 28 17:13:03 icingaclient NetworkManager[865]: 
            
              nameserver '192.168.137.1' Jan 28 17:13:03 icingaclient NetworkManager[865]: 
             
               domain name 'mshome.net' Jan 28 17:13:03 icingaclient dbus[779]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper) Jan 28 17:13:03 icingaclient dbus[779]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher' 
             
            
           
          
         
        
       
      
     
    
   
  

要加载所有最新的日志文件类型,请运行以下命令:

sudo lnav -a

与Lnav一起使用热键

您还可以使用各种热键选项导航lnav命令的输出。

首先,运行lnav命令:

sudo lnav

您应该看到以下输出:

现在,使用键盘上的i键将lnav输出切换到直方图视图,如下所示:

接下来,使用p键查看日志解析器结果,如下所示:

接下来,使用m键标记顶部日志文件,如下所示:

结论

我希望你现在可以使用lnav轻松识别问题。 您也可以参考其官方网站了解更多详情。 如果您有任何问题,请随时发表评论。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏