diff --git a/Linux Script/configure-linux.sh b/Linux Script/configure-linux.sh new file mode 100644 index 0000000..a36315f --- /dev/null +++ b/Linux Script/configure-linux.sh @@ -0,0 +1,623 @@ +#!/bin/bash + +#trapping Control + C +#these statements must be the first statements in the script to trap the CTRL C event + +trap ctrl_c INT + +function ctrl_c() { + logMsgToConfigSysLog "INFO" "INFO: Aborting the script." + exit 1 +} + +########## Variable Declarations - Start ########## + +#name of the current script. This will get overwritten by the child script which calls this +SCRIPT_NAME=configure-linux.sh +#version of the current script. This will get overwritten by the child script which calls this +SCRIPT_VERSION=1.01 + +#application tag. This will get overwritten by the child script which calls this +APP_TAG= + +#directory location for syslog +RSYSLOG_ETCDIR_CONF=/etc/rsyslog.d +#name and location of loggly syslog file +LOGGLY_RSYSLOG_CONFFILE=$RSYSLOG_ETCDIR_CONF/22-loggly.conf +#name and location of loggly syslog backup file +LOGGLY_RSYSLOG_CONFFILE_BACKUP=$LOGGLY_RSYSLOG_CONFFILE.loggly.bk + +#syslog directory +RSYSLOG_DIR=/var/spool/rsyslog +#rsyslog service name +RSYSLOG_SERVICE=rsyslog +#rsyslogd +RSYSLOGD=rsyslogd +#minimum version of rsyslog to enable logging to loggly +MIN_RSYSLOG_VERSION=5.8.0 +#this variable will hold the users syslog version +RSYSLOG_VERSION= + +#this variable will hold the host name +HOST_NAME= +#this variable will hold the name of the linux distribution +LINUX_DIST= + +#host name for logs-01.loggly.com +LOGS_01_HOST=logs-01.loggly.com +LOGS_01_URL=https://$LOGS_01_HOST +#this variable will contain loggly account url in the format https://$LOGGLY_ACCOUNT.loggly.com +LOGGLY_ACCOUNT_URL= +#loggly.com URL +LOGGLY_COM_URL=https://www.loggly.com + +######Inputs provided by user###### +#this variable will hold the loggly account name provided by user. +#this is a mandatory input +LOGGLY_ACCOUNT= +#this variable will hold the loggly authentication token provided by user. +#this is a mandatory input +LOGGLY_AUTH_TOKEN= +#this variable will identify if the user has selected to rollback settings +LOGGLY_ROLLBACK= +#this variable will hold the user name provided by user +#this is a mandatory input +LOGGLY_USERNAME= +#this variable will hold the password provided by user +#this is a mandatory input +LOGGLY_PASSWORD= + +#variables used in 22-loggly.conf file +LOGGLY_SYSLOG_PORT=514 +LOGGLY_DISTRIBUTION_ID="41058" + +#Instruction link on how to configure loggly on linux manually. This will get overwritten by the child script which calls this +#on how to configure the child application +MANUAL_CONFIG_INSTRUCTION="Manual instructions to configure rsyslog on Linux are available at https://www.loggly.com/docs/rsyslog-manual-configuration/." + +#this variable is set if the script is invoked via some other calling script +IS_INVOKED= + + +########## Variable Declarations - End ########## + +# executing the script for loggly to install and configure rsyslog. +installLogglyConf() +{ + + #log message indicating starting of Loggly configuration + logMsgToConfigSysLog "INFO" "INFO: Initiating Configure Loggly for Linux." + + #check if the user has root permission to run this script + checkIfUserHasRootPrivileges + + #check if the OS is supported by the script. If no, then exit + checkIfSupportedOS + + #set the basic variables needed by this script + setLinuxVariables + + #check if the Loggly servers are accessible. If no, ask user to check network connectivity & exit + checkIfLogglyServersAccessible + + #check if user credentials are valid. If no, then exit + checkIfValidUserNamePassword + + #check if authentication token is valid. If no, then exit + checkIfValidAuthToken + + #check if rsyslog is configured as service. If no, then exit + checkIfRsyslogConfiguredAsService + + #check if multiple rsyslog are present in the system. If yes, then exit + checkIfMultipleRsyslogConfigured + + #check for the minimum version of rsyslog i.e 5.8.0. If no, then exit + checkIfMinVersionOfRsyslog + + #check if selinux service is enforced. if yes, ask the user to manually disable and exit the script + checkIfSelinuxServiceEnforced + + #if all the above check passes, write the 22-loggly.conf file + write22LogglyConfFile + + # Create rsyslog dir if it doesn't exist, Modify the permission on rsyslog directory if exist on Ubuntu + createRsyslogDir + + #check if the logs are going to loggly fro linux system now + checkIfLogsMadeToLoggly + + #log success message + logMsgToConfigSysLog "SUCCESS" "SUCCESS: Linux system successfully configured to send logs via Loggly." + +} +# End of configure rsyslog for linux + +#remove loggly configuration from Linux system +removeLogglyConf() +{ + #log message indicating starting of Loggly configuration + logMsgToConfigSysLog "INFO" "INFO: Initiating uninstall Loggly for Linux." + + #check if the user has root permission to run this script + checkIfUserHasRootPrivileges + + #check if the OS is supported by the script. If no, then exit + checkIfSupportedOS + + #set the basic variables needed by this script + setLinuxVariables + + #remove 22-loggly.conf file + remove22LogglyConfFile + + #restart rsyslog service + restartRsyslog + + #log success message + logMsgToConfigSysLog "SUCCESS" "SUCCESS: Uninstalled Loggly configuration from Linux system." +} + +#checks if user has root privileges +checkIfUserHasRootPrivileges() +{ + #This script needs to be run as a sudo user + if [[ $EUID -ne 0 ]]; then + logMsgToConfigSysLog "ERROR" "ERROR: This script must be run as root." + exit 1 + fi +} + +#check if supported operating system +checkIfSupportedOS() +{ + #set value for linux distribution name + LINUX_DIST=$(lsb_release -ds) + + if [ $? -ne 0 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: This operating system is not supported by the script." + exit 1 + else + #remove double quotes (if any) from the linux distribution name + LINUX_DIST="${LINUX_DIST%\"}" + LINUX_DIST="${LINUX_DIST#\"}" + case "$LINUX_DIST" in + *"Ubuntu"* ) + echo "INFO: Operating system is Ubuntu." + ;; + *"Red Hat"* ) + echo "INFO: Operating system is Red Hat." + ;; + *"CentOS"* ) + echo "INFO: Operating system is CentOS." + ;; + * ) + logMsgToConfigSysLog "ERROR" "ERROR: This operating system is not supported by the script." + exit 1 + ;; + esac + fi +} + + +#sets linux variables which will be used across various functions +setLinuxVariables() +{ + #set host name + HOST_NAME=$(hostname) + + #set loggly account url + LOGGLY_ACCOUNT_URL=https://$LOGGLY_ACCOUNT.loggly.com +} + +#checks if all the various endpoints used for configuring loggly are accessible +checkIfLogglyServersAccessible() +{ + echo "INFO: Checking if $LOGGLY_ACCOUNT_URL is reachable." + if [ $(curl -s --head --request GET $LOGGLY_ACCOUNT_URL/login | grep "200 OK" | wc -l) == 1 ]; then + echo "INFO: $LOGGLY_ACCOUNT_URL is reachable." + else + logMsgToConfigSysLog "WARNING" "WARNING: $LOGGLY_ACCOUNT_URL is not reachable. Please check your network and firewall settings. Continuing to configure Loggly on your system." + fi + + echo "INFO: Checking if $LOGS_01_HOST is reachable." + if [ $(ping -c 1 $LOGS_01_HOST | grep "1 packets transmitted, 1 received, 0% packet loss" | wc -l) == 1 ]; then + echo "INFO: $LOGS_01_HOST is reachable." + else + logMsgToConfigSysLog "WARNING" "WARNING: $LOGS_01_HOST is not reachable. Please check your network and firewall settings. Continuing to configure Loggly on your system." + fi + + echo "INFO: Checking if Gen2 account" + if [ $(curl -s --head --request GET $LOGGLY_ACCOUNT_URL/apiv2/customer | grep "404 NOT FOUND" | wc -l) == 1 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: This scripts need a Gen2 account. Please contact Loggly support." + else + echo "INFO: It is a Gen2 account" + fi +} + +#check if user name and password is valid +checkIfValidUserNamePassword() +{ + echo "INFO: Checking if provided username and password is correct." + if [ $(curl -s -u $LOGGLY_USERNAME:$LOGGLY_PASSWORD $LOGGLY_ACCOUNT_URL/apiv2/customer | grep "Unauthorized" | wc -l) == 1 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: Invalid Loggly username or password." + exit 1 + else + logMsgToConfigSysLog "INFO" "INFO: Username and password authorized successfully." + fi +} + +#check if authentication token is valid +checkIfValidAuthToken() +{ + echo "INFO: Checking if provided auth token is correct." + if [ $(curl -s -u $LOGGLY_USERNAME:$LOGGLY_PASSWORD $LOGGLY_ACCOUNT_URL/apiv2/customer | grep \"$LOGGLY_AUTH_TOKEN\" | wc -l) == 1 ]; then + logMsgToConfigSysLog "INFO" "INFO: Authentication token validated successfully." + else + logMsgToConfigSysLog "ERROR" "ERROR: Invalid authentication token $LOGGLY_AUTH_TOKEN. You can get valid authentication token by following instructions at https://www.loggly.com/docs/customer-token-authentication-token/." + exit 1 + fi +} + +#check if rsyslog is configured as service. If it is configured as service and not started, start the service +checkIfRsyslogConfiguredAsService() +{ + if [ -f /etc/init.d/$RSYSLOG_SERVICE ]; then + logMsgToConfigSysLog "INFO" "INFO: $RSYSLOG_SERVICE is present as service." + else + logMsgToConfigSysLog "ERROR" "ERROR: $RSYSLOG_SERVICE is not present as service." + exit 1 + fi + + if [ $(ps -ef | grep -v grep | grep "$RSYSLOG_SERVICE" | wc -l) -eq 0 ]; then + logMsgToConfigSysLog "INFO" "INFO: $RSYSLOG_SERVICE is not running. Attempting to start service." + sudo service $RSYSLOG_SERVICE start + fi +} + + +#check if multiple versions of rsyslog is configured +checkIfMultipleRsyslogConfigured() +{ + if [ $(ps -ef | grep -v grep | grep "$RSYSLOG_SERVICE" | wc -l) -gt 1 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: Multiple (more than 1) $RSYSLOG_SERVICE is running." + fi +} + +#check if mimimum version of rsyslog required to configure loggly is met +checkIfMinVersionOfRsyslog() +{ + RSYSLOG_VERSION=$(sudo $RSYSLOGD -version | grep "$RSYSLOGD") + RSYSLOG_VERSION=${RSYSLOG_VERSION#* } + RSYSLOG_VERSION=${RSYSLOG_VERSION%,*} + RSYSLOG_VERSION=$RSYSLOG_VERSION | tr -d " " + if [ $(compareVersions $RSYSLOG_VERSION $MIN_RSYSLOG_VERSION 3) -lt 0 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: Min rsyslog version required is 5.8.0." + exit 1 + fi +} + +#check if SeLinux service is enforced +checkIfSelinuxServiceEnforced() +{ + isSelinuxInstalled=$(getenforce -ds 2>/dev/null) + if [ $? -ne 0 ]; then + logMsgToConfigSysLog "INFO" "INFO: selinux status is not enforced." + elif [ $(sudo getenforce | grep "Enforcing" | wc -l) -gt 0 ]; then + logMsgToConfigSysLog "ERROR" "ERROR: selinux status is 'Enforcing'. Please disable it and start the rsyslog daemon manually." + fi +} + +#write 22-loggly,conf file to /etc/rsyslog.d directory after checking with user if override is needed +write22LogglyConfFile() +{ + echo "INFO: Checking if loggly sysconf file $LOGGLY_RSYSLOG_CONFFILE exist." + if [ -f "$LOGGLY_RSYSLOG_CONFFILE" ]; then + logMsgToConfigSysLog "WARN" "WARN: Loggly rsyslog file $LOGGLY_RSYSLOG_CONFFILE already exist." + while true; do + read -p "Do you wish to override $LOGGLY_RSYSLOG_CONFFILE? (yes/no)" yn + case $yn in + [Yy]* ) + logMsgToConfigSysLog "INFO" "INFO: Going to back up the conf file: $LOGGLY_RSYSLOG_CONFFILE to $LOGGLY_RSYSLOG_CONFFILE_BACKUP"; + sudo mv -f $LOGGLY_RSYSLOG_CONFFILE $LOGGLY_RSYSLOG_CONFFILE_BACKUP; + checkAuthTokenAndWriteContents; + break;; + [Nn]* ) break;; + * ) echo "Please answer yes or no.";; + esac + done + else + logMsgToConfigSysLog "INFO" "INFO: Loggly rsyslog file $LOGGLY_RSYSLOG_CONFFILE does not exist, creating file $LOGGLY_RSYSLOG_CONFFILE" + checkAuthTokenAndWriteContents + fi +} + +#check if authentication token is valid and then write contents to 22-loggly.conf file to /etc/rsyslog.d directory +checkAuthTokenAndWriteContents() +{ + if [ "$LOGGLY_ACCOUNT" != "" ]; then + writeContents $LOGGLY_ACCOUNT $LOGGLY_AUTH_TOKEN $LOGGLY_DISTRIBUTION_ID $LOGS_01_HOST $LOGGLY_SYSLOG_PORT + restartRsyslog + else + logMsgToConfigSysLog "ERROR" "ERROR: Loggly auth token is required to configure rsyslog. Please pass -a while running script." + exit 1 + fi +} + +#write the contents to 22-loggly.conf file +writeContents() +{ +inputStr=" +# ------------------------------------------------------- +# Syslog Logging Directives for Loggly ($1.loggly.com) +# ------------------------------------------------------- + +# Define the template used for sending logs to Loggly. Do not change this format. +\$template LogglyFormat,\"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [$2@$3] %msg%\" + +# Send messages to Loggly over TCP using the template. +*.* @@$4:$5;LogglyFormat + +# ------------------------------------------------------- +# End of Syslog Logging Directives for Loggly +# ------------------------------------------------------- +" +sudo cat << EOIPFW >> $LOGGLY_RSYSLOG_CONFFILE +$inputStr +EOIPFW +} + +#create /var/spool/rsyslog directory if not already present. Modify the permission of this directory for Ubuntu +createRsyslogDir() +{ + if [ -d "$RSYSLOG_DIR" ]; then + logMsgToConfigSysLog "INFO" "INFO: $RSYSLOG_DIR already exist, so not creating directory." + if [[ "$LINUX_DIST" == *"Ubuntu"* ]]; then + logMsgToConfigSysLog "INFO" "INFO: Changing the permission on the rsyslog in /var/spool" + sudo chown -R syslog:adm $RSYSLOG_DIR + fi + else + logMsgToConfigSysLog "INFO" "INFO: Creating directory $SYSLOGDIR" + sudo mkdir -v $RSYSLOG_DIR + if [[ "$LINUX_DIST" == *"Ubuntu"* ]]; then + sudo chown -R syslog:adm $RSYSLOG_DIR + fi + fi +} + +#check if the logs made it to Loggly +checkIfLogsMadeToLoggly() +{ + logMsgToConfigSysLog "INFO" "INFO: Sending test message to Loggly." + uuid=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) + + queryParam="syslog.appName%3ALOGGLYVERIFY%20$uuid" + logger -t "LOGGLYVERIFY" "LOGGLYVERIFY-Test message for verification with UUID $uuid" + + counter=1 + maxCounter=10 + finalCount=0 + + queryUrl="$LOGGLY_ACCOUNT_URL/apiv2/search?q=$queryParam" + logMsgToConfigSysLog "INFO" "INFO: Search URL: $queryUrl" + + logMsgToConfigSysLog "INFO" "INFO: Verifying if the log made it to Loggly." + logMsgToConfigSysLog "INFO" "INFO: Verification # $counter of total $maxCounter." + searchAndFetch finalCount "$queryUrl" + let counter=$counter+1 + + while [ "$finalCount" -eq 0 ]; do + echo "INFO: Did not find the test log message in Loggly's search yet. Waiting for 30 secs." + sleep 30 + echo "INFO: Done waiting. Verifying again." + logMsgToConfigSysLog "INFO" "INFO: Verification # $counter of total $maxCounter." + searchAndFetch finalCount "$queryUrl" + let counter=$counter+1 + if [ "$counter" -gt "$maxCounter" ]; then + MANUAL_CONFIG_INSTRUCTION=$MANUAL_CONFIG_INSTRUCTION" Rsyslog troubleshooting instructions are available at https://www.loggly.com/docs/troubleshooting-rsyslog/" + logMsgToConfigSysLog "ERROR" "ERROR: Verification logs did not make it to Loggly in time. Please check your token & network/firewall settings and retry." + exit 1 + fi + done + + if [ "$finalCount" -eq 1 ]; then + logMsgToConfigSysLog "SUCCESS" "SUCCESS: Verification logs successfully transferred to Loggly! You are now sending Linux system logs to Loggly." + if [ "$IS_INVOKED" = "" ]; then + exit 0 + fi + fi + +} + +#delete 22-loggly.conf file +remove22LogglyConfFile() +{ + if [ -f "$LOGGLY_RSYSLOG_CONFFILE" ]; then + sudo rm -rf "$LOGGLY_RSYSLOG_CONFFILE" + fi +} + +#compares two version numbers, used for comparing versions of various softwares +compareVersions () +{ + typeset IFS='.' + typeset -a v1=( $1 ) + typeset -a v2=( $2 ) + typeset n diff + + for (( n=0; n<$3; n+=1 )); do + diff=$((v1[n]-v2[n])) + if [ $diff -ne 0 ] ; then + [ $diff -le 0 ] && echo '-1' || echo '1' + return + fi + done + echo '0' +} + +#restart rsyslog +restartRsyslog() +{ + logMsgToConfigSysLog "INFO" "INFO: Restarting the $RSYSLOG_SERVICE service." + sudo service $RSYSLOG_SERVICE restart + if [ $? -ne 0 ]; then + logMsgToConfigSysLog "WARNING" "WARNING: $RSYSLOG_SERVICE did not restart gracefully. Please restart $RSYSLOG_SERVICE manually." + fi +} + +#logs message to config syslog +logMsgToConfigSysLog() +{ + #$1 variable will be SUCCESS or ERROR or INFO or WARNING + #$2 variable will be the message + cslStatus=$1 + cslMessage=$2 + echo "$cslMessage" + currentTime=$(date) + + #for Linux system, we need to use -d switch to decode base64 whereas + #for Mac system, we need to use -D switch to decode + varUname=$(uname) + if [[ $varUname == 'Linux' ]]; then + enabler=$(echo MWVjNGU4ZTEtZmJiMi00N2U3LTkyOWItNzVhMWJmZjVmZmUw | base64 -d) + elif [[ $varUname == 'Darwin' ]]; then + enabler=$(echo MWVjNGU4ZTEtZmJiMi00N2U3LTkyOWItNzVhMWJmZjVmZmUw | base64 -D) + fi + + if [ $? -ne 0 ]; then + echo "ERROR: Base64 decode is not supported on your Operating System. Please update your system to support Base64." + exit 1 + fi + + sendPayloadToConfigSysLog "$cslStatus" "$cslMessage" "$enabler" + + #if it is an error, then log message "Script Failed" to config syslog and exit the script + if [[ $cslStatus == "ERROR" ]]; then + sendPayloadToConfigSysLog "ERROR" "Script Failed" "$enabler" + echo $MANUAL_CONFIG_INSTRUCTION + exit 1 + fi + + #if it is a success, then log message "Script Succeeded" to config syslog and exit the script + if [[ $cslStatus == "SUCCESS" ]]; then + sendPayloadToConfigSysLog "SUCCESS" "Script Succeeded" "$enabler" + fi +} + +#payload construction to send log to config syslog +sendPayloadToConfigSysLog() +{ + if [ "$APP_TAG" = "" ]; then + var="{\"sub-domain\":\"$LOGGLY_ACCOUNT\", \"user-name\":\"$LOGGLY_USERNAME\", \"host-name\":\"$HOST_NAME\", \"script-name\":\"$SCRIPT_NAME\", \"script-version\":\"$SCRIPT_VERSION\", \"status\":\"$1\", \"time-stamp\":\"$currentTime\", \"linux-distribution\":\"$LINUX_DIST\", \"messages\":\"$2\"}" + else + var="{\"sub-domain\":\"$LOGGLY_ACCOUNT\", \"user-name\":\"$LOGGLY_USERNAME\", \"host-name\":\"$HOST_NAME\", \"script-name\":\"$SCRIPT_NAME\", \"script-version\":\"$SCRIPT_VERSION\", \"status\":\"$1\", \"time-stamp\":\"$currentTime\", \"linux-distribution\":\"$LINUX_DIST\", $APP_TAG, \"messages\":\"$2\"}" + fi + curl -s -H "content-type:application/json" -d "$var" $LOGS_01_URL/inputs/$3 > /dev/null 2>&1 +} + +#$1 return the count of records in loggly, $2 is the query param to search in loggly +searchAndFetch() +{ + url=$2 + result=$(wget -qO- /dev/null --user "$LOGGLY_USERNAME" --password "$LOGGLY_PASSWORD" "$url") + if [ -z "$result" ]; then + logMsgToConfigSysLog "ERROR" "ERROR: Please check your network/firewall settings & ensure Loggly subdomain, username and password is specified correctly." + exit 1 + fi + id=$(echo "$result" | grep -v "{" | grep id | awk '{print $2}') + # strip last double quote from id + id="${id%\"}" + # strip first double quote from id + id="${id#\"}" + url="$LOGGLY_ACCOUNT_URL/apiv2/events?rsid=$id" + + # retrieve the data + result=$(wget -qO- /dev/null --user "$LOGGLY_USERNAME" --password "$LOGGLY_PASSWORD" "$url") + count=$(echo "$result" | grep total_events | awk '{print $2}') + count="${count%\,}" + eval $1="'$count'" + if [ "$count" -gt 0 ]; then + timestamp=$(echo "$result" | grep timestamp) + fi +} + +#get password in the form of asterisk +getPassword() +{ + unset LOGGLY_PASSWORD + prompt="Please enter Loggly Password:" + while IFS= read -p "$prompt" -r -s -n 1 char + do + if [[ $char == $'\0' ]] + then + break + fi + prompt='*' + LOGGLY_PASSWORD+="$char" + done + echo +} + +#display usage syntax +usage() +{ +cat << EOF +usage: configure-linux [-a loggly auth account or subdomain] [-t loggly token] [-u username] [-p password (optional)] +usage: configure-linux [-a loggly auth account or subdomain] [-r to remove] +usage: configure-linux [-h for help] +EOF +} + +########## Get Inputs from User - Start ########## +if [ "$1" != "being-invoked" ]; then + if [ $# -eq 0 ]; then + usage + exit + else + while [ "$1" != "" ]; do + case $1 in + -t | --token ) shift + LOGGLY_AUTH_TOKEN=$1 + echo "AUTH TOKEN $LOGGLY_AUTH_TOKEN" + ;; + -a | --account ) shift + LOGGLY_ACCOUNT=$1 + echo "Loggly account or subdomain: $LOGGLY_ACCOUNT" + ;; + -u | --username ) shift + LOGGLY_USERNAME=$1 + echo "Username is set" + ;; + -p | --password ) shift + LOGGLY_PASSWORD=$1 + ;; + -r | --remove ) + LOGGLY_REMOVE="true" + ;; + -h | --help) + usage + exit + ;; + *) usage + exit + ;; + esac + shift + done + fi + + if [ "$LOGGLY_REMOVE" != "" -a "$LOGGLY_ACCOUNT" != "" ]; then + removeLogglyConf + elif [ "$LOGGLY_AUTH_TOKEN" != "" -a "$LOGGLY_ACCOUNT" != "" -a "$LOGGLY_USERNAME" != "" ]; then + if [ "$LOGGLY_PASSWORD" = "" ]; then + getPassword + fi + installLogglyConf + else + usage + fi +else + IS_INVOKED="true" +fi + +########## Get Inputs from User - End ########## diff --git a/Modular Scripts/Tomcat/configure-linux.sh b/Modular Scripts/Tomcat/configure-linux.sh deleted file mode 100644 index 45d3c16..0000000 --- a/Modular Scripts/Tomcat/configure-linux.sh +++ /dev/null @@ -1,342 +0,0 @@ -#!/bin/bash - -########## Variable Declarations - Start ########## - -#name of the current script. This will get overwritten by the child script which calls this -SCRIPT_NAME=configure-linux.sh -#version of the current script. This will get overwritten by the child script which calls this -SCRIPT_VERSION=1.0 -#minimum version of syslog to enable logging to loggly -MIN_SYSLOG_VERSION=5.8.0 - -#application tag. This will get overwritten by the child script which calls this -APP_TAG= - -#directory location for syslog -SYSLOG_ETCDIR_CONF=/etc/rsyslog.d -#name and location of loggly syslog file -LOGGLY_SYSLOG_CONFFILE=$SYSLOG_ETCDIR_CONF/22-loggly.conf -#syslog directory -SYSLOG_DIR=/var/spool/rsyslog - -#this variable will hold the host name -HOST_NAME= -#this variable will hold the name of the linux distribution -LINUX_DIST= - -#this variable will hold the users syslog version -SYSLOG_VERSION= - -#host name for logs-01.loggly.com -LOGS_01_HOST=logs-01.loggly.com -LOGS_01_URL=https://$LOGS_01_HOST -#this variable will contain loggly account url in the format -#https://$LOGGLY_ACCOUNT.loggly.com -LOGGLY_ACCOUNT_URL= -#loggly.com URL -LOGGLY_COM_URL=https://www.loggly.com - -######Inputs provided by user###### -#this variable will hold the loggly account name provided by user. -#this is a mandatory input -LOGGLY_ACCOUNT= -#this variable will hold the loggly authentication token provided by user. -#this is a mandatory input -LOGGLY_AUTH_TOKEN= -#this variable will hold if debug is enabled by the user. -#this option is not used at present -LOGGLY_DEBUG= -#this variable will identify if the user has selected to rollback settings -LOGGLY_ROLLBACK= -#this variable will hold the user name provided by user -#this is a mandatory input -LOGGLY_USERNAME= -#this variable will hold the password provided by user -#this is a mandatory input -LOGGLY_PASSWORD= - -#Instruction link on how to configure loggly on linux manually. This will get overwritten by the child script which calls this -#on how to configure the child application -MANUAL_CONFIG_INSTRUCTION="Manual instructions to configure rsyslog on Linux is available at https://www.loggly.com/docs/rsyslog-manual-configuration/" -########## Variable Declarations - End ########## - -#sets linux variables which will be used across various functions -setLinuxVariables() -{ - #set host name - HOST_NAME=$(hostname) - - #set value for linux distribution name - LINUX_DIST=$(lsb_release -ds) - - if [ $? -ne 0 ]; then - logMsgToConfigSysLog "ERROR" "ERROR: This operating system is not supported by the script." - exit 1 - else - #remove double quotes (if any) from the linux distribution name - LINUX_DIST="${LINUX_DIST%\"}" - LINUX_DIST="${LINUX_DIST#\"}" - case "$LINUX_DIST" in - *"Ubuntu"* ) - echo "INFO: Operating system is Ubuntu." - ;; - *"Red Hat"* ) - echo "INFO: Operating system is Red Hat." - ;; - *"CentOS"* ) - echo "INFO: Operating system is CentOS." - ;; - * ) - logMsgToConfigSysLog "ERROR" "ERROR: This operating system is not supported by the script." - exit 1 - ;; - esac - fi - - #set loggly account url - LOGGLY_ACCOUNT_URL=https://$LOGGLY_ACCOUNT.loggly.com -} - -#compares two version numbers, used for comparing versions of various softwares -compareVersions () -{ - typeset IFS='.' - typeset -a v1=( $1 ) - typeset -a v2=( $2 ) - typeset n diff - - for (( n=0; n<$3; n+=1 )); do - diff=$((v1[n]-v2[n])) - if [ $diff -ne 0 ] ; then - [ $diff -le 0 ] && echo '-1' || echo '1' - return - fi - done - echo '0' -} - - -#checks if all the various endpoints used for configuring loggly are accessible -checkLogglyServersAccessiblilty() -{ - echo "INFO: Checking if $LOGGLY_ACCOUNT_URL is reachable" - if [ $(curl -s --head --request GET $LOGGLY_ACCOUNT_URL/login | grep "200 OK" | wc -l) == 1 ]; then - echo "INFO: $LOGGLY_ACCOUNT_URL is reachable" - else - logMsgToConfigSysLog "WARNING" "WARNING: $LOGGLY_ACCOUNT_URL is not reachable. Please check your network and firewall settings. Continuing to configure Loggly on your system." - fi - - echo "INFO: Checking if $LOGS_01_HOST is reachable" - if [ $(ping -c 1 $LOGS_01_HOST | grep "1 packets transmitted, 1 received, 0% packet loss" | wc -l) == 1 ]; then - echo "INFO: $LOGS_01_HOST is reachable" - else - logMsgToConfigSysLog "WARNING" "WARNING: $LOGS_01_HOST is not reachable. Please check your network and firewall settings. Continuing to configure Loggly on your system." - fi - - echo "INFO: Checking if provided username and password is correct" - if [ $(curl -s -u $LOGGLY_USERNAME:$LOGGLY_PASSWORD $LOGGLY_ACCOUNT_URL/apiv2/customer | grep "Unauthorized" | wc -l) == 1 ]; then - logMsgToConfigSysLog "ERROR" "ERROR: Invalid Loggly username or password." - exit 1 - else - logMsgToConfigSysLog "INFO" "INFO: Username and password authorized successfully." - fi -} - -# executing the script for loggly to install and configure syslog. -configureLogglyForLinux() -{ - checkIfUserHasRootPrivileges - setLinuxVariables - logMsgToConfigSysLog "INFO" "INFO: Initiating Configure Loggly for Linux." - checkLogglyServersAccessiblilty - - sudo service rsyslog start - SYSLOG_VERSION=$(sudo rsyslogd -version | grep "rsyslogd") - SYSLOG_VERSION=${SYSLOG_VERSION#* } - SYSLOG_VERSION=${SYSLOG_VERSION%,*} - SYSLOG_VERSION=$SYSLOG_VERSION | tr -d " " - if [ $(compareVersions $SYSLOG_VERSION $MIN_SYSLOG_VERSION 3) -lt 0 ]; then - logMsgToConfigSysLog "ERROR" "ERROR: Min syslog version required is 5.8.0." - exit 1 - fi - - echo "INFO: Checking if loggly sysconf file $LOGGLY_SYSLOG_CONFFILE exist" - # if the loggly configuration file exist, then don't create it. - if [ -f "$LOGGLY_SYSLOG_CONFFILE" ]; then - logMsgToConfigSysLog "INFO" "INFO: Loggly syslog file $LOGGLY_SYSLOG_CONFFILE exist, not creating file." - else - logMsgToConfigSysLog "INFO" "INFO: Creating file $LOGGLY_SYSLOG_CONFFILE" - if [ "$LOGGLY_ACCOUNT" != "" ]; then - wget -q -O - $LOGGLY_COM_URL/install/configure-syslog.py | sudo python - setup --auth $LOGGLY_AUTH_TOKEN --account $LOGGLY_ACCOUNT - else - logMsgToConfigSysLog "ERROR" "ERROR: Loggly auth token is required to configure rsyslog. Please pass -a while running script." - exit 1 - fi - fi - - # Create rsyslog dir if it doesn't exist, Modify the rsyslog directory if exist - if [ -d "$SYSLOG_DIR" ]; then - logMsgToConfigSysLog "INFO" "INFO: $SYSLOG_DIR exist, not creating dir." - if [[ "$LINUX_DIST" == *"Ubuntu"* ]]; then - logMsgToConfigSysLog "INFO" "INFO: Changing the permission on the rsyslog in /var/spool." - sudo chown -R syslog:adm $SYSLOG_DIR - fi - else - logMsgToConfigSysLog "INFO" "INFO: Creating directory $SYSLOGDIR." - sudo mkdir -v $SYSLOG_DIR - if [[ "$LINUX_DIST" == *"Ubuntu"* ]]; then - sudo chown -R syslog:adm $SYSLOG_DIR - fi - fi - - logMsgToConfigSysLog "SUCCESS" "SUCCESS: Linux system successfully configured to send logs via Loggly." - -} -# End of configure rsyslog for linux - - -#restart syslog -restartsyslog() -{ - logMsgToConfigSysLog "INFO" "INFO: Restarting the rsyslog service." - sudo service rsyslog restart - if [ $? -ne 0 ]; then - logMsgToConfigSysLog "WARNING" "WARNING: rsyslog did not restart gracefully. Please restart rsyslog manually." - fi -} - -#logs message to config syslog -logMsgToConfigSysLog() -{ - #$1 variable will be SUCCESS or ERROR or INFO or WARNING - #$2 variable will be the message - cslStatus=$1 - cslMessage=$2 - echo "$cslMessage" - currentTime=$(date) - - #for Linux system, we need to use -d switch to decode base64 whereas - #for Mac system, we need to use -D switch to decode - varUname=$(uname) - if [[ $varUname == 'Linux' ]]; then - enabler=$(echo MWVjNGU4ZTEtZmJiMi00N2U3LTkyOWItNzVhMWJmZjVmZmUw | base64 -d) - elif [[ $varUname == 'Darwin' ]]; then - enabler=$(echo MWVjNGU4ZTEtZmJiMi00N2U3LTkyOWItNzVhMWJmZjVmZmUw | base64 -D) - fi - - if [ $? -ne 0 ]; then - echo "ERROR: Base64 decode is not supported on your Operating System. Please update your system to support Base64." - exit 1 - fi - - sendPayloadToConfigSysLog "$cslStatus" "$cslMessage" "$enabler" - - #if it is an error, then log message "Script Failed" to config syslog and exit the script - if [[ $cslStatus == "ERROR" ]]; then - sendPayloadToConfigSysLog "ERROR" "Script Failed" "$enabler" - echo $MANUAL_CONFIG_INSTRUCTION - exit 1 - fi - - #if it is a success, then log message "Script Succeeded" to config syslog and exit the script - if [[ $cslStatus == "SUCCESS" ]]; then - sendPayloadToConfigSysLog "SUCCESS" "Script Succeeded" "$enabler" - fi -} - -sendPayloadToConfigSysLog() -{ - if [ "$APP_TAG" = "" ]; then - var="{\"sub-domain\":\"$LOGGLY_ACCOUNT\", \"host-name\":\"$HOST_NAME\", \"script-name\":\"$SCRIPT_NAME\", \"script-version\":\"$SCRIPT_VERSION\", \"status\":\"$1\", \"time-stamp\":\"$currentTime\", \"linux-distribution\":\"$LINUX_DIST\", \"messages\":\"$2\"}" - else - var="{\"sub-domain\":\"$LOGGLY_ACCOUNT\", \"host-name\":\"$HOST_NAME\", \"script-name\":\"$SCRIPT_NAME\", \"script-version\":\"$SCRIPT_VERSION\", \"status\":\"$1\", \"time-stamp\":\"$currentTime\", \"linux-distribution\":\"$LINUX_DIST\", $APP_TAG, \"messages\":\"$2\"}" - fi - curl -s -H "content-type:application/json" -d "$var" $LOGS_01_URL/inputs/$3 > /dev/null 2>&1 -} - -#get password in the form of asterisk -getPassword() -{ - unset LOGGLY_PASSWORD - prompt="Please enter Loggly Password:" - while IFS= read -p "$prompt" -r -s -n 1 char - do - if [[ $char == $'\0' ]] - then - break - fi - prompt='*' - LOGGLY_PASSWORD+="$char" - done - echo -} - -#checks if user has root privileges -checkIfUserHasRootPrivileges() -{ - #This script needs to be run as a sudo user - if [[ $EUID -ne 0 ]]; then - logMsgToConfigSysLog "ERROR" "ERROR: This script must be run as root." - exit 1 - fi -} - -#display usage syntax -usage() -{ -cat << EOF -usage: configure-linux [-a loggly auth account or subdomain] [-t loggly token] [-u username] [-p password (optional)] -usage: configure-linux [-h for help] -EOF -} - -########## Get Inputs from User - Start ########## -if [ "$1" != "being-invoked" ]; then - if [ $# -eq 0 ]; then - usage - exit - else - while [ "$1" != "" ]; do - case $1 in - -t | --token ) shift - LOGGLY_AUTH_TOKEN=$1 - echo "AUTH TOKEN $LOGGLY_AUTH_TOKEN" - ;; - -a | --account ) shift - LOGGLY_ACCOUNT=$1 - echo "Loggly account or subdomain: $LOGGLY_ACCOUNT" - ;; - -u | --username ) shift - LOGGLY_USERNAME=$1 - echo "Username is set" - ;; - -p | --password ) shift - LOGGLY_PASSWORD=$1 - ;; - -h | --help) - usage - exit - ;; - esac - shift - done - fi - - if [ "$LOGGLY_DEBUG" != "" -a "$LOGGLY_AUTH_TOKEN" != "" -a "$LOGGLY_ACCOUNT" != "" -a "$LOGGLY_USERNAME" != "" ]; then - if [ "$LOGGLY_PASSWORD" = "" ]; then - getPassword - fi - debug - elif [ "$LOGGLY_AUTH_TOKEN" != "" -a "$LOGGLY_ACCOUNT" != "" -a "$LOGGLY_USERNAME" != "" ]; then - if [ "$LOGGLY_PASSWORD" = "" ]; then - getPassword - fi - configureLogglyForLinux - elif [ "$LOGGLY_ROLLBACK" != "" ]; then - rollback - else - usage - fi -fi - -########## Get Inputs from User - End ########## diff --git a/Modular Scripts/Tomcat/configure-tomcat.sh b/Modular Scripts/Tomcat/configure-tomcat.sh index 243f9f0..c1a927a 100644 --- a/Modular Scripts/Tomcat/configure-tomcat.sh +++ b/Modular Scripts/Tomcat/configure-tomcat.sh @@ -20,6 +20,8 @@ SERVICE=tomcat6 SYSLOG_ETCDIR_CONF=/etc/rsyslog.d #name and location of tomcat syslog file TOMCAT_SYSLOG_CONFFILE=$SYSLOG_ETCDIR_CONF/21-tomcat.conf +#name and location of tomcat syslog backup file +TOMCAT_SYSLOG_CONFFILE_BACKUP=$SYSLOG_ETCDIR_CONF/21-tomcat.conf.loggly.bk #syslog directory SYSLOG_DIR=/var/spool/rsyslog @@ -54,78 +56,99 @@ LOGGLY_CATALINA_HOME= MANUAL_CONFIG_INSTRUCTION="Manual instructions to configure Tomcat is available at https://www.loggly.com/docs/tomcat-application-server" ########## Variable Declarations - End ########## -#sets tomcat variables which will be used across various functions -setTomcatVariables() +# executing the script for loggly to install and configure syslog. +installLogglyConfForTomcat() { -#get CATALINA_HOME, this sets the value for LOGGLY_CATALINA_HOME variable -getCatalinaHome $SERVICE - -#set value for catalina conf home path, logging.properties path and -#logging.properties.loggly.bk path -LOGGLY_CATALINA_CONF_HOME=$LOGGLY_CATALINA_HOME/conf -LOGGLY_CATALINA_PROPFILE=$LOGGLY_CATALINA_CONF_HOME/logging.properties -LOGGLY_CATALINA_BACKUP_PROPFILE=$LOGGLY_CATALINA_PROPFILE.loggly.bk - -LOGGLY_CATALINA_LOG_HOME=/var/log/$SERVICE - -#default path for catalina.jar -CATALINA_JAR_PATH=$LOGGLY_CATALINA_HOME/lib/catalina.jar - -#check if the identified CATALINA_HOME has the catalina.jar -if [ ! -f "$CATALINA_JAR_PATH" ]; then - #if not, search it throughout the system. If we find no entries or more than - #1 entry, then we cannot determine the version of the tomcat - logMsgToConfigSysLog "INFO" "INFO: Could not find catalina.jar in $LOGGLY_CATALINA_HOME/lib. Searching at other locations, this may take some time." - if [ $(sudo find / -name catalina.jar | grep tomcat6 | wc -l) = 1 ]; then - CATALINA_JAR_PATH=$(sudo find / -name catalina.jar | grep tomcat6) - logMsgToConfigSysLog "INFO" "INFO: Found catalina.jar at $CATALINA_JAR_PATH." - else - logMsgToConfigSysLog "WARNING" "WARNING: Unable to determine the correct version of tomcat 6. Assuming its >= to 6.0.33." - fi -fi + installLogglyConf -#get the tomcat version number -if [ -f "$CATALINA_JAR_PATH" ]; then - TOMCAT_VERSION=$(sudo java -cp $CATALINA_JAR_PATH org.apache.catalina.util.ServerInfo | grep "Server number") - TOMCAT_VERSION=${TOMCAT_VERSION#*: } - TOMCAT_VERSION=$TOMCAT_VERSION | tr -d ' ' - APP_TAG="\"tomcat-version\":\"$TOMCAT_VERSION\"" + #log message indicating starting of Loggly configuration + logMsgToConfigSysLog "INFO" "INFO: Initiating Configure Loggly for Tomcat." - tomcatMajorVersion=${TOMCAT_VERSION%%.*} - if [ $tomcatMajorVersion -ne 6 ]; then - echo "ERROR" "ERROR: This script only supports Tomcat version 6." - exit 1 - fi -fi + #get CATALINA_HOME, this sets the value for LOGGLY_CATALINA_HOME variable + getTomcatHome $SERVICE + + #check if the provided or deduced tomcat home is correct or not + checkIfValidTomcatHome + + #set all the required tomcat variables by this script + setTomcatVariables + + #check if tomcat version is supported by the script. The script only support tomcat 6 and 7 + checkIfSupportedTomcatVersion + + #check if tomcat is configured with log4j. If yes, then exit + checkIfTomcatConfiguredWithLog4J + + #backing up the logging.properties file + backupLoggingPropertiesFile + + #update logging.properties file for log rotation + updateLoggingPropertiesFile + + #create 21tomcat.conf file + write21TomcatConfFile + + #verify if the tomcat logs made it to loggly + checkIfTomcatLogsMadeToLoggly + + #log success message + logMsgToConfigSysLog "SUCCESS" "SUCCESS: Tomcat successfully configured to send logs via Loggly." +} +# End of configure rsyslog for tomcat + + +removeLogglyConfForTomcat() +{ + logMsgToConfigSysLog "INFO" "INFO: Initiating rollback." + + #check if the user has root permission to run this script + checkIfUserHasRootPrivileges + + #check if the OS is supported by the script. If no, then exit + checkIfSupportedOS + + #get CATALINA_HOME, this sets the value for LOGGLY_CATALINA_HOME variable + getTomcatHome $SERVICE + + #check if the provided or deduced tomcat home is correct or not + checkIfValidTomcatHome + + #set all the required tomcat variables by this script + setTomcatVariables + + #restore original loggly properties file from backup + restoreLogglyPropertiesFile + + #remove 21tomcat.conf file + remove21TomcatConfFile + + logMsgToConfigSysLog "INFO" "INFO: Rollback completed." } -#try to deduce tomcat home if user has not provided one -getCatalinaHome() +#Get default location of tomcat home on various supported OS if user has not provided one +getTomcatHome() { #if user has not provided the catalina home if [ "$LOGGLY_CATALINA_HOME" = "" ]; then case "$LINUX_DIST" in *"Ubuntu"* ) - checkIfValidCatalinaHome "/var/lib/$1" + LOGGLY_CATALINA_HOME="/var/lib/$1" ;; *"Red Hat"* ) - checkIfValidCatalinaHome "/usr/share/$1" + LOGGLY_CATALINA_HOME="/usr/share/$1" ;; *"CentOS"* ) - checkIfValidCatalinaHome "/usr/share/$1" + LOGGLY_CATALINA_HOME="/usr/share/$1" ;; esac - else - checkIfValidCatalinaHome "$LOGGLY_CATALINA_HOME" fi logMsgToConfigSysLog "INFO" "INFO: CATALINA HOME: $LOGGLY_CATALINA_HOME" } #checks if the catalina home is a valid one by searching for logging.properties and #checks for startup.sh if tomcat is not configured as service -checkIfValidCatalinaHome() +checkIfValidTomcatHome() { - LOGGLY_CATALINA_HOME=$1 #check if logging.properties files is present if [ ! -f "$LOGGLY_CATALINA_HOME/conf/logging.properties" ]; then logMsgToConfigSysLog "ERROR" "ERROR: Unable to find conf/logging.properties file within $LOGGLY_CATALINA_HOME. Please provide correct Catalina Home using -ch option." @@ -140,11 +163,58 @@ checkIfValidCatalinaHome() fi } +#sets tomcat variables which will be used across various functions +setTomcatVariables() +{ + #set value for catalina conf home path, logging.properties path and + #logging.properties.loggly.bk path + LOGGLY_CATALINA_CONF_HOME=$LOGGLY_CATALINA_HOME/conf + LOGGLY_CATALINA_PROPFILE=$LOGGLY_CATALINA_CONF_HOME/logging.properties + LOGGLY_CATALINA_BACKUP_PROPFILE=$LOGGLY_CATALINA_PROPFILE.loggly.bk + + LOGGLY_CATALINA_LOG_HOME=/var/log/$SERVICE + + #default path for catalina.jar + CATALINA_JAR_PATH=$LOGGLY_CATALINA_HOME/lib/catalina.jar +} + +#checks if the tomcat version is supported by this script, currently the script +#only supports tomcat 6 and tomcat 7 +checkIfSupportedTomcatVersion() +{ + #check if the identified CATALINA_HOME has the catalina.jar + if [ ! -f "$CATALINA_JAR_PATH" ]; then + #if not, search it throughout the system. If we find no entries or more than + #1 entry, then we cannot determine the version of the tomcat + logMsgToConfigSysLog "INFO" "INFO: Could not find catalina.jar in $LOGGLY_CATALINA_HOME/lib. Searching at other locations, this may take some time." + if [ $(sudo find / -name catalina.jar | grep $SERVICE | wc -l) = 1 ]; then + CATALINA_JAR_PATH=$(sudo find / -name catalina.jar | grep $SERVICE) + logMsgToConfigSysLog "INFO" "INFO: Found catalina.jar at $CATALINA_JAR_PATH" + else + logMsgToConfigSysLog "WARNING" "WARNING: Unable to determine the correct version of tomcat 6. Assuming its >= to 6.0.33." + TOMCAT_VERSION=6.0.33.0 + fi + fi + + #get the tomcat version number + if [ -f "$CATALINA_JAR_PATH" ]; then + TOMCAT_VERSION=$(sudo java -cp $CATALINA_JAR_PATH org.apache.catalina.util.ServerInfo | grep "Server number") + TOMCAT_VERSION=${TOMCAT_VERSION#*: } + TOMCAT_VERSION=$TOMCAT_VERSION | tr -d ' ' + APP_TAG="\"tomcat-version\":\"$TOMCAT_VERSION\"" + + tomcatMajorVersion=${TOMCAT_VERSION%%.*} + if [[ ($tomcatMajorVersion -ne 6 ) && ($tomcatMajorVersion -ne 7) ]]; then + echo "ERROR" "ERROR: This script only supports Tomcat version 6 or 7." + exit 1 + fi + fi +} #checks if the tomcat is already configured with log4j. If yes, then exit checkIfTomcatConfiguredWithLog4J() { - echo "INFO: Checking if tomcat is configured with log4j logger" + echo "INFO: Checking if tomcat is configured with log4j logger." #default path for log4j files LOG4J_FILE_PATH=$LOGGLY_CATALINA_HOME/lib/log4j* #check if the log4j files are present, if yes, then exit @@ -160,21 +230,17 @@ checkIfTomcatConfiguredWithLog4J() exit 1 fi fi - logMsgToConfigSysLog "INFO" "INFO: Tomcat seems not to be configured with log4j logger" + logMsgToConfigSysLog "INFO" "INFO: Tomcat seems not to be configured with log4j logger." } -# executing the script for loggly to install and configure syslog. -configureLogglyForTomcat() +#backup the logging.properties file in the CATALINA_HOME folder +backupLoggingPropertiesFile() { - configureLogglyForLinux - setTomcatVariables - logMsgToConfigSysLog "INFO" "INFO: Tomcat logging properties file: $LOGGLY_CATALINA_PROPFILE" - # backup the logging properties file just in case it need to reverted. - echo "INFO: Going to back up the properties file: $LOGGLY_CATALINA_PROPFILE to $LOGGLY_CATALINA_BACKUP_PROPFILE." + echo "INFO: Going to back up the properties file: $LOGGLY_CATALINA_PROPFILE to $LOGGLY_CATALINA_BACKUP_PROPFILE" if [ ! -f $LOGGLY_CATALINA_PROPFILE ]; then - logMsgToConfigSysLog "ERROR" "ERROR: logging.properties file not found!. Looked at location $LOGGLY_CATALINA_PROPFILE." + logMsgToConfigSysLog "ERROR" "ERROR: logging.properties file not found!. Looked at location $LOGGLY_CATALINA_PROPFILE" exit 1 else # dont take a backup of logging properties file if it is already there @@ -183,6 +249,12 @@ configureLogglyForTomcat() fi fi +} + +#update logging.properties file to enable log rotation. If the version of tomcat +#is less than 6.0.33, then log rotation cannot be enabled +updateLoggingPropertiesFile() +{ #check if tomcat version is less than 6.0.33.0, if yes, throw a warning if [ $(compareVersions $TOMCAT_VERSION $MIN_TOMCAT_VERSION 4) -lt 0 ]; then logMsgToConfigSysLog "WARNING" "WARNING: Tomcat version is less than 6.0.33. Log rotation cannot be disabled for version <6.0.33; only catalina.out log will be monitored." @@ -226,58 +298,39 @@ sudo cat << EOIPFW >> $LOGGLY_CATALINA_PROPFILE EOIPFW fi fi +} +write21TomcatConfFile() +{ #Create tomcat syslog config file if it doesn't exist echo "INFO: Checking if tomcat sysconf file $TOMCAT_SYSLOG_CONFFILE exist." if [ -f "$TOMCAT_SYSLOG_CONFFILE" ]; then - logMsgToConfigSysLog "INFO" "INFO: Tomcat syslog file $TOMCAT_SYSLOG_CONFFILE exist, not creating file." + logMsgToConfigSysLog "WARN" "WARN: Tomcat syslog file $TOMCAT_SYSLOG_CONFFILE already exist." + while true; do + read -p "Do you wish to override $TOMCAT_SYSLOG_CONFFILE? (yes/no)" yn + case $yn in + [Yy]* ) + logMsgToConfigSysLog "INFO" "INFO: Going to back up the conf file: $TOMCAT_SYSLOG_CONFFILE to $TOMCAT_SYSLOG_CONFFILE_BACKUP"; + sudo mv -f $TOMCAT_SYSLOG_CONFFILE $TOMCAT_SYSLOG_CONFFILE_BACKUP; + write21TomcatFileContents; + break;; + [Nn]* ) break;; + * ) echo "Please answer yes or no.";; + esac + done else - logMsgToConfigSysLog "INFO" "INFO: Creating file $TOMCAT_SYSLOG_CONFFILE." - sudo touch $TOMCAT_SYSLOG_CONFFILE - sudo chmod o+w $TOMCAT_SYSLOG_CONFFILE - generateTomcat21File - fi - - tomcatInitialLogCount=0 - tomcatLatestLogCount=0 - queryParam="tag%3Atomcat&from=-15m&until=now&size=1" - searchAndFetch tomcatInitialLogCount "$queryParam" - - logMsgToConfigSysLog "INFO" "INFO: Restarting rsyslog and tomcat to generate logs for verification." - # restart the syslog service. - restartsyslog - # restart the tomcat service. - restartTomcat - searchAndFetch tomcatLatestLogCount "$queryParam" - - counter=1 - maxCounter=10 - #echo "latest tomcat log count: $tomcatLatestLogCount and before query count: $tomcatInitialLogCount" - while [ "$tomcatLatestLogCount" -le "$tomcatInitialLogCount" ]; do - echo "######### waiting for 30 secs while loggly processes the test events." - sleep 30 - echo "######## Done waiting. verifying again..." - logMsgToConfigSysLog "INFO" "INFO: Try # $counter of total $maxCounter" - searchAndFetch tomcatLatestLogCount "$queryParam" - #echo "Again Fetch: initial count $tomcatInitialLogCount : latest count : $tomcatLatestLogCount counter: $counter max counter: $maxCounter" - let counter=$counter+1 - if [ "$counter" -gt "$maxCounter" ]; then - logMsgToConfigSysLog "ERROR" "ERROR: Tomcat logs did not make to Loggly in stipulated time. Please check your token & network/firewall settings and retry." - exit 1 - fi - done - - if [ "$tomcatLatestLogCount" -gt "$tomcatInitialLogCount" ]; then - logMsgToConfigSysLog "SUCCESS" "SUCCESS: Tomcat logs successfully transferred to Loggly." - exit 0 + write21TomcatFileContents fi } -# End of configure rsyslog for tomcat -#function to generate tomcat syslog config file -generateTomcat21File() +#function to write the contents of tomcat syslog config file +write21TomcatFileContents() { + logMsgToConfigSysLog "INFO" "INFO: Creating file $TOMCAT_SYSLOG_CONFFILE" + sudo touch $TOMCAT_SYSLOG_CONFFILE + sudo chmod o+w $TOMCAT_SYSLOG_CONFFILE + imfileStr="\$ModLoad imfile \$WorkDirectory $SYSLOG_DIR " @@ -364,67 +417,82 @@ $imfileStr EOIPFW } -#rollback tomcat loggly configuration -rollback() +#checks if the tomcat logs made to loggly +checkIfTomcatLogsMadeToLoggly() { - checkIfUserHasRootPrivileges - setLinuxVariables - setTomcatVariables - logMsgToConfigSysLog "INFO" "INFO: Initiating rollback" - echo "INFO: Reverting the catalina file." + counter=1 + maxCounter=10 + + tomcatInitialLogCount=0 + tomcatLatestLogCount=0 + queryParam="tag%3Atomcat&from=-15m&until=now&size=1" + + queryUrl="$LOGGLY_ACCOUNT_URL/apiv2/search?q=$queryParam" + logMsgToConfigSysLog "INFO" "INFO: Search URL: $queryUrl" + + logMsgToConfigSysLog "INFO" "INFO: Getting initial tomcat log count." + #get the initial count of tomcat logs for past 15 minutes + searchAndFetch tomcatInitialLogCount "$queryUrl" + + logMsgToConfigSysLog "INFO" "INFO: Restarting rsyslog and tomcat to generate logs for verification." + # restart the syslog service. + restartRsyslog + # restart the tomcat service. + restartTomcat + + logMsgToConfigSysLog "INFO" "INFO: Verifying if the tomcat logs made it to Loggly." + logMsgToConfigSysLog "INFO" "INFO: Verification # $counter of total $maxCounter." + #get the final count of tomcat logs for past 15 minutes + searchAndFetch tomcatLatestLogCount "$queryUrl" + let counter=$counter+1 + + while [ "$tomcatLatestLogCount" -le "$tomcatInitialLogCount" ]; do + echo "INFO: Did not find the test log message in Loggly's search yet. Waiting for 30 secs." + sleep 30 + echo "INFO: Done waiting. Verifying again." + logMsgToConfigSysLog "INFO" "INFO: Verification # $counter of total $maxCounter." + searchAndFetch tomcatLatestLogCount "$queryUrl" + let counter=$counter+1 + if [ "$counter" -gt "$maxCounter" ]; then + logMsgToConfigSysLog "ERROR" "ERROR: Tomcat logs did not make to Loggly in time. Please check your token & network/firewall settings and retry." + exit 1 + fi + done + + if [ "$tomcatLatestLogCount" -gt "$tomcatInitialLogCount" ]; then + logMsgToConfigSysLog "SUCCESS" "SUCCESS: Tomcat logs successfully transferred to Loggly! You are now sending Tomcat logs to Loggly." + exit 0 + fi +} + +#restore original loggly properties file from backup +restoreLogglyPropertiesFile() +{ + echo "INFO: Reverting the logging.properties file." if [ -f "$LOGGLY_CATALINA_BACKUP_PROPFILE" ]; then sudo rm -fr $LOGGLY_CATALINA_PROPFILE sudo cp -f $LOGGLY_CATALINA_BACKUP_PROPFILE $LOGGLY_CATALINA_PROPFILE sudo rm -fr $LOGGLY_CATALINA_BACKUP_PROPFILE fi +} + +#remove 21tomcat.conf file +remove21TomcatConfFile() +{ echo "INFO: Deleting the loggly tomcat syslog conf file." if [ -f "$TOMCAT_SYSLOG_CONFFILE" ]; then sudo rm -rf "$TOMCAT_SYSLOG_CONFFILE" fi echo "INFO: Removed all the modified files." restartTomcat - logMsgToConfigSysLog "INFO" "INFO: Rollback completed." -} - -#$1 return the count of records in loggly, $2 is the query param to search in loggly -searchAndFetch() -{ - searchquery="$2" - url="$LOGGLY_ACCOUNT_URL/apiv2/search?q=$searchquery" - logMsgToConfigSysLog "INFO" "INFO: Search URL: $url" - result=$(wget -qO- /dev/stdout --user "$LOGGLY_USERNAME" --password "$LOGGLY_PASSWORD" "$url") - if [ -z "$result" ]; then - logMsgToConfigSysLog "ERROR" "ERROR: Please check your network/firewall settings & ensure Loggly subdomain, username and password is specified correctly." - exit 1 - fi - id=$(echo "$result" | grep -v "{" | grep id | awk '{print $2}') - # strip last double quote from id - id="${id%\"}" - # strip first double quote from id - id="${id#\"}" - #echo "rsid for the search is: $id" - url="$LOGGLY_ACCOUNT_URL/apiv2/events?rsid=$id" - - # retrieve the data - result=$(wget -qO- /dev/stdout --user "$LOGGLY_USERNAME" --password "$LOGGLY_PASSWORD" "$url") - #echo "actual result based on rsid: $result" - count=$(echo "$result" | grep total_events | awk '{print $2}') - count="${count%\,}" - eval $1="'$count'" - echo "Count of events from loggly: "$count"" - if [ "$count" > 0 ]; then - timestamp=$(echo "$result" | grep timestamp) - #echo "timestamp: "$timestamp"" - #echo "Data made successfully to loggly!!!" - fi } #restart tomcat restartTomcat() { #sudo service tomcat restart or home/bin/start.sh - if [ $(ps -ef | grep -v grep | grep "$SERVICE" | wc -l) > 0 ]; then - logMsgToConfigSysLog "INFO" "INFO: $SERVICE is running" + if [ $(ps -ef | grep -v grep | grep "$SERVICE" | wc -l) -gt 0 ]; then + logMsgToConfigSysLog "INFO" "INFO: $SERVICE is running." if [ -f /etc/init.d/$SERVICE ]; then logMsgToConfigSysLog "INFO" "INFO: $SERVICE is running as service." logMsgToConfigSysLog "INFO" "INFO: Restarting the tomcat service." @@ -512,9 +580,9 @@ elif [ "$LOGGLY_AUTH_TOKEN" != "" -a "$LOGGLY_ACCOUNT" != "" -a "$LOGGLY_USERNAM if [ "$LOGGLY_PASSWORD" = "" ]; then getPassword fi - configureLogglyForTomcat + installLogglyConfForTomcat elif [ "$LOGGLY_ROLLBACK" != "" ]; then - rollback + removeLogglyConfForTomcat else usage fi