Skip to content

spirit posteets tagged log

 

  • 6 years ago
    1. # Add the file /etc/logrotate.d/nginx with the following content:
    2. /path/to/log/nginx_* {
    3.     missingok
    4.     notifempty
    5.     compress
    6.     sharedscripts
    7.     postrotate
    8.         test ! -f /var/run/nginx.pid || kill -USR1 `cat /var/run/nginx.pid`
    9.     endscript
    10. }
  • 7 years ago
    Lire continuellement un fichier de log et en cas de détection d’une chaine, exécuter une liste de commande
    1. #!/bin/sh
    2. tail -fn0 /var/log/file.log | while read line ; do
    3.         echo “$line” | grep “pattern”
    4.         if [ $? = 0 ]
    5.         then
    6.                 # Actions
    7.                 echo “Yeah: $line”
    8.         fi
    9. done
  • 7 years ago
    # onlogdo: a hack to handle a common problem: when a certain message shows up in the log file, an action should be performed. (credits to Ben Wong)
    1. #!/bin/bash
    2. # Author: Ben Wong <[email protected]>
    3. # Created on: <2001-08-08 11:56:03 hackerb9>
    4. # Time-stamp: <2002-11-20 14:13:11 bbb>
    5. # onlogdo: a hack to handle a common problem: when a certain message
    6. # shows up in the log file, an action should be performed. E.g.: when
    7. # a PC-card network device is inserted, it should be ifconfig’d or
    8. # perhaps dhclient should be run. This program can do that in a fairly
    9. # reasonable way.
    10. ### FIRST, THE HELPER FUNCTIONS ###
    11. function showmanpage () {
    12.     cat <<‘EOF’
    13. NAME
    14.    onlogdo – monitor a log file and execute commands based on patterns
    15. SYNOPSIS
    16.    onlogdo <logfile> <pattern> <command> [ <pattern> <command> … ]
    17.      logfile: a file that gets appended to (e.g., /var/log/messages),
    18.      pattern: a string (can use bash‘s extended pathname globbing syntax),
    19.      command: the command to run when the previous pattern is seen.
    20. DESCRIPTION
    21.     Onlogdo is a hack to handle a common problem: when a certain
    22.     message shows up in a log file, an action should be performed.
    23.     E.g.: when a PC-card network device is inserted, it should be
    24.     ifconfig’d or perhaps dhclient should be run. This program can do
    25.     that in a fairly reasonable way.
    26.     Onlogdo continuously reads lines as they are appended to a log
    27.     file. When a line matches a given pattern, the command associated
    28.     with that pattern is run in the background. The patterns
    29.     recognized are standard pathname globbing (*, ?, []) plus bash‘s
    30.     extended pattern matching (extglob) syntax (see bash(1)).
    31.     The log file is reopened properly, if the log file being read is
    32.     rotated. Also, since it blocks when waiting for new lines, onlogdo
    33.     takes up almost no CPU time.
    34. PATTERN MATCHING SYNTAX
    35.     Don’t worry about learning the pattern matching the first time you
    36.     read this manual; you‘ll rarely need it, unless you’re anal about
    37.     being as succinct as possible. (Like the author).
    38.     The following is an excerpt from bash‘s man page; see bash(1) for
    39.     full details. In the following description, a pattern-list is a
    40.     list of one or more patterns separated by a |. Composite patterns
    41.     may be formed using one or more of the following sub-patterns:
    42.        *      Matches any string, including the null string.
    43.        ?      Matches any single character.
    44.        […]  Matches any one of the enclosed characters. A pair of
    45.               characters separated by a hyphen denotes a range
    46.               expression. If the first character following the [ is a
    47.               ! or a ^ then any character not enclosed is matched.
    48.       ?(pattern-list)
    49.              Matches zero or one occurrence of the  given patterns
    50.       *(pattern-list)
    51.              Matches  zero  or  more  occurrences  of the given patterns
    52.       +(pattern-list)
    53.              Matches one or more occurrences of the given patterns
    54.       @(pattern-list)
    55.              Matches exactly one of the given patterns
    56.       !(pattern-list)
    57.              Matches  anything  except  one  of the given patterns
    58. EXAMPLES
    59.     Pipelines are okay:
    60.       onlogdo /var/log/messages ‘*‘ ‘sleep 5; echo olleH | rev
    61.     Wildcards match filenames as usual in a command:
    62.       onlogdo /var/log/messages ‘Segfault‘ ‘rm /*.core
    63.     Multiple pattern and command pairs are allowed:
    64.       onlogdo /var/log/messages \
    65.             ‘ep0 at pcmcia‘ ‘/etc/rc.d/dhclient start\
    66.             ‘ep0 detached‘  ‘/etc/rc.d/dhclient stop‘  \
    67.             ‘wi0 at pcmcia‘ ‘/etc/rc.d/dhclient start\
    68.             ‘wi0 detached‘  ‘/etc/rc.d/dhclient stop
    69.     Bash’s extended pathname globbing to do the same as above:
    70.       onlogdo /var/log/messages \
    71.             ‘@(ep|wi)[0-9] at pcmcia’ ‘/etc/rc.d/dhclient start’ \
    72.             ‘@(ep|wi)[0-9] detached’  ‘/etc/rc.d/dhclient stop’
    73.     A useful example for NetBSD/hpcmips:
    74.       onlogdo /var/log/messages ‘hpcapm: resume’ ‘sleep 1; xrefresh’
    75.     Under NetBSD/hpcmips-1.5.1, the X server screen is overwritten by the
    76.     console on an APM resume. I put the last “onlogdo” example in my
    77.     system xinitrc, so xrefresh will be run automatically. (The sleep
    78.     is there to wait for the console to finish junking up the screen).
    79. SEE ALSO
    80.     tail(1), bash(1)
    81. BUGS
    82.     If this had been written in Perl it would have used “normal”
    83.     regexp syntax instead of pathname expansion. Bash‘s extensions
    84.     make the patterns as powerful as regexps, but more recondite.
    85.     There is no (documented) way for a command to refer to specifics
    86.     in the line that matched. E.g., if the pattern was “@(ep|wi)0”,
    87.     the command wouldn’t know if the line contained ep0 or wi0.
    88.     The author has spent way too much time perfecting a kludge. No
    89.     matter what you‘re using onlogdo for, there’s probably a more
    90.     “correct” way to do it. On the other hand, onlogdo is widely
    91.     applicable and easy to use, so at least you won‘t be wasting much
    92.     time while doing it the “wrong” way.
    93.     There should be a real man page.
    94. AUTHOR
    95.     Ben Wong <[email protected]>
    96. HISTORY
    97.     Onlogdo started life as a one line kludge to work around a bug in
    98.     the interaction between APM and the X server in NetBSD-1.5/hpcmips
    99.     in September of 2001.
    100. EOF
    101. }
    102. ### USAGE AND ARGUMENT SANITY CHECKING ###
    103. if [[ $# -lt 3 ]]; then
    104.     showmanpage
    105.     exit 1
    106. fi
    107. # Emacs’s syntax highlighting doesn‘t handle single ticks (‘) properly.
    108. # Check if -v (verbose) flag was given.
    109. if [[ “$1” == “-v” ]]; then
    110.     ifverbose=echo            # Echo commands verbosely, if -v.
    111.     shift
    112. else
    113.     ifverbose=:   # Usually just run a no-op.
    114. fi
    115. # Make sure the log file is readable.
    116. if [[ ! -r “$1” ]]; then
    117.     echo “onlogdo: \”$1\” is not readable” >&2
    118.     exit 1
    119. fi
    120. if [[ -d “$1” ]]; then
    121.     echo “onlogdo: \”$1\” is a directory” >&2
    122.     exit 1
    123. fi
    124. ### SIGNAL HANDLING AND EXIT CLEANUP ###
    125. # Run cleanup function when shell exits.
    126. trap cleanup EXIT
    127. cleanup () {
    128.     echo “onlogdo: cleaning up…” >&2
    129.     if [[ “$TAILPID” ]]; then
    130.         kill $TAILPID      # Kill the bg tail process.
    131.     fi
    132.     rm -f “$PIPE”              # Delete the named pipe.
    133.     rmdir “$PIPEDIR”
    134.     return 0
    135. }
    136. ### MAIN ROUTINE STARTS HERE ###
    137. # Don’t expand wildcards in pattern arguments as filenames.
    138. set -o noglob
    139. # Use bash’s extended pattern matching operators
    140. shopt -s extglob
    141. # Create a pipe to read the log file a line at a time;
    142. # Use mktemp for security.
    143. PIPEDIR=$(mktemp -d /tmp/onlogdo.XXXXXX)
    144. PIPE=“$PIPEDIR/$(echo $1 | sed ‘s#^/##; s#/#.#g’)”      # var.log.messages
    145. rm -f $PIPE
    146. mknod $PIPE p
    147. tail -Fn0 $1 >$PIPE &      # FSF tail: “tail –retry -fn0”
    148. TAILPID=$!
    149. exec 5< $PIPE
    150. # Copy the positional paramaters in to a variable that can be indexed.
    151. params=(“$0” “$@”)
    152. # Repeatedly read from the pipe, process each line.
    153. while :; do
    154.     while read REPLY <&5; do
    155.         $ifverbose -e \nline: \”$REPLY\”
    156.         for ((i=2; i<$#; i+=2)); do
    157.             pattern=${params[$i]}
    158.             command=${params[$((i+1))]}
    159.             $ifverbose “pattern: \”$pattern\”
    160.             if [[ -z “${REPLY##*$pattern*}” ]]; then
    161.                 $ifverbose “*MATCHED*”
    162.                 $ifverbose “command: \”$command\”
    163.                 set +o noglob   # Allow pathname matching in commands
    164.                 eval $command &
    165.                 set -o noglob   # No pathname expansion for patterns
    166.             fi
    167.         done
    168.     done
    169.     # We get here whenever the pipe first blocks (returns EOF)
    170.     sleep 1               # Don’t loop too quickly
    171. done
    172. ### MAIN ROUTINE ENDS HERE ###
    173. # Note: if we ever get here, the cleanup() function will be called
    174. # automatically since we’re trapping on signal 0.

    “log” related tags

    spirit’s tags