Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mem-measurements v1.0.0 #124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions tools/mem-measurements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Script mem-measurements.sh v1.0.0

## Introduction

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.

## 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:

- 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:

<pre>
<code>
<i>output-dir</i>/data/data_YYYY-MM-DD_hh-mm-ss.dat
</code>
</pre>

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 a directory _install-dir_, e.g. `/opt/mem-measurements/`
2. Change the attributes to `500`
```
# chmod 500 mem-measurements.sh
```

3. Add the record in crontab:

<pre>
<code>
# crontab -e
20 * * * * <i>install-dir</i>/mem-measurements.sh 2>&1 | logger
</code>
</pre>

4. The directory _output-dir_/data/ will keep one week of data.
77 changes: 77 additions & 0 deletions tools/mem-measurements/mem-measurements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/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 default value for output directory
outputDataDir="/opt/mem-measurements"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattibf @adriancz
log-agent is installed in /opt/monasca-log-agent/ in customer environment and we can't create this directory. So please change this variable as /opt/monasca-log-agent/.


# 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 #
##############################

######### handle input params ##############
if [ $# -gt 1 ]; then
echo "ERROR: illegal number of parameters, expected format: $0 <output directory>"
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
{
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 {}