Completed
Push — master ( 3dcd25...20576f )
by Nicolas
01:26
created

Plugin.get_alert()   C

Complexity

Conditions 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
dl 0
loc 16
rs 6.6666
c 2
b 0
f 0
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2016 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
import numbers
23
24
from glances.folder_list import FolderList as glancesFolderList
25
from glances.plugins.glances_plugin import GlancesPlugin
26
27
28
class Plugin(GlancesPlugin):
29
30
    """Glances folder plugin."""
31
32
    def __init__(self, args=None):
33
        """Init the plugin."""
34
        super(Plugin, self).__init__(args=args)
35
36
        # We want to display the stat in the curse interface
37
        self.display_curse = True
38
39
        # Init stats
40
        self.glances_folders = None
41
        self.reset()
42
43
    def get_key(self):
44
        """Return the key of the list."""
45
        return 'path'
46
47
    def reset(self):
48
        """Reset/init the stats."""
49
        self.stats = []
50
51
    def load_limits(self, config):
52
        """Load the foldered list from the config file, if it exists."""
53
        self.glances_folders = glancesFolderList(config)
54
55
    def update(self):
56
        """Update the foldered list."""
57
        # Reset the list
58
        self.reset()
59
60
        if self.input_method == 'local':
61
            # Folder list only available in a full Glances environment
62
            # Check if the glances_folder instance is init
63
            if self.glances_folders is None:
64
                return self.stats
65
66
            # Update the foldered list (result of command)
67
            self.glances_folders.update()
68
69
            # Put it on the stats var
70
            self.stats = self.glances_folders.get()
71
        else:
72
            pass
73
74
        return self.stats
75
76
    def get_alert(self, stat):
77
        """Manage limits of the folder list"""
78
79
        if not isinstance(stat['size'], numbers.Number):
80
            return 'DEFAULT'
81
        else:
82
            ret = 'OK'
83
84
        if stat['critical'] is not None and stat['size'] > int(stat['critical']) * 1000000:
85
            ret = 'CRITICAL'
86
        elif stat['warning'] is not None and stat['size'] > int(stat['warning']) * 1000000:
87
            ret = 'WARNING'
88
        elif stat['careful'] is not None and stat['size'] > int(stat['careful']) * 1000000:
89
            ret = 'CAREFUL'
90
91
        return ret
92
93
    def msg_curse(self, args=None):
94
        """Return the dict to display in the curse interface."""
95
        # Init the return message
96
        ret = []
97
98
        # Only process if stats exist and display plugin enable...
99
        if not self.stats or args.disable_folder:
100
            return ret
101
102
        # Build the string message
103
        # Header
104
        msg = '{}'.format('FOLDERS')
105
        ret.append(self.curse_add_line(msg, "TITLE"))
106
107
        # Data
108
        for i in self.stats:
109
            ret.append(self.curse_new_line())
110
            if len(i['path']) > 15:
111
                # Cut path if it is too long
112
                path = '_' + i['path'][-15 + 1:]
113
            else:
114
                path = i['path']
115
            msg = '{:<16} '.format(path)
116
            ret.append(self.curse_add_line(msg))
117
            try:
118
                msg = '{:>6}'.format(self.auto_unit(i['size']))
119
            except (TypeError, ValueError):
120
                msg = '{:>6}'.format(i['size'])
121
            ret.append(self.curse_add_line(msg, self.get_alert(i)))
122
123
        return ret
124