Răsfoiți Sursa

暴露出宿主机性能API接口

James Iter 9 ani în urmă
părinte
comite
4d4f1a8843
5 a modificat fișierele cu 50 adăugiri și 45 ștergeri
  1. 4 4
      api/host.py
  2. 29 29
      api/host_performance.py
  3. 12 12
      api_route_table.py
  4. 4 0
      main.py
  5. 1 0
      models/rules.py

+ 4 - 4
api/host.py

@@ -64,7 +64,7 @@ def r_get(ids):
             if db.r.hexists(app.config['hosts_info'], _id):
                 v = json.loads(db.r.hget(app.config['hosts_info'], _id))
                 v = alive_check(v)
-                v['uuid'] = _id
+                v['node_id'] = _id
                 ret['data'] = v
 
         else:
@@ -72,7 +72,7 @@ def r_get(ids):
                 if db.r.hexists(app.config['hosts_info'], _id):
                     v = json.loads(db.r.hget(app.config['hosts_info'], _id))
                     v = alive_check(v)
-                    v['uuid'] = _id
+                    v['node_id'] = _id
                     ret['data'].append(v)
 
         return ret
@@ -91,7 +91,7 @@ def r_get_by_filter():
         for k, v in db.r.hgetall(app.config['hosts_info']).items():
             v = json.loads(v)
             v = alive_check(v)
-            v['uuid'] = k
+            v['node_id'] = k
             ret['data'].append(v)
 
         return ret
@@ -119,7 +119,7 @@ def r_content_search():
             v = json.loads(v)
             if -1 != v['hostname'].find(keyword):
                 v = alive_check(v)
-                v['uuid'] = k
+                v['node_id'] = k
                 ret['data'].append(v)
 
         return ret

+ 29 - 29
api/host_performance.py

@@ -49,15 +49,15 @@ def r_disk_usage_io_get_by_filter():
     return host_disk_usage_io.get_by_filter()
 
 
-def get_performance_data(uuid, uuid_field, the_class=None, granularity='hour'):
+def get_performance_data(node_id, the_class=None, granularity='hour'):
 
     args_rules = [
-        Rules.UUID.value,
+        Rules.NODE_ID.value,
     ]
 
     try:
-        ji.Check.previewing(args_rules, {'uuid': uuid})
-        uuids_str = ':'.join([uuid_field, 'in', uuid])
+        ji.Check.previewing(args_rules, {'node_id': node_id})
+        node_ids_str = 'node_id:in:' + node_id.__str__()
 
         ret = dict()
         ret['state'] = ji.Common.exchange_state(20000)
@@ -81,7 +81,7 @@ def get_performance_data(uuid, uuid_field, the_class=None, granularity='hour'):
         else:
             pass
 
-        filter_str = ';'.join([uuids_str, 'timestamp:gt:' + _boundary.__str__()])
+        filter_str = ';'.join([node_ids_str, 'timestamp:gt:' + _boundary.__str__()])
 
         _rows, _rows_count = the_class.get_by_filter(
             offset=0, limit=max_limit, order_by='id', order='asc', filter_str=filter_str)
@@ -143,62 +143,62 @@ def get_performance_data(uuid, uuid_field, the_class=None, granularity='hour'):
 
 
 @Utils.dumps2response
-def r_cpu_memory_last_hour(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostCPUMemory, granularity='hour')
+def r_cpu_memory_last_hour(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='hour')
 
 
 @Utils.dumps2response
-def r_cpu_memory_last_six_hours(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostCPUMemory, granularity='six_hours')
+def r_cpu_memory_last_six_hours(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='six_hours')
 
 
 @Utils.dumps2response
-def r_cpu_memory_last_day(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostCPUMemory, granularity='day')
+def r_cpu_memory_last_day(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='day')
 
 
 @Utils.dumps2response
-def r_cpu_memory_last_seven_days(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostCPUMemory, granularity='seven_days')
+def r_cpu_memory_last_seven_days(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostCPUMemory, granularity='seven_days')
 
 
 @Utils.dumps2response
-def r_traffic_last_hour(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostTraffic, granularity='hour')
+def r_traffic_last_hour(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostTraffic, granularity='hour')
 
 
 @Utils.dumps2response
-def r_traffic_last_six_hours(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostTraffic, granularity='six_hours')
+def r_traffic_last_six_hours(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostTraffic, granularity='six_hours')
 
 
 @Utils.dumps2response
-def r_traffic_last_day(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostTraffic, granularity='day')
+def r_traffic_last_day(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostTraffic, granularity='day')
 
 
 @Utils.dumps2response
-def r_traffic_last_seven_days(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostTraffic, granularity='seven_days')
+def r_traffic_last_seven_days(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostTraffic, granularity='seven_days')
 
 
 @Utils.dumps2response
-def r_disk_usage_io_last_hour(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostDiskUsageIO, granularity='hour')
+def r_disk_usage_io_last_hour(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostDiskUsageIO, granularity='hour')
 
 
 @Utils.dumps2response
-def r_disk_usage_io_last_six_hours(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostDiskUsageIO, granularity='six_hours')
+def r_disk_usage_io_last_six_hours(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostDiskUsageIO, granularity='six_hours')
 
 
 @Utils.dumps2response
-def r_disk_usage_io_last_day(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostDiskUsageIO, granularity='day')
+def r_disk_usage_io_last_day(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostDiskUsageIO, granularity='day')
 
 
 @Utils.dumps2response
-def r_disk_usage_io_last_seven_days(uuid):
-    return get_performance_data(uuid=uuid, uuid_field='host_uuid', the_class=HostDiskUsageIO, granularity='seven_days')
+def r_disk_usage_io_last_seven_days(node_id):
+    return get_performance_data(node_id=node_id, the_class=HostDiskUsageIO, granularity='seven_days')
 
 

+ 12 - 12
api_route_table.py

@@ -143,40 +143,40 @@ add_rule_api(host_performance.blueprint, '/traffic', api_func='host_performance.
              methods=['GET'])
 add_rule_api(host_performance.blueprint, '/disk_io', api_func='host_performance.r_disk_usage_io_get_by_filter',
              methods=['GET'])
-add_rule_api(host_performance.blueprint, '/cpu_memory/last_hour/<uuid>',
+add_rule_api(host_performance.blueprint, '/cpu_memory/last_hour/<node_id>',
              api_func='host_performance.r_cpu_memory_last_hour', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/cpu_memory/last_six_hours/<uuid>',
+add_rule_api(host_performance.blueprint, '/cpu_memory/last_six_hours/<node_id>',
              api_func='host_performance.r_cpu_memory_last_six_hours', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/cpu_memory/last_day/<uuid>',
+add_rule_api(host_performance.blueprint, '/cpu_memory/last_day/<node_id>',
              api_func='host_performance.r_cpu_memory_last_day', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/cpu_memory/last_seven_days/<uuid>',
+add_rule_api(host_performance.blueprint, '/cpu_memory/last_seven_days/<node_id>',
              api_func='host_performance.r_cpu_memory_last_seven_days', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/traffic/last_hour/<uuid>',
+add_rule_api(host_performance.blueprint, '/traffic/last_hour/<node_id>',
              api_func='host_performance.r_traffic_last_hour', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/traffic/last_six_hours/<uuid>',
+add_rule_api(host_performance.blueprint, '/traffic/last_six_hours/<node_id>',
              api_func='host_performance.r_traffic_last_six_hours', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/traffic/last_day/<uuid>',
+add_rule_api(host_performance.blueprint, '/traffic/last_day/<node_id>',
              api_func='host_performance.r_traffic_last_day', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/traffic/last_seven_days/<uuid>',
+add_rule_api(host_performance.blueprint, '/traffic/last_seven_days/<node_id>',
              api_func='host_performance.r_traffic_last_seven_days', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/disk_io/last_hour/<uuid>',
+add_rule_api(host_performance.blueprint, '/disk_io/last_hour/<node_id>',
              api_func='host_performance.r_disk_usage_io_last_hour', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/disk_io/last_six_hours/<uuid>',
+add_rule_api(host_performance.blueprint, '/disk_io/last_six_hours/<node_id>',
              api_func='host_performance.r_disk_usage_io_last_six_hours', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/disk_io/last_day/<uuid>',
+add_rule_api(host_performance.blueprint, '/disk_io/last_day/<node_id>',
              api_func='host_performance.r_disk_usage_io_last_day', methods=['GET'])
 
-add_rule_api(host_performance.blueprint, '/disk_io/last_seven_days/<uuid>',
+add_rule_api(host_performance.blueprint, '/disk_io/last_seven_days/<node_id>',
              api_func='host_performance.r_disk_usage_io_last_seven_days', methods=['GET'])
 
 

+ 4 - 0
main.py

@@ -36,6 +36,8 @@ from api.host import blueprint as host_blueprint
 from api.host import blueprints as host_blueprints
 from api.performance import blueprint as performance_blueprint
 from api.performance import blueprints as performance_blueprints
+from api.host_performance import blueprint as host_performance_blueprint
+from api.host_performance import blueprints as host_performance_blueprints
 
 from views.guest import blueprint as view_guest_blueprint
 from views.guest import blueprints as view_guest_blueprints
@@ -114,6 +116,8 @@ try:
     app.register_blueprint(host_blueprints)
     app.register_blueprint(performance_blueprint)
     app.register_blueprint(performance_blueprints)
+    app.register_blueprint(host_performance_blueprint)
+    app.register_blueprint(host_performance_blueprints)
 
     app.register_blueprint(view_guest_blueprint)
     app.register_blueprint(view_guest_blueprints)

+ 1 - 0
models/rules.py

@@ -46,6 +46,7 @@ class Rules(Enum):
     RSA_PUBLIC = (basestring, 'rsa_public')
 
     UUID = (basestring, 'uuid', (36, 36))
+    NODE_ID = (basestring, 'node_id', (14, 14))
     UUIDS = (REG_UUIDS, 'uuids')
     CPU = (int, 'cpu')
     MEMORY = (int, 'memory')