Giter Club home page Giter Club logo

dmon's Introduction

dmon

Overview

This is a file monitoring application called dmon , which scans a root directory for any changes to files underneath this folder and executes user specified actions on the files based on pattern matching rules.

For instance, suppose dmon was monitoring the root folder /folder/to/monitor and a file named /folder/to/monitor/Foster the People/Helena Beat.mp3 was created. A scan of the root folder would detect that this file was created and check if there is a pattern rule that matches the name of the file.

For example, suppose there is a pattern rule that looks like this: CREATE *.mp3 mpv ${FULLPATH}. When the scan detects that the /folder/to/monitor/Foster the People/Helena Beat.mp3 is created, it will see that this file matches the *.mp3 pattern and will execute the mpv ${FULLPATH} command (i.e. it will play the song).

Objective

This service can be used to trigger actions, such as rebuilding a project, deploying code, etc, based on the different events (file modification, creation, or removal).

Design

Scanning

To begin a scan, a user should start dmon in the following manner:

# Scan current directory every 5 seconds

$ ./dmon .
Monitoring /usr/home

This will start the dmon program such that it scans the current directory every 5 seconds. The user may specify a different scanning frequency via the -t command-line option:

# Scan current directory every 60 seconds
$ ./dmon -t 60 .
Monitoring /usr/home

Events

During the scan, dmon will examine all the files under the root directory and any sub-directories (i.e. nested directories), to determine if any of the following system events occurred:

  1. CREATE: A file has been created under the monitored directory.
  2. MODIFY: A file has been modified under the monitored directory.
  3. DELETE: A file has been deleted under the monitored directory.

Note: On the initial scan, no action is taken since dmon doesn't know about any of the files yet, so it doesn't know if the file was created, modified, or deleted.

Rules

By default, dmon will load in pattern rules from the file rules unless otherwise specified by the -f flag as shown below:

# Scan current directory every 5 seconds with custom.rules
$ ./dmon -f custom.rules .
Monitoring /usr/home

This rules file contains a series of rules (one per line) specified in the following format:

EVENT       PATTERN     ACTION

Any empty lines or lines that begin with a # in the rules file are ignored. Any invalid lines (i.e. do not match the format above) displays an error message Invalid rule: ... and quits the program.

For example, to automatically compile a C program when it has been modified, you can have the following rule:

# Compilation rule
MODIFY      *.c         cc -o ${BASEPATH} ${FULLPATH}

This rule means that when a MODIFY event is detected, any files thatmathc the pattern *.c should execute the command cc -o ${BASEPATH} ${FULLPATH}.

Note: The pattern is checked against both the full path of the file in question and its basename. If either the full path of the basename matches the pattern, then the rule is considered a match and the action is executed.

If a user were to modify say the hello.c in the root directory, then dmon would detect this file modification, match the rule above, print out the message below, and execute the corresponding action:

# Scan current directory every 5 seconds
$ ./dmon .
Monitoring /usr/home
Detected "CREATE" event on "hello.c"    # Detect existence of hello.c

# Modify hello.c in another terminal
Detected "MODIFY" event on "hello.c"    # Detect modification
Matched "*.c" pattern on "hello.c"      # Detect pattern match
Executing action "cc -o ${BASEPATH} ${FULLPATH}" on "hello.c"

Note: The loggin messages as show above are included as part of the normal operation of dmon.

Environment Variables

To allow actions to use information about the corresponding event, dmon must pass the following [environmental variables] to specified command:

  1. BASEPATH: This is the base path of the file (i.e without any proceeding extensions).
  2. FULLPATH: This is the full path of the file.
  3. EVENT: This is the type of even detected.
  4. TIMESTAMP: This is the current timestamp.

In the example above, the ${BASEPATH} and ${FULLPATH} variables will be expanded to hello.c and /usr/home/hello.c respectively.

SIGINT

Upon receiving the SIGINT signal, dmon cleans up any allocated resources (where possible) and exits gracefully.

$ ./dmon .
Monitoring /usr/home

# Send Control-C
Cleaning up
Bye!

Usage

The full set of dmon command-line options are shown below:

$ ./dmon -h
Usage: dmon [options] ROOT

Options:
    -h          Prints this help message
    -f RULES    Load rules from this file (default is rules)
    -t SECONDS  Time between scans (default is 5 seconds)

Building the App

The repo includes a Makefile that builds and cleans the project (and all its components):

$ make          # Builds dmon

$ make clean    # Remove dmon and any intermediate files. 

dmon's People

Contributors

kalesh-singh avatar zonalwings avatar harbinger55555 avatar

Watchers

James Cloos avatar

Forkers

kattenlaf

dmon's Issues

Detect Events

Library for detecting changes in the root directory and other nested directories.

Directory vs File comparison

Assuming Tree 1 is the original tree and Tree 2 is the new tree.
What if a directory is deleted from Tree 1 and a file with the same name is created in Tree 2? We may have to compare file/directory types as well.

Actions

Library that will be spawn off different actions if any of the pattern rules were matched on an event occurring.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.