| 1 | rabit | 1.1 | #!/bin/sh | 
| 2 |  |  | #----------------------------------------------------------------------------- | 
| 3 |  |  | D='Log cycle control script' | 
| 4 |  |  | C='rabit@netfrag.org, 2004/11/07' | 
| 5 |  |  | I='$Id$' | 
| 6 |  |  | #----------------------------------------------------------------------------- | 
| 7 |  |  |  | 
| 8 |  |  | # The rough original taken from: | 
| 9 |  |  |  | 
| 10 |  |  | #       Written by Martin Schulze <joey@debian.org>. | 
| 11 |  |  | #       $-Id-: syslog-cron,v 1.4 2003/11/13 19:07:11 avenj Exp $ | 
| 12 |  |  |  | 
| 13 |  |  | #----------------------------------------------------------------------------- | 
| 14 |  |  | #- Configuration: | 
| 15 |  |  | #- (Change this section according to your needs.) | 
| 16 |  |  |  | 
| 17 |  |  | # Working directory: | 
| 18 |  |  | #WD=/val/log | 
| 19 |  |  |  | 
| 20 |  |  | # Additional list of files containing a list of log files: | 
| 21 |  |  | LISTS='/var/log/.cyclelogs' | 
| 22 |  |  |  | 
| 23 |  |  | # Number of copies to keep: | 
| 24 |  |  | COPIES=7 | 
| 25 |  |  |  | 
| 26 |  |  | # User to change log files to: | 
| 27 |  |  | USER=root | 
| 28 |  |  |  | 
| 29 |  |  | # Group to change log files to: | 
| 30 |  |  | GROUP=adm | 
| 31 |  |  |  | 
| 32 |  |  | # Mode to change log files to: | 
| 33 |  |  | MODE=640 | 
| 34 |  |  |  | 
| 35 |  |  | #----------------------------------------------------------------------------- | 
| 36 |  |  | #- Constants: | 
| 37 |  |  |  | 
| 38 |  |  | e='echo -e' | 
| 39 |  |  | yw="\033[1;33m" | 
| 40 |  |  | gy="\033[0;37m" | 
| 41 |  |  | b0="\033[0m" | 
| 42 |  |  | b1="\033[1m" | 
| 43 |  |  |  | 
| 44 |  |  | N=$(basename $0) | 
| 45 |  |  | I=${I:5} | 
| 46 |  |  | I=${I:-No CVS ID yet} | 
| 47 |  |  |  | 
| 48 |  |  | CycleCommand="/usr/sbin/savelog -g $GROUP -m $MODE -u $USER -c $COPIES" | 
| 49 |  |  |  | 
| 50 |  |  | #----------------------------------------------------------------------------- | 
| 51 |  |  | #- Functions: | 
| 52 |  |  |  | 
| 53 |  |  | # Syntax: createlist | 
| 54 |  |  | # Creates the list of log files to work with. | 
| 55 |  |  | # | 
| 56 |  |  | createlist() { | 
| 57 |  |  |  | 
| 58 |  |  | # First, build a list of all log files known to syslogd: | 
| 59 |  |  | LOGFILES=$(/usr/sbin/syslogd-listfiles -a) | 
| 60 |  |  |  | 
| 61 |  |  | # Add all log files in '/var/log/.cyclelogs' to the list: | 
| 62 |  |  | LOGFILES=$LOGFILES' '$(/bin/cat /var/log/.cyclelogs) | 
| 63 |  |  |  | 
| 64 |  |  | } | 
| 65 |  |  |  | 
| 66 |  |  | # Syntax: cyclelogs | 
| 67 |  |  | # Uses 'savelog' to cycle all given log files in $LOGFILES. | 
| 68 |  |  | # | 
| 69 |  |  | cyclelogs() { | 
| 70 |  |  |  | 
| 71 |  |  | # Iterate each entry in $LOGFILES: | 
| 72 |  |  | for LOG in $LOGFILES; do | 
| 73 |  |  |  | 
| 74 |  |  | # If the file $LOG exists...: | 
| 75 |  |  | if [ -f $LOG ]; then | 
| 76 |  |  |  | 
| 77 |  |  | if [ "$Pretend" == 1 ]; then | 
| 78 |  |  | $e "Would cycle file '$b1$LOG$b0'." | 
| 79 |  |  | else | 
| 80 |  |  | $CycleCommand $LOG >/dev/null | 
| 81 |  |  | fi | 
| 82 |  |  |  | 
| 83 |  |  | fi | 
| 84 |  |  |  | 
| 85 |  |  | done | 
| 86 |  |  |  | 
| 87 |  |  | return | 
| 88 |  |  |  | 
| 89 |  |  | } | 
| 90 |  |  |  | 
| 91 |  |  | # Syntax: listfiles | 
| 92 |  |  | # Outputs the list of all log files to work with (one file name per line). | 
| 93 |  |  | # | 
| 94 |  |  | listfiles() { | 
| 95 |  |  |  | 
| 96 |  |  | for LOG in $LOGFILES; do | 
| 97 |  |  | echo $LOG | 
| 98 |  |  | done | 
| 99 |  |  |  | 
| 100 |  |  | return | 
| 101 |  |  |  | 
| 102 |  |  | } | 
| 103 |  |  |  | 
| 104 |  |  | printhelp() { | 
| 105 |  |  |  | 
| 106 |  |  | $e "${b1}Function${b0}: | 
| 107 |  |  | $N cycles a list of log files using the 'savelog' command. | 
| 108 |  |  | The list of files consists of all log files known to syslogd and additional | 
| 109 |  |  | files from list files you may specify. | 
| 110 |  |  | Without options called $N immediately begins doing its job. | 
| 111 |  |  | At last syslogd is restarted to make it use the emptied log files again. | 
| 112 |  |  | $N usually is called by CRON (or something like it). For this, | 
| 113 |  |  | place a link to it in one of the CRON directories. | 
| 114 |  |  | ${b1}Usage${b0}: | 
| 115 |  |  | $b1$N [<Option>]$b0 | 
| 116 |  |  | ${b1}Options${b0}: | 
| 117 |  |  | ${b1}-t${b0}|${b1}--test${b0} | 
| 118 |  |  | Test mode. $N only prints out what would be done. | 
| 119 |  |  | ${b1}-l${b0}|${b1}--list${b0} | 
| 120 |  |  | $N only lists all known log files, then exits. | 
| 121 |  |  | ${b1}-h${b0}|${b1}--help${b0} | 
| 122 |  |  | Outputs this command line help and exits. | 
| 123 |  |  | ${b1}-v${b0}|${b1}--version${b0} | 
| 124 |  |  | Shows title and version and exits." | 
| 125 |  |  |  | 
| 126 |  |  | return | 
| 127 |  |  |  | 
| 128 |  |  | } | 
| 129 |  |  |  | 
| 130 |  |  | printinfo() { | 
| 131 |  |  |  | 
| 132 |  |  | $e "- Test mode active. | 
| 133 |  |  | (All files would become mode '$b1$MODE$b0' set and chown'd to '$b1$USER:$GROUP$b0'. '$b1$COPIES$b0' copies would be kept.)" | 
| 134 |  |  | return | 
| 135 |  |  |  | 
| 136 |  |  | } | 
| 137 |  |  |  | 
| 138 |  |  | printtitle() { | 
| 139 |  |  |  | 
| 140 |  |  | $e "$yw$N - $D$gy ($C)\n($I)" | 
| 141 |  |  | return | 
| 142 |  |  |  | 
| 143 |  |  | } | 
| 144 |  |  |  | 
| 145 |  |  | #----------------------------------------------------------------------------- | 
| 146 |  |  |  | 
| 147 |  |  | Pretend=0 | 
| 148 |  |  |  | 
| 149 |  |  | while [ -n "$1" ]; do | 
| 150 |  |  |  | 
| 151 |  |  | case $1 in | 
| 152 |  |  |  | 
| 153 |  |  | (-h|--help) | 
| 154 |  |  | printtitle | 
| 155 |  |  | printhelp | 
| 156 |  |  | exit | 
| 157 |  |  | ;; | 
| 158 |  |  |  | 
| 159 |  |  | (-l|--list) | 
| 160 |  |  | createlist | 
| 161 |  |  | listfiles | 
| 162 |  |  | exit | 
| 163 |  |  | ;; | 
| 164 |  |  |  | 
| 165 |  |  | (-t|--test) | 
| 166 |  |  | printtitle | 
| 167 |  |  | printinfo | 
| 168 |  |  | Pretend=1 | 
| 169 |  |  | ;; | 
| 170 |  |  |  | 
| 171 |  |  | (-v|--version) | 
| 172 |  |  | printtitle | 
| 173 |  |  | exit | 
| 174 |  |  | ;; | 
| 175 |  |  | esac | 
| 176 |  |  |  | 
| 177 |  |  | shift | 
| 178 |  |  |  | 
| 179 |  |  | done | 
| 180 |  |  |  | 
| 181 |  |  | #----------------------------------------------------------------------------- | 
| 182 |  |  |  | 
| 183 |  |  | createlist | 
| 184 |  |  | cyclelogs | 
| 185 |  |  |  | 
| 186 |  |  | # Restart syslogd to make it resume with the emptied log files: | 
| 187 |  |  |  | 
| 188 |  |  | SyslogCommand='/bin/killall -HUP syslogd' | 
| 189 |  |  |  | 
| 190 |  |  | if [ "$Pretend" == 1 ]; then | 
| 191 |  |  | $e "Would call '$b1$SyslogCommand$b0' to make syslogd restart." | 
| 192 |  |  | else | 
| 193 |  |  | $SyslogCommand | 
| 194 |  |  | fi | 
| 195 |  |  |  | 
| 196 |  |  | #----------------------------------------------------------------------------- |