Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Davide Madrisan <davide.madrisan@ext.soprasteria.com>
- Loading branch information
Davide Madrisan
committed
Feb 9, 2017
1 parent
fb84362
commit 9573312
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
SaltStack code snippets | ||
This module is a simple wrapper to status.meminfo | ||
Copyright (C) 2017 Davide Madrisan <davide.madrisan.gmail.com> | ||
''' | ||
|
||
# Define the module's virtual name | ||
__virtualname__ = 'memory' | ||
|
||
def _sizeof_fmt(tok, factor=1024.0, skip=1, suffix='B'): | ||
''' | ||
Divide 'tok' to its best unit and append it to the output. | ||
''' | ||
try: | ||
num = int(tok) | ||
except ValueError: | ||
return tok | ||
|
||
units = ['', 'k','M','G','T','P'] | ||
for unit in units[skip:]: | ||
if num < factor: | ||
return "%3.0f %s%s" % (num, unit, suffix) | ||
num /= factor | ||
return "%.0f %s%s" % (num, 'p', suffix) | ||
|
||
def usage(human_readable=True): | ||
''' | ||
Return some informations on physical memory and swap | ||
CLI Example: | ||
.. code-block:: bash | ||
salt '*' memory.usage | ||
''' | ||
fmt = lambda num: _sizeof_fmt(num) if human_readable else num | ||
|
||
mem_infos = __salt__['status.meminfo']() | ||
getvalue = lambda memtype: fmt(mem_infos.get(memtype, {}).get('value', 0)) | ||
|
||
memtypes = ['MemFree', 'MemTotal', 'SwapFree', 'SwapTotal'] | ||
infos = dict((memtype, getvalue(memtype)) for memtype in memtypes) | ||
|
||
try: | ||
infos['MemAvailable'] = getvalue('MemAvailable') | ||
except: | ||
pass | ||
|
||
return infos |