Giter Site home page Giter Site logo

cf-debug-tools's People

Contributors

andrey-bushik avatar datianshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

cf-debug-tools's Issues

Advise to enhance parameters validation

I added some code to enhance parameters validation.

1.) Check if the param after "-t" flag is a number. If not, the "usage" function will be called

2.) Check if all other params except "-t " are files and also they do exist in the vm

#!/bin/bash
#
# Borrowed from: https://github.com/lynhines/CDN_work_scripts/blob/master/toplogs.sh
#
# Modified by: Daniel Mikusa <[email protected]>
#
set -e  # dont add `-o pipefail`, this will cause false errors

LOGREGEX='^(.*?) - \[(\d{2})\/(\w{3})\/(\d{4}):(\d{2}):(\d{2}):(\d{2}) (.*?)\] "(.*?) (.*?) (.*?)" (\d+) (\d+) "(.*?)" "(.*?)" (.*?) vcap_request_id:(.*?) response_time:(\d+\.\d+)$'

usage () {
    echo "USAGE:"
    echo "toplogs-cloudcontroller.sh [-t|--top 10] <file1> <file2> <file3> ..."
    echo ""
    echo "    -t|--top - defaults to 10, sets the number of results to return"
    echo ""
    echo "NOTES:"
    echo "  Assumes standard CloudController Nginx access log format:"
    echo '    <client ip> - [<date>:<time>] "<method> <request path> <http version>" <status code> <bytes> "<referrer" "<user agent>" <x-forwarded-for> vcap_request_id:<reqest_id> response_time:<response_time>'
    exit 1
}

printHeader () {
    printf "\n--------------------------------------\n"
    printf "  %s" "$1"
    printf "\n--------------------------------------\n\n"
}

# parse out args
#  - https://stackoverflow.com/a/14203146/1585136
TOP=10
POSITIONAL=()
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -t|--top)
        TOP="$2"
        shift
        shift
        ;;
    *)
    POSITIONAL+=("$1")
    shift
    ;;
esac
done

re='^[0-9]+$'
if ! [[ $TOP =~ $re ]]; then
    usage
fi

if [ ${#POSITIONAL[@]} -eq 0 ]; then
    usage
fi

set -- "${POSITIONAL[@]}"

while [[ $# -gt 0 ]]; do
    if [ ! -f $1 ]; then
       usage
    fi
    shift
done

set -- "${POSITIONAL[@]}"

main () {
    printHeader 'Duration'
    perl -n -e '/'"$LOGREGEX"'/ && print $2."/".$3."/".$4." ".$5.":".$6.":".$7."\r\n"' <( cat "$@" ) | sort | sed -e 1b -e '$!d'

    printHeader 'Response Codes'
    perl -n -e '/'"$LOGREGEX"'/ && print $12."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr

    printHeader 'Request Methods'
    perl -n -e '/'"$LOGREGEX"'/ && print $9."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr

    printHeader 'Top '"$TOP"' Requests (no query params)'
    perl -n -e '/'"$LOGREGEX"'/ && print $10."\r\n"' <( cat "$@" ) | cut -d '?' -f 1 | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Requests (with query params)'
    perl -n -e '/'"$LOGREGEX"'/ && print $10."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' User Agents'
    perl -n -e '/'"$LOGREGEX"'/ && print $15."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Referrers'
    perl -n -e '/'"$LOGREGEX"'/ && print $14."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Forwarded IPs'
    perl -n -e '/'"$LOGREGEX"'/ && print $16."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Direct Client IP'
    perl -n -e '/'"$LOGREGEX"'/ && print $1."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Days'
    perl -n -e '/'"$LOGREGEX"'/ && print $2."/".$3."/".$4."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Hours'
    perl -n -e '/'"$LOGREGEX"'/ && print $2."/".$3."/".$4." ".$5."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Minutes'
    perl -n -e '/'"$LOGREGEX"'/ && print $2."/".$3."/".$4." ".$5.":".$6."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Seconds'
    perl -n -e '/'"$LOGREGEX"'/ && print $2."/".$3."/".$4." ".$5.":".$6.":".$7."\r\n"' <( cat "$@" ) | sort | uniq -c | sort -nr | head -n "$TOP"

    printHeader 'Top '"$TOP"' Response Times (secs)'
    perl -n -e '/'"$LOGREGEX"'/ && print $18."\n"' <( cat "$@" ) | xargs printf "%.0f\n" | sort -n | uniq -c | sort -nr | head -n "$TOP"
}

main "$@"`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.