Commit 45c6aa33 authored by 류은제's avatar 류은제
Browse files

Update app.py

parent 3caa7a16
from datetime import datetime from datetime import datetime
import pytz
import hashlib import hashlib
import hmac import hmac
import json import json
...@@ -156,6 +157,7 @@ def extract_utilization(): ...@@ -156,6 +157,7 @@ def extract_utilization():
agents=query_agent_list() agents=query_agent_list()
result_dict={} result_dict={}
item = 1
for agent in agents['agent_list']['items']: for agent in agents['agent_list']['items']:
...@@ -163,6 +165,9 @@ def extract_utilization(): ...@@ -163,6 +165,9 @@ def extract_utilization():
if agent['id'] == 'i-gs-gpu-1080ti-01': if agent['id'] == 'i-gs-gpu-1080ti-01':
break break
ag=agent['id']
#print(ag)
#print(type(ag))
#live's type is dict #live's type is dict
live = json.loads(agent['live_stat']) live = json.loads(agent['live_stat'])
...@@ -183,15 +188,26 @@ def extract_utilization(): ...@@ -183,15 +188,26 @@ def extract_utilization():
disk =live['node']['disk']['pct'] disk =live['node']['disk']['pct']
result_dict[agent['id']] ={'cpu':cpu, 'cuda':cuda, 'mem':mem, 'disk':disk} result_dict['item'+str(item)] ={ 'id':ag, 'cpu':cpu, 'cuda':cuda, 'mem':mem, 'disk':disk}
#result_dict[agent['id']] ={'cpu':cpu, 'cuda':cuda, 'mem':mem, 'disk':disk}
item+=1
#print("cpu_util : ",cpu/64,"%") #print("cpu_util : ",cpu/64,"%")
#print("memory_util : ", mem,"%") #print("memory_util : ", mem,"%")
#print("gpu_util : ", cuda/8,"%") #print("gpu_util : ", cuda/8,"%")
#print("disk_util : ", disk, "%") #print("disk_util : ", disk, "%")
# adding root key of the result_dict #current_time = str(datetime.now())
result_dict ={"agent":result_dict} kst = pytz.timezone('Asia/Seoul')
now = datetime.now(kst)
# ISO 8601 형식으로 출력
current_time=now.isoformat()
# adding time, root of the result_dict
result_dict['time']=current_time
result_dict ={"results":result_dict}
# convert type dict to json # convert type dict to json
result_json=json.dumps(result_dict) result_json=json.dumps(result_dict)
...@@ -211,18 +227,23 @@ app = Flask(__name__) ...@@ -211,18 +227,23 @@ app = Flask(__name__)
def myfunction_api(): def myfunction_api():
result = extract_utilization() result = extract_utilization()
return result
# json 결과값을 Content-type: application/json으로 반환하도록 jsonify 사용
return jsonify(result)
if __name__ == "__main__": if __name__ == "__main__":
app.run(host='0.0.0.0', port=31000, debug=True) app.run(host='0.0.0.0', port=31000, debug=True)
# api url # api url
# http://10.231.238.231:31000/api/getMonitoring # http://10.231.238.231:31000/api/getMonitoring
# api url 결과값값 예시
''' '''
{"agent": {"i-ai-1": {"cpu": 4.09, "cuda": 38.38, "mem": "4.54", "disk": "25.02"}, "i-ai-2": {"cpu": 5.22, "cuda": 99.62, "mem": "7.25", "disk": "8.44"}, "i-ai-3": {"cpu": 1.05, "cuda": 12.5, "mem": "1.93", "disk": "8.27"}, "i-ai-4": {"cpu": 2.92, "cuda": 87.62, "mem": "5.54", "disk": "8.35"}, "i-ai-5": {"cpu": 1.72, "cuda": 50.0, "mem": "8.65", "disk": "40.45"}, "i-ai-6": {"cpu": 0.09, "cuda": 0.0, "mem": "1.23", "disk": "87.7"}}} result (type: json)
"{\"results\": {\"item1\": {\"id\": \"i-ai-1\", \"cpu\": 4.08, \"cuda\": 53.25, \"mem\": \"4.76\", \"disk\": \"25.02\"}, \"item2\": {\"id\": \"i-ai-2\", \"cpu\": 5.27, \"cuda\": 45.25, \"mem\": \"7.41\", \"disk\": \"8.44\"}, \"item3\": {\"id\": \"i-ai-3\", \"cpu\": 1.41, \"cuda\": 20.62, \"mem\": \"4.13\", \"disk\": \"8.27\"}, \"item4\": {\"id\": \"i-ai-4\", \"cpu\": 4.16, \"cuda\": 37.5, \"mem\": \"5.46\", \"disk\": \"8.35\"}, \"item5\": {\"id\": \"i-ai-5\", \"cpu\": 1.08, \"cuda\": 33.88, \"mem\": \"8.98\", \"disk\": \"41.02\"}, \"item6\": {\"id\": \"i-ai-6\", \"cpu\": 0.12, \"cuda\": 0.0, \"mem\": \"1.15\", \"disk\": \"90.91\"}, \"time\": \"2023-03-06T19:38:45.710251+09:00\"}}"
''' '''
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment