clean_no_alarm.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. number_of_arguments=$#
  2. directory=$1
  3. mode_argument=$2
  4. if [ "$number_of_arguments" -eq 0 ] || [ "$number_of_arguments" -gt 2 ]; then
  5. echo "Usage: clean_no_alarm.sh directory [-a]"
  6. echo
  7. echo "Remove wireshark files that don't contain Profinet alarm frames."
  8. echo
  9. echo "By default this script will examine the next-newest file in the directory."
  10. echo "Give the -a argument to scan all files in the directory."
  11. echo
  12. echo "Use the watch command to run this repeatedly:"
  13. echo " watch -n 10 ./clean_no_alarm.sh DIRECTORYNAME"
  14. echo
  15. echo "where the -n argument specifies the number of seconds between each invocation."
  16. echo
  17. echo "This tool uses the tshark program, which must be installed."
  18. echo
  19. echo "To record the files it is recommended to use the tcpdump utility to continuously"
  20. echo "save data into 10 MByte files:"
  21. echo " sudo tcpdump -i INTERFACENAME -C 10 -w recordingfile -K -n"
  22. echo "The directory that the files are saved into must have write permission for all:"
  23. echo " chmod o+w DIRECTORYNAME"
  24. # Handling both sourced and executed script
  25. return 1 2>/dev/null
  26. exit 1
  27. fi
  28. # Delete a wireshark pcap file if it doesn't contain any Profinet alarm frames
  29. examine_file () {
  30. full_path=$1
  31. echo "Examining the file: ${full_path}"
  32. file_description=`file ${full_path}`
  33. case "$file_description" in
  34. *capture*) ;;
  35. * ) echo " Not a wireshark file"; return ;;
  36. esac
  37. # Check if the file contains the Profinet frame ID for ALARM_PRIO_LOW
  38. number_of_alarm_frames=`tshark -r ${full_path} -T fields -e frame.number -Y "pn_rt.frame_id == 65025" | wc -l`
  39. if [ "$number_of_alarm_frames" -eq 0 ]; then
  40. echo " Deleting ${full_path} as it has no alarms"
  41. rm "$full_path"
  42. fi
  43. }
  44. if [ "$mode_argument" = "-a" ]
  45. then
  46. # Examine all files in directory
  47. for file in $directory/*
  48. do
  49. examine_file "$file"
  50. done
  51. else
  52. # Examine second newest file
  53. filename=`ls -1t $directory | sed -n '2p'`
  54. examine_file ${directory}/${filename}
  55. fi