Script to monitor GLPI Agent version
This commit is contained in:
parent
c3d305186a
commit
06341fa303
|
@ -0,0 +1,257 @@
|
||||||
|
#!/bin/sh
|
||||||
|
# Purpose {{{
|
||||||
|
## Create a temp file (to monitor) if an upgrade is available for GLPI Agent
|
||||||
|
## project on Github − https://github.com/glpi-project/glpi-agent/releases
|
||||||
|
## It's based on .deb package installation to check the current version.
|
||||||
|
## It can also compare the current available version in APT repositories
|
||||||
|
## if "--repo" option is given.
|
||||||
|
## How-to use {{{
|
||||||
|
### 1. Needs releasetags script, in the same directory
|
||||||
|
### cf. https://git.ipr.univ-rennes1.fr/cellinfo/scripts/src/master/github/releasetags
|
||||||
|
# wget https://git.ipr.univ-rennes1.fr/cellinfo/scripts/raw/master/github/releasetags
|
||||||
|
### 2. Create a cron job, eg :
|
||||||
|
#00 20 * * * root /opt/repos/ipr.scripts/github/check.glpi-agent.update
|
||||||
|
### 2-1 Create a cron job to compare the version available in a Debian repository :
|
||||||
|
#00 20 * * * root /opt/repos/ipr.scripts/github/check.glpi-agent.update --repo
|
||||||
|
### 3. Monitor the temp file : /tmp/.github.glpi-agent.upgrade
|
||||||
|
# Or enable MAILTO in cronjob and edit the script to print a message.
|
||||||
|
# Or send a mail.
|
||||||
|
# …
|
||||||
|
## }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# Vars {{{
|
||||||
|
PROGNAME=$(basename "${0}"); readonly PROGNAME
|
||||||
|
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
|
||||||
|
ARGS="${*}"; readonly ARGS
|
||||||
|
readonly NBARGS="${#}"
|
||||||
|
[ -z "${DEBUG}" ] && DEBUG=1
|
||||||
|
## Export DEBUG for sub-script
|
||||||
|
export DEBUG
|
||||||
|
|
||||||
|
## Default values for some vars
|
||||||
|
### Don't use APT repository by default
|
||||||
|
readonly REPOSITORY_MODE_DEFAULT=1
|
||||||
|
|
||||||
|
readonly GLPI_AGENT_REPO_URL="https://github.com/glpi-project/glpi-agent"
|
||||||
|
readonly GLPI_AGENT_NEW_VERSION_FILE="/tmp/.github.glpi-agent.upgrade"
|
||||||
|
|
||||||
|
## Colors
|
||||||
|
readonly PURPLE='\033[1;35m'
|
||||||
|
readonly RED='\033[0;31m'
|
||||||
|
readonly RESET='\033[0m'
|
||||||
|
readonly COLOR_DEBUG="${PURPLE}"
|
||||||
|
# }}}
|
||||||
|
usage() { # {{{
|
||||||
|
|
||||||
|
cat <<- HELP
|
||||||
|
usage: $PROGNAME [-d|-h|-r]
|
||||||
|
|
||||||
|
Compare GLPI Agent installed version and latest one.
|
||||||
|
|
||||||
|
EXAMPLES :
|
||||||
|
- Compare the current version installed from .deb file
|
||||||
|
${PROGNAME}
|
||||||
|
|
||||||
|
- Compare the current version from apt's repository
|
||||||
|
${PROGNAME} --repo
|
||||||
|
|
||||||
|
OPTIONS :
|
||||||
|
-d,--debug
|
||||||
|
Enable debug messages.
|
||||||
|
|
||||||
|
-h,--help
|
||||||
|
Print this help message.
|
||||||
|
|
||||||
|
-r,--repo,--repository
|
||||||
|
Use GLPI Agent version from APT repository.
|
||||||
|
HELP
|
||||||
|
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
debug_message() { # {{{
|
||||||
|
|
||||||
|
local_debug_message="${1}"
|
||||||
|
|
||||||
|
## Print message if DEBUG is enable (=0)
|
||||||
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}"
|
||||||
|
|
||||||
|
unset local_debug_message
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
error_message() { # {{{
|
||||||
|
|
||||||
|
local_error_message="${1}"
|
||||||
|
local_error_code="${2}"
|
||||||
|
|
||||||
|
## Print message
|
||||||
|
printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}"
|
||||||
|
|
||||||
|
unset local_error_message
|
||||||
|
|
||||||
|
exit "${local_error_code:=66}"
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
define_vars() { # {{{
|
||||||
|
|
||||||
|
## If repository_mode wasn't defined (argument) {{{
|
||||||
|
if [ -z "${repository_mode}" ]; then
|
||||||
|
## Use default value
|
||||||
|
readonly repository_mode="${REPOSITORY_MODE_DEFAULT}"
|
||||||
|
fi
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
is_var_empty() { # {{{
|
||||||
|
|
||||||
|
## Return False by default
|
||||||
|
return_var_empty="1"
|
||||||
|
## Total number of variables to test
|
||||||
|
local_total_var="${#}"
|
||||||
|
|
||||||
|
loop_count_var_empty="0"
|
||||||
|
|
||||||
|
## While it remains a variable to test
|
||||||
|
while [ "${local_total_var}" -gt "${loop_count_var_empty}" ]; do
|
||||||
|
debug_message "is_var_empty − \
|
||||||
|
Test var: ${1}."
|
||||||
|
### Test if this is empty and set return value to True
|
||||||
|
[ -z "${1}" ] && return_var_empty="0"
|
||||||
|
|
||||||
|
### Increase the number of tested variables
|
||||||
|
loop_count_var_empty=$((loop_count_var_empty+1))
|
||||||
|
|
||||||
|
### Shift to the next variable
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
unset local_total_var
|
||||||
|
unset loop_count_var_empty
|
||||||
|
|
||||||
|
return "${return_var_empty}"
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
main() { # {{{
|
||||||
|
|
||||||
|
## Define all vars
|
||||||
|
define_vars
|
||||||
|
|
||||||
|
## Get current version from APT repository or from .deb package {{{
|
||||||
|
|
||||||
|
if [ "${repository_mode}" -eq "0" ]; then
|
||||||
|
debug_message "--- MAIN Check current version from ${RED}APT repositories${COLOR_DEBUG}."
|
||||||
|
glpi_agent_current_version=$(apt-cache policy glpi-agent \
|
||||||
|
| awk '/Candidate:/ {print $2}' \
|
||||||
|
| sed 's/.:\(.*\)-.*/\1/')
|
||||||
|
else
|
||||||
|
debug_message "--- MAIN Check current version from ${RED}manually installed .deb package${COLOR_DEBUG}."
|
||||||
|
glpi_agent_current_version=$(dpkg -l glpi-agent \
|
||||||
|
| awk '/^ii.*glpi-agent/ {print $3}' \
|
||||||
|
| sed 's/.:\(.*\)-.*/\1/')
|
||||||
|
fi
|
||||||
|
## }}}
|
||||||
|
## Get latest version from project repository {{{
|
||||||
|
### OR Exit with error message
|
||||||
|
debug_message "--- MAIN Check latest version from project repository (${RED}${GLPI_AGENT_REPO_URL}${COLOR_DEBUG})."
|
||||||
|
glpi_agent_latest_version=$("${PROGDIR}/releasetags" "${GLPI_AGENT_REPO_URL}" | head --lines=1 \
|
||||||
|
|| error_message "Can't get latest GLPI Agent version from repository (${GLPI_AGENT_REPO_URL})." 02 )
|
||||||
|
## }}}
|
||||||
|
|
||||||
|
## If any of the two variables is empty {{{
|
||||||
|
### OR Exit with error message
|
||||||
|
is_var_empty "${glpi_agent_current_version}" "${glpi_agent_latest_version}" \
|
||||||
|
&& error_message "At least one variable is empty (current version : ${glpi_agent_current_version}, latest version : ${glpi_agent_latest_version}) !" 04
|
||||||
|
## }}}
|
||||||
|
|
||||||
|
# Check if the current version is the last one {{{
|
||||||
|
## If the two versions are the same {{{
|
||||||
|
if [ "${glpi_agent_current_version}" = "${glpi_agent_latest_version}" ]; then
|
||||||
|
debug_message "Test version − \
|
||||||
|
The current GLPI Agent version (${RED}${glpi_agent_current_version}${COLOR_DEBUG}) is up-to-date."
|
||||||
|
|
||||||
|
### Ensure to remove any temp file
|
||||||
|
rm --force -- "${GLPI_AGENT_NEW_VERSION_FILE}"
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
## }}}
|
||||||
|
## If the versions are different {{{
|
||||||
|
else
|
||||||
|
debug_message "Test version − \
|
||||||
|
Current version (${RED}${glpi_agent_current_version}${COLOR_DEBUG}) and latest one (${RED}${glpi_agent_latest_version}${COLOR_DEBUG}) seems to be different."
|
||||||
|
|
||||||
|
## Create a temp file to monitor
|
||||||
|
touch -- "${GLPI_AGENT_NEW_VERSION_FILE}"
|
||||||
|
debug_message "An upgrade is available for Cachet (current : ${glpi_agent_current_version}) : ${glpi_agent_latest_version}." >> "${GLPI_AGENT_NEW_VERSION_FILE}"
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
## }}}
|
||||||
|
# }}}
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# Manage arguments # {{{
|
||||||
|
# This code can't be in a function due to argument management
|
||||||
|
|
||||||
|
if [ ! "${NBARGS}" -eq "0" ]; then
|
||||||
|
|
||||||
|
manage_arg="0"
|
||||||
|
|
||||||
|
## If the first argument is not an option
|
||||||
|
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
|
||||||
|
then
|
||||||
|
## Print help message and exit
|
||||||
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
||||||
|
printf '%b\n' "---"
|
||||||
|
usage
|
||||||
|
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse all options (start with a "-") one by one
|
||||||
|
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
|
||||||
|
|
||||||
|
case "${1}" in
|
||||||
|
--debug ) ## debug
|
||||||
|
DEBUG=0
|
||||||
|
debug_message "--- Manage argument BEGIN"
|
||||||
|
;;
|
||||||
|
-h|--help ) ## help
|
||||||
|
usage
|
||||||
|
## Exit after help informations
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-r|--repo|--repository ) ## Set repository_mode
|
||||||
|
repository_mode=0
|
||||||
|
;;
|
||||||
|
* ) ## unknow option
|
||||||
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
||||||
|
printf '%b\n' "---"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
debug_message "| ${RED}${1}${COLOR_DEBUG} option managed."
|
||||||
|
|
||||||
|
## Move to the next argument
|
||||||
|
shift
|
||||||
|
manage_arg=$((manage_arg+1))
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
debug_message "| ${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
|
||||||
|
else
|
||||||
|
debug_message "| No arguments/options to manage."
|
||||||
|
fi
|
||||||
|
|
||||||
|
debug_message "--- Manage argument END"
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
main
|
||||||
|
|
||||||
|
exit 255
|
Loading…
Reference in New Issue