-
Notifications
You must be signed in to change notification settings - Fork 0
/
artemis.py
69 lines (54 loc) · 1.62 KB
/
artemis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Artemis
~~~~~~
An app tp fetch metrics which uses celery backend for distributed task processing
__author__ = 'Utkarsh Sengar'
"""
from flask import Flask, request, jsonify
from tasks.fetch import perform_collection
import markdown
app = Flask(__name__)
"""
Home of the app, for now renders the markdown README file as html
"""
@app.route('/')
def index():
f = open("README.md", "r")
html = markdown.markdown(f.read())
f.close()
return html
"""
The primary endpoint which will be called with POST request, GET is also enabled to give correct error message if called by mistake
"""
@app.route('/fetch', methods=['POST', 'GET'])
def fetch():
if request.method == "POST":
list_of_nodes=request.form.getlist('node')
url_pattern=request.form['url']
if list_of_nodes and url_pattern:
perform_collection.delay(list_of_nodes, url_pattern)
result = jsonify(success="Request is being processed")
else:
result = jsonify(error="Invalid request")
else:
result = jsonify(error='Please try a POST request with: 1. List of nodes (eg: nodes=machine1.foo.com, machine2.foo.com), 2. URL Pattern of the endpoint (eg: url=metric)')
return result
"""
This is a sample endpoint to test the celery task
"""
@app.route('/metric', methods=['GET'])
def test_metric():
sample_response = {
"cpu":{
"core1":"80%",
"core2":"33%"
},
"mem":{
"used":"1234M",
"free":"6666M"
}
}
return jsonify(sample_response)
if __name__ == '__main__':
app.run()