java saltstack_Java springMVC saltstack实现服务器集群管理

Java springMVC

saltstack实现服务器集群管理

之前的博文中一直介绍的都是用django python

saltstack做服务器的集群管理,这次换个方法玩saltstack,用java来操作。

tip:代码太多,字数超了只能贴图片了,关键的java代码还是贴的源码(java真的是需要非常大代码量的语言啊,用python10行搞定的代码用java得如下这么多。。。)

实验目的

1、

搭建maven springMVC web平台

2、

java实现链接saltstack进行操作

3、

通过web平台实现服务器信息的可视化管理

4、

最终完成服务器信息通过页面呈现的demo(只是简单展示,根据需求更改)

实验开始

1、安装salt-api

对于salt-api,个人感觉优点很明显,相较于之前一直用python引用salt.client来进行saltstack操作,这个方法可以忽略python版本,django版本,这个有点是在太大了!(python的版本问题太头疼),但是缺点也比较明显,速度绝对和salt.client直接操作没法比。

安装过程跳过,本文不讲述,可以百度查看进行安装,比较容易。

2、搭建maven

springMVC web平台

这步没什么可说的,把maven配置文件共享吧,就不用大家去找了。

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

3、java实现链接saltstack进行操作

通过发送https请求,获取token值

private String url = "https://192.168.168.147:9000";

private String charset = "utf-8";

private HttpClientUtil

httpClientUtil = null;

public TestController(){

httpClientUtil = new HttpClientUtil();

}

public String test(){

String httpOrgCreateTest = url;

Listlist

=

new ArrayList();

//

此处信息用于获取token值

// list.add(new

BasicNameValuePair("username","saltapi"));

// list.add(new

BasicNameValuePair("password","omygad911"));

// list.add(new

BasicNameValuePair("eauth","pam"));

//

此处信息用于获取从机进行台信息,分别为[主机名,主机ip,用途,所属机房,机柜,内存大小,cpu数,cpu核数,编码,操作系统]

// list.add(new BasicNameValuePair("client","local"));

// list.add(new BasicNameValuePair("tgt","*"));

// list.add(new

BasicNameValuePair("expr_form","glob"));

// list.add(new BasicNameValuePair("fun","grains.item"));

// list.add(new BasicNameValuePair("arg","host"));

// list.add(new BasicNameValuePair("arg","ipv4"));

// list.add(new BasicNameValuePair("arg","use"));

// list.add(new

BasicNameValuePair("arg","spath"));

// list.add(new BasicNameValuePair("arg","where"));

// list.add(new

BasicNameValuePair("arg","mem_total"));

// list.add(new

BasicNameValuePair("arg","num_gpus"));

// list.add(new

BasicNameValuePair("arg","num_cpus"));

// list.add(new

BasicNameValuePair("arg","locale_info"));

// list.add(new

BasicNameValuePair("arg","os"));

// list.add(new

BasicNameValuePair("arg","osrelease"));

String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,list,charset);

return httpOrgCreateTestRtn;

}

public class HttpClientUtil {

public String doPost(String url,List list , String

charset){

HttpClient httpClient = null;

HttpPost httpPost = null;

String result = null;

try{

httpClient = new SSLClient();

httpPost = new HttpPost(url);

if(list.size() > 0){

UrlEncodedFormEntity entity = new

UrlEncodedFormEntity(list,charset);

httpPost.setEntity(entity);

}

HttpResponse response = httpClient.execute(httpPost);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,charset);

}

}

}catch(Exception ex){

ex.printStackTrace();

}

return result;

}

}

public class SSLClient extends

DefaultHttpClient{

public SSLClient() throws Exception{

super();

SSLContext ctx =

SSLContext.getInstance("TLS");

X509TrustManager tm = new X509TrustManager()

{

public void checkClientTrusted(X509Certificate[]

chain,

String authType) throws CertificateException

{

}

public void checkServerTrusted(X509Certificate[]

chain,

String

authType) throws CertificateException

{

}

public X509Certificate[] getAcceptedIssuers()

{

return null;

}

};

ctx.init(null, new TrustManager[]{tm},

null);

SSLSocketFactory ssf = new

SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

ClientConnectionManager ccm =

this.getConnectionManager();

SchemeRegistry sr =

ccm.getSchemeRegistry();

sr.register(new Scheme("https", 9000,

ssf));

}

}

java代码也没什么特别的,就是注意header信息的设置就可以了,如果要用上边的代码,需要引用httpclient4

jar包,maven配置中已经包含了。

js代码:

a4c26d1e5885305701be709a3d33442f.png

随便写个table,然后加载页面时自动执行以上js,然后通过后台获取数据,静态刷新,实现数据展示

4最终完成服务器信息通过页面呈现的demo

效果图:

a4c26d1e5885305701be709a3d33442f.png

服务器的内存,cpu,机房信息,编码,操作系统及版本一目了然

实验结束。

至此,各服务器的基本信息都可以通过web页面一目了然,并且到此就和之前博文中的服务器集群管理链接上了,通过之前几片文章中讲的方法,将那些功能加到这个页面中就同样实现(css,html,js都是通用的,也不用担心python

django写了一遍还得再用java版在写一遍,直接复制粘贴就行!)

1、

服务器集群管理

2、

可视化管理

3、

通过java管理

本文只是介绍了登录(获取token)和查询信息(grains.item模块),还可以通过cp,cmd等等模块进行操作,实现前几篇博文中通过python

django来实现的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值