This file is indexed.

/usr/lib/python3/dist-packages/glances/outputs/glances_history.py is in glances 2.1.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""History class."""

# Import system lib
import os
import tempfile

# Import Glances lib
from glances.core.glances_globals import logger

# Import specific lib
try:
    from matplotlib import __version__ as matplotlib_version
    import matplotlib.pyplot as plt
    import matplotlib.dates as dates
except:
    matplotlib_check = False
    logger.warning('Can not load Matplotlib library. Please install it using "pip install matplotlib"')
else:
    matplotlib_check = True
    logger.info('Load Matplotlib version %s' % matplotlib_version)


class GlancesHistory(object):

    """This class define the object to manage stats history"""

    def __init__(self, output_folder=tempfile.gettempdir()):
        # !!! MINUS: matplotlib footprint (mem/cpu) => Fork process ?
        # !!! MINUS: Mem used to store history
        # !!! TODO: sampling before graph => Usefull ?
        # !!! TODO: do not display first two point (glances is running)
        # !!! TODO: replace /tmp by a cross platform way to get /tmp folder
        self.output_folder = output_folder

    def get_output_folder(self):
        """Return the output folder where the graph are generated"""
        return self.output_folder

    def graph_enabled(self):
        """Return True if Glances can generaate history graphs"""
        return matplotlib_check

    def reset(self, stats):
        """
        Reset all the history
        """
        if not self.graph_enabled():
            return False
        for p in stats.getAllPlugins():
            h = stats.get_plugin(p).get_stats_history()
            if h is not None:
                stats.get_plugin(p).reset_stats_history()
        return True

    def generate_graph(self, stats):
        """
        Generate graphs from plugins history
        """
        if not self.graph_enabled():
            return False

        for p in stats.getAllPlugins():
            h = stats.get_plugin(p).get_stats_history()
            if h is not None:
                # Build the graph
                # fig = plt.figure(dpi=72)
                ax = plt.subplot(1, 1, 1)

                # Label
                plt.title("%s stats" % p)
                
                handles = []
                for i in stats.get_plugin(p).get_items_history_list():
                    handles.append(plt.Rectangle((0, 0), 1, 1, fc=i['color'], ec=i['color'], linewidth=1))
                labels = [i['name'] for i in stats.get_plugin(p).get_items_history_list()]
                plt.legend(handles, labels, loc=1, prop={'size':9})
                formatter = dates.DateFormatter('%H:%M:%S')
                ax.xaxis.set_major_formatter(formatter)
                # ax.set_ylabel('%')

                # Draw the stats
                for i in stats.get_plugin(p).get_items_history_list():
                    ax.plot_date(h['date'], h[i['name']], 
                                 i['color'], 
                                 label='%s' % i['name'], 
                                 xdate=True, ydate=False)

                # Save and display
                plt.savefig(os.path.join(self.output_folder, 'glances_%s.png' % p), dpi=72)
                # plt.show()

        return True