From 02c59de942e81e106f500cba2602586f931f71c1 Mon Sep 17 00:00:00 2001 From: Martin Chacon Piza Date: Wed, 9 Sep 2020 22:50:45 +0200 Subject: [PATCH 1/4] Added mem-measurements v1.0.0 --- tools/mem-measurements/README.md | 55 +++++++++++++++++++ tools/mem-measurements/mem-measurements.sh | 62 ++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 tools/mem-measurements/README.md create mode 100755 tools/mem-measurements/mem-measurements.sh diff --git a/tools/mem-measurements/README.md b/tools/mem-measurements/README.md new file mode 100644 index 000000000..2f5778ecc --- /dev/null +++ b/tools/mem-measurements/README.md @@ -0,0 +1,55 @@ +# Script mem-measurements.sh v1.0.0 + +## Introduction + +This document describes the use of script `mem-measurements.sh v1.0.0` + +**Background**: + +The goal of this script is tracking the memory consumption through the execution of a set of memory debug commands. + +## Description + +The script calls the following commands: + +- cat /proc/meminfo +- free -h +- vmstat +- ps axo pmem,vsize,rss,pid,euser,cmd | sort -nr | head -n 1000 + +On each execution the script creates a dat file with the outputs of commands above: + +``` +/opt/mem-measurements/data/data_YYYY-MM-DD_hh-mm-ss.dat +``` + +By default the script keeps 168 dat files, which means one week of data if the script is executed once every hour. + +## Excecution + +The script should be executed by the **root** user and the attributes should be changed to `500` +``` +# chmod 500 mem-measurements.sh +# ./mem-measurements.sh +``` + +## Cron Job + +In order to set `mem-measurements.sh` as a cron job follow these instructions as the **root** user: + +In this example the script `mem-measurements.sh` will be executed once every hour at minute 20. + +1. Save the script `mem-measurements.sh` in the directory `/opt/mem-measurements/` +2. Change the attributes to `500` +``` +# chmod 500 mem-measurements.sh +``` + +3. Add the record in crontab: + +``` +# crontab -e +20 * * * * /opt/mem-measurements/mem-measurements.sh +``` + +4. The directory `/opt/mem-measurements/data/` will keep one week of data. diff --git a/tools/mem-measurements/mem-measurements.sh b/tools/mem-measurements/mem-measurements.sh new file mode 100755 index 000000000..0dc53a12f --- /dev/null +++ b/tools/mem-measurements/mem-measurements.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# (C) Copyright 2020 Fujitsu Enabling Software Technology GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +########################### +# mem-measurements v1.0.0 # +########################### + +# Uncomment for verbose logging in bash +#set -x + +# Set the output directory +outputDataDir="/opt/mem-measurements" + +# Default amount of max files to keep. +# With a cron job running each hour: 24h x 7d = 168 +maxAmountFiles=168 + +# Commands to be executed +declare -a metricNames=( + "cat /proc/meminfo" + "free -h" + "vmstat" + "ps axo pmem,vsize,rss,pid,euser,cmd | sort -nr | head -n 1000" + ) + +############################## +# Don't edit below this line # +############################## + +# Creating output data dir +mkdir -p $outputDataDir/data + +# Collecting memory status data +for metricName in "${metricNames[@]}"; do + { + echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + echo "+ COMMAND: $metricName" + echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + eval "$metricName" + echo -e "\n" + } &>> tmpdata.dat +done + +mv tmpdata.dat $outputDataDir/data/"data_$(date +%Y-%m-%d_%H-%M-%S).dat" + +# Removing old data files +((maxAmountFiles++)) +# shellcheck disable=SC2012 +ls $outputDataDir/data/data_*.dat -t | tail -n +"$maxAmountFiles" | xargs -I {} rm {} From a4277fefd0ad5e4e275c0021081908a2d26db6b1 Mon Sep 17 00:00:00 2001 From: mattibf Date: Thu, 17 Sep 2020 13:22:31 +0200 Subject: [PATCH 2/4] Update README.md Add info for crontab entry: write stdout/stderr to syslog/messages --- tools/mem-measurements/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/mem-measurements/README.md b/tools/mem-measurements/README.md index 2f5778ecc..46af6d950 100644 --- a/tools/mem-measurements/README.md +++ b/tools/mem-measurements/README.md @@ -49,7 +49,7 @@ In this example the script `mem-measurements.sh` will be executed once every hou ``` # crontab -e -20 * * * * /opt/mem-measurements/mem-measurements.sh +20 * * * * /opt/mem-measurements/mem-measurements.sh 2>&1 | logger ``` 4. The directory `/opt/mem-measurements/data/` will keep one week of data. From 2e27e247f6c1f9b1803508b0948fe5edb60b4a1c Mon Sep 17 00:00:00 2001 From: mattibf Date: Tue, 22 Sep 2020 10:38:53 +0200 Subject: [PATCH 3/4] Update README.md Extend with description of parameter "output directory" --- tools/mem-measurements/README.md | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/mem-measurements/README.md b/tools/mem-measurements/README.md index 46af6d950..18d6609bc 100644 --- a/tools/mem-measurements/README.md +++ b/tools/mem-measurements/README.md @@ -2,13 +2,20 @@ ## Introduction -This document describes the use of script `mem-measurements.sh v1.0.0` +This document describes the use of script `mem-measurements.sh v1.0.0`. +Please replace variables written in _italic_ with concrete values. **Background**: The goal of this script is tracking the memory consumption through the execution of a set of memory debug commands. -## Description +## Parameters + +The script can be called with one optional parameter _output-dir_. +If the script willl be called without parameter, the default value "/opt/mem-measurements" will be used. +_output-dir_ will be created, if it doesn't exist. + +## Description The script calls the following commands: @@ -19,9 +26,11 @@ The script calls the following commands: On each execution the script creates a dat file with the outputs of commands above: -``` -/opt/mem-measurements/data/data_YYYY-MM-DD_hh-mm-ss.dat -``` +
+
+output-dir/data/data_YYYY-MM-DD_hh-mm-ss.dat
+
+
By default the script keeps 168 dat files, which means one week of data if the script is executed once every hour. @@ -39,7 +48,7 @@ In order to set `mem-measurements.sh` as a cron job follow these instructions as In this example the script `mem-measurements.sh` will be executed once every hour at minute 20. -1. Save the script `mem-measurements.sh` in the directory `/opt/mem-measurements/` +1. Save the script `mem-measurements.sh` in a directory _install-dir_, e.g. `/opt/mem-measurements/` 2. Change the attributes to `500` ``` # chmod 500 mem-measurements.sh @@ -47,9 +56,11 @@ In this example the script `mem-measurements.sh` will be executed once every hou 3. Add the record in crontab: -``` +
+
 # crontab -e
-20 * * * * /opt/mem-measurements/mem-measurements.sh 2>&1 | logger
-```
+20 * * * * install-dir/mem-measurements.sh 2>&1 | logger
+
+
-4. The directory `/opt/mem-measurements/data/` will keep one week of data. +4. The directory _output-dir_/data/ will keep one week of data. From c94b5d99410eac03189a6e27a2e74f0c1bad1971 Mon Sep 17 00:00:00 2001 From: bandorf Date: Tue, 22 Sep 2020 10:47:01 +0200 Subject: [PATCH 4/4] Allow optional parameter for output directory Signed-off-by: bandorf --- tools/mem-measurements/mem-measurements.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/mem-measurements/mem-measurements.sh b/tools/mem-measurements/mem-measurements.sh index 0dc53a12f..934512b76 100755 --- a/tools/mem-measurements/mem-measurements.sh +++ b/tools/mem-measurements/mem-measurements.sh @@ -21,7 +21,7 @@ # Uncomment for verbose logging in bash #set -x -# Set the output directory +# Set default value for output directory outputDataDir="/opt/mem-measurements" # Default amount of max files to keep. @@ -40,9 +40,24 @@ declare -a metricNames=( # Don't edit below this line # ############################## +######### handle input params ############## +if [ $# -gt 1 ]; then + echo "ERROR: illegal number of parameters, expected format: $0 " + exit 1; +elif [ $# -eq 1 ]; then + outputDataDir=$1 +fi + # Creating output data dir mkdir -p $outputDataDir/data +if [ $? -ne 0 ]; then + echo "$0: ERROR: output directory $outputDataDir/data could not be created" + exit 1 +fi + +echo "$0: collecting information in directory $outputDataDir" + # Collecting memory status data for metricName in "${metricNames[@]}"; do {