Last active
February 28, 2020 20:22
-
-
Save sqqqrly/fe812e0fe332c5c64bcefe9d242d48db to your computer and use it in GitHub Desktop.
A utility to restart select services and/or truncate log files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./bounce_services.sh | |
=== What would you like to do with openstack services? | |
services --> logs: | |
openstack-nova-compute-8202E4C_10B54ER.service --> /var/log/nova/nova-compute-8202E4C_10B54ER.log | |
openstack-cinder-volume-generic0.service --> /var/log/cinder/volume-generic0.log | |
none --> /tmp/infinidat_logit.log | |
1) truncate logs only 3) truncate and restart | |
2) restart services only 4) exit | |
#? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# A utility to restart select services and/or truncate log files. | |
set -o nounset | |
set -o pipefail | |
set -o noclobber | |
#set -o errexit | |
# To enable info and die functions logging to syslog, set to true. | |
declare -r enable_syslogging="false" | |
# Report error with $leader prepended and die. | |
# Override leader with an optional replacement. | |
# Optionally send to logger. | |
function die { | |
local msg="$1" | |
local leader="${2:-Fatal:}" | |
(>&2 printf "$leader $msg\n") # Subshell avoids interactions with other redirections | |
if [ "$enable_syslogging" == "true" ]; then | |
logger -p user.error -t "$(basename $0)" "$leader $msg" | |
fi | |
kill -SIGPIPE "$$" # Die with exit code 141 | |
} | |
# Report msg with $leader prepended. | |
# Override leader with an optional replacement. | |
# Optionally send to logger. | |
function info { | |
local msg="$1" | |
local leader="${2:-Info:}" | |
(printf "$leader $msg\n") # Subshell avoids interactions with other redirections | |
if [ "$enable_syslogging" == "true" ]; then | |
logger -p user.notice -t "$(basename $0)" "$leader $msg" | |
fi | |
} | |
function truncate_logs { | |
for log in "${logs[@]}"; do | |
if [ "$verbose" == "true" ]; then | |
echo "truncating $log" | |
fi | |
if [ -f "$log" ]; then # Do not create missing log | |
truncate -s 0 "$log" || die "cannot truncate log $log" | |
fi | |
done | |
} | |
function manage_services { | |
local -r action="$1" | |
for svc in "${services[@]}"; do | |
if [[ "$verbose" == "true" && "$svc" != "none" ]]; then | |
echo "$action $svc" | |
fi | |
if [ "$svc" != "none" ]; then | |
systemctl "$action" "$svc" || die "cannot $action service $svc" | |
fi | |
done | |
} | |
function inputs_menu { | |
info "What would you like to do with openstack services?" "===" | |
echo | |
info "services --> logs:" " " | |
for (( i=0; i<${#logs[@]}; i++ )); do | |
info "${services[$i]} --> ${logs[$i]}" " " | |
done | |
echo | |
select choice in "truncate logs only" "restart services only" "truncate and restart" "exit"; do | |
case $choice in | |
"truncate logs only") truncate_logs "$logs"; break;; | |
"restart services only") manage_services "restart" "$services"; break;; | |
"truncate and restart") manage_services "stop" "$services"; truncate_logs "$logs"; manage_services "start" "$services"; break;; | |
"exit") exit;; | |
esac | |
done | |
} | |
### MAIN ### | |
declare -r verbose="false" | |
declare -ar logs=( \ | |
"/var/log/nova/nova-compute-8202E4C_10B54ER.log" \ | |
"/var/log/cinder/volume-generic0.log" \ | |
"/tmp/infinidat_logit.log" | |
) | |
declare -ar services=( \ | |
"openstack-nova-compute-8202E4C_10B54ER.service" \ | |
"openstack-cinder-volume-generic0.service" \ | |
"none" | |
) | |
inputs_menu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment