针对Linux服务器,用平台一键、磁盘占用,由于采用用户名,密码登录SSH远程登录拿取数据,所以不需要安装客户端
原文地址:http://blog.csdn.net/chenjiazhu/article/details/77532484
查看磁盘占用
平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行df -lh获取数据
public int[] viewDisk(String ip) throws IOException, InterruptedException { RemoteShellTool tool = new RemoteShellTool(ip,"utf-8"); tool.loginCentos(); String returnString = tool.exec("df -lh"); int data[]; if(returnString.indexOf("Filesystem")<0) { logger.info("viewDisk:"+ip+" return "+returnString); data=new int[]{0,0}; } else { returnString=returnString.replaceAll(" +"," "); String result[]=returnString.split(" "); String res1[] = result[1].split(" "); data=new int[]{Integer.parseInt(res1[3].substring(0, res1[3].length()-1)),Integer.parseInt(res1[1].substring(0, res1[1].length()-1))}; } return data; }
附其他代码:
html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="/css/reset.css" rel="stylesheet"> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link href="/css/layout.css" rel="stylesheet"> <script type="text/javascript" src="/lib/jquery.js"></script> <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="/lib/avalon.js"></script> <script type="text/javascript" src="/js/common/util.js"></script> <script type="text/javascript" src="/js/common/common.js"></script> <script type="text/javascript" src="/js/index/vmdetails.js"></script> <title>虚拟机详情</title> </head> <body ms-controller="vm"> <!-- HEAD --> <!--HEAD --> <div ms-include-src="'/home/header.html'"></div> <!-- Content --> <div class="container"> <div ms-controller="vmdetailsvm"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">虚拟机 : {{vmip}}</h3> </div> <div class="panel-body"> <table class="table table-bordered"> <thead> <tr> <td class="width-50">ID</td> <td>域名</td> <td>名称</td> <td class="width-200">应用类型</td> <td class="width-100">部门</td> <td class="width-100">负责人</td> </tr> </thead> <tbody> <tr ms-repeat="appenvs"> <td>{{$index+1}}</td> <td>{{el.application.domain}}</td> <td>{{el.application.name}}</td> <td>{{el.application.applicationtype.type}}</td> <td>{{el.application.department.name}}</td> <td>{{el.application.devs}}</td> </tr> </tbody> </table> </div> </div> </div> </div> <!--FOOTER --> <div ms-include-src="'/home/footer.html'"></div> </body> </html>
js
/** * Created by chenjiazhu on 2017/5/10. */ var vmdisk = avalon.define({ $id : 'vmdisk', // VM Start pagesize1 : "20", pagesize1Cls : "pageSizeSelected", pagesize2 : "50", pagesize2Cls : "", pagesize3 : "100", pagesize3Cls : "", changePageSize : function(pgsize) { vmdisk.jpageSize = pgsize; vmdisk.listVmInfosByPage("init"); }, clearsearch : function() { vmdisk.conType = ""; vmdisk.listVmInfosByPage("init"); }, jpageIndex : 1, jpageSize : 20, envType : "STRESS", conType : "", vmsList : [], listVmInfosByPage : function(tag) { if (tag) { vmdisk.jpageIndex = 1; } $.ajax({ type : "post", url : 'listVmInfosByPageByEnvType.action', data : { "pageindex" : vmdisk.jpageIndex, "pagesize" : vmdisk.jpageSize, "type" : vmdisk.conType, "envType" : vmdisk.envType }, dataType : "json", success : function(data) { if (tag) { $('#pagination').bootpag({ total : data.pagenum, page : vmdisk.jpageIndex }); } if (data.retCode == "1000") { vmdisk.vmsList = data.vms; } else { alert(data.retMSG); } $("[class^=free]").html(""); $("[class^=total]").html(""); $("[class*=progress-bar]").css("width", "0%"); }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:" + errorThrown + ",textStatus:" + textStatus); } }); }, postViewStatus : function(name, ip, os) { $(".loadDiv_view_" + name).show(); $(".buttonDiv_view_" + name).hide(); $ .ajax({ type : "post", url : 'viewDisk.action', data : { "ip" : ip, "os" : os }, dataType : "json", success : function(data) { if (data.retCode == "1") { $(".free_" + name).html("fail"); $(".total_" + name).html("fail"); } else { $(".free_" + name).html(data.result[0]); $(".total_" + name).html(data.result[1]); var processdata = 100 - (data.result[0] * 100 / data.result[1]); $(".process_" + name).css("width", processdata+"%"); if (processdata > 90) { $(".process_" + name).removeClass( "progress-bar-success"); $(".process_" + name).addClass( "progress-bar-danger"); } else { if (processdata > 60) { $(".process_" + name).removeClass( "progress-bar-success"); $(".process_" + name).addClass( "progress-bar-warning"); } } } $(".loadDiv_view_" + name).hide(); $(".buttonDiv_view_" + name).show(); }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:" + errorThrown + ",textStatus:" + textStatus); $(".loadDiv_view_" + name).hide(); $(".buttonDiv_view_" + name).show(); } }); }, viewAllStatus : function() { $("input[type='checkbox']").each(function() { if ($(this).get(0).checked) { var name = $(this).attr("class").substr(6); // alert(name); $(".i_view_" + name).click(); } }); }, checkAll : function() { $("input[type='checkbox']").each(function() { $(this).attr("checked", "true"); }); }, uncheckAll : function() { $("input[type='checkbox']").each(function() { $(this).removeAttr("checked"); }); }, loadVmTAB : function() { vmdisk.listVmInfosByPage("init"); $('#vms').tab('show'); }, // VM END userOps : ops(4), bootpagFuc : function() { $('#pagination').bootpag({ total : 1, maxVisible : 10 }).on('page', function(event, num) { vmdisk.jpageIndex = num; vmdisk.listVmInfosByPage(); }); } }); /* * avalon.ready(function() { appsvm.bootpagFuc(); appsvm.listApp("init"); * appsvm.depList = getAllDepartments(); appsvm.envsList = getAllEnvs(); * appsvm.applicationsTypeList = getAllAppType(); }); */ avalon.ready(function() { /* * if (vmdisk.userOps) { vmdisk.loadVmTAB(); } else { * redirectAdminIndexPage(); } */ vmdisk.bootpagFuc(); vmdisk.listVmInfosByPage("init"); // $(".loadDiv").hide(); }); vmdisk.$watch("jpageSize", function(newValue) { vmdisk.pagesize1Cls = ""; vmdisk.pagesize2Cls = ""; vmdisk.pagesize3Cls = ""; if (newValue == vmdisk.pagesize1) { vmdisk.pagesize1Cls = "pageSizeSelected"; } else if (newValue == vmdisk.pagesize2) { vmdisk.pagesize2Cls = "pageSizeSelected"; } else if (newValue == vmdisk.pagesize3) { vmdisk.pagesize3Cls = "pageSizeSelected"; } })
工具类
package com.ymt.testplatform.util; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; public class RemoteShellTool { private Connection conn; private String ipAddr; private String charset = Charset.defaultCharset().toString(); private String userName; private String password; public RemoteShellTool(String ipAddr, String charset) { this.ipAddr = ipAddr; if (charset != null) { this.charset = charset; } } public RemoteShellTool(String ipAddr, String userName, String password, String charset) { this.ipAddr = ipAddr; this.userName = userName; this.password = password; if (charset != null) { this.charset = charset; } } public boolean login() throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 return conn.authenticateWithPassword(userName, password); // 认证 } public boolean loginCentos() throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 String [] pass = {"ymt@123","abcd@1234","root@1234","1qaz@WSX","1234qwer"}; for (String pa : pass) { if(conn.authenticateWithPassword("root", pa)) { this.userName = "root"; this.password = pa; return true; } } return false; } public boolean login(String userName,String password) throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 this.userName = userName; this.password = password; return conn.authenticateWithPassword(userName, password); // 认证 } public String exec(String cmds) { InputStream in = null; String result = ""; try { if (this.login()) { Session session = conn.openSession(); // 打开一个会话 session.execCommand(cmds); in = session.getStdout(); result = this.processStdout(in, this.charset); session.close(); conn.close(); } } catch (IOException e1) { e1.printStackTrace(); } return result; } public String processStdout(InputStream in, String charset) { byte[] buf = new byte[1024]; StringBuffer sb = new StringBuffer(); try { while (in.read(buf) != -1) { sb.append(new String(buf, charset)); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /** * @param args */ public static void main(String[] args) { RemoteShellTool tool = new RemoteShellTool("172.16.103.126", "ftpuser", "1qaz@WSX", "utf-8"); String result = tool.exec("sh Deploy.sh 172.16.103.121"); System.out.print("result:"+result); RemoteShellTool tool2 = new RemoteShellTool("172.16.103.121", "ftpuser", "1qaz@WSX", "utf-8"); String result2 = tool2.exec("sh tools/clientDeploy.sh"); System.out.print("result2:"+result2); } }
查看磁盘占用
平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行df -lh获取数据
附其他代码:
html
工具类