Completed
Push — master ( bcff18...adb900 )
by Nicolas
01:15
created

glances.plugins.Plugin.get_alert()   C

Complexity

Conditions 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
dl 0
loc 16
rs 6.6666
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Folder plugin."""
21
22
from glances.folder_list import FolderList as glancesFolderList
23
from glances.plugins.glances_plugin import GlancesPlugin
24
25
26
class Plugin(GlancesPlugin):
27
28
    """Glances folder plugin."""
29
30
    def __init__(self, args=None):
31
        """Init the plugin."""
32
        super(Plugin, self).__init__(args=args)
33
34
        # We want to display the stat in the curse interface
35
        self.display_curse = True
36
37
        # Init stats
38
        self.glances_folders = None
39
        self.reset()
40
41
    def get_key(self):
42
        """Return the key of the list."""
43
        return 'path'
44
45
    def reset(self):
46
        """Reset/init the stats."""
47
        self.stats = []
48
49
    def load_limits(self, config):
50
        """Load the foldered list from the config file, if it exists."""
51
        self.glances_folders = glancesFolderList(config)
52
53
    def update(self):
54
        """Update the foldered list."""
55
        # Reset the list
56
        self.reset()
57
58
        if self.input_method == 'local':
59
            # Folder list only available in a full Glances environment
60
            # Check if the glances_folder instance is init
61
            if self.glances_folders is None:
62
                return self.stats
63
64
            # Update the foldered list (result of command)
65
            self.glances_folders.update()
66
67
            # Put it on the stats var
68
            self.stats = self.glances_folders.get()
69
        else:
70
            pass
71
72
        return self.stats
73
74
    def get_alert(self, stat):
75
        """Manage limits of the folder list"""
76
77
        if stat['size'] is None:
78
            return 'DEFAULT'
79
        else:
80
            ret = 'OK'
81
82
        if stat['critical'] is not None and stat['size'] > int(stat['critical']) * 1000000:
83
            ret = 'CRITICAL'
84
        elif stat['warning'] is not None and stat['size'] > int(stat['warning']) * 1000000:
85
            ret = 'WARNING'
86
        elif stat['careful'] is not None and stat['size'] > int(stat['careful']) * 1000000:
87
            ret = 'CAREFUL'
88
89
        return ret
90
91
    def msg_curse(self, args=None):
92
        """Return the dict to display in the curse interface."""
93
        # Init the return message
94
        ret = []
95
96
        # Only process if stats exist and display plugin enable...
97
        if not self.stats or args.disable_folder:
98
            return ret
99
100
        # Build the string message
101
        # Header
102
        msg = '{0}'.format('FOLDERS')
103
        ret.append(self.curse_add_line(msg, "TITLE"))
104
105
        # Data
106
        for i in self.stats:
107
            ret.append(self.curse_new_line())
108
            if len(i['path']) > 15:
109
                # Cut path if it is too long
110
                path = '_' + i['path'][-15 + 1:]
111
            else:
112
                path = i['path']
113
            msg = '{0:<16} '.format(path)
114
            ret.append(self.curse_add_line(msg))
115
            try:
116
                msg = '{0:>6}'.format(self.auto_unit(i['size']))
117
            except TypeError:
118
                msg = '{0:>6}'.format('?')
119
            ret.append(self.curse_add_line(msg, self.get_alert(i)))
120
121
        return ret
122