提交 a7944969 编写于 作者: N nicolargo

Add Pygal option in the configuration file

上级 368cfd56
......@@ -315,6 +315,17 @@ all=False
# Exports
##############################################################################
[graph]
# Configuration for the --export graph option
# Set the path where the graph (.svg files) will be created
# Can be overwrite by the --graph-path command line option
path=/tmp
# See followings configuration keys definitions in the Pygal lib documentation
# http://pygal.org/en/stable/documentation/index.html
width=800
height=600
style=DarkStyle
[influxdb]
# Configuration for the --export influxdb option
# https://influxdb.com/
......
......@@ -20,7 +20,7 @@
"""Graph exporter interface class."""
from pygal import DateTimeLine
from pygal.style import DarkStyle
import pygal.style
import sys
import os
import tempfile
......@@ -39,25 +39,37 @@ class Export(GlancesExport):
"""Init the export IF."""
super(Export, self).__init__(config=config, args=args)
# Graph export folder path
self._graph_path = args.export_graph_path
# Load the Graph configuration file section (is exists)
self.export_enable = self.load_conf('graph',
options=['path',
'width',
'height',
'style'])
# Manage options (command line arguments overwrite configuration file)
self.path = args.export_graph_path or self.path
self.width = int(getattr(self, 'width', 800))
self.height = int(getattr(self, 'height', 600))
self.style = getattr(pygal.style,
getattr(self, 'style', 'DarkStyle'),
pygal.style.DarkStyle)
# Create export folder
try:
os.makedirs(self._graph_path)
os.makedirs(self.path)
except OSError as e:
if e.errno != errno.EEXIST:
logger.critical("Cannot create the Graph output folder {} ({})".format(self._graph_path, e))
logger.critical("Cannot create the Graph output folder {} ({})".format(self.path, e))
sys.exit(2)
# Check if output folder is writeable
try:
tempfile.TemporaryFile(dir=self._graph_path)
tempfile.TemporaryFile(dir=self.path)
except OSError as e:
logger.critical("Graph output folder {} is not writeable".format(self._graph_path))
logger.critical("Graph output folder {} is not writeable".format(self.path))
sys.exit(2)
logger.info("Graphs will be created in the folder {}".format(self._graph_path))
logger.info("Graphs will be created in the folder {}".format(self.path))
logger.info("Graphs are created when 'g' key is pressed")
def exit(self):
......@@ -75,7 +87,7 @@ class Export(GlancesExport):
if plugin_name in self.plugins_to_export():
self.export(plugin_name, plugin.get_export_history())
logger.info("Graphs created in the folder {}".format(self._graph_path))
logger.info("Graphs created in the folder {}".format(self.path))
self.args.generate_graph = False
def export(self, title, data):
......@@ -91,13 +103,24 @@ class Export(GlancesExport):
...
]
}
Return:
* True if the graph have been generated
* False if the graph have not been generated
"""
if data == {}:
return False
chart = DateTimeLine(title=title.capitalize(),
style=DarkStyle,
width=self.width,
height=self.height,
style=self.style,
show_dots=False,
legend_at_bottom=True,
x_label_rotation=20,
x_value_formatter=lambda dt: dt.strftime('%Y/%m/%d %H:%M:%S'))
for k, v in iteritems(data):
chart.add(k, v)
chart.render_to_file(os.path.join(self._graph_path,
chart.render_to_file(os.path.join(self.path,
title + '.svg'))
return True
......@@ -44,7 +44,6 @@ snmp_oid = {'default': {'user': '1.3.6.1.4.1.2021.11.9.0',
# Define the history items list
# - 'name' define the stat identifier
# - 'y_unit' define the Y label
# All items in this list will be historised if the --enable-history tag is set
items_history_list = [{'name': 'user',
'description': 'User CPU usage',
'y_unit': '%'},
......
......@@ -27,7 +27,6 @@ import psutil
# Define the history items list
# All items in this list will be historised if the --enable-history tag is set
items_history_list = [{'name': 'read_bytes',
'description': 'Bytes read per second',
'y_unit': 'B/s'},
......
......@@ -22,6 +22,14 @@
from glances.cpu_percent import cpu_percent
from glances.plugins.glances_plugin import GlancesPlugin
# Define the history items list
items_history_list = [{'name': 'user',
'description': 'User CPU usage',
'y_unit': '%'},
{'name': 'system',
'description': 'System CPU usage',
'y_unit': '%'}]
class Plugin(GlancesPlugin):
"""Glances per-CPU plugin.
......@@ -32,7 +40,8 @@ class Plugin(GlancesPlugin):
def __init__(self, args=None):
"""Init the plugin."""
super(Plugin, self).__init__(args=args)
super(Plugin, self).__init__(args=args,
items_history_list=items_history_list)
# We want to display the stat in the curse interface
self.display_curse = True
......
......@@ -170,7 +170,7 @@ class GlancesPlugin(object):
# interface)
for l in self.stats:
self.stats_history.add(
l[item_name] + '_' + i['name'],
str(l[item_name]) + '_' + i['name'],
l[i['name']],
description=i['description'],
history_max_size=self._limits['history_size'])
......
......@@ -22,7 +22,19 @@
from glances.processes import glances_processes
from glances.plugins.glances_plugin import GlancesPlugin
# Note: history items list is not compliant with process count
# Define the history items list
items_history_list = [{'name': 'total',
'description': 'Total number of processes',
'y_unit': ''},
{'name': 'running',
'description': 'Total number of running processes',
'y_unit': ''},
{'name': 'sleeping',
'description': 'Total number of sleeping processes',
'y_unit': ''},
{'name': 'thread',
'description': 'Total number of threads',
'y_unit': ''}]
class Plugin(GlancesPlugin):
......@@ -33,7 +45,8 @@ class Plugin(GlancesPlugin):
def __init__(self, args=None):
"""Init the plugin."""
super(Plugin, self).__init__(args=args)
super(Plugin, self).__init__(args=args,
items_history_list=items_history_list)
# We want to display the stat in the curse interface
self.display_curse = True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册