Scripts/enable imap logging
From Scalix Wiki
#!/bin/bash # Initializes the database if not already initialized. # Additionally, starts the database if not running. # # Usage: enable_imap_logging -u "User Name" # enable_imap_logging -a # PROG_NAME=enable_imap_logging LOG_LEVEL=8 LOG_FILE='~/tmp/imap.%u.%p' USER_NAME="" INSTANCE="" FOR_ALL=0 REMOVE_ONLY=0 LOOP_REMOVE=0 function usage() { cat <<EOF Usage $PROG_NAME: -u <user_name> User for which to enable/disable logging -a Enable/disable for all users (general.cfg) -i Scalix instance name. -r Remove (disable) logging instead of enabling. -X With '-a -r' also recursively remove logging for individual users (users.cfg). Examples: $PROG_NAME -u "User Person" $PROG_NAME -u "User Person" -r $PROG_NAME -a $PROG_NAME -a -r -X EOF exit 2 } while getopts "i:u:arXh" flag do case "$flag" in u) USER_NAME="${OPTARG}";; a) USER_NAME=""; FOR_ALL=1;; r) REMOVE_ONLY=1;; X) REMOVE_ONLY=1; LOOP_REMOVE=1;; i) INSTANCE="${OPTARG}";; h) usage;; *) usage;; esac done function fail_on_error() { if [ $1 -ne 0 ]; then echo "$2" 1>&2 exit $1 fi return 0 } function base() { if [ -x /opt/scalix/bin/omcheckgc -a -r /etc/opt/scalix/instance.cfg ] then dirname "$(OMCURRENT=${1:-$(omcheckgc -s)} omcheckgc -d)" else local host=${1:-${OMCURRENT:-$(hostname -s)}} echo "/var/opt/scalix/${host:0:1}${host: -1:1}" fi } INSTDIR=`base $INSTANCE` function get_user_id() { local rc uid rc=0 uid=`omshowu -n "$1" -G | grep 'Internal user Id' | cut -d':' -f2 | tr -d '[:blank:]'` if [ ! -z "$uid" ]; then echo "$uid" else rc=1 fi return $rc } function remove_imap_logging_from_file() { local has_logging cfg_file cfg_file="$1" tmp_file=`mktemp` egrep '\<IMAP_LOGFILE\>|\<IMAP_LOGLEVEL\>' $cfg_file 1>/dev/null 2>/dev/null if [ $? -eq 0 ]; then sed -e 's/^\s*IMAP_LOGFILE\s*=.*$//g' \ -e 's/^\s*IMAP_LOGLEVEL\s*=.*$//g' $cfg_file > $tmp_file if [ $? -eq 0 ]; then cp -f $tmp_file $cfg_file fi rm -f $tmp_file fi } function add_imap_logging_to_file() { local cfg_file cfg_file="$1" echo "IMAP_LOGFILE=${LOG_FILE}" >> $cfg_file echo "IMAP_LOGLEVEL=${LOG_LEVEL}" >> $cfg_file } ################################# # Main code ################################# if [ -z "$USER_NAME" ] && [ $FOR_ALL -eq 0 ]; then fail_on_error 2 "You must provide a user name (-u) or use the all option (-a)." fi cfg_file="" if [ $FOR_ALL -eq 1 ]; then # Enable for all users cfg_file="${INSTDIR}/s/sys/general.cfg" else # Enable for a specific user uid=`get_user_id "$USER_NAME"` fail_on_error $? "Unable to locate ID for ${USER_NAME}." cfg_file="${INSTDIR}/s/sys/user.cfg/${uid}" fi if [ -f $cfg_file ]; then remove_imap_logging_from_file "$cfg_file" fail_on_error $? "Cannot clear existing IMAP log settings." if [ $FOR_ALL -eq 1 ] && [ $LOOP_REMOVE -eq 1 ]; then for f in `ls ${INSTDIR}/s/sys/user.cfg` do remove_imap_logging_from_file "${INSTDIR}/s/sys/user.cfg/${f}" fail_on_error $? "Cannot clear existing IMAP log of file $f." done fi fi if [ $REMOVE_ONLY -ne 1 ]; then add_imap_logging_to_file "$cfg_file" fail_on_error $? "Cannot add logging settings to $cfg_file." if [ -z "$uid" ]; then lfname="$LOG_FILE" else lfname=$(echo "$LOG_FILE" | sed "s/%u/${uid}/g") fi echo "Using log file(s): $lfname" fi if [ -z "$USER_NAME" ]; then pids=`omstat -u all 2>/dev/null | awk '{print $(NF-1)}'` else pids=`omstat -u all 2>/dev/null | grep -i "$USER_NAME" | awk '{print $(NF-1)}'` fi if [ ! -z "$pids" ]; then for p in $pids do kill $p done fi