]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding cleanup-loghosts-hosts
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 16:51:26 +0000 (17:51 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 18 Mar 2019 16:51:26 +0000 (17:51 +0100)
sbin/cleanup-loghosts-hosts [new file with mode: 0755]

diff --git a/sbin/cleanup-loghosts-hosts b/sbin/cleanup-loghosts-hosts
new file mode 100755 (executable)
index 0000000..1b33c9c
--- /dev/null
@@ -0,0 +1,207 @@
+#!/usr/bin/bash
+
+HOSTS_DIR=/opt/log/HOSTS
+
+if [[ ! -d "${HOSTS_DIR}" ]] ; then
+    echo "Logging directory for hosts '${HOSTS_DIR}' not found." >&2
+    exit 5
+fi
+
+CUR_YEAR=$( date +'%Y' )
+CUR_MONTH=$( date +'%m' )
+
+#------------------------------------------------------------------------------
+perform_day() {
+
+    local ddir="$1"
+    local this_day=$( basename "${ddir}" )
+    local cmd=
+
+    if [[ "${this_day}" =~ ^[0-3][0-9]$ ]] ; then
+        :
+    else
+        return
+    fi
+
+    echo "        * day: ${this_day}"
+
+    local empty="y"
+    local entry=
+    for entry in $( ls -1d "${ddir}"/* "${ddir}"/.[^.]* 2>/dev/null ) ; do
+        if [[ -e "${entry}" ]] ; then
+            empty="n"
+            echo "               Found '${entry}'."
+            break
+        fi
+    done
+
+    if [[ "${empty}" == "y" ]] ; then
+        echo "               Removing ..."
+        cmd="rmdir \"${ddir}\""
+        echo "               ${cmd}"
+        eval ${cmd}
+    else
+        echo "               Day not empty"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+perform_month() {
+
+    local mdir="$1"
+    local this_month=$( basename "${mdir}" )
+    local ddir=
+    local cmd=
+
+    if [[ "${this_month}" =~ ^[0-1][0-9]$ ]] ; then
+        :
+    else
+        return
+    fi
+
+    echo "     * month: ${this_month}"
+
+    for ddir in $( ls -1d "${mdir}"/* ) ; do
+        if [[ ! -d "${ddir}" ]] ; then
+            continue
+        fi
+        perform_day "${ddir}"
+    done
+
+    local empty="y"
+    local entry=
+    for entry in $( ls -1d "${mdir}"/* "${mdir}"/.[^.]* 2>/dev/null ) ; do
+        if [[ -e "${entry}" ]] ; then
+            empty="n"
+            echo "           Found '${entry}'."
+            break
+        fi
+    done
+
+    if [[ "${empty}" == "y" ]] ; then
+        echo "           Removing ..."
+        cmd="rmdir \"${mdir}\""
+        echo "           ${cmd}"
+        eval ${cmd}
+    else
+        echo "           Month not empty"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+perform_year() {
+
+    local ydir="$1"
+    local this_year=$( basename "${ydir}" )
+    local mdir=
+    local cmd=
+
+    if [[ "${this_year}" =~ ^[12][0-9][0-9][0-9]$ ]] ; then
+        :
+    else
+        return
+    fi
+
+    echo "  * year: ${this_year}"
+
+    for mdir in $( ls -1d "${ydir}"/* ) ; do
+        if [[ ! -d "${mdir}" ]] ; then
+            continue
+        fi
+        perform_month "${mdir}"
+    done
+
+    local empty="y"
+    local entry=
+    for entry in $( ls -1d "${ydir}"/* "${ydir}"/.[^.]* 2>/dev/null ) ; do
+        if [[ -e "${entry}" ]] ; then
+            empty="n"
+            echo "        Found '${entry}'."
+            break
+        fi
+    done
+
+    if [[ "${empty}" == "y" ]] ; then
+        echo "        Removing ..."
+        cmd="rmdir \"${ydir}\""
+        echo "        ${cmd}"
+        eval ${cmd}
+    else
+        echo "        Year not empty"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+perform_host() {
+
+    local hdir="$1"
+    local this_host=$( basename "${hdir}" )
+    local ydir=
+    local cmd=
+
+    echo
+    echo "Performing host '${this_host}' ..."
+
+    for ydir in $( ls -1d "${hdir}"/* ) ; do
+        if [[ ! -d "${ydir}" ]] ; then
+            continue
+        fi
+        perform_year "${ydir}"
+    done
+
+    local empty="y"
+    local entry=
+    for entry in $( ls -1d "${hdir}"/* "${hdir}"/.[^.]* 2>/dev/null ) ; do
+        if [[ -e "${entry}" ]] ; then
+            empty="n"
+            echo "  Found '${entry}'."
+            break
+        fi
+    done
+
+    if [[ "${empty}" == "y" ]] ; then
+        echo "  Removing ..."
+        cmd="rmdir \"${hdir}\""
+        echo "  ${cmd}"
+        eval ${cmd}
+    else
+        echo "  Host not empty"
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+do_cleanup() {
+
+    local dir=
+    local oifs="${IFS}"
+    IFS="
+"
+
+    for dir in $( ls -1d "${HOSTS_DIR}"/* ) ; do
+        if [[ ! -d "${dir}" ]] ; then
+            continue
+        fi
+        perform_host "${dir}"
+    done
+
+}
+
+
+#------------------------------------------------------------------------------
+main() {
+
+    do_cleanup
+}
+
+main "$@"
+
+
+exit 0
+
+# vim: et ts=4 list
+
+