Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
0be5c24a
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0be5c24a
编写于
8月 11, 2019
作者:
N
nicolargo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make CSV export append instead of replace #1525
上级
7208bd3d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
61 addition
and
9 deletion
+61
-9
docs/gw/csv.rst
docs/gw/csv.rst
+12
-2
glances/exports/glances_csv.py
glances/exports/glances_csv.py
+47
-7
glances/main.py
glances/main.py
+2
-0
未找到文件。
docs/gw/csv.rst
浏览文件 @
0be5c24a
...
...
@@ -11,5 +11,15 @@ It's possible to export stats to a CSV file.
CSV file description:
- Stats description (first line)
- Stats (other lines)
- first line: Stats description (header)
- others lines: Stats (data)
By default, data will be append any existing CSV file.
If the header did not match with a previous one, an error is logged.
The --export-csv-overwrite tag should be used if you want to delete the existing CSV file when Glances starts.
It is possible to remove some exported data using the --disable-plugin tag:
$ glances --export csv --export-csv-file /tmp/glances.csv --disable-plugin load,swap
glances/exports/glances_csv.py
浏览文件 @
0be5c24a
...
...
@@ -19,6 +19,7 @@
"""CSV interface class."""
import
os.path
import
csv
import
sys
import
time
...
...
@@ -40,11 +41,27 @@ class Export(GlancesExport):
self
.
csv_filename
=
args
.
export_csv_file
# Set the CSV output file
# (see https://github.com/nicolargo/glances/issues/1525)
if
not
os
.
path
.
isfile
(
self
.
csv_filename
)
or
args
.
export_csv_overwrite
:
# File did not exist, create it
file_mode
=
'w'
self
.
old_header
=
None
else
:
# A CSV file already exit, append new data
file_mode
=
'a'
# Header will be check later
# Get the existing one
try
:
self
.
csv_file
=
open_csv_file
(
self
.
csv_filename
,
'r'
)
reader
=
csv
.
reader
(
self
.
csv_file
)
except
IOError
as
e
:
logger
.
critical
(
"Cannot open existing CSV file: {}"
.
format
(
e
))
sys
.
exit
(
2
)
self
.
old_header
=
next
(
reader
,
None
)
self
.
csv_file
.
close
()
try
:
if
PY3
:
self
.
csv_file
=
open
(
self
.
csv_filename
,
'w'
,
newline
=
''
)
else
:
self
.
csv_file
=
open
(
self
.
csv_filename
,
'wb'
)
self
.
csv_file
=
open_csv_file
(
self
.
csv_filename
,
file_mode
)
self
.
writer
=
csv
.
writer
(
self
.
csv_file
)
except
IOError
as
e
:
logger
.
critical
(
"Cannot create the CSV file: {}"
.
format
(
e
))
...
...
@@ -91,8 +108,31 @@ class Export(GlancesExport):
csv_data
+=
itervalues
(
all_stats
[
plugin
])
# Export to CSV
# Manage header
if
self
.
first_line
:
self
.
writer
.
writerow
(
csv_header
)
if
self
.
old_header
is
None
:
# New file, write the header on top on the CSV file
self
.
writer
.
writerow
(
csv_header
)
# File already exist, check if header are compatible
if
self
.
old_header
!=
csv_header
:
# Header are differents, log an error and do not write data
logger
.
error
(
"Cannot append data to existing CSV file. Headers are differents."
)
logger
.
debug
(
"Old header: {}"
.
format
(
self
.
old_header
))
logger
.
debug
(
"New header: {}"
.
format
(
csv_header
))
else
:
# Header are equals, ready to write data
self
.
old_header
=
None
# Only do this once
self
.
first_line
=
False
self
.
writer
.
writerow
(
csv_data
)
self
.
csv_file
.
flush
()
# Manage data
if
self
.
old_header
is
None
:
self
.
writer
.
writerow
(
csv_data
)
self
.
csv_file
.
flush
()
def
open_csv_file
(
file_name
,
file_mode
):
if
PY3
:
csv_file
=
open
(
file_name
,
file_mode
,
newline
=
''
)
else
:
csv_file
=
open
(
file_name
,
file_mode
+
'b'
)
return
csv_file
glances/main.py
浏览文件 @
0be5c24a
...
...
@@ -169,6 +169,8 @@ Examples of use:
default
=
'./glances.csv'
,
dest
=
'export_csv_file'
,
help
=
'file path for CSV exporter'
)
parser
.
add_argument
(
'--export-csv-overwrite'
,
action
=
'store_true'
,
default
=
False
,
dest
=
'export_csv_overwrite'
,
help
=
'overwrite existing CSV file'
)
parser
.
add_argument
(
'--export-json-file'
,
default
=
'./glances.json'
,
dest
=
'export_json_file'
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录