ASPL Manual v 1.00
© 2024 by SetSphere.COM


12-4

   Monitoring Changes in UNIX Directories

You can monitor a directory tree structure in real time by using ASPL set operators. This chapter consists of two sections. In the first section the script dirchange-monitor.sh shows how to monitor a directory, and in the second section ASPL commands are used to do the same from within an ASPL session.

■ Script dirchange-monitor.sh

In the following section we show how to monitor a directory tree on the Linux system. The following are two Korn shell script that invoke ASPL to monitor any change in a directory along its subdirectories and files. The script can be run on a terminal to find in real time changes in subdirectories and files. The monitoring method employed in here does not use any system service or any specialized package. The changes are being detected by simply interrogating a set variable in a loop. The directory content is turned into a group represented by a dataset, then the constituent of the group is being updated should any change be detected. The script gather the changes in the group at a delta time interval hence showing the differences in the directory: changes in subdirectories and files.

The following script monitors a directory on the UNIX system. Line 4 to 9 invokes ASPL to create a workspace DIRECTORY 123 whose grouping class is POSIX, and create a set variable dirvar to gather the directory from the GG-function ggdir(). The workspace is saved temporarily, so lines 12 to 22 will load it then interrogate the dirvar set variable showing changes in a loop. Lines 25 to 27 will discard the workspace. The comparison is displayed comparing the initial directory with any subsequent changes.


       [Top Text]

 -L- Listing. 12.4.1   [LISTING dirchange-monitor1.sh][ASPL Script dirchange-monitor1.sh]
(raw text)
1.     #!/bin/ksh
2.     
3.     # warm up
4.       asplcmd "  \
5.         createworkspace DIRECTORY123 POSIX;    \
6.         dirvar = ggdir(dir,$1);         \
7.         save; \
8.         quit; \
9.         "
10.    
11.    # loop 7 times with of 2 seconds delay in between
12.    for i in {1..7}
13.    do
14.      sleep 2
15.      asplcmd "                    \
16.        load DIRECTORY123;         \
17.        ks mtime chksum ppdd ffl;  \
18.        ? dirvar;                  \
19.        v sorted;                  \
20.        print ################### DONE LOOP $i ###################; \
21.        "
22.    done
23.    
24.    # delete workspace DIRECTORY123
25.      asplcmd "  \
26.        wrm DIRECTORY123;                 \
27.        "
28.    

ASPL(C) 2024 Bassem Jamaleddine


The following figure shows the result when monitoring the directory /tmp/aaa1.

This example is shown in the following terminal.

       display or terminal for Example: Script dirchange-monitor1.sh
viewme

 -E- Display. 12.4.1   [Script dirchange-monitor1.sh][Script dirchange-monitor1.sh]
run for dirchange-monitor1.sh


It is possible to show more detailed information about the changes in the directory by adding the group union statement ,gU dirvar dirvar@1 shown in the following listing. The script is similar to the previous script, however we added the group union statement show on line 20. The effect of the statement can be seen in the output displayed in the terminal.

       [Top Text]

 -L- Listing. 12.4.2   [LISTING dirchange-monitor2.sh][ASPL Script dirchange-monitor2.sh]
(raw text)
1.     #!/bin/ksh
2.     
3.     # warm up
4.       asplcmd "  \
5.         createworkspace DIRECTORY1234 POSIX;    \
6.         dirvar = ggdir(dir,$1);         \
7.         save; \
8.         quit; \
9.         "
10.    
11.    # loop 7 times with of 2 seconds delay in between
12.    for i in {1..7}
13.    do
14.      sleep 2
15.      asplcmd "                    \
16.        load DIRECTORY1234;         \
17.        ks mtime chksum ppdd ffl;  \
18.        ? dirvar;                  \
19.        v sorted;                  \
20.        ,gU dirvar dirvar@1;       \
21.        print ################### DONE LOOP $i ###################; \
22.        "
23.    done
24.    
25.    # delete workspace DIRECTORY123
26.      asplcmd "  \
27.        wrm DIRECTORY1234;                 \
28.        "
29.    

ASPL(C) 2024 Bassem Jamaleddine


The following figure shows the result when monitoring the directory /tmp/aaa1.

This example is shown in the following terminal.

       display or terminal for Example: Script dirchange-monitor2.sh
viewme

 -E- Display. 12.4.2   [Script dirchange-monitor2.sh][Script dirchange-monitor2.sh]
run for dirchange-monitor2.sh


 

■ Monitoring Directories From Within an ASPL Session

Both of the scripts shown previously have started ASPL in a shell loop, restarting the ASPL interpreter with every iteration. There is no harm doing that since the session will be discarded and terminated by the end of each iteration. However one could have monitored the directory by starting ASPL only once in a session: here we start ASPL in a terminal then we set the variable dirvar to be associated with the directory, then we gather the changes collected incrementally in the archived set variable.

Show below the commands to monitor the directory /tmp/aaa1 from within an ASPL session. Assuming the workspace DIRECTORY123 does not exist, here we start ASPL with some workspace named DIRECTORY123 whose grouping class is POSIX, then we create the set variable vardir to glob the directory /tmp/aaa1 by calling the GG-functioon ggdir(). The command ?7,5 vardir causes to interrogate vardir in a loop iteration: looping seven times with a delay of five seconds in between each iteration. Unlike the loop shown in the above scripts, the iterations in here are within the same ASPL statement: checking if any change in dataset dataset associated with vardir, should any change be detected then the group is updated, and old dataset is archived. The command v sorted print the symbol table with all historical data. The following two commands print the similarity between vardir and all its historical and display their groups union.
  # asplcmd 'wrm DIRECTORY123'
  # aspl -wsname DIRECTORY123 -groupingclass POSIX
  aspl> vardir = ggdir(dir,/tmp/aaa1)
  aspl> ?10,5 vardir
  aspl> v sorted
  aspl> @sim vardir
  aspl> ,gU dir dir@1 dir@2 dir@3 dir@4 dir@5
  aspl> ,gU vardir vardir@1 vardir@2 vardir@3 vardir@4 vardir@5
  aspl> ,gU vardir vardir@5
  aspl> quit



       display or terminal for Example: Script vardir-monitor-links
viewme

 -E- Display. 12.4.3   [Script vardir-monitor-links][Script vardir-monitor-links]
run for vardir-monitor-links