Skip to content

Instantly share code, notes, and snippets.

@mugendi
Last active January 26, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugendi/735f8c61626d0428699ef0d964512b93 to your computer and use it in GitHub Desktop.
Save mugendi/735f8c61626d0428699ef0d964512b93 to your computer and use it in GitHub Desktop.
Simple Bash Script that creates a monit config script from your PM2 Processes. Run using ./script.sh [-m YOUR_MONIT_DIR]
#!/bin/bash
# Some utility functions....
trailing_slash () {
STR="${1}"
length=${#STR}
last_char=${STR:length-1:1}
[[ $last_char != "/" ]] && STR="$monitDir/";
echo $STR;
}
# slice array
sliceArray () {
#get array variable
A=("$@")
ALength=${#A[@]}
AStart=3
AStop=$(expr $ALength - $AStart - 2)
echo "${A[@]:AStart:AStop}"
}
# To style messages for console
styled_echo () {
case ${2} in
red)
text="\e[31m${1}\e[0m"
;;
green)
text="\e[32m${1}\e[0m"
;;
blue)
text="\e[34m${1}\e[0m"
;;
yellow)
text="\e[33m${1}\e[0m"
;;
*) # default color
text="\e[39m${1}\e[0m"
;;
esac
echo -e $text
}
# No need to do anything if monit is not installed!
hasMonit=$(command -v monit)
if ((${#hasMonit}==0))
then
styled_echo "Hey! It doesn't look like monit is installed! Get it here... https://mmonit.com/monit/" red
exit
fi
while [[ $# -gt 0 ]]
do
# echo $#
key="${1}"
# echo $key
case ${key} in
-m|--monit)
monitDir="${2}"
;;
-h|--help)
cat << EndOfMessage
Usage: ./pm2-make.sh [options]
Options are as follows:
-m monit Path to the monit folder. (Often /etc/monit/)
EndOfMessage
# shift # past argument
shift
;;
*) # unknown option
shift # past argument
;;
esac
shift
done
# Use Default Dir
monitDir=${monitDir:-"/etc/monit"}
# Ensure Trailing Slash
monitDir=$(trailing_slash $monitDir )
# Ensure directory exists else
if [ ! -d "$monitDir" ]; then
#No need to proceed, stop right here
styled_echo "The monit folder entered '${monitDir}' does not exist!'" red
exit
fi
# Monit Folders
CONF_AVAILABLE_DIR="${monitDir}conf-available/"
CONF_ENABLED_DIR="${monitDir}conf-enabled/" ;
# Working Directory
WORKING_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Make pids directory
if [ ! -d "${WORKING_DIR}/pids" ] ; then
styled_echo "Creating PIDs directory" green
mkdir "${WORKING_DIR}/pids"
fi
PIDS=()
NAMES=()
styled_echo 'Fetching PM2 Ids...' blue
pm2 ps | tr -s ' '| cut -d ' ' -f 10 | (while read i; do
PIDS+=($i)
done
pm2 ps | tr -s ' '| cut -d ' ' -f 2 | (while read i; do
NAMES+=($i)
done
# make arrays from the pm2 ps table
IFS=' ' read -r -a NAMES <<< $(echo $(sliceArray "${NAMES[@]}") )
IFS=' ' read -r -a PIDS <<< $(echo $(sliceArray "${PIDS[@]}") )
MONIT_CMDS=' '
# printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
styled_echo 'Writing process files...' blue
echo
for i in "${!NAMES[@]}"; do
if ((${PIDS[$i]}>0))
then
FILE="${WORKING_DIR}/pids/${NAMES[$i]}";
MONIT_CMDS+="check process PM2-${NAMES[$i]} with pidfile ${FILE}__ "
echo ${PIDS[$i]} >> $FILE
styled_echo "PID ${FILE} created with PID: ${PIDS[$i]}" yellow
fi
done
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
MONIT_FILE="${CONF_AVAILABLE_DIR}PM2";
# Drop File if exists
if [ -f $MONIT_FILE ] ; then
styled_echo "Deleting old monit config file: ${MONIT_FILE}..." red
rm $MONIT_FILE
fi
styled_echo "Writing monit config file: ${MONIT_FILE}..." blue
# Format the string adding new-lines and spaces where required then write to file
echo " ${MONIT_CMDS}" | tr '_' '\n' | tr '+' '\t' >> $MONIT_FILE
styled_echo "Done!" green
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
# create symbolic link...
styled_echo "Enabling monit config file ${MONIT_FILE} i.e (symbolic link to ${CONF_ENABLED_DIR})" blue
# Check that symbolink link points to the right file in /conf-available
if [[ -L "${CONF_ENABLED_DIR}PM2" && -f "${CONF_AVAILABLE_DIR}PM2" ]]
then
# All good!
styled_echo "Symbolic link exists!" green
else
if [ -f "${CONF_ENABLED_DIR}PM2" ]
then
# Symbolic link does not point to the right file or is broken, replace
styled_echo "Replacing symbolic link..." red
ln -sf $MONIT_FILE $CONF_ENABLED_DIR
else
# Symbolic link does not exist. Create
styled_echo "Creating new symbolic link..." yellow
ln -s $MONIT_FILE $CONF_ENABLED_DIR
fi
fi
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
# Reload Monit
styled_echo "Reloading Monit" blue
monit reload
styled_echo "All Ready!" green
)
) #Keeping wihin the right scope of the while loops
@mugendi
Copy link
Author

mugendi commented Jan 26, 2019

Run using ./script.sh [-m YOUR_MONIT_DIR] for example ./script.sh -m /etc/monit

Note:

  • -m (if not entered) defaults to "/etc/monit"
  • This script uses pm2 ps command and thus will only create config file for processes that are RUNNING on PM2. Also, it only monitors status and does not add stop/start methods. After all, PM2 is a process manager, that's fine. Let PM2 do what it is built to do, i.e manage process restarts and so on.

This Script has been updated!! While it works fine, you really should use THIS UPDATED ONE!

@mugendi
Copy link
Author

mugendi commented Jan 26, 2019

Should output something like...

image

Now go ahead and check out monit...

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment