From 4458ac97ea958490ffa2e03e93b814fb1c30910e Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 23 Oct 2016 12:29:57 +0200 Subject: [PATCH] Export uptime to CSV (issue #890) --- glances/exports/glances_csv.py | 9 ++++++++- glances/exports/glances_export.py | 3 ++- glances/plugins/glances_uptime.py | 12 ++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/glances/exports/glances_csv.py b/glances/exports/glances_csv.py index 833c6da6..16f6b5c0 100644 --- a/glances/exports/glances_csv.py +++ b/glances/exports/glances_csv.py @@ -68,7 +68,8 @@ class Export(GlancesExport): plugins = stats.getAllPlugins() # Init data with timestamp (issue#708) - csv_header = ['timestamp'] + if self.first_line: + csv_header = ['timestamp'] csv_data = [time.strftime('%Y-%m-%d %H:%M:%S')] # Loop over available plugin @@ -90,6 +91,12 @@ class Export(GlancesExport): for fieldname in fieldnames) # Others lines: stats csv_data += itervalues(all_stats[i]) + elif isinstance(all_stats[i], (int, float, str)): + # First line: header + if self.first_line: + csv_header.append(str(plugin)) + # Others lines: stats + csv_data.append(str(all_stats[i])) # Export to CSV if self.first_line: diff --git a/glances/exports/glances_export.py b/glances/exports/glances_export.py index a0472810..96994395 100644 --- a/glances/exports/glances_export.py +++ b/glances/exports/glances_export.py @@ -64,7 +64,8 @@ class GlancesExport(object): 'system', 'uptime', 'sensors', - 'docker'] + 'docker', + 'uptime'] def get_item_key(self, item): """Return the value of the item 'key'.""" diff --git a/glances/plugins/glances_uptime.py b/glances/plugins/glances_uptime.py index 25217e87..f557a0e8 100644 --- a/glances/plugins/glances_uptime.py +++ b/glances/plugins/glances_uptime.py @@ -47,12 +47,20 @@ class Plugin(GlancesPlugin): self.align = 'right' # Init the stats + self.uptime = datetime.now() self.reset() def reset(self): """Reset/init the stats.""" self.stats = {} + def get_export(self): + """Overwrite the default export method. + + Export uptime in seconds. + """ + return self.uptime.seconds + @GlancesPlugin._check_decorator @GlancesPlugin._log_result_decorator def update(self): @@ -62,10 +70,10 @@ class Plugin(GlancesPlugin): if self.input_method == 'local': # Update stats using the standard system lib - uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time()) + self.uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time()) # Convert uptime to string (because datetime is not JSONifi) - self.stats = str(uptime).split('.')[0] + self.stats = str(self.uptime).split('.')[0] elif self.input_method == 'snmp': # Update stats using SNMP uptime = self.get_stats_snmp(snmp_oid=snmp_oid)['_uptime']