From bb33dcc2c10c7568e421d2f4fb09d6562fdadb27 Mon Sep 17 00:00:00 2001 From: Li Haoyu Date: Wed, 8 Jan 2020 14:52:01 +0800 Subject: [PATCH] 添加获取GPU的列表接口; --- Linux服务器监听运维功能.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java | 13 +++++++++++++ src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java | 8 ++++++++ src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java | 4 ++-- src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java | 27 +++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) diff --git a/Linux服务器监听运维功能.md b/Linux服务器监听运维功能.md index cf2be9a..d8771ad 100644 --- a/Linux服务器监听运维功能.md +++ b/Linux服务器监听运维功能.md @@ -362,6 +362,52 @@ } ``` +## 1.7 获取GPU的列表 + +| 调用方式 | 接口地址 | +| ------------ | :---------------------------------------------------- | +| POST | http://ip:port/occupationOfBasicResources/getGpuItems | +| Content-Type | application/json;charset=UTF-8 | + +| 返回结果 | | | | +| -------- | ----------- | ------ | --------------------------------- | +| 参数项 | 名称 | 类型 | 描述 | +| code | 响应码 | int | 200为操作成功,其他code表示失败 | +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 | +| data | 返回信息 | | | +| +uuid | GPU对应uuid | String | GPU对应uuid | +| +index | gpu编号 | String | gpu编号 | +| +gpuName | GPU名称 | String | GPU名称 | + +``` +响应示例 +{ + "code": 200, + "message": "操作成功", + "data": [ + { + "gpuName": "GeForce GTX 1080 Ti", + "index": "GPU 0", + "uuid": " GPU-99b735ea-99c6-a569-fadb-01f39ae045f7" + }, + { + "gpuName": "GeForce RTX 2080", + "index": "GPU 1", + "uuid": " GPU-c8f60855-479c-6658-16d1-140dac044159" + }, + { + "gpuName": "Tesla P4", + "index": "GPU 2", + "uuid": " GPU-0d60b67b-8df9-e3bb-2aa0-c632b1726ef1" + }, + { + "gpuName": "Tesla P4", + "index": "GPU 3", + "uuid": " GPU-95e656d9-fbe3-b17c-3f37-a05814bbcc79" + } + ] +} +``` diff --git a/src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java b/src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java index 2325185..6718791 100644 --- a/src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java +++ b/src/main/java/com/objecteye/controller/OccupationOfBasicResourcesController.java @@ -1,6 +1,7 @@ package com.objecteye.controller; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.objecteye.common.CommonResult; import com.objecteye.service.IOccupationOfBasicResourcesService; import io.swagger.annotations.Api; @@ -8,6 +9,7 @@ import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.List; import java.util.Map; @RestController @@ -29,6 +31,17 @@ public class OccupationOfBasicResourcesController extends BasicController { } } + @ApiOperation("获取GPU的列表") + @RequestMapping(value = "/getGpuItems", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") + public CommonResult> getGpuItems() { + List resultList = iOccupationOfBasicResourcesService.getGpuItems(); + if (resultList == null || resultList.size() == 0) { + return CommonResult.success(201, "没有找到合适数据", null); + } else { + return CommonResult.success(resultList); + } + } + @ApiOperation("jar包发布") @RequestMapping(value = "/deployJarFile", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public CommonResult deployJarFile(@RequestBody Map requestMap) { diff --git a/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java b/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java index 2b47f95..cbd00f0 100644 --- a/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java +++ b/src/main/java/com/objecteye/service/IOccupationOfBasicResourcesService.java @@ -3,6 +3,7 @@ package com.objecteye.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import java.util.List; import java.util.Map; /** @@ -27,6 +28,13 @@ public interface IOccupationOfBasicResourcesService { JSONArray getInfoByModule(String module); /** + * 获取GPU的列表 + * + * @return 结果集 + */ + List getGpuItems(); + + /** * jar包发布 * * @param requestMap 请求参数 diff --git a/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java b/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java index c6509b9..c98c06c 100644 --- a/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/LogFileListenerServiceImpl.java @@ -139,8 +139,8 @@ public class LogFileListenerServiceImpl implements ILogFileListenerService { */ @Override public JSONObject downloadLogFile(HttpServletResponse httpServletResponse, String logKey) { -// String logPath = (String) redisTemplate.opsForHash().get(GeneralContent.REDIS_LOG_KEY_PATH, logKey); - String logPath = "E:\\testResources\\111.jpg"; + String logPath = (String) redisTemplate.opsForHash().get(GeneralContent.REDIS_LOG_KEY_PATH, logKey); +// String logPath = "E:\\testResources\\111.jpg"; JSONObject resultObj = new JSONObject(); if (logPath == null) { resultObj.put("error", "找不到文件"); diff --git a/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java b/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java index ff4e388..56ebb87 100644 --- a/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/OccupationOfBasicResourcesServiceImpl.java @@ -68,6 +68,33 @@ public class OccupationOfBasicResourcesServiceImpl implements IOccupationOfBasic } /** + * 获取GPU的列表 + * + * @return 结果集 + */ + @Override + public List getGpuItems() { + final String gpuCommand = "nvidia-smi -L"; + List outList = new ArrayList<>(); + LinuxUtils.executeLinuxCmd(gpuCommand, outList); + List resultList = new ArrayList<>(); + for (String outStr : outList) { + if (outStr == null || "".equals(outStr)) { + continue; + } + String uuid = outStr.substring(outStr.lastIndexOf(":") + 1, outStr.indexOf(")")); + String index = outStr.substring(0, outStr.indexOf(":")); + String gpuName = outStr.substring(outStr.indexOf(":") + 1, outStr.indexOf("(")).trim(); + JSONObject itemObj = new JSONObject(); + itemObj.put("uuid", uuid); + itemObj.put("index", index); + itemObj.put("gpuName", gpuName); + resultList.add(itemObj); + } + return resultList; + } + + /** * jar包发布 * * @param requestMap 请求参数 -- libgit2 0.21.4