summaryrefslogtreecommitdiff
path: root/overseerlib/plugins/ramfree_plugin.py
blob: b1161eee2db27a7e41bac6452018bf030ce33d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import cliapp

import overseerlib


class RamFreePlugin(cliapp.Plugin):

    def enable(self):
        self.app.add_subcommand('ramfree', self.ramfree,
                                arg_synopsis='THRESHOLD')

    def disable(self):
        pass

    def ramfree(self, args):
        if len(args) < 1:
            raise cliapp.AppException('ramfree requires a threshold to be '
                                      'specified')
        threshold = args[0]
        threshold_type = args[1] if len(args) == 2 else 'high'

        if threshold.isdigit() == False:
            threshold = overseerlib.app.human_to_bytes(threshold)

        with open('/proc/meminfo') as f:
            for line in f:
                if line.startswith('MemFree'):
                    free = line.split()[1]
        free = int(free) * 1024

        message = ''
        if free >= threshold:
            message+=('%s bytes of memory free\n' % free)
        if message != '':
            print message