Fix CVS export [issue #348]

While I was there:
- complete overhaul of the CVS class
- fix open file issue w/ python3
- has_key() option was removed in python3
- usage simplified: there is no need to define the CVS filename, only the
  output folder, as for the HTML export
- update help, doc and man accordingly
- fix typos
This commit is contained in:
Alessio Sergi 2014-05-11 16:19:16 +02:00
parent f47dd40396
commit 2d0d5d6086
4 changed files with 65 additions and 54 deletions

View File

@ -125,7 +125,7 @@ td.option-group {
<p>This manual describes <em>Glances</em> version 1.7.6.</p>
<p>Copyright © 2012-2014 Nicolas Hennion &lt;<a class="reference external" href="mailto:nicolas&#64;nicolargo.com">nicolas&#64;nicolargo.com</a>&gt;</p>
<p>March 2014</p>
<p>May 2014</p>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first">Table of Contents</p>
<ul class="simple">
@ -158,7 +158,7 @@ td.option-group {
</ul>
</li>
<li><a class="reference internal" href="#api-documentation" id="id29">API documentation</a></li>
<li><a class="reference internal" href="#others-outputs" id="id30">Others outputs</a></li>
<li><a class="reference internal" href="#other-outputs" id="id30">Other outputs</a></li>
<li><a class="reference internal" href="#support" id="id31">Support</a></li>
</ul>
</div>
@ -231,8 +231,8 @@ just run on the server:</p>
<kbd><span class="option">-e</span></kbd></td>
<td>Enable sensors module (requires pysensors, Linux-only)</td></tr>
<tr><td class="option-group">
<kbd><span class="option">-f <var>FILE</var></span></kbd></td>
<td>Set the HTML output folder or CSV file</td></tr>
<kbd><span class="option">-f <var>FOLDER</var></span></kbd></td>
<td>Set the HTML or CSV output folder</td></tr>
<tr><td class="option-group">
<kbd><span class="option">-h</span></kbd></td>
<td>Display the help and exit</td></tr>
@ -636,23 +636,25 @@ is installed on your system then Glances displays the available percent capacity
<p>Glances uses a <a class="reference external" href="http://docs.python.org/2/library/simplexmlrpcserver.html">XML-RPC server</a> and can be used by another client software.</p>
<p>API documentation is available at <a class="reference external" href="https://github.com/nicolargo/glances/wiki/The-Glances-API-How-To">https://github.com/nicolargo/glances/wiki/The-Glances-API-How-To</a></p>
</div>
<div class="section" id="others-outputs">
<h1><a class="toc-backref" href="#id30">Others outputs</a></h1>
<p>Thanks to the -o option, it is possible to export statistics to CSV or HTML files.</p>
<div class="section" id="other-outputs">
<h1><a class="toc-backref" href="#id30">Other outputs</a></h1>
<p>Thanks to the -o (output) option, it is possible to export statistics to <cite>CSV</cite> or <cite>HTML</cite> files.</p>
<pre class="code console literal-block">
<span class="generic prompt">$</span> glances -o CSV -f /tmp/glances.csv
<span class="generic prompt">$</span> glances -o CSV -f /tmp
</pre>
<p>The CSV output file is named <tt class="docutils literal">glances.csv</tt>.</p>
<p>CSV files have on line per stats:</p>
<ul class="simple">
<li>load,load1,load5,load15</li>
<li>mem,total,used,free</li>
<li>swap,total,used,free</li>
<li>cpu,user,system,nice,idel,iowait,irq</li>
<li>cpu,user,system,nice,idle,iowait,irq</li>
</ul>
<pre class="code console literal-block">
<span class="generic prompt">$</span> glances -o HTML -f /tmp
</pre>
<p>Note: The css and img folders (glances/data) should be in the /tmp folder</p>
<p>The HTML output file is named <tt class="docutils literal">glances.html</tt>.</p>
<p><em>Note</em>: The css and img folders (glances/data) should be in the /tmp folder.</p>
</div>
<div class="section" id="support">
<h1><a class="toc-backref" href="#id31">Support</a></h1>

View File

@ -6,7 +6,7 @@ This manual describes *Glances* version 1.7.6.
Copyright © 2012-2014 Nicolas Hennion <nicolas@nicolargo.com>
March 2014
May 2014
.. contents:: Table of Contents
@ -84,7 +84,7 @@ Command-line options
-C FILE Path to the configuration file
-d Disable disk I/O module
-e Enable sensors module (requires pysensors, Linux-only)
-f FILE Set the HTML output folder or CSV file
-f FOLDER Set the HTML or CSV output folder
-h Display the help and exit
-m Disable mount module
-n Disable network module
@ -374,7 +374,7 @@ Three views are available for processes:
The processes summary line display:
* Tasks number (total number of processes)
* Threads number
* Threads number
* Running tasks number
* Sleeping tasks number
* Other tasks number (not running or sleeping)
@ -528,27 +528,31 @@ Glances uses a `XML-RPC server`_ and can be used by another client software.
API documentation is available at https://github.com/nicolargo/glances/wiki/The-Glances-API-How-To
Others outputs
==============
Other outputs
=============
Thanks to the -o option, it is possible to export statistics to CSV or HTML files.
Thanks to the -o (output) option, it is possible to export statistics to `CSV` or `HTML` files.
.. code-block:: console
$ glances -o CSV -f /tmp/glances.csv
$ glances -o CSV -f /tmp
The CSV output file is named ``glances.csv``.
CSV files have on line per stats:
- load,load1,load5,load15
- mem,total,used,free
- swap,total,used,free
- cpu,user,system,nice,idel,iowait,irq
- cpu,user,system,nice,idle,iowait,irq
.. code-block:: console
$ glances -o HTML -f /tmp
Note: The css and img folders (glances/data) should be in the /tmp folder
The HTML output file is named ``glances.html``.
*Note*: The css and img folders (glances/data) should be in the /tmp folder.
Support
=======

View File

@ -48,6 +48,9 @@ import collections
from base64 import b64decode
from hashlib import md5
# PY3?
is_PY3 = sys.version_info >= (3, 2)
# Somes libs depends of OS
is_BSD = sys.platform.find('bsd') != -1
is_Linux = sys.platform.startswith('linux')
@ -167,7 +170,7 @@ try:
# CSV output (optional)
import csv
except ImportError:
cvs_lib_tag = False
csv_lib_tag = False
else:
csv_lib_tag = True
@ -397,7 +400,7 @@ class Config:
for path in self.get_paths_list():
if os.path.isfile(path) and os.path.getsize(path) > 0:
try:
if sys.version_info >= (3, 2):
if is_PY3:
self.parser.read(path, encoding='utf-8')
else:
self.parser.read(path)
@ -4043,10 +4046,10 @@ class glancesHtml:
self.__refresh_time = refresh_time
# Set the HTML output file
self.html_file = os.path.join(html_path, html_filename)
self.html_file = os.path.realpath(os.path.join(html_path, html_filename))
# Get data path
data_path = os.path.join(work_path, 'data')
data_path = os.path.realpath(os.path.join(work_path, 'data'))
# Set the template path
template_path = os.path.join(data_path, 'html')
@ -4168,45 +4171,50 @@ class glancesCsv:
This class manages the CSV output
"""
def __init__(self, cvsfile="./glances.csv", refresh_time=1):
# Init refresh time
def __init__(self, csv_path, refresh_time=1):
csv_filename = 'glances.csv'
self.__refresh_time = refresh_time
# Set the ouput (CSV) path
# Set the CSV output file
csv_file = os.path.realpath(os.path.join(csv_path, csv_filename))
try:
self.__cvsfile_fd = open("%s" % cvsfile, "wb")
self.__csvfile = csv.writer(self.__cvsfile_fd)
if is_PY3:
self.__csvfile_fd = open(csv_file, 'w', newline='')
else:
self.__csvfile_fd = open(csv_file, 'wb')
self.__csvfile = csv.writer(self.__csvfile_fd)
except IOError as error:
print("Cannot create the output CSV file: ", error[1])
sys.exit(0)
print(_("Cannot create the CSV output file: %s") % error)
sys.exit(2)
def exit(self):
self.__cvsfile_fd.close()
self.__csvfile_fd.close()
def update(self, stats):
if stats.getCpu():
# Update CSV with the CPU stats
cpu = stats.getCpu()
# Standard CPU stats
l = ["cpu", cpu['user'], cpu['system'], cpu['nice']]
cpu_line = ["cpu", cpu['user'], cpu['system'], cpu['nice']]
# Extra CPU stats
for s in ('idle', 'iowait', 'irq'):
l.append(cpu[s] if cpu.has_key(s) else None)
self.__csvfile.writerow(l)
for key in ('idle', 'iowait', 'irq'):
cpu_line.append(cpu[key] if key in cpu.keys() else None)
self.__csvfile.writerow(cpu_line)
if stats.getLoad():
# Update CSV with the LOAD stats
load = stats.getLoad()
self.__csvfile.writerow(["load", load['min1'], load['min5'],
load['min15']])
self.__csvfile.writerow(
["load", load['min1'], load['min5'], load['min15']])
if stats.getMem() and stats.getMemSwap():
# Update CSV with the MEM stats
mem = stats.getMem()
self.__csvfile.writerow(["mem", mem['total'], mem['used'],
mem['free']])
self.__csvfile.writerow(
["mem", mem['total'], mem['used'], mem['free']])
memswap = stats.getMemSwap()
self.__csvfile.writerow(["swap", memswap['total'], memswap['used'],
memswap['free']])
self.__cvsfile_fd.flush()
self.__csvfile.writerow(
["swap", memswap['total'], memswap['used'], memswap['free']])
self.__csvfile_fd.flush()
class GlancesXMLRPCHandler(SimpleXMLRPCRequestHandler):
@ -4529,7 +4537,7 @@ def printSyntax():
print(_("\t-C FILE\t\tPath to the configuration file"))
print(_("\t-d\t\tDisable disk I/O module"))
print(_("\t-e\t\tEnable sensors module"))
print(_("\t-f FILE\t\tSet the HTML output folder or CSV file"))
print(_("\t-f FOLDER\tSet the HTML or CSV output folder"))
print(_("\t-h\t\tDisplay the help and exit"))
print(_("\t-m\t\tDisable mount module"))
print(_("\t-n\t\tDisable network module"))
@ -4693,8 +4701,7 @@ def main():
sensors_tag = True
elif opt in ("-y", "--hddtemp"):
hddtemp_tag = True
elif opt in ("-f", "--file"):
output_file = arg
elif opt in ("-f", "--folder"):
output_folder = arg
elif opt in ("-t", "--time"):
if not (arg.isdigit() and int(arg) > 0):
@ -4755,8 +4762,7 @@ def main():
try:
output_folder
except UnboundLocalError:
print(_("Error: HTML export (-o html) need "
"output folder definition (-f <folder>)"))
print(_("Error: HTML export (-o html) need output folder definition (-f <folder>)"))
sys.exit(2)
if csv_tag:
@ -4764,10 +4770,9 @@ def main():
print(_("Error: Need CSV library to export into CSV"))
sys.exit(2)
try:
output_file
output_folder
except UnboundLocalError:
print(_("Error: CSV export (-o csv) need "
"output file definition (-f <file>)"))
print(_("Error: CSV export (-o csv) need output folder definition (-f <folder>)"))
sys.exit(2)
# Catch CTRL-C
@ -4863,7 +4868,7 @@ def main():
# Init CSV output
if csv_tag:
csvoutput = glancesCsv(cvsfile=output_file,
csvoutput = glancesCsv(csv_path=output_folder,
refresh_time=refresh_time)
# Init screen

View File

@ -1,4 +1,4 @@
.TH glances 1 "March, 2014" "version 1.7.6" "USER COMMANDS"
.TH glances 1 "May, 2014" "version 1.7.6" "USER COMMANDS"
.SH NAME
glances \- A cross-platform curses-based monitoring tool
.SH SYNOPSIS
@ -34,8 +34,8 @@ Disable disk I/O module
.B \-e
Enable sensors module (requires pysensors, Linux-only)
.TP
.B \-f FILE
Set the HTML output folder or CSV file
.B \-f FOLDER
Set the HTML or CSV output folder
.TP
.B \-h
Display the help and exit