commit 4767e76060cac4668fd70e4fddb874aa7e8e1696 Author: default Date: Thu May 30 17:42:32 2024 +0200 new: [add] init commit, elab-update, elab-manage-install & scripts diff --git a/README.md b/README.md new file mode 100644 index 0000000..00aca51 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Elabore Manage monitoring Script + +This allow to handle alerting and monitoring using cron scripts + send function from myc-manage + +> Warning : your server should be under myc-manage administration to have the send function installed + +## Usage : + +1. Clone the repo +2. run the script in `bin/elab-manage-install` + +Then each time you want to update this repos : `elab-update` from the server + +## Adding a new script : + +Take exemple on the simples scripts that are in cron.daily or cron.hourly and make your own \ No newline at end of file diff --git a/bin/elab-manage-install b/bin/elab-manage-install new file mode 100755 index 0000000..ada9f79 --- /dev/null +++ b/bin/elab-manage-install @@ -0,0 +1,20 @@ +#!/bin/bash + +# Get the directory where the script resides +SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +# TODO : ${##*/} + +# Define the destination directory where the elab-update symbolic link will be created +DEST_DIR="/usr/local/bin/" + +# Check if the elab-update script exists in the source directory +if [ ! -f "$SOURCE_DIR/elab-update" ]; then + echo "Error: elab-update script not found in the source directory." + exit 1 +fi + +# Create a symbolic link to the elab-update script in the destination directory +echo "Creating symbolic link to elab-update script in $DEST_DIR..." +sudo ln -sf "$SOURCE_DIR/elab-update" "$DEST_DIR" || { echo "Error: Unable to create symbolic link."; exit 1; } + +echo "elab-update command has been successfully installed as a symbolic link." diff --git a/bin/elab-update b/bin/elab-update new file mode 100755 index 0000000..4038e6c --- /dev/null +++ b/bin/elab-update @@ -0,0 +1,37 @@ +#!/bin/bash + +# Define the repository path +REPO_PATH="/opt/apps/elab-manage" + +# Function to update the git repository +update_repo() { + echo "Updating the git repository..." + cd "$REPO_PATH" || { + echo "Error: Unable to find the repository at $REPO_PATH"; + exit 1; + } + git pull -r || { + echo "Error: Unable to update the repository"; + exit 1; + } +} + + + +# Function to update symbolic links +update_symlinks() { + echo "Updating symbolic links for cron.daily..." + # Update or add new symbolic links for cron.daily and cron.hourly + for dir in "cron."{daily,hourly}; do + find -L /etc/$dir -maxdepth 1 -type l -ilname $REPO_PATH/etc/$dir\* -delete + ln -sf $REPO_PATH/etc/$dir/* /etc/$dir + done + + echo "Symbolic links have been successfully updated." +} + +# Update the git repository and symbolic links +update_repo +update_symlinks + +echo "Elabore Update complete." diff --git a/etc/cron.daily/remove_state_file_48h b/etc/cron.daily/remove_state_file_48h new file mode 100755 index 0000000..e9050d2 --- /dev/null +++ b/etc/cron.daily/remove_state_file_48h @@ -0,0 +1,7 @@ +#!/bin/bash + +SHELL=/bin/bash +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +## Remove every .state file older than 48h -- Cleanup script if the others aren’t strong enougth +find /var/run/elab-manage -name "*.state" -type f -mtime +2 -delete \ No newline at end of file diff --git a/etc/cron.hourly/disk_usage b/etc/cron.hourly/disk_usage new file mode 100755 index 0000000..1d4178c --- /dev/null +++ b/etc/cron.hourly/disk_usage @@ -0,0 +1,40 @@ +#!/bin/bash + +SHELL=/bin/bash +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +## check disk usage and send a notification if it's above 75% or 90% + +percent_usage=$(df /srv -h) +percent_usage=${percent_usage##*$'\n'} +percent_usage=${percent_usage% *} +percent_usage=${percent_usage##* } +percent_usage=${percent_usage%\%} + +STATE_WORKING_DIR="/var/run/elab-manage" +mkdir -p "$STATE_WORKING_DIR" + +if [ "$percent_usage" -ge "90" ]; then + if [ -e $STATE_WORKING_DIR/disk_usage_90.state ]; then + exit 0 + else + touch $STATE_WORKING_DIR/disk_usage_90.state + message="$(hostname): WARNING disk usage >=90%" + send -c disk.crit "$message" + fi +elif [ "$percent_usage" -ge "75" ]; then + if [ -e $STATE_WORKING_DIR/disk_usage_75.state ]; then + exit 0 + else + touch $STATE_WORKING_DIR/disk_usage_75.state + message="$(hostname): WARNING disk usage >=75 <90%" + send -c disk.alert "$message" + fi +else + if [ -e $STATE_WORKING_DIR/disk_usage_75.state ]; then + rm $STATE_WORKING_DIR/disk_usage_75.state + fi + if [ -e $STATE_WORKING_DIR/disk_usage_90.state ]; then + rm $STATE_WORKING_DIR/disk_usage_90.state + fi +fi diff --git a/etc/cron.hourly/load_average_max b/etc/cron.hourly/load_average_max new file mode 100755 index 0000000..c31c13d --- /dev/null +++ b/etc/cron.hourly/load_average_max @@ -0,0 +1,32 @@ +#!/bin/bash + +SHELL=/bin/bash +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +MAX_PER_PROC=3 + +## integer only for the 5m load avg + +LOCK_WORKING_DIR="/var/run/elab-manage" +mkdir -p "$LOCK_WORKING_DIR" + +int_avg=$(while read line; do + echo "$line" + done < /proc/loadavg) +int_avg=${int_avg#* } +int_avg=${int_avg%%.*} +max=$[$MAX_PER_PROC * $(grep -c ^processor /proc/cpuinfo)] +if [ "$int_avg" -gt "$max" ]; then + if [ -e $LOCK_WORKING_DIR/load_average_max.lock ]; then + exit 0 + else + touch $LOCK_WORKING_DIR/load_average_max.lock + message="[$(hostname)] : WARNING - load average ($int_avg) is above max per processor : ($MAX_PER_PROC * $(grep -c ^processor /proc/cpuinfo) = $max)" + echo $message | logger -t load_average_max + send "$message" + fi +else + if [ -e $LOCK_WORKING_DIR/load_average_max.lock ]; then + rm $LOCK_WORKING_DIR/load_average_max.lock + fi +fi