Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this article you will learn on how to create script which allows to create alerts when some directories have been deleted or some have been created after running script. This script has algorithm which is checking all directories 100 times.

This scrip allows to check, you can modify it if you wish to your needs. This script collects all dirs that have name from jira01 to jira99, after that it is counting them all.

Code Block
languagebash
# this will return all jira dir where script has been executed
# it should be executed inside ./docker : sudo bash ./docker/mswitala/cronjobs.sh
dires=$(ls | grep "jira*")

# Convert the space-separated list into an array
IFS=$'\n' read -r -d '' -a sorted_array <<< "$dires"

Then it is saving data to an array if it is matching string name from jira01 to jira99

Code Block
languagebash
      if [ "$item" == "$string" ]; then
           jiras+=("$item")
        break

        fi
        ((counter++))

You can modify the script for example to check other dir names then Jira and count them and then compare if there has been done any changes like deleting or creating new dir and create alert.

Here you have full script which can be run with command inside docker dir ( have in mind there is infinite while loop to keep scanning so to stop it you have to kill process or use ctrl + z to stop the script ) :

michals@dockersupphost3:/docker$ sudo bash ./mswitala/cronjob.sh 

...

Code Block
languagebash
#!/bin/bash


# Compare two strings
string1="jira"
string2="09"
string3="10"

# array with created jira servers
jiras=()

# declare inital start here
initial_start=true
scanning=true
total_jiras=

# Specify the file name for Alerts
# Check if the file already exists
if [ ! -e "alerts.txt" ]; then
          # Create the file only if it doesn't exist
            echo "Hello, this is some text." > "alerts.txt"
            echo "File created successfully."
      else
            echo "File already exists. Not overwriting."
fi


# start scanning
while [ scanning ]; do

# this will return all jira dir where script has been executed
# it should be executed inside ./docker : sudo bash ./docker/mswitala/crobjobscronjobs.sh
dires=$(ls | grep "jira*")

# Convert the space-separated list into an array
IFS=$'\n' read -r -d '' -a sorted_array <<< "$dires"

# test
echo " first === scanning loop === "


for item in "${sorted_array[@]}"; do

counter2=10
counter=1
max=9
max2=99

        #echo " just checking items in array : $item"
       
#echo
"count
number
: $counter"

#checking dirs from 01 to 09
while [ "$counter" -le "$max" ]; do

  number=0
  string=$string1$number$counter

 # echo " test checking string >  $string"

        if [ "$item" == "$string" ]; then
        #echo
" adding $item because it is equal to $string"
        jiras+=("$item")
        break
        #else
     
  #echo "The string $item is equal to $string"
        #echo " "
        fi
        ((counter++))
                                                                                                                           45,1          31%

done

# chceking dirs from 10 to 99
while [ "$counter2" -le "$max2" ]; do

  string=$string1$counter2

  #echo " test checking string >  $string"

        if [ "$item" == "$string" ]; then
        #   echo " adding $item because it is equal to $string"
       jiras+=("$item")
        break
        #else
        #      echo "The string $item ise not equal to $string"
        fi
        ((counter2++))
done


done

# Check the number of
items
in
the array


# check now if all jira dirs have been collected if new have been created
if $initial_start; then

        #test
        # save just ONCE how many jira dir there is now
        total_jiras="${#jiras[@]}"
        initial_start=false
        echo " total jiras as for now is equal
to ===== >
$total_jiras
"


fi

current_jiras=${#jiras[@]}

# create alerts here to notifty user
if [ "$current_jiras" -gt "$total_jiras" ]; then

        current_date=$(date)
 
      echo "ALERT there has been created new jira server."
        echo "ALERT on $current_date there has been created new jira server. " >> "alerts.txt"


        break
fi

if [ "$current_jiras" -lt "$total_jiras" ]; then


        echo "ALERT some jira server has been deleted. "

        current_date=$(date)
 
      echo "ALERT some jira server has been deleted."
        echo  "ALERT on $current_date ALERT some jira server has been deleted. " >> "alerts.txt"


        break

fi

# remove data from variables before next run
unset jiras
unset dires

done