warnings.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/sh
  2. #
  3. # Usage:
  4. # tools/warnings.sh [node]
  5. # Output: for each node outputs its warnings and the number of times that
  6. # warning has ocurred. If the argument node is specified, it only shows
  7. # the warnings of that node.
  8. # Examples: tools/warnings.sh
  9. # tools/warnings.sh 000a
  10. # Environmental variables:
  11. # CHUTNEY_WARNINGS_IGNORE_EXPECTED: set to "true" to filter expected warnings
  12. # CHUTNEY_WARNINGS_SUMMARY: set to "true" to merge warnings from all instances
  13. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" ]; then
  14. # looks like a broken path: use the path to this tool instead
  15. TOOLS_PATH=`dirname "$0"`
  16. export CHUTNEY_PATH=`dirname "$TOOLS_PATH"`
  17. fi
  18. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  19. # looks like a relative path: make chutney path absolute
  20. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  21. fi
  22. # Get a working net path
  23. if [ ! -d "$CHUTNEY_DATA_DIR" ]; then
  24. # looks like a broken path: use the chutney path as a base
  25. export CHUTNEY_DATA_DIR="$CHUTNEY_PATH/net"
  26. fi
  27. if [ -d "$PWD/$CHUTNEY_DATA_DIR" ]; then
  28. # looks like a relative path: make chutney path absolute
  29. export CHUTNEY_DATA_DIR="$PWD/$CHUTNEY_DATA_DIR"
  30. fi
  31. show_warnings() {
  32. # Work out the file and filter settings
  33. if [ "$CHUTNEY_WARNINGS_SUMMARY" = true ]; then
  34. FILE="$1/*/$LOG_FILE"
  35. else
  36. FILE="$1/$LOG_FILE"
  37. fi
  38. if [ "$CHUTNEY_WARNINGS_IGNORE_EXPECTED" = true -a \
  39. -e "$IGNORE_FILE" ]; then
  40. CAT="grep -v -f"
  41. else
  42. CAT=cat
  43. IGNORE_FILE=
  44. fi
  45. # Silence any messages if we are in summary mode, and there are no warnings
  46. # must be kept in sync with the filter commands below
  47. if [ `$CAT $IGNORE_FILE $FILE | $SED_E "$FILTER" | wc -c` -eq 0 -a \
  48. "$CHUTNEY_WARNINGS_SUMMARY" = true ]; then
  49. ECHO_Q=true
  50. ECHO_A=true
  51. else
  52. # if there is output, always echo the detail message
  53. ECHO_A=echo
  54. fi
  55. # Give context to the warnings we're about to display
  56. if [ "$CHUTNEY_WARNINGS_SUMMARY" = true ]; then
  57. $ECHO_Q "${GREEN}Summary: `basename $1`${NC}"
  58. else
  59. $ECHO_Q "${GREEN}Node: `basename $1`${NC}"
  60. fi
  61. if [ "$CHUTNEY_WARNINGS_IGNORE_EXPECTED" = true -a \
  62. -e "$IGNORE_FILE" ]; then
  63. PERMANENT_DIR=`readlink -n "$1" || echo "$1"`
  64. $ECHO_A "${GREEN}Detail: chutney/tools/warnings.sh $PERMANENT_DIR${NC}"
  65. fi
  66. # Display the warnings, after filtering and counting occurrences
  67. # must be kept in sync with the filter commands above
  68. $CAT $IGNORE_FILE $FILE | $SED_E "$FILTER" | sort | uniq -c | \
  69. sed -e 's/^\s*//' -e "s/ *\([0-9][0-9]*\) *\(.*\)/${YELLOW}Warning:${NC} \2${YELLOW} Number: \1${NC}/"
  70. if [ "$CHUTNEY_WARNINGS_SUMMARY" != true ]; then
  71. $ECHO_Q ""
  72. fi
  73. }
  74. usage() {
  75. echo "Usage: $NAME [node]"
  76. exit 1
  77. }
  78. # Don't colour in log files
  79. if [ -t 1 ]; then
  80. NC=$(tput sgr0)
  81. YELLOW=$(tput setaf 3)
  82. GREEN=$(tput setaf 2)
  83. fi
  84. CHUTNEY="$CHUTNEY_PATH/chutney"
  85. NAME=$(basename "$0")
  86. DEST="$CHUTNEY_DATA_DIR/nodes"
  87. LOG_FILE=info.log
  88. # ignore warnings we expect to get every time chutney runs
  89. CHUTNEY_WARNINGS_IGNORE_EXPECTED=${CHUTNEY_WARNINGS_IGNORE_EXPECTED:-0}
  90. # don't put spaces in CHUTNEY_PATH or IGNORE_FILE
  91. IGNORE_FILE="$CHUTNEY_PATH/tools/ignore.warnings"
  92. # merge all log files into one before counting entries
  93. CHUTNEY_WARNINGS_SUMMARY=${CHUTNEY_WARNINGS_SUMMARY:-0}
  94. SED_E='sed -n -E'
  95. # Label errs as "Warning:", they're infrequent enough it doesn't matter
  96. FILTER='s/^.*\[(warn|err)\]//p'
  97. # use the --quiet setting from test-network.sh, if available
  98. ECHO_Q=${ECHO:-"echo"}
  99. [ -d "$DEST" ] || { echo "$NAME: no logs available"; exit 1; }
  100. if [ $# -eq 0 ];
  101. then
  102. if [ "$CHUTNEY_WARNINGS_SUMMARY" = true ]; then
  103. show_warnings "$DEST"
  104. exit 0
  105. fi
  106. for dir in "$DEST"/*;
  107. do
  108. [ -e "${dir}/$LOG_FILE" ] || continue
  109. show_warnings "$dir"
  110. done
  111. elif [ $# -eq 1 ];
  112. then
  113. [ -e "$DEST/$1/$LOG_FILE" ] || \
  114. { echo "$NAME: no log available"; exit 1; }
  115. show_warnings "$DEST/$1"
  116. else
  117. usage
  118. fi