Skip to content

Commit

Permalink
New module memory
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Madrisan <davide.madrisan@ext.soprasteria.com>
  • Loading branch information
Davide Madrisan committed Feb 9, 2017
1 parent fb84362 commit 9573312
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -107,6 +107,23 @@ myserver:
...
```

### memory
* __memory.usage__ - Return some informations on physical memory and swap
```bash
myserver:
----------
MemAvailable:
15 GB
MemFree:
11 GB
MemTotal:
15 GB
SwapFree:
2 GB
SwapTotal:
2 GB
```

### rpmpck, rpmlibpkg
* __rpmpck.buildtime__ - Return the build date and time
```bash
Expand Down
51 changes: 51 additions & 0 deletions memory.py
@@ -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

0 comments on commit 9573312

Please sign in to comment.