warnings.sh 4.1 KB

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