mirror of https://github.com/nicolargo/glances
Implement the CSV output
parent
b897168a9b
commit
59f82b788c
|
@ -74,10 +74,10 @@ class GlancesMain(object):
|
|||
password = ""
|
||||
# Output type (default is no output)
|
||||
# output_list = ['html', 'csv']
|
||||
html_tag = False
|
||||
csv_tag = False
|
||||
output_file = None
|
||||
output_folder = None
|
||||
# html_tag = False
|
||||
# csv_tag = False
|
||||
# output_file = None
|
||||
# output_folder = None
|
||||
|
||||
def __init__(self):
|
||||
"""Manage the command line arguments."""
|
||||
|
@ -96,6 +96,7 @@ class GlancesMain(object):
|
|||
help=_('connect to a Glances server by IPv4/IPv6 address or hostname'))
|
||||
parser.add_argument('-C', '--config', dest='conf_file',
|
||||
help=_('path to the configuration file'))
|
||||
# Enable or disable option on startup
|
||||
parser.add_argument('--disable-bold', action='store_false', default=True,
|
||||
dest='disable_bold', help=_('disable bold mode in the terminal'))
|
||||
parser.add_argument('--disable-diskio', action='store_true', default=False,
|
||||
|
@ -110,10 +111,10 @@ class GlancesMain(object):
|
|||
dest='disable_process', help=_('disable process module'))
|
||||
parser.add_argument('--disable-log', action='store_true', default=False,
|
||||
dest='disable_log', help=_('disable log module'))
|
||||
parser.add_argument('-f', '--file', dest='file',
|
||||
help=_('set the HTML output folder or CSV file'))
|
||||
parser.add_argument('-o', '--output', choices=['HTML', 'CSV'], dest='output',
|
||||
help=_("Define additional HTML or CSV output"))
|
||||
# CSV output feature
|
||||
parser.add_argument('--output-csv', default=None,
|
||||
dest='output_csv', help=_('export stats to a csv file'))
|
||||
# Server option
|
||||
parser.add_argument('-p', '--port', default=self.server_port, type=int, dest='port',
|
||||
help=_('define the client/server TCP port [default: %d]') % self.server_port)
|
||||
parser.add_argument('-P', '--password', dest='password_arg',
|
||||
|
@ -133,9 +134,10 @@ class GlancesMain(object):
|
|||
parser.add_argument('--snmp-auth', default='password', dest='snmp_auth',
|
||||
help=_('SNMP authentication key (only for SNMPv3)'))
|
||||
parser.add_argument('-t', '--time', default=self.refresh_time, type=int,
|
||||
dest='seconds', help=_('set refresh time in seconds [default: %s sec]') % self.refresh_time)
|
||||
dest='time', help=_('set refresh time in seconds [default: %s sec]') % self.refresh_time)
|
||||
parser.add_argument('-w', '--webserver', action='store_true', default=False,
|
||||
dest='webserver', help=_('run Glances in web server mode'))
|
||||
# Other options
|
||||
parser.add_argument('-1', '--percpu', action='store_true', default=False,
|
||||
dest='percpu', help=_('start Glances in per CPU mode'))
|
||||
|
||||
|
@ -182,11 +184,11 @@ class GlancesMain(object):
|
|||
self.client_tag = True
|
||||
self.server_ip = args.client
|
||||
|
||||
if args.output is not None:
|
||||
setattr(self, args.output.lower() + '_tag', True)
|
||||
if args.file is not None:
|
||||
output_file = args.file
|
||||
output_folder = args.file
|
||||
# if args.output is not None:
|
||||
# setattr(self, args.output.lower() + '_tag', True)
|
||||
# if args.file is not None:
|
||||
# output_file = args.file
|
||||
# output_folder = args.file
|
||||
# /!!!
|
||||
|
||||
# Interactive cmds like CLI args?
|
||||
|
|
|
@ -35,17 +35,14 @@ class GlancesStandalone():
|
|||
# Initial system informations update
|
||||
self.stats.update()
|
||||
|
||||
# Init HTML output
|
||||
# !!! TODO
|
||||
# if html_tag:
|
||||
# htmloutput = glancesHtml(html_path=output_folder,
|
||||
# refresh_time=refresh_time)
|
||||
|
||||
# Init CSV output
|
||||
# !!! TODO
|
||||
# if csv_tag:
|
||||
# csvoutput = glancesCsv(cvsfile=output_file,
|
||||
# refresh_time=refresh_time)
|
||||
if args.output_csv is not None:
|
||||
from glances.outputs.glances_csv import glancesCsv
|
||||
|
||||
self.csvoutput = glancesCsv(args=args)
|
||||
self.csv_tag = True
|
||||
else:
|
||||
self.csv_tag = False
|
||||
|
||||
# Init screen
|
||||
self.screen = glancesCurses(args=args)
|
||||
|
@ -61,18 +58,16 @@ class GlancesStandalone():
|
|||
# Update the screen
|
||||
self.screen.update(self.stats)
|
||||
|
||||
# Update the HTML output
|
||||
# !!! TODO
|
||||
# if html_tag:
|
||||
# htmloutput.update(stats)
|
||||
|
||||
# Update the CSV output
|
||||
# !!! TODO
|
||||
# if csv_tag:
|
||||
# csvoutput.update(stats)
|
||||
if self.csv_tag:
|
||||
self.csvoutput.update(self.stats)
|
||||
|
||||
def end(self):
|
||||
"""
|
||||
End of the CLI
|
||||
"""
|
||||
self.screen.end()
|
||||
|
||||
# Close the CSV file
|
||||
if self.csv_tag:
|
||||
self.csvoutput.exit()
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
# -*- 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/>.
|
||||
|
||||
# Import sys libs
|
||||
import os
|
||||
import sys
|
||||
try:
|
||||
import csv
|
||||
except ImportError:
|
||||
print('CSV module not found. Glances cannot extract to .csv file.')
|
||||
sys.exit(1)
|
||||
|
||||
# Import Glances libs
|
||||
from glances.core.glances_globals import is_py3
|
||||
|
||||
# List of stats enable in the CSV output
|
||||
csv_stats_list = [ 'cpu', 'load', 'mem', 'memswap' ]
|
||||
|
||||
|
||||
class glancesCsv:
|
||||
"""
|
||||
This class manages the CSV output
|
||||
"""
|
||||
|
||||
def __init__(self, args=None):
|
||||
|
||||
# Init refresh time
|
||||
self.__refresh_time = args.time
|
||||
|
||||
# CSV file name
|
||||
self.__csvfile_name = args.output_csv
|
||||
|
||||
# Set the CSV output file
|
||||
try:
|
||||
if is_py3:
|
||||
self.__csvfile_fd = open(self.__csvfile_name, 'w', newline='')
|
||||
else:
|
||||
self.__csvfile_fd = open(self.__csvfile_name, 'wb')
|
||||
self.__csvfile = csv.writer(self.__csvfile_fd, quoting=csv.QUOTE_NONE)
|
||||
except IOError as error:
|
||||
print(_("Cannot create the CSV output file: %s") % error)
|
||||
sys.exit(2)
|
||||
|
||||
print("{0}: {1}".format(_("Stats dumped in the CSV file"), self.__csvfile_name))
|
||||
|
||||
def exit(self):
|
||||
self.__csvfile_fd.close()
|
||||
|
||||
def update(self, stats):
|
||||
"""
|
||||
Update stats in the CSV output file
|
||||
"""
|
||||
|
||||
all_stats = stats.getAll()
|
||||
|
||||
# Loop over available plugin
|
||||
i = 0
|
||||
for p in stats.getAllPlugins():
|
||||
if p in csv_stats_list:
|
||||
# First line for comment: csv_comment
|
||||
csv_comment = [ '# ' + str(p) + ': ' + '|'.join(all_stats[i].keys()) ]
|
||||
self.__csvfile.writerow(csv_comment)
|
||||
# Second line for stats (CSV): csv_stats
|
||||
self.__csvfile.writerow(all_stats[i].values())
|
||||
i += 1
|
||||
self.__csvfile_fd.flush()
|
|
@ -149,7 +149,7 @@ class glancesCurses:
|
|||
self.term_window = self.screen.subwin(0, 0)
|
||||
|
||||
# Init refresh time
|
||||
self.__refresh_time = args.seconds
|
||||
self.__refresh_time = args.time
|
||||
|
||||
# Init process sort method
|
||||
self.args.process_sorted_by = 'auto'
|
||||
|
|
Loading…
Reference in New Issue