diff --git a/README.md b/README.md index b9de5b9..394a6d2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/2e2526c5266848f8ae3e03a87079bc49)](https://www.codacy.com/app/nico.wellpott/logwatch-scripts?utm_source=github.com&utm_medium=referral&utm_content=mightyBroccoli/logwatch-scripts&utm_campaign=Badge_Grade) + # TeamSpeak3 Logwatch Script Script which iterates through the latest teamspeak logfiles and captures the important parts. Main goal is to filter relevant loglines from the past day (UTC). Finally the selected lines are being formatted nicely, so it is easy to see what happened. There are two possible notification modes: diff --git a/script/filters.sh b/script/filters.sh new file mode 100644 index 0000000..60fb57d --- /dev/null +++ b/script/filters.sh @@ -0,0 +1,141 @@ +#!/bin/bash + +#### Filter File #### + +general_filter() +{ + # general filter which will shift through all positional parameters and filter for that + while [ "$1" ] + do + grep -E "$1" "$daily_log" | sort >> "$composition1" + + # only paste the results if there are any + if [ -s "$composition1" ]; then + { echo -e "------------------- $1 --------------------\\n" + cat "$composition1" + echo -e "----------------- $1 End ------------------\\n" + } >> "$message_body" + fi + clearcomp + shift + done +} + +filter_server() +{ + # generate report header with info from the systemd script + { echo -e "LogReport Teamspeak $(date -d "yesterday" '+%d.%m.%Y')\\n" + start_finish --teamspeak-start + # server section + echo -e "-------------------- Server -----------------------\\n" + echo -e "Status : $(systemctl status $service_script)\\n" + } >> "$message_body" + + # only run this filter if $enable_server is true + if [ "$enable_server" != "false" ]; then + general_filter "ServerMain|stopped" "ERROR" "WARNING" "Accounting" "CIDRManager" + fi + + echo -e "------------------ Server End ---------------------\\n" >> "$message_body" +} + +filter_group() +{ + # only run this filter if $enable_groups is true + if [ "$enable_groups" != "false" ]; then + grep -E "was deleted by|was copied by|was added to|was added by|was removed from" "$daily_log" | grep -v "permission '" >> "$composition1" + + # only paste the results if there are + if [ -s "$composition1" ]; then + echo -e "------------------ Group change -------------------" >> "$message_body" + + # filter for created or copied group events + grep -E "was copied by|was added by" $composition1 > $composition2 + if [ -s "$composition2" ]; then + { echo -e "--- created/ copied ---\\n" + cat "$composition2" + changed=true + } >> "$composition3" + fi + + # filter for deleted group events + grep "was deleted by" "$composition1" > "$composition2" + if [ -s "$composition2" ]; then + { echo -e "--- deleted ---\\n" + cat "$composition2" + changed=true + } >> "$composition3" + fi + + # filter if sombody was added to servergroup + grep "was added to" "$composition1" > "$composition2" + if [ -s "$composition2" ]; then + { echo -e "--- added to---\\n" + cat "$composition2" + changed=true + } >> "$composition3" + fi + + # filter if somebody was removed from a group + grep "was removed from" "$composition1" > "$composition2" + if [ -s "$composition2" ]; then + { echo -e "--- removed from---\\n" + cat "$composition2" + changed=true + } >> "$composition3" + fi + + # if "changed" is true then paste the results in the mask + if [ "$changed" = "true" ]; then + { echo -e "---- Group ----" + cat "$composition3" + echo -e "\\n---- Group End ----" + } >> "$message_body" + fi + echo -e "---------------- Group change End -----------------\\n" >> "$message_body" + fi + clearcomp + fi +} + +filter_channel() +{ + # only run this filter if $enable_channel is true + if [ "$enable_channel" != "false" ]; then + + # filter for channel creation events + grep "channel" "$daily_log" | grep "VirtualServerBase">> "$composition1" + + # only paste the results if there are + if [ -s "$composition1" ]; then + { echo -e "--------------------- Channel ---------------------\\n" + cat "$composition1" + echo -e "------------------- Channel End -------------------\\n" + } >> "$message_body" + fi + clearcomp + fi +} + +filter_user_activity() +{ + # only enable this filter if $enable_complaint is true + if [ "$enable_complaint" != "false" ]; then + general_filter "^complaint" + fi + + # only enable this filter if $enable_ban is true + if [ "$enable_ban" != "false" ]; then + general_filter "ban added|BanManager" + fi + + # only enalbe this filter if $enable_kick is true + if [ "$enable_kick" != "false" ]; then + general_filter "reason 'invokerid" + fi + + # only enable this filter if $enable_perm is true + if [ "$enable_perm" != "false" ]; then + general_filter "permission" + fi +} \ No newline at end of file diff --git a/script/teamspeak_log_daily_mail b/script/teamspeak_log_daily_mail.sh similarity index 51% rename from script/teamspeak_log_daily_mail rename to script/teamspeak_log_daily_mail.sh index c5ef5fc..f80b5ca 100644 --- a/script/teamspeak_log_daily_mail +++ b/script/teamspeak_log_daily_mail.sh @@ -17,6 +17,11 @@ # replace mailaddr_to/_from and possibly the tslog path to fit your needs # place inside /etc/cron.daily to run this script on a daily basis +# cd into script directory +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +#### Settings #### + # user specific config file tmp_directory=/tmp/teamspeak/daily_mail archive=/var/log/teamspeak @@ -24,13 +29,15 @@ configfile=$tmp_directory/.user.config configfile_secured=$tmp_directory/tmp.config backupconf=/var/backups/teamspeak_log_daily_mail_user.config -#system specific variables +# system specific variables tslogs=/etc/teamspeak3-server_linux_amd64/logs service_script=ts3server.service tsuser=teamspeak -#### DONT CHANGE FROM HERE #### -#log files +#### Business logic #### +#### DO NOT ALTER #### + +# log files logfiles=$tmp_directory/teamspeak_daily_log_files.txt daily_log=$tmp_directory/teamspeak_daily_log.txt @@ -44,8 +51,9 @@ mailcomposition=$tmp_directory/teamspeak_mail_composition.txt # debug parameter debug=false -###### FUNCTION SECTION ###### -## preparations +#### FUNCTION SECTION #### + +# preparations prerun_check() { # check if all commands needed to run are present in $PATH @@ -63,12 +71,12 @@ prerun_check() exit 11 fi - #check if tmp directory is present if not create it + # check if tmp directory is present if not create it if [ ! -d "$tmp_directory" ]; then mkdir -p "$tmp_directory" fi - #first run check + # first run check # check for presents of the configfile if not exit if [ ! -f "$configfile" ]; then if [ -f "$backupconf" ]; then @@ -107,11 +115,12 @@ collect_and_prepare() grep "$(date -d "$1" '+%Y-%m-%d')" "$tslogs/$(sed -n '2p' $logfiles)" >> $daily_log } -###### General Functions ###### +#### General Functions #### + clearcomp() { if [ "$1" = "-all" ]; then - #deleting possible old content + # deleting possible old content rm -f "$composition1" "$composition2" "$logfiles" "$daily_log" "$message_body" "$mailcomposition" fi # remove the composition files @@ -119,7 +128,8 @@ clearcomp() changed=false } -###### Mail ###### +#### Mail #### + start_finish() { # function to generate sections header @@ -135,7 +145,7 @@ archive_mail() { # only run this functions if $enable_archive is true if [ "$enable_archive" != "false" ]; then - #check if archive directory is present + # check if archive directory is present if [ ! -d "$archive" ]; then mkdir -p "$tmp_directory" fi @@ -147,7 +157,7 @@ send_mail() { # only send mails if $email = yes if [ "$email" = yes ]; then - #file is not empty + # file is not empty { echo -e "To: $mailaddr_to" echo -e "From: $mailaddr_from" echo -e "Subject: LogReport Teamspeak $(date -d "yesterday" '+%d.%m.%Y')\\n" @@ -157,147 +167,12 @@ send_mail() fi } -###### FILTER CODE SECTION ###### -general_filter() -{ - # general filter which will shift through all positional parameters and filter for that - while [ "$1" ] - do - grep -E "$1" "$daily_log" | sort >> "$composition1" - - # only paste the results if there are - if [ -s "$composition1" ]; then - { echo -e "------------------- $1 --------------------\\n" - cat "$composition1" - echo -e "----------------- $1 End ------------------\\n" - } >> "$message_body" - fi - clearcomp - shift - done -} - -filter_server() -{ - # generate report header with info from the systemd script - { echo -e "LogReport Teamspeak $(date -d "yesterday" '+%d.%m.%Y')\\n" - start_finish --teamspeak-start - # server section - echo -e "-------------------- Server -----------------------\\n" - echo -e "Status : $(systemctl status $service_script)\\n" - } >> "$message_body" - - # only run this filter if $enable_server is true - if [ "$enable_server" != "false" ]; then - general_filter "ServerMain|stopped" "ERROR" "WARNING" "Accounting" "CIDRManager" - fi - - echo -e "------------------ Server End ---------------------\\n" >> "$message_body" -} - -filter_group() -{ - # only run this filter if $enable_groups is true - if [ "$enable_groups" != "false" ]; then - grep -E "was deleted by|was copied by|was added to|was added by|was removed from" "$daily_log" | grep -v "permission '" >> "$composition1" - - # only paste the results if there are - if [ -s "$composition1" ]; then - echo -e "------------------ Group change -------------------" >> "$message_body" - - # filter for created or copied group events - grep -E "was copied by|was added by" $composition1 > $composition2 - if [ -s "$composition2" ]; then - { echo -e "--- created/ copied ---\\n" - cat "$composition2" - changed=true - } >> "$composition3" - fi - - # filter for deleted group events - grep "was deleted by" "$composition1" > "$composition2" - if [ -s "$composition2" ]; then - { echo -e "--- deleted ---\\n" - cat "$composition2" - changed=true - } >> "$composition3" - fi - - # filter if sombody was added to servergroup - grep "was added to" "$composition1" > "$composition2" - if [ -s "$composition2" ]; then - { echo -e "--- added to---\\n" - cat "$composition2" - changed=true - } >> "$composition3" - fi - - #filter if somebody was removed from a group - grep "was removed from" "$composition1" > "$composition2" - if [ -s "$composition2" ]; then - { echo -e "--- removed from---\\n" - cat "$composition2" - changed=true - } >> "$composition3" - fi - - # if "changed" is true then paste the results in the mask - if [ "$changed" = "true" ]; then - { echo -e "---- Group ----" - cat "$composition3" - echo -e "\\n---- Group End ----" - } >> "$message_body" - fi - echo -e "---------------- Group change End -----------------\\n" >> "$message_body" - fi - clearcomp - fi -} - -filter_channel() -{ - # only run this filter if $enable_channel is true - if [ "$enable_channel" != "false" ]; then - - # filter for channel creation events - grep "channel" "$daily_log" | grep "VirtualServerBase">> "$composition1" - - # only paste the results if there are - if [ -s "$composition1" ]; then - { echo -e "--------------------- Channel ---------------------\\n" - cat "$composition1" - echo -e "------------------- Channel End -------------------\\n" - } >> "$message_body" - fi - clearcomp - fi -} +# Source filters +source "${DIR}/filters.sh" -filter_user_activity() -{ - # only enable this filter if $enable_complaint is true - if [ "$enable_complaint" != "false" ]; then - general_filter "^complaint" - fi - - # only enable this filter if $enable_ban is true - if [ "$enable_ban" != "false" ]; then - general_filter "ban added|BanManager" - fi - - # only enalbe this filter if $enable_kick is true - if [ "$enable_kick" != "false" ]; then - general_filter "reason 'invokerid" - fi - - # only enable this filter if $enable_perm is true - if [ "$enable_perm" != "false" ]; then - general_filter "permission" - fi -} +#### MAIN CODE SECTION #### -###### MAIN CODE SECTION ###### -#run all preparations and inital checks +# run all preparations and inital checks # clear old stuff clearcomp -all @@ -307,7 +182,7 @@ prerun_check # collect and prepare logs and needed files if [ "$debug" = "true" ]; then - #forcefully use the logentries from today + # forcefully use the logentries from today collect_and_prepare today else # the normal routine diff --git a/script/teamspeak_log_xmpp-push.sh b/script/teamspeak_log_xmpp-push.sh index 96ad0b4..c881fd5 100644 --- a/script/teamspeak_log_xmpp-push.sh +++ b/script/teamspeak_log_xmpp-push.sh @@ -20,14 +20,21 @@ # run this script every x minutes to send the lines accumulated in the log files via cron eg # */x * * * * /PATH/teamspeak_log_xmpp-push.sh -#variables +# cd into script directory +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +#### Settings #### + +# variables tmp_directory=/tmp/teamspeak/xmpp_push tslogs=/etc/teamspeak3-server_linux_amd64/logs configfile=$tmp_directory/.user.config configfile_secured=$tmp_directory/.tmp.config backupconf=/var/backups/teamspeak_log_xmpp-push_user.config -#### DONT CHANGE FROM HERE #### +#### Business logic #### +#### DO NOT ALTER #### + # compositon variables composition1=$tmp_directory/composition1.txt composition2=$tmp_directory/composition2.txt @@ -52,7 +59,8 @@ currentday=$(date -u '+%Y-%m-%d') # debug parameter debug=false -###### PRE RUN FUNCTION SECTION ###### +#### PRE RUN FUNCTION SECTION #### + prerun_check() { needed_commands="rm ls echo grep sort cat date sendxmpp" @@ -69,18 +77,18 @@ prerun_check() exit 11 fi - #check if tmp directory is present if not create it + # check if tmp directory is present if not create it if [ ! -d "$tmp_directory" ]; then mkdir -p $tmp_directory fi - #first run check + # first run check if [ ! -f "$configfile" ]; then if [ -f "$backupconf" ]; then echo -e "no config inside $tmp_directory using $backupconf" cp "$backupconf" "$configfile" else - #config file is not present + # config file is not present echo -e "no config file has been set. copy the sample config file to $configfile" exit 10 fi @@ -98,15 +106,15 @@ prerun_check() # source the config file source "$configfile" - #is the history relevant? - #check if date file is present + # is the history relevant? + # check if date file is present if [ ! -f "$tmp_directory/date.txt" ]; then date -u '+%Y-%m-%d' > $tmp_directory/date.txt fi if [ ! "$currentday" = "$(cat $tmp_directory/date.txt)" ]; then - #they are not the same remove the old history + # they are not the same remove the old history rm $log_history - #set the new date inside the date file + # set the new date inside the date file date -u '+%Y-%m-%d' > $tmp_directory/date.txt fi clearcomp -all @@ -115,17 +123,17 @@ prerun_check() collect_and_prepare() { ## log file selection - #get the currently used logfiles + # get the currently used logfiles ls -t $tslogs | head -n2 | sort > $logfiles - #from the $logfiles get everything from today + # from the $logfiles get everything from today grep "$(date -u '+%Y-%m-%d')" "$tslogs/$(sed -n '1p' $logfiles)" > $log_selection_today_unsorted grep "$(date -u '+%Y-%m-%d')" "$tslogs/$(sed -n '2p' $logfiles)" >> $log_selection_today_unsorted # sort logentries sort $log_selection_today_unsorted > $log_selection_today - #if $log_history file exists append if not create it + # if $log_history file exists append if not create it if [ -s $log_history ]; then #it does exist grep -v -F -x -f $log_history $log_selection_today > $log_removed_old @@ -138,7 +146,8 @@ collect_and_prepare() fi } -###### General Functions ###### +#### General Functions #### + clearcomp() { # check if there is a final composition push if true @@ -187,212 +196,41 @@ pushstuff() clearcomp -all } -###### FILTER CODE SECTION ###### -# Filter Server Messages Accounting/ ServerMain / Warning / ERROR / CIDRManager -filter_server() -{ - # only run this filter if $enable_server is true - if [ "$enable_server" != false ]; then - grep -E 'ServerMain|stopped|Accounting|Warning|ERROR|CIDRManager' $log_removed_old >> $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - { echo -e "\\n---- Server ----\\n" - cat $composition1 - echo -e "\\n---- Server End ----" - } >> $server - cat $server >> $final_composition - fi - clearcomp - fi -} - -# Filter complaints -filter_complaint() -{ - # only run this filter if $enable_complaint is true - if [ "$enable_complaint" != false ]; then - grep "^complaint" $log_removed_old >> $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - { echo -e "\\n---- Complaint ----\\n" - cat $composition1 - echo -e "\\n---- Complaint End ----" - } >> $complaint - cat $complaint >> $final_composition - fi - clearcomp - fi -} - -# Filter Kick messages -filter_kick() -{ - # only run this filter if $enable_kick is true - if [ "$enable_kick" != false ]; then - grep "reason 'invokerid" $log_removed_old | grep -v "bantime" >> $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - { echo -e "\\n---- Kick ----\\n" - cat $composition1 - echo -e "\\n---- Kick End ----" - } >> $kick - cat $kick >> $final_composition - fi - clearcomp - fi -} - -# Filter Channels added /deleted / edited -filter_channel() -{ - # only run this filter if $enable_channel is true - if [ "$enable_channel" != false ];then - grep "channel" $log_removed_old | grep VirtualServerBase > $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - { echo -e "\\n---- Channel ----\\n" - cat $composition1 - echo -e "\\n---- Channel End ----" - } >> $channel - cat $channel >> $final_composition - fi - clearcomp - fi -} - -# Filter bans added / deleted and general BanManager messages -filter_ban() -{ - # only run this filter if $enable_ban is true - if [ "$enable_ban" != false ]; then - # first collect all ban related events - grep -E 'ban added|BanManager|ban deleted' $log_removed_old >> $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - # filter added bans - grep -E 'ban added' $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- Ban added ---\\n" - cat $composition2 - # change the "changed" value to true to indicate something has changed - changed=true - } >> $composition3 - fi - - # filter deleted bans - grep -E 'BanManager|ban deleted' $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- Ban deleted ---\\n" - cat $composition2 - # change the "changed" value to true to indicate something has changed - changed=true - } >> $composition3 - fi - fi - - # if "changed" is true paste the results in this mask - if [ "$changed" = true ]; then - { echo -e "\\n---- Ban ----" - cat $composition3 - echo -e "\\n---- Ban End ----" - }>> $ban - cat $ban >> $final_composition - fi - clearcomp - fi -} +# Source filters +source "${DIR}/filters.sh" -# Filter Group changes -filter_groups() -{ - # only enable this filter if $enable_groups is true - if [ "$enable_groups" != false ]; then - grep -E "was deleted by|was copied by|was added to|was added by|was removed from" $log_removed_old | grep -v "permission '" > $composition1 - - # only paste the results if there are - if [ -s $composition1 ]; then - #filter created or copied group events - grep -E "was copied by|was added by" $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- created/ copied ---\\n" - cat $composition2 - changed=true - } >> $composition3 - fi - - # filter deleted group events - grep "was deleted by" $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- deleted ---\\n" - cat $composition2 - changed=true - } >> $composition3 - fi - - # filter somebodys group changed - grep "was added to" $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- added to---\\n" - cat $composition2 - changed=true - } >> $composition3 - fi - - # filter somebody got removed from a group - grep "was removed from" $composition1 > $composition2 - if [ -s $composition2 ]; then - { echo -e "--- removed from---\\n" - cat $composition2 - changed=true - } >> $composition3 - fi - fi - - # if "changed" = true paste all results in this mask - if [ "$changed" = true ]; then - { echo -e "\\n---- Group ----" - cat $composition3 - echo -e "\\n---- Group End ----" - } >> $groupchange - cat $groupchange >> $final_composition - fi - clearcomp - fi -} +#### MAIN CODE SECTION #### -###### MAIN CODE SECTION ###### -#run all preparations and inital checks +# run all preparations and inital checks # clear old stuff clearcomp -all + # prepare folders and config files prerun_check + # collect and prepare logs and needed files collect_and_prepare + #debug parameters debug ## server filter_server -## Complaint +## complaint filter_complaint -## Ban +## ban filter_ban -## Kick +## kick filter_kick -## Group change +## group change filter_groups -## Channel +## channel filter_channel -## Push Message +## push message pushstuff