app.py 7.19 KB
Newer Older
류은제's avatar
류은제 committed
1
from datetime import datetime
류은제's avatar
류은제 committed
2
import pytz
류은제's avatar
류은제 committed
3
4
5
6
7
8
9
10
11
12
13
14
import hashlib
import hmac
import json
import textwrap

from dateutil.tz import tzutc
import requests
from yarl import URL
from config import configurations

# for API
from flask import Flask, request, jsonify
류은제's avatar
류은제 committed
15
#import logging
류은제's avatar
류은제 committed
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159


# == Configurations
cfg = configurations[1]

# Manager's endpoint
MANAGER_SERVICE_URL = URL(cfg['MANAGER_ENDPOINT'])
# Access key of a user (use superadmin's key to query every sessions)
ACCESS_KEY = cfg['ADMIN_ACCESS_KEY']
# Secret key of a user
SECRET_KEY = cfg['ADMIN_SECRET_KEY']
# API version. Refer to https://github.com/lablup/backend.ai-manager/blob/05f21a09bc6eff8678747f1199c48ed221be974a/src/ai/backend/manager/server.py#L82.
API_VERSION = cfg['API_VERSION']
# Hash algorithm to generate Backend.AI request signature
HASH_TYPE = cfg['HASH_TYPE']


def generate_signature(
    *,
    method,
    version,
    endpoint,
    date,
    rel_url,
    content_type,
    access_key,
    secret_key,
    hash_type,
):
   
    hash_type = hash_type
    hostname = endpoint._val.netloc
    body_hash = hashlib.new(hash_type, b'').hexdigest()

    sign_str = '{}\n{}\n{}\nhost:{}\ncontent-type:{}\nx-backendai-version:{}\n{}'.format(
        method.upper(),
        rel_url,
        date.isoformat(),
        hostname,
        content_type.lower(),
        version,
        body_hash
    )
    sign_bytes = sign_str.encode()

    sign_key = hmac.new(secret_key.encode(),
                        date.strftime('%Y%m%d').encode(), hash_type).digest()
    sign_key = hmac.new(sign_key, hostname.encode(), hash_type).digest()

    signature = hmac.new(sign_key, sign_bytes, hash_type).hexdigest()
    headers = {
        'Authorization': 'BackendAI signMethod=HMAC-{}, credential={}:{}'.format(
            hash_type.upper(),
            access_key,
            signature
        ),
    }
    return headers, signature



def query_agent_list():
    # == Generate authorization header.
    # HTTP method to use: POST, GET, HEAD, etc.
    method = 'POST'
    # Request date time. Should be UTC.
    date = datetime.now(tzutc())
    # Request url. To fetch compute session list, a client have to use
    # a GraphQL request. Manager serves every GraphQl requests by a single
    # URL, `/admin/graphql`.
    rel_url = '/admin/graphql'
    # Content type of the request.
    content_type = 'application/json'
    hdrs, _ = generate_signature(
        method=method,
        version=API_VERSION,
        endpoint=MANAGER_SERVICE_URL,
        date=date,
        rel_url=str(rel_url),
        content_type=content_type,
        access_key=ACCESS_KEY,
        secret_key=SECRET_KEY,
        hash_type=HASH_TYPE,
    )

    # == Build request headers
    headers = {
        # Client can set User-Agent string, which will be displayed in
        # Manager's log. This is optional, but good to know to categorize
        # the origin of the request.
        'User-Agent': 'Backed.AI Session Query Test',
        # Following three headers are mandatory.
        'Content-Type': content_type,
        'X-BackendAI-Version': API_VERSION,
        'Date': date.isoformat(),
    }
    headers.update(hdrs)

    # == Prepare session query (using GraphQL)
    # Fields to fetch. Refer following link to see possible fields.
    # https://github.com/lablup/backend.ai-manager/blob/main/src/ai/backend/manager/models/kernel.py#L687
    fields = [
        'id',
        'occupied_slots',
        'available_slots',
        'live_stat',
    ]
    # Query definition for compute_session_list can be found at
    # https://github.com/lablup/backend.ai-manager/blob/05f21a09bc6eff8678747f1199c48ed221be974a/src/ai/backend/manager/models/gql.py#L403
    query = '''
        query($limit:Int!, $offset:Int!) {
            agent_list(limit:$limit, offset:$offset) {
                items { $fields }
                total_count
            }
        }
    '''
    query = query.replace('$fields', ' '.join(fields))
    query = textwrap.dedent(query).strip()
    variables = {
      'limit': 50,
      'offset': 0,
      #'sess_id': '91edc3fd-8c23-45e1-93b6-d15a2a0cd888',
     
    }
    data = {
        'query': query,
        'variables': variables,
    }

    # Send requests and return response after parsing into JSON.
    r = requests.post(MANAGER_SERVICE_URL / rel_url[1:],
                      headers=headers, data=json.dumps(data))
    result = r.json()
    return result



def extract_utilization():

    #agents's type is dict
    agents=query_agent_list()

    result_dict={}
류은제's avatar
류은제 committed
160
    item = 1
류은제's avatar
류은제 committed
161
162
163
164
165
166
167

    for agent in agents['agent_list']['items']:


        if agent['id'] == 'i-gs-gpu-1080ti-01':
            break

류은제's avatar
류은제 committed
168
169
170
        ag=agent['id']
        #print(ag)
        #print(type(ag))
류은제's avatar
류은제 committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

        #live's type is dict
        live = json.loads(agent['live_stat'])

        #6400% 중에 pct %.
        cpu=live['node']['cpu_util']['pct']
        cpu=float(cpu)
        #100%기준으로 표시하며 소수점 둘째 자리까지만 사용 
        cpu=round(int(cpu)/64, 2)

        mem=live['node']['mem']['pct']

        #800% 중에 current %.
        cuda=live['node']['cuda_util']['current']
        cuda=float(cuda)
        #100%기준으로 표시하며 소수점 둘째 자리까지만 사용 
        cuda=round(int(cuda)/8, 2)

        disk =live['node']['disk']['pct']

류은제's avatar
류은제 committed
191
192
193
194
        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
류은제's avatar
류은제 committed
195
196
197
198
199
200

        #print("cpu_util : ",cpu/64,"%")
        #print("memory_util : ", mem,"%")
        #print("gpu_util : ", cuda/8,"%")
        #print("disk_util : ", disk, "%")

류은제's avatar
류은제 committed
201
202
203
204
205
206
207
208
209
210
    #current_time = str(datetime.now())
    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}
    
류은제's avatar
류은제 committed
211
212
213
214
215
216
217
218
219

    # convert type dict to json
    result_json=json.dumps(result_dict)

    print(result_json)
    return result_json


# == API Logger ==
류은제's avatar
류은제 committed
220
#logging.basicConfig(filename = "../logs/monitoringAPI.log", level = logging.DEBUG)
류은제's avatar
류은제 committed
221
222
223
224
225
226
227
228
229


# == restAPI ==
app = Flask(__name__)  
 
@app.route('/api/getMonitoring', methods=['GET'])
def myfunction_api():
    
    result = extract_utilization()
류은제's avatar
류은제 committed
230
231
232
233

    # json 결과값을 Content-type: application/json으로 반환하도록 jsonify 사용
    return jsonify(result)

류은제's avatar
류은제 committed
234
235
236
237
238


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=31000, debug=True)

류은제's avatar
류은제 committed
239
# api url
류은제's avatar
류은제 committed
240
241
# http://10.231.238.231:31000/api/getMonitoring

류은제's avatar
류은제 committed
242

류은제's avatar
류은제 committed
243
244
'''

류은제's avatar
류은제 committed
245
246
247
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\"}}"
류은제's avatar
류은제 committed
248

류은제's avatar
류은제 committed
249
'''