针对linux服务器,用平台一键同步时间、查看内存占用、磁盘占用,由于采用用户名,密码登录SSH远程登录拿取数据,所以不需要安装客户端
查看内存占用
平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行cat /proc/meminfo获取数据
public int[] viewMemory(String ip) throws IOException, InterruptedException { RemoteShellTool tool = new RemoteShellTool(ip,"utf-8"); tool.loginCentos(); String returnString = tool.exec("cat /proc/meminfo"); int data[]; if(returnString.indexOf("MemTotal")<0) { logger.info("viewMemory:"+ip+" return "+returnString); data=new int[]{0,0}; } else { returnString=returnString.replace(" ", ""); String result[]=returnString.split(" "); int total = (int) ((Integer.parseInt(result[0].substring(9,result[0].length()-2))+0.5)/1024); int free = (int) ((Integer.parseInt(result[1].substring(8,result[1].length()-2))+0.5)/1024); data=new int[]{free,total}; } 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"> <link href="/css/index/applications.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="/bootstrap/js/jquery.bootpag.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/vmmemory.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="vmmemory"> <div class="tabbable"> <div class="tab-content"> <div class="tab-pane active" id="vms"> <div id="vmsTab-pane"> <br /> <div class="row" id="search1Div"> <div class="col-md-2"> <div class="input-group"> <span class="input-group-addon">环境:</span> <select class="form-control" ms-duplex="envType"> <option value="STRESS" selected>STRESS</option> <option value="ALL">全部</option> <option value="SIT1">SIT1</option> <option value="SIT2">SIT2</option> <option value="UAT">UAT</option> <option value="other">其他</option> </select> </div> </div> <div class="col-md-2" id="search2Div"> <div class="input-group"> <span class="input-group-addon">类型:</span> <select class="form-control" ms-duplex="conType"> <option value="" selected>请选择</option> <option value=".Net Web">.Net Web</option> <option value="Windows Service">Windows Service</option> <option value="Node">Node</option> <option value="Java App">Java App</option> <option value="Java Web">Java Web</option> <option value="其他">其他</option> </select> </div> </div> <div class="col-md-1"> <button type="button" id="searchBtn" class="btn btn-primary" ms-click="listVmInfosByPage('init')" style="margin: auto;"> 搜 索</button> </div> <div class="col-md-1"> <button type="button" id="allCheckBtn" class="btn btn-info" ms-click="checkAll()" style="margin: auto;">全选</button> </div> <div class="col-md-1"> <button type="button" id="allUncheckBtn" class="btn btn-info" ms-click="uncheckAll()" style="margin: auto;">全不选</button> </div> <div class="col-md-2"> <button type="button" id="allStatusBtn" class="btn btn-success" ms-click="viewAllStatus()" style="margin: auto;">查看当前页所选机器内存</button> </div> </div> <div> <div id="pageSizeSelect"> <a><span ms-class="{{pagesize1Cls}}" ms-click="changePageSize(pagesize1)">{{pagesize1}}</span></a> | <a><span ms-class="{{pagesize2Cls}}" ms-click="changePageSize(pagesize2)">{{pagesize2}}</span></a> | <a> <spam ms-class="{{pagesize3Cls}}" ms-click="changePageSize(pagesize3)">{{pagesize3}}</spam> </a> </div> </div> <table class="table table-condensed table-hover"> <thead> <tr> <td class="width-50"></td> <td class="width-50">ID</td> <td>名称</td> <td class="width-125">IP</td> <td class="width-100">操作系统</td> <td class="width-100">可用内存MB</td> <td class="width-100">总内存MB</td> <td class="width-200">使用百分比</td> <td class="width-50">查看</td> </tr> </thead> <tbody> <tr ms-repeat="vmsList"> <td><label><input type="checkbox" ms-class="check_{{el.name}}"></label></td> <td>{{$index+jpageSize*(jpageIndex-1)+1}}</td> <td>{{el.name}}</td> <td><a ms-href="'/admin/vmdetails.html?vmid='+el.vm.id" target="_blank">{{el.ip}}</a></td> <td>{{el.os}}</td> <td><label ms-class="free_{{el.name}}" /></td> <td><label ms-class="total_{{el.name}}" /></td> <td><div class="progress"> <div ms-class="progress-bar progress-bar-success process_{{el.name}}" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style=" 0%;"> <span class="sr-only"></span> </div> </div></td> <td> <div ms-class="buttonDiv_view_{{el.name}}"> <i style="color: #009100;" ms-class="glyphicon glyphicon-eye-open icon-white i_view_{{el.name}}" ms-click="postViewStatus(el.name,el.ip,el.os)"></i> </div> <div ms-class="loadDiv_view_{{el.name}} loadDiv" style="display: none"> <img src="/img/load2.jpg" style=" 16px; height: 16px;" /> </div> </td> </tr> </tbody> </table> <div class="text-center"> <p id="pagination"></p> </div> </div> </div> </div> </div> </div> </div> <!--/.container--> <!--FOOTER --> <div ms-include-src="'/home/footer.html'"></div> </body> </html>
js
/** * Created by chenjiazhu on 2017/5/10. */ var vmmemory = avalon .define({ $id : 'vmmemory', // VM Start pagesize1 : "20", pagesize1Cls : "pageSizeSelected", pagesize2 : "50", pagesize2Cls : "", pagesize3 : "100", pagesize3Cls : "", changePageSize : function(pgsize) { vmmemory.jpageSize = pgsize; vmmemory.listVmInfosByPage("init"); }, clearsearch : function() { vmmemory.conType = ""; vmmemory.listVmInfosByPage("init"); }, jpageIndex : 1, jpageSize : 20, envType : "STRESS", conType : "", vmsList : [], listVmInfosByPage : function(tag) { if (tag) { vmmemory.jpageIndex = 1; } $.ajax({ type : "post", url : 'listVmInfosByPageByEnvType.action', data : { "pageindex" : vmmemory.jpageIndex, "pagesize" : vmmemory.jpageSize, "type" : vmmemory.conType, "envType" : vmmemory.envType }, dataType : "json", success : function(data) { if (tag) { $('#pagination').bootpag({ total : data.pagenum, page : vmmemory.jpageIndex }); } if (data.retCode == "1000") { vmmemory.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 : 'viewMemory.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() { vmmemory.listVmInfosByPage("init"); $('#vms').tab('show'); }, // VM END userOps : ops(4), bootpagFuc : function() { $('#pagination').bootpag({ total : 1, maxVisible : 10 }).on('page', function(event, num) { vmmemory.jpageIndex = num; vmmemory.listVmInfosByPage(); }); } }); /* * avalon.ready(function() { appsvm.bootpagFuc(); appsvm.listApp("init"); * appsvm.depList = getAllDepartments(); appsvm.envsList = getAllEnvs(); * appsvm.applicationsTypeList = getAllAppType(); }); */ avalon.ready(function() { /* * if (vmmemory.userOps) { vmmemory.loadVmTAB(); } else { * redirectAdminIndexPage(); } */ vmmemory.bootpagFuc(); vmmemory.listVmInfosByPage("init"); // $(".loadDiv").hide(); }); vmmemory.$watch("jpageSize", function(newValue) { vmmemory.pagesize1Cls = ""; vmmemory.pagesize2Cls = ""; vmmemory.pagesize3Cls = ""; if (newValue == vmmemory.pagesize1) { vmmemory.pagesize1Cls = "pageSizeSelected"; } else if (newValue == vmmemory.pagesize2) { vmmemory.pagesize2Cls = "pageSizeSelected"; } else if (newValue == vmmemory.pagesize3) { vmmemory.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); } }