#!/bin/bash declare -a SPECIAL_UNITS=( hprobe.timer telemd.socket telemd.path ) declare -a SERVICES=( hprobe.service journal-probe.service oops-probe.service pstore-probe.service klogscanner.service telemd.service ) SCRIPT="$0" TELEM_DIR=/etc/telemetrics OPT_OUT_FILE=${TELEM_DIR}/opt-out TELEM_WRK_DIRS_CONF=/usr/lib/tmpfiles.d/telemetrics-dirs.conf create_work_dirs() { # Creates dirs if missing, adjust ownership if exists systemd-tmpfiles --create ${TELEM_WRK_DIRS_CONF} } telem_remove_work_dirs() { # Remove dirs awk '/^d/{print $2}' ${TELEM_WRK_DIRS_CONF} | xargs rm -rf; } exit_ok() { echo "$1" > /dev/stderr exit 0 } exit_err() { echo "$1" > /dev/stderr exit 1 } notice() { echo "$1" > /dev/stderr } for_each_service() { local action=$1 && shift local -a array=($*) for service in "${array[@]}"; do systemctl $action $service [ $? -ne 0 ] && notice "Failed to $action ${service}. Continuing..." done } telem_stop() { # the special units must be stopped first so that activation no longer happens for_each_service "stop" ${SPECIAL_UNITS[@]} for_each_service "stop" ${SERVICES[@]} } telem_start() { [ -f $OPT_OUT_FILE ] && exit_err "Opt out is enabled. Cannot start services." # trigger systemd-tmpfiles work dirs creation create_work_dirs # the units in SERVICES are activated as needed, so no need to start them for_each_service "start" ${SPECIAL_UNITS[@]} } telem_is_active() { # check only activation units systemctl is-active telemd.socket } telem_opt_out() { [ -f $OPT_OUT_FILE ] && exit_ok "Already opted out. Nothing to do." mkdir -p $TELEM_DIR || exit_err "Failed to create ${TELEM_DIR}." touch $OPT_OUT_FILE || exit_err "Failed to create ${OPT_OUT_FILE}." telem_stop telem_remove_work_dirs } telem_opt_in() { [ ! -f $OPT_OUT_FILE ] && exit_ok "Already opted in. Nothing to do." rm -f $OPT_OUT_FILE || exit_err "Failed to remove ${OPT_OUT_FILE}." telem_start } telem_restart() { telem_stop telem_start } telem_journal_cli() { telem_journal "$@" } usage() { format=' %-10s %s\n' printf "\n" printf "%s - Control actions for telemetry services\n" "$SCRIPT" printf "\n" printf "$format" "stop" "Stops all running telemetry services" printf "$format" "start" "Starts all telemetry services" printf "$format" "restart" "Restarts all telemetry services" printf "$format" "is-active" "Checks if telemd is active" printf "$format" "opt-in" "Opts in to telemetry, and starts telemetry services" printf "$format" "opt-out" "Opts out of telemetry, and stops telemetry services" printf "$format" "journal" "Prints telemd journal contents. Use -h argument with" printf "$format" "" "command for more options" printf "\n" exit 2 } if [ "$1" != "journal" ] && [ $# -ne 1 ]; then usage fi if [ $EUID -ne 0 ]; then exit_err "Must be root to run this command. Exiting..." fi SUBCOMMAND=$1 case $SUBCOMMAND in opt-out) telem_opt_out ;; opt-in) telem_opt_in ;; stop) telem_stop ;; start) telem_start ;; restart) telem_restart ;; is-active) telem_is_active ;; journal) telem_journal_cli "$@" ;; *) notice "Unknown command passed to $SCRIPT" usage ;; esac exit 0 # vi: ts=8 sw=2 sts=2 et tw=80