Searching through IT logs can be boring and tedious. With the swatch (Simple WATCHer) Linux utility, automate actions based on matching logging conditions.
Awhile back, I showed you how to take advantage of setting up a centralized remote logging facility for Linux. Centralized logging gives you one location to head for when trouble arises. Using the Linux tool swatch, let's take logging one step further by automating alerts or actions based on conditions matching regular expressions.
This utility has been around for a long time and started as Perl script, but it has since grown into a more complete package.
Installing swatch is pretty simple. As usual, if your Linux distribution has packages to install swatch, it's typically easier to use those since all dependencies will be resolved as well. In my case, the third-party repository RPMForge for CentOS/Red Hat had the appropriate packages available. A simple “yum install swatch” installed the utility, as well all required Perl packages. Other distributions, such as Ubuntu and Debian, also have packaged versions.
If you can't install binary packages, you can download the package at Sourceforge. Unpack the files, and you'll find the INSTALL text file with instructions for installing swatch, along with the details for the Perl modules required.
In its simplest form, swatch only expects to find a configuration file in the user home directory of whoever is invoking it ($HOME/.swatchrc). This file contains the conditions in text form as regular expressions for whichever actions you want swatch to act on. There is a daemon switch available if you're interested in using it in this form. Depending on your Linux choice, there may be some setup required to turn swatch into a service. Consult your OS documentation on configurations of programs as services.
If you want swatch to email you when users on the system attempt to run a command under sudo for which they don't have permission, then create the following file in your home directory. This of course implies that either you are root or your user account has the appropriate permissions on the system.
watchfor /user NOT in sudoers/
Save the file and then you can invoke swatch at the command line, feeding it the configuration file.
# swatch -c /root/.swatchrc --script-dir=/var/log -t /var/log/secure
*** swatch version 3.1.1 (pid:27622) started at Fri Sep 24 13:19:00 EDT 2010
The c switch tells swatch where the configuration file is located. When swatch is launched, it actually creates a watcher file with the configuration options specified. If you don't use the script-dir directive telling it where to put this watcher file, swatch will assume you want the file created in your home directory. Then, the t switch tells swatch to tail the file you want to monitor.
Now that you have swatch started, log into the same Linux machine as a normal user and type a command that you know the user has no sudo access for.
$ sudo /sbin/service iptables stop
Check your email inbox of the address you listed, and you should see a security message with details about the user who attempted to stop the firewall.
Other Useful Configurations
The swatch utility has many nice options allowing various controls of conditions. You can consult swatch's man page to read the details of all of them, but a few are worth mentioning here.
If you want to display the alert to your console in various colors, you can use the echo=color command. The colors allowed are shown in the man page.
To set up thresholds for allowing a given number of conditions in a given time period (in seconds) before the options are executed, use the thresholds parameter.
The swatch process allows you to execute a command when your expression matches. For this, use the exec command.
Last, if you want the action to be performed only during certain hours or certain days, consult the when option.
All commands and options are thoroughly explained in the man page of the swatch command.
Regular Expressions, Ugh!
I'm not sure whether I saved the best for last or the worst for last. Regular expressions in the Linux world can mean some awesome pattern-matching abilities. In my case, utilizing regular expressions is a struggle and something I sometimes can't wrap my brain around. In either case, the ability of swatch to use regular expressions gives the utility its power and makes pattern matches in logs wide open.
One of the easier regular expressions to demonstrate is the logical and/or operator. This gives you the capability to match more than one condition. For instance, if you needed to match several different types of similar words, you could use the following.
In this instance, the pipe command (|) matches any line in the log file that contains the text denied or failed. Obviously, this is the simplest form of regular expression, but you should get the idea.
Make Log Watching Lazy
From simple text patterns as I demonstrated above in the sudo event, to very robust regular expressions, swatch gives system administrators great log-monitoring options. It's a perfect tool for monitoring SSH or denial-of-service (DoS) attacks on your Linux servers, possibly alerting you to trouble before it's too late.