{"id":13895,"date":"2019-04-06T13:57:14","date_gmt":"2019-04-06T13:57:14","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13895"},"modified":"2019-04-06T13:57:14","modified_gmt":"2019-04-06T13:57:14","slug":"swatchdog-simple-log-file-watcher-in-real-time-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/swatchdog-simple-log-file-watcher-in-real-time-in-linux\/","title":{"rendered":"Swatchdog \u2013 Simple Log File Watcher in Real-Time in Linux"},"content":{"rendered":"<p><strong>Swatchdog<\/strong>\u00a0(the \u201c<strong>Simple WATCH DOG<\/strong>\u201d) is a simple Perl script for monitoring active log files on Unix-like systems such as Linux. It watches your logs based on regular expressions that you can define in a configuration file. You can run it from the command line or in the background, detached from any terminal using the daemon mode option.<\/p>\n<p>Note that the program was originally called\u00a0<strong>swatch<\/strong>\u00a0(the \u201c<strong>Simple Watcher<\/strong>\u201d) but a request by the old Swiss watch company for a name change saw the developer change its name to\u00a0<strong>swatchdog<\/strong>.<\/p>\n<p><strong>Read Also<\/strong>:\u00a0<a href=\"https:\/\/www.tecmint.com\/best-linux-log-monitoring-and-management-tools\/\" target=\"_blank\" rel=\"noopener\">4 Good Open Source Log Monitoring and Management Tools for Linux<\/a><\/p>\n<p>Importantly,\u00a0<strong>swatchdog<\/strong>\u00a0has grown from a script for watching logs produced by Unix\u2019s syslog facility, and it can monitor just about any kind of logs.<\/p>\n<h3>How to Install Swatch in Linux<\/h3>\n<p>The package\u00a0<strong>swatchdog<\/strong>\u00a0is available to install from the official repositories of mainstream Linux distributions as a package \u201c<strong>swatch<\/strong>\u201d via a package manager as shown.<\/p>\n<pre>$ sudo apt install swatch\t[<strong>On Ubuntu\/Debian<\/strong>]\r\n$ sudo yum install epel-release &amp;&amp; sudo yum install swatch\t[<strong>On RHEL\/CentOS<\/strong>]\r\n$ sudo dnf install swatch\t[<strong>On Fedora 22+<\/strong>]\r\n<\/pre>\n<p>To install most latest version of\u00a0<strong>swatchdog<\/strong>, you need to compile it from source using following commands in any Linux distribution.<\/p>\n<pre>$ git clone https:\/\/github.com\/ToddAtkins\/swatchdog.git\r\n$ cd swatchdog\/\r\n$ perl Makefile.PL\r\n$ make\r\n$ sudo make install\r\n$ sudo make realclean\r\n<\/pre>\n<p>Once you have installed the\u00a0<strong>swatch<\/strong>, you need to create its configuration file (default location is\u00a0<strong>\/home\/$USER\/.swatchdogrc<\/strong>\u00a0or\u00a0<strong>.swatchrc<\/strong>), to determine what types of expression patterns to look for and what type of action(s) should be taken when a pattern is matched.<\/p>\n<pre>$ touch \/home\/tecmint\/.swatchdogrc\r\nOR\r\n$ touch \/home\/tecmint\/.swatchrc\r\n<\/pre>\n<p>Add your regular expression in this file and each line should contain a keyword and value (sometimes optional), separated by a\u00a0<strong>space<\/strong>\u00a0or an equal\u00a0<code>(=)<\/code>\u00a0sign. You need to specify a pattern and an action(s) to be taken when a pattern is matched.<\/p>\n<p>We will use a simple configuration file, you can find more options in the swatchdog man page, for instance.<\/p>\n<pre>watchfor  \/sudo\/\r\n\techo red\r\n\tmail=admin@tecmint.com, subject=\"Sudo Command\"\r\n<\/pre>\n<p>Here, our regular expression is a literal string \u2013\u00a0<strong>\u201csudo\u201d<\/strong>, means any time the string\u00a0<strong>sudo<\/strong>\u00a0appeared in the log file, would be printed to the terminal in red text and\u00a0<strong>mail<\/strong>\u00a0specify the action to be taken, which is to echo the matched pattern on the terminal and send an e-mail to the specified address, receptively.<\/p>\n<p>After you have configured it, swatchdog reads the\u00a0<strong>\/var\/log\/syslog<\/strong>\u00a0log file by default, if this file is not present, it reads\u00a0<strong>\/var\/log\/messages<\/strong>.<\/p>\n<pre>$ swatch     [<strong>On RHEL\/CentOS &amp; Fedora<\/strong>]\r\n$ swatchdog  [<strong>On Ubuntu\/Debian<\/strong>]\r\n<\/pre>\n<p>You can specify a different configuration file using the\u00a0<code>-c<\/code>\u00a0flag as shown in the following example.<\/p>\n<p>First create a swatch configuration directory and a file.<\/p>\n<pre>$ mkdir swatch\r\n$ touch swatch\/secure.conf\r\n<\/pre>\n<p>Next, add the following configuration in the file to monitor failed login attempts, failed SSH login attempts, successful SSH logins from the\u00a0<strong>\/var\/log\/secure<\/strong>\u00a0log file.<\/p>\n<pre>watchfor \/FAILED\/\r\necho red\r\nmail=admin@tecmint.com, subject=\"<strong>Failed Login Attempt<\/strong>\"\r\n\r\nwatchfor \/ROOT LOGIN\/\r\necho red\r\nmail=admin@tecmint.com, subject=\"<strong>Successful Root Login<\/strong>\"\r\n\r\nwatchfor \/ssh.*: Failed password\/\r\necho red\r\nmail=admin@tecmint.com, subject=\"<strong>Failed SSH Login Attempt<\/strong>\"\r\n\r\nwatchfor \/ssh.*: session opened for user root\/ \r\necho red\r\nmail=admin@tecmint.com, subject=\"<strong>Successful SSH Root Login<\/strong>\"\r\n<\/pre>\n<p>Now run the Swatch by specifying the configuration file using the\u00a0<code>-c<\/code>\u00a0and log file using\u00a0<code>-t<\/code>\u00a0flag as shown.<\/p>\n<pre>$ swatchdog -c ~\/swatch\/secure.conf -t \/var\/log\/secure\r\n<\/pre>\n<p>To run it in the background, use the\u00a0<code>--daemon<\/code>\u00a0flag; in this mode, it is detached from any terminal.<\/p>\n<pre>$ swatchdog ~\/swatch\/secure.conf -t \/var\/log\/secure --daemon  \r\n<\/pre>\n<p>Now to test the swatch configuration, try to login into server from the different terminal, you see the following output printed to the terminal where Swatchdog is running.<\/p>\n<pre>*** swatch version 3.2.3 (pid:16531) started at Thu Jul 12 12:45:10 BST 2018\r\n\r\nJul 12 12:51:19 tecmint sshd[16739]: Failed password for root from 192.168.0.103 port 33324 ssh2\r\nJul 12 12:51:19 tecmint sshd[16739]: Failed password for root from 192.168.0.103 port 33324 ssh2\r\nJul 12 12:52:07 tecmint sshd[16739]: pam_unix(sshd:session): session opened for user root by (uid=0)\r\nJul 12 12:52:07 tecmint sshd[16739]: pam_unix(sshd:session): session opened for user root by (uid=0)\r\n<\/pre>\n<div id=\"attachment_30183\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/08\/Monitor-Linux-Logs-in-Realtime.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-30183\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/08\/Monitor-Linux-Logs-in-Realtime.png\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/08\/Monitor-Linux-Logs-in-Realtime.png 970w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/08\/Monitor-Linux-Logs-in-Realtime-768x182.png 768w\" alt=\"Monitor Linux Logs in Real Time\" width=\"970\" height=\"230\" aria-describedby=\"caption-attachment-30183\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-30183\" class=\"wp-caption-text\">Monitor Linux Logs in Real Time<\/p>\n<\/div>\n<p>You can also run multiple swatch processes to monitor various log files.<\/p>\n<pre>$ swatchdog -c ~\/site1_watch_config -t \/var\/log\/nginx\/site1\/access_log --daemon  \r\n$ swatchdog -c ~\/messages_watch_config -t \/var\/log\/messages --daemon\r\n$ swatchdog -c ~\/auth_watch_config -t \/var\/log\/auth.log --daemon\r\n<\/pre>\n<p>For more information, check out the swatchdog man page.<\/p>\n<pre>$ man swatchdog\r\n<\/pre>\n<p>Swatchdog SourceForge Repository:\u00a0<a href=\"https:\/\/sourceforge.net\/projects\/swatch\/\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/sourceforge.net\/projects\/swatch\/<\/a><\/p>\n<p>The following are some additional log monitoring guides that you will find useful:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/watch-or-monitor-linux-log-files-in-real-time\/\" target=\"_blank\" rel=\"noopener\">4 Ways to Watch or Monitor Log Files in Real Time<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/create-centralized-log-server-with-rsyslog-in-centos-7\/\" target=\"_blank\" rel=\"noopener\">How to Create a Centralized Log Server with Rsyslog<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/linux-server-log-monitoring-with-log-io\/\" target=\"_blank\" rel=\"noopener\">Monitor Server Logs in Real-Time with \u201cLog.io\u201d Tool<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/lnav-watch-and-analyze-apache-logs-in-linux\/\" target=\"_blank\" rel=\"noopener\">lnav \u2013 Watch and Analyze Apache Logs from a Linux Terminal<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/ngxtop-monitor-nginx-log-files-in-real-time-in-linux\/\" target=\"_blank\" rel=\"noopener\">ngxtop \u2013 Monitor Nginx Log Files in Real Time in Linux<\/a><\/li>\n<\/ol>\n<p><strong>Swatchdog<\/strong>\u00a0is a simple active log file monitoring tool for Unix-like systems such as Linux. Try it out and share your thoughts or ask any questions in the comments section.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/swatch-linux-log-file-watcher\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swatchdog\u00a0(the \u201cSimple WATCH DOG\u201d) is a simple Perl script for monitoring active log files on Unix-like systems such as Linux. It watches your logs based on regular expressions that you can define in a configuration file. You can run it from the command line or in the background, detached from any terminal using the daemon &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/swatchdog-simple-log-file-watcher-in-real-time-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Swatchdog \u2013 Simple Log File Watcher in Real-Time in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13895","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13895"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13895\/revisions"}],"predecessor-version":[{"id":13896,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13895\/revisions\/13896"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}