From 5bf7069053d33276e770341079e6a907613903de Mon Sep 17 00:00:00 2001 From: genevera Date: Wed, 13 Mar 2019 03:36:28 -0400 Subject: [PATCH 1/4] add date to index, unbreak object push --- glances/exports/glances_elasticsearch.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/glances/exports/glances_elasticsearch.py b/glances/exports/glances_elasticsearch.py index 703e35c5..4309cc69 100644 --- a/glances/exports/glances_elasticsearch.py +++ b/glances/exports/glances_elasticsearch.py @@ -57,6 +57,7 @@ class Export(GlancesExport): if not self.export_enable: return None + self.index='{}-{}'.format(self.index, datetime.today().strftime("%Y.%m.%d")) try: es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) except Exception as e: @@ -86,13 +87,16 @@ class Export(GlancesExport): for c, p in zip(columns, points): action = { "_index": self.index, - "_type": name, "_id": c, + "_type": "glances", "_source": { + "name": name, "value": str(p), + "type": "keyword", "timestamp": datetime.now() } } + logger.debug("Exporting the following object to elasticsearch: {}".format(action)) actions.append(action) # Write input to the ES index From b7041b3acbb263ea3839fdb5a4a48a7590a7fb15 Mon Sep 17 00:00:00 2001 From: genevera Date: Wed, 13 Mar 2019 04:50:48 -0400 Subject: [PATCH 2/4] use dynamic templating for values, clean up fields some more --- glances/exports/glances_elasticsearch.py | 43 +++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/glances/exports/glances_elasticsearch.py b/glances/exports/glances_elasticsearch.py index 4309cc69..42730b48 100644 --- a/glances/exports/glances_elasticsearch.py +++ b/glances/exports/glances_elasticsearch.py @@ -57,7 +57,38 @@ class Export(GlancesExport): if not self.export_enable: return None - self.index='{}-{}'.format(self.index, datetime.today().strftime("%Y.%m.%d")) + self.index='{}-{}'.format(self.index, datetime.utcnow().strftime("%Y.%m.%d")) + template_body = { + "mappings": { + "glances": { + "dynamic_templates": [ + { + "integers": { + "match_mapping_type": "long", + "mapping": { + "type": "integer" + } + } + }, + { + "strings": { + "match_mapping_type": "string", + "mapping": { + "type": "text", + "fields": { + "raw": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + ] + } + } + } + try: es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) except Exception as e: @@ -71,7 +102,7 @@ class Export(GlancesExport): except Exception as e: # Index did not exist, it will be created at the first write # Create it... - es.indices.create(self.index) + es.indices.create(index=self.index,body=template_body) else: logger.info("There is already %s entries in the ElasticSearch %s index" % (index_count, self.index)) @@ -85,15 +116,17 @@ class Export(GlancesExport): # https://elasticsearch-py.readthedocs.io/en/master/helpers.html actions = [] for c, p in zip(columns, points): + dtnow = datetime.utcnow() action = { "_index": self.index, "_id": c, "_type": "glances", "_source": { - "name": name, + "plugin": name, + "metric": c, "value": str(p), - "type": "keyword", - "timestamp": datetime.now() + "_timestamp": dtnow.timestamp(), + "timestamp": dtnow.isoformat() } } logger.debug("Exporting the following object to elasticsearch: {}".format(action)) From b24ad72557734cfde158b72f3863a2f7735137dc Mon Sep 17 00:00:00 2001 From: genevera Date: Wed, 13 Mar 2019 05:37:34 -0400 Subject: [PATCH 3/4] remove extra time field --- glances/exports/glances_elasticsearch.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/glances/exports/glances_elasticsearch.py b/glances/exports/glances_elasticsearch.py index 42730b48..b0bca602 100644 --- a/glances/exports/glances_elasticsearch.py +++ b/glances/exports/glances_elasticsearch.py @@ -125,8 +125,7 @@ class Export(GlancesExport): "plugin": name, "metric": c, "value": str(p), - "_timestamp": dtnow.timestamp(), - "timestamp": dtnow.isoformat() + "utc_datetime": dtnow.isoformat('T') } } logger.debug("Exporting the following object to elasticsearch: {}".format(action)) From 65e748a15c49e7db0f2cc3b4baa1c6b0e6d073cb Mon Sep 17 00:00:00 2001 From: genevera Date: Wed, 13 Mar 2019 05:42:16 -0400 Subject: [PATCH 4/4] make _id more precise --- glances/exports/glances_elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glances/exports/glances_elasticsearch.py b/glances/exports/glances_elasticsearch.py index b0bca602..6713a7ab 100644 --- a/glances/exports/glances_elasticsearch.py +++ b/glances/exports/glances_elasticsearch.py @@ -119,7 +119,7 @@ class Export(GlancesExport): dtnow = datetime.utcnow() action = { "_index": self.index, - "_id": c, + "_id": '{}.{}'.format(name,c), "_type": "glances", "_source": { "plugin": name,