iwatch(1) | realtime filesystem monitoring program using inotify | iwatch(1) |
iwatch - realtime filesystem monitoring program using inotify
iwatch [-d] [-f <config file>] [-v] [-p <pid file>] iwatch [-c command] [-C charset] [-e event[,event[,..]]] [-h|--help] [-m <email address>]
[-r] [-s <on|off>] [-t <filter string>] [-v] [--version] [-x exception]
[-X <regex string as exception>] <target>
inotify (inode notify) is a Linux kernel subsystem that monitors events in filesystems and reports those events to applications in real time.
inotify can be used to monitor individual files or directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside this directory. The inotify support was added to Linux Kernel 2.6.13.
Do not forget to consider directories as "special files". See more details at http://www.tldp.org/LDP/intro-linux/html/sect_03_01.html
iWatch is a Perl wrap to inotify to monitor changes in specific directories or files, sending alarms to the system administrator in real time. So, iWatch can do:
iWatch can run as daemon, as well a simple command. The daemon mode uses a XML configuration file, and put a list of directories and files (targets) to monitor. The command line mode will run without a configuration file. You just need to put the necessary information (target to watch, email, exception, recursivity, events to monitor and command to execute) in the command line. The options for both modes cannot be mixed together.
In the XML configuration file, each target can have its own email contact point. This contact point will get an email notification for any changes in the monitored targets. You can monitor a directory recursively, and you can also setup a list of exceptions where you do not want to monitor directory/file inside a monitored directory. It is also possible to disable email notification, and instead setup a command to be executed if an event occurs. Per default iWatch only monitor following events: close_write, create, delete, move, delete_self and move_self. But you can specify any possible events, like access, attrib, modify or all_events. See the EVENTS section for more details.
A good example of iWatch usage is to monitor the pages directory in webservers to notify, in real time, about defacements or file insertions. Other example is to synchronize configuration files between machines, when they are changed, as in DHCP servers acting in failover mode. You also use to synchronize files, via rsync when these files are changed.
Usage for daemon mode (background) of iWatch:
Usage for command line mode (foreground) of iWatch:
When using the '-c <command>' option, these strings will be available:
Following are the possible events you can use with the '-e' option:
The default configuration file is /etc/iwatch/iwatch.xml. See an example:
The two first lines will define the XML version and the file that defines the pattern used by iWatch (the default is /etc/iwatch/iwatch.dtd). These lines needn't be changed.
<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "/etc/iwatch/iwatch.dtd" >
<config>
<guard email="root@example.com" name="iWatch"/>
<watchlist>
<title>WEB server integrity monitoring</title>
<contactpoint email="someone@example.com" name="Administrator"/>
<path type="recursive" syslog="off" alert="off" exec="echo %p: %e %f | /usr/bin/sendxmpp -t foo@jabber-br.org">/var/www</path>
<path type="exception">/var/www/counter</path>
</watchlist>
</config>
The <config> statement is used to mark the configuration start point. The last line of the configuration must be </config>. The 'guard email' line is used to specify the sender email and name to be used when sending notifications by email. In other words, this line defines the 'From:' email field. The <watchlist></watchlist> delimits a block of definitions about a watch or some watches procedures.
The <config></config> place can have several <watchlist></watchlist> blocks. Inside these blocks (<watchlist></watchlist>), the <title></title> space is used to add a title that will identify the purpose of the block. This field is a visual text to indicate what the block does. The <title></title> does nothing. It is a tag for people reading the configuration file. The 'contactpoint' line contains the destination email address (To:) and name when sending notifications by email.
Each <path></path> line can monitor a file/directory and execute actions. The first path line showed will monitor recursively the directory /var/www. As no events was defined, iWatch will employ the default event (close_write, create, delete, move, delete_self and move_self events). If an event occurs, the syslog will not register it and a message reporting the program name (%p = iWatch), the event (%e) and the monitored file/directory name (%f) will be sent via XMPP protocol (sendxmpp external program) to foo@jabber-br.org. Note that alert="off" will disable any sending email. Other way to avoid an email message is drop the line "contactpoint". Another important point is that a second line is excluding the /var/www/counter file/directory from observation.
The showed example uses the sendxmpp command. Other good possibility is to apply the yowsup-cli command to send WhatsApp messages.
A new example. With configuration showed below, iWatch will work over three <watchlist> blocks.
The first <watchlist> block monitors a directory and has special actions for a file but no execute a command for it. The first path is a single (non recursive) directory /var/www/localhost/htdocs and any notification will be sent to the contact point webmaster@example.com. Note that have no events specified. So, the default events (close_write,
<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "iwatch.dtd">
<config>
<guard email="admin@localhost" name="iWatch"></guard>
<watchlist>
<title>Public Website</title>
<contactpoint email="webmaster@example.com" name="WebMaster"/>
<path type="single">/var/www/localhost/htdocs</path>
<path type="recursive">/var/www/localhost/htdocs/Photos</path>
</watchlist>
<watchlist>
<title>Operating System</title>
<contactpoint email="admin@localhost" name="Administrator"/>
<path type="recursive">/etc/apache2</path>
<path type="recursive">/etc/mail</path>
<path type="exception">/etc/mail/statistics</path>
<path type="single" filter="shadow|passwd">/etc</path>
</watchlist>
<watchlist>
<title>Only a test</title>
<contactpoint email="root@localhost" name="Administrator"/>
<path type="single" alert="off" exec="(w;ps -aux) | mail -s %f root@localhost">/tmp/dir1</path>
<path type="single" events="access,close" alert="off" exec="(w;ps -aux) | mail -s %f root@localhost">/tmp/dir2</path>
<path type="single" events="default,access" alert="off" exec="(w;ps -aux) | mail -s '%f is accessed' root@localhost">/tmp/dir3</path>
<path type="single" events="all_events">/tmp/dir4</path>
</watchlist>
</config>
The second block has four monitoring. All notification will be sent to admin@localhost. The main novelty over the first block is that a path uses a 'filter' instruction to watch /etc/shadow and /etc/passwd at the same time. To understand better this situation, see the '-t' at OPTIONS section.
The last block monitors default events in first line and several non default events in the following three lines. In several lines the 'alert' is defined as 'off'. So, iWatch will not send emails using the builtin mail engine. However, in three lines the external command 'mail' was used to send personalized emails.
A tip to learn about events is watch the iWatch command executing with '-e all_events' option. The following example will monitor a 'ls /tmp' command.
Another example, that monitors the creating of a file inside /tmp:
$ iwatch -e all_events /tmp
[17/Jun/2014 11:22:59] IN_ISDIR,IN_OPEN /tmp
[17/Jun/2014 11:22:59] IN_ISDIR,IN_CLOSE_NOWRITE /tmp
So, in the last example occurred modify, open and close_write actions.
$ iwatch -e all_events /tmp
[17/Jun/2014 11:29:43] IN_MODIFY /tmp/file.txt
[17/Jun/2014 11:29:43] IN_OPEN /tmp/file.txt
[17/Jun/2014 11:29:43] IN_MODIFY /tmp/file.txt
[17/Jun/2014 11:29:43] IN_CLOSE_WRITE /tmp/file.txt
[17/Jun/2014 11:29:43] * /tmp/file.txt is closed
Since version 0.2.0 iWatch checks the validity of XML file if it has following entry in the first two lines:
The check will be made over a pattern described by /etc/iwatch/iwatch.dtd file. Without the showed two lines, iWatch will just give a warning that you have to use DTD file, and it continues to run as normal without XML validation. The iWatch's XML format is very simple and easy to understand, and it uses following DTD :
<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "iwatch.dtd">
<!ELEMENT config (guard,watchlist+)>
<!ATTLIST config
charset CDATA "utf-8"
>
<!ELEMENT guard (#PCDATA)>
<!ATTLIST guard
email CDATA #REQUIRED
name CDATA #IMPLIED
>
<!ELEMENT watchlist (title,contactpoint,path+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT contactpoint (#PCDATA)>
<!ATTLIST contactpoint
email CDATA #REQUIRED
name CDATA #IMPLIED
>
<!ELEMENT path (#PCDATA)>
<!ATTLIST path
type CDATA #REQUIRED
alert (on|off) "off"
events CDATA #IMPLIED
exec CDATA #IMPLIED
filter CDATA #IMPLIED
syslog (on|off) "off"
On Debian systems, when running iwatch as a daemon via systemd, it will work under the root user. Be sure that your system is ready to make all things via root user. E.g. if you are using sendxmpp command, you will need a .sendxmpprc file under the /root/ directory.
iWatch was written by Cahya Wirawan <cahya@gmx.at>.
This manual page was written by Michael Prokop <mika@debian.org> and was updated/expanded by Joao Eriberto Mota Filho <eriberto@debian.org> for the Debian project (but may be used by others).
04 Dec 2022 | iwatch-0.2.2 |