diff options
Diffstat (limited to 'roles/lava-elk-deps/templates/devicehealth.py.j2')
-rwxr-xr-x | roles/lava-elk-deps/templates/devicehealth.py.j2 | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/roles/lava-elk-deps/templates/devicehealth.py.j2 b/roles/lava-elk-deps/templates/devicehealth.py.j2 new file mode 100755 index 00000000..3821439a --- /dev/null +++ b/roles/lava-elk-deps/templates/devicehealth.py.j2 @@ -0,0 +1,78 @@ +#!/usr/bin/python3 + +import requests +import json +import argparse +from elasticsearch import Elasticsearch, TransportError +from datetime import datetime +import sys + +index_mappings = { + "settings": { + "index.mapping.ignore_malformed": True, + "dynamic": False, + "number_of_shards":1, + "number_of_replicas":0, + }, +} + +def main(): + + parser = argparse.ArgumentParser(description='Get device health status from LAVA server') + + parser.add_argument("-s", "--server", type=str, + help="LAVA server to get the info from") + parser.add_argument("-t", "--token", type=str, + help="User authentication token") + parser.add_argument("-u", "--username", type=str, + help="Username to use for query") + parser.add_argument("-e_host", "--elastic_host", type=str, + help="Elasticsearch server hostname", + default='localhost') + parser.add_argument("-e_port", "--elastic_port", type=str, + help="Elasticsearch server port", + default='9200') + parser.add_argument("-e_index", "--elastic_index", type=str, + help="Elasticsearch index to store results", + default='devicehealth') + parser.add_argument("-n", "--dry_run", action='store_true', + help="Do not execute elasticsearch commands", + default=False) + parser.add_argument("-d", "--debug", action='store_true', + help="Print debugging information", + default=False) + options = parser.parse_args() + + es = Elasticsearch([ + {'host': options.elastic_host, 'port': options.elastic_port}, + ]) + index_name = options.elastic_index + doc_type = options.elastic_index + if not options.debug: + print("Index set to: %s" % index_name) + if not options.dry_run: + es.indices.create(index=index_name, ignore=400, body=index_mappings) + + + devices_url = 'https://{}/api/v0.2/devices/'.format(options.server) + + next_page = devices_url + while next_page is not None: + r = requests.get(next_page) + + for device in r.json()["results"]: + doc = {'device':{}} + for key in device: + doc['device'][key] = device[key] + doc['server'] = options.server + doc['@timestamp'] = datetime.utcnow() + + if options.debug: + print("%s" % doc) + if not options.dry_run: + es.index(index=index_name,doc_type=doc_type, body=doc) + if "next" in r.json(): + next_page = r.json()["next"] + +if __name__ == '__main__': + main() |