memory-usage 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/sh
  2. #
  3. # memory-usage analyzer
  4. # Copyright (C) 2023 Distopico <distopico@riseup.net>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # Description:
  20. # --------------
  21. #
  22. # Check memory usage by process/program in an interval of time
  23. # and save the result data into a file, you can use the data with
  24. # gnuplot or other tools.
  25. #
  26. # Requirements:
  27. # --------------
  28. # - grep or pgrep
  29. # - procps (for `ps` command)
  30. #
  31. # Usage:
  32. #
  33. # ./memory-usage $(node | grep script.js)
  34. # Output: $ top.data with lines such as `1539689171 305m 2.0`, i.e. unix time - memory with % suffix - CPU load in %
  35. #
  36. # Type `-h` to more examples and available parameters
  37. function show_usage() {
  38. local script_name="$(basename $0)"
  39. echo "Usage: $script_name [-n name_file (default top.data)] [-d max_duration (default 180sec)] [-a max_attempts (default 1000)] <process_name>"
  40. echo
  41. echo 'Arguments:'
  42. echo -e '\t-n name_file\t name of the file to save cpu stats data'
  43. echo -e '\t-d max_duration\t duration time in seconds to watch the process'
  44. echo -e '\t-a max_attempts\t how many attempts to check if the process is running before exit'
  45. echo -e '\t-o overwrite\t overwrite output file'
  46. echo -e '\t-h show this menu'
  47. echo
  48. echo 'Example:'
  49. echo -e '\t./memory-usage -d 90 -a 100 -n top2.data $(node | grep script.js)'
  50. }
  51. function _pgrep() {
  52. if ! [[ -x "$(command -v pgrep)" ]]; then
  53. # Set pgrep fallback
  54. ps axf | grep "$1" | grep -v grep | awk '{print $1}'
  55. else
  56. pgrep $@
  57. fi
  58. }
  59. OPTIND=1
  60. while getopts ':a:d:n:o:h' opt; do
  61. case "$opt" in
  62. a ) _max_attempts="$OPTARG" ;;
  63. d ) _max_duration="$OPTARG" ;;
  64. n ) _name_output="$OPTARG" ;;
  65. o ) _overwrite="$OPTARG" ;;
  66. h ) show_usage; exit 0 ;;
  67. \? ) echo "Unknown option: -$OPTARG" >&2; exit 1 ;;
  68. : ) echo "Missing argument for option: -$OPTARG" >&2; exit 2 ;;
  69. esac
  70. done
  71. shift $((OPTIND - 1))
  72. [ "$1" = '--' ] && shift
  73. process_name=$1
  74. output="${_name_output-top.data}"
  75. max_attempts=${_max_duration-1000}
  76. max_time=${_max_attempts-180}
  77. should_overwrite="${_overwrite-false}"
  78. attempts=0
  79. [ -z "$process_name" ] && echo -e "Proccess is require\n" && show_usage && exit 1
  80. while [ $attempts -le $max_attempts ]
  81. do
  82. echo "_pgrep -lfa $process_name"
  83. pid_process=$(_pgrep -lfa $process_name | awk '{print $1}')
  84. if [ ! -z "$pid_process" ]
  85. then
  86. echo "Found PID $pid_process"
  87. break
  88. fi
  89. echo "Watting to process"
  90. attempts=$(( $attempts + 1 ))
  91. done
  92. if [ -z "$pid_process" ]
  93. then
  94. echo "Not PID found"
  95. unset _PROCESS_PID
  96. exit 0
  97. fi
  98. if [ -f "$output" ] && [ "$should_overwrite" != "true" ]; then
  99. echo $should_overwrite
  100. echo "Already exist file with name: $output, use [-o true] to overwrite"
  101. exit 0
  102. fi
  103. echo "Checking memory/cpu usage"
  104. seconds=0
  105. time=$((seconds+max_time))
  106. export _PROCESS_PID=$pid_process
  107. while [ $seconds -lt $time ]; do ps -o pmem,pcpu $_PROCESS_PID | grep -E '^\s[0-9]+' | awk -v now=$(date +%s) '{print now,$1,$2}' >> $output; done
  108. unset _PROCESS_PID
  109. echo "done"