From d18c1ca7027b9e439304607d85470fb15ab7dec6 Mon Sep 17 00:00:00 2001 From: hmdhszd Date: Thu, 27 Jan 2022 11:05:35 +0330 Subject: [PATCH] added application files --- app/metrics.txt | 3 ++ app/python-node-exporter.py | 73 +++++++++++++++++++++++++++++++++++++ app/requirements.txt | 1 + 3 files changed, 77 insertions(+) create mode 100644 app/metrics.txt create mode 100644 app/python-node-exporter.py create mode 100644 app/requirements.txt diff --git a/app/metrics.txt b/app/metrics.txt new file mode 100644 index 0000000..d451eb1 --- /dev/null +++ b/app/metrics.txt @@ -0,0 +1,3 @@ + +Bitcoin_USD: 36141.7367 +Bitcoin_EUR: 32214.5033 \ No newline at end of file diff --git a/app/python-node-exporter.py b/app/python-node-exporter.py new file mode 100644 index 0000000..515f7ee --- /dev/null +++ b/app/python-node-exporter.py @@ -0,0 +1,73 @@ +import requests +import http.server +import socketserver +import time +from threading import Thread + + + +def GetDataFunc(): + while True : + + #---------------------------------------------------------------------------------- + #---------------------------------------------------------------------------------- + # in this part, i'll get some data from an API + # + # and put it into "metricsDictionary" + # + # you can remove this part and add your own script + # + # (put your key/value items into "metricsDictionary") + #---------------------------------------------------------------------------------- + #---------------------------------------------------------------------------------- + + service = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json",timeout=3) + service_json = service.json() + + metricsDictionary = {} + metricsDictionary["Bitcoin_USD: "] = service_json["bpi"]["USD"]["rate"].replace(",", "") + metricsDictionary["Bitcoin_EUR: "] = service_json["bpi"]["EUR"]["rate"].replace(",", "") + + #---------------------------------------------------------------------------------- + #---------------------------------------------------------------------------------- + # + # Now, I'll put all key/value items of metricsDictionary into metrics.txt file + # + #---------------------------------------------------------------------------------- + #---------------------------------------------------------------------------------- + + # This function will get data every 5 seconds + time.sleep(5) + + # open file to write new data + file_object = open('metrics.txt', 'w') + + for item in metricsDictionary : + file_object.write("\n") + file_object.write(item) + file_object.write(metricsDictionary[item]) + + +# run GetDataFunc func in the background +background_thread = Thread(target = GetDataFunc) +background_thread.start() + + + + + +#----------------run http server on port 9999----------------- + +def WebServer(): + PORT = 9999 + + Handler = http.server.SimpleHTTPRequestHandler + + with socketserver.TCPServer(("", PORT), Handler) as httpd: + print("serving at port", PORT) + httpd.serve_forever() + + +# run WebServer func in the background +background_thread = Thread(target = WebServer) +background_thread.start() diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file