kqueue in a jail for the pool?

brettyj

Dabbler
Joined
Jul 30, 2020
Messages
23
Hey,

Currently considering moving to FreeNAS from Storage Spaces. One of the things I've done with Storage Spaces, is for things that need to know when a file is changed in a particular directory, is I've run a Windows App on the server, which monitors the directory through the file watching API, and then broadcasts a message on MQTT when something has changed, so the service running on another machine (and connected via SMB or NFS so can't run this API itself), can react and do something.

I was looking at how to implement this with FreeNAS. I was thinking that potentially creating a Jail with a simple app that does this, using the same technique, using kqueue to monitor a certain directory structure in the pool, and if something happens, fire it off onto MQTT. Is this a viable solution? I'm aware kqueue isn't ideal compared to INotify or Windows APIs, but it might be sufficient to do stuff I need to do.

Thanks!
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
This capability is already built-in in ZFS. You could snapshot your dataset on a regular interval, and then run zfs diff (n-1)th-snapshot nth-snapshot, and pipe the output to your favorite mailbox.

From the man page for zfs:
Code:
     zfs diff [-FHt] snapshot [snapshot|filesystem]

         Display the difference between a snapshot of a given filesystem and
         another snapshot of that filesystem from a later time or the current
         contents of the filesystem.  The first column is a character
         indicating the type of change, the other columns indicate pathname,
         new pathname (in case of rename), change in link count, and
         optionally file type and/or change time.

         The types of change are:

           -         path was removed
           +         path was added
           M         path was modified
           R         path was renamed

         -F      Display an indication of the type of file, in a manner
                 similar to the -F option of ls(1).

                   B         block device
                   C         character device
                   F         regular file
                   /         directory
                   @         symbolic link
                   =         socket
                   >         door (not supported on FreeBSD)
                   |         named pipe (not supported on FreeBSD)
                   P         event port (not supported on FreeBSD)

         -H      Give more parsable tab-separated output, without header lines
                 and without arrows.

         -t      Display the path's inode change time as the first column of
                 output.
 

brettyj

Dabbler
Joined
Jul 30, 2020
Messages
23
The issue is I need nigh on real-time notifications, otherwise that would be a good solution
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
How real-time is real-time? You could snapshot every 5 or 10 minutes, and set the snapshots to expire after 1-2 weeks.
 

brettyj

Dabbler
Joined
Jul 30, 2020
Messages
23
Well in my application it was detecting new motion recordings to then convert to GIF and post to slack, so within a few seconds
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
OK, you'll likely have to use a BSD port to accomplish this. Either sysutils/fswatch-mon or sysutils/inotify-tools should install utilities to watch a directory via kqueue(). Inside a jail, run portsnap fetch to build the /usr/ports tree. Then navigate to either of the 2 directories above in /usr/ports/sysutils, and then run make to install the port and its dependencies.
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,553
Samba does its own internal file change notifications. kqueue / inotify is only needed if you want to coordinate file changes made by local (non-samba) processes. If everything is going over the SMB protocol, you can just rely on the SMB server to do this for you.
 

brettyj

Dabbler
Joined
Jul 30, 2020
Messages
23
OK, you'll likely have to use a BSD port to accomplish this. Either sysutils/fswatch-mon or sysutils/inotify-tools should install utilities to watch a directory via kqueue(). Inside a jail, run portsnap fetch to build the /usr/ports tree. Then navigate to either of the 2 directories above in /usr/ports/sysutils, and then run make to install the port and its dependencies.

Ah that's helpful - thanks, glad it's achievable - puts my mind at rest

Samba does its own internal file change notifications. kqueue / inotify is only needed if you want to coordinate file changes made by local (non-samba) processes. If everything is going over the SMB protocol, you can just rely on the SMB server to do this for you.

Ah good to know, thanks - does this work even if using the SMB protocol in Linux machines?
 
Last edited:

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,553
Ah that's helpful - thanks, glad it's achievable - puts my mind at rest



Ah good to know, thanks - does this work even if using the SMB protocol in Linux machines?
I'm not familiar with what parts of the SMB protocol are implemented in the Linux kernel SMB client, but clients can technically send change notification requests.

Looks like Steve French may have recently added support for it via SMB3: https://patchwork.kernel.org/patch/11368325/

Of course, if you do this through the SMB protocol, then you have something that's OS-agnostic (should work equally well against Windows and MacOS SMB servers).
 
Top