关于Linux服务器集群的开机自启动和有序关闭应用的实现
来源:互联网 发布:unity3d素材 编辑:程序博客网 时间:2024/06/05 11:25
有如下需求:公司一套开发环境多个服务器,比较耗电,决定每天早上开启,下班关机,为了方便,需要实现以下功能:
1、开机时自启动相应服务。
2、关机前有序关闭各应用然后关机。
3、服务器多台,Linux系统,包含oracle服务器、web应用、hbase(Hadoop集群)、svn、中间件、应用支撑等
方案如下:
1、各服务器分别配置,Linux开机自启动脚本,修改/etc/rc.d/rc.local加入启动应用的脚本(或者新建Linux服务)。
2、用java语言基于ssh2.jar(远程连接Linux主机并可以通过Linux命令操作远程主机的一个java工具包)、jdom、log4j等jar包,
3、主机列表、关闭应用的命令通过xml配置,通过jdom解析,并有序执行,log4j打印并记录日志。
具体实现:
1、ssh调用及运行命令
SSHConnection.java
package com.run.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SSHConnection {
private static final Logger logger = Logger.getLogger(SSHConnection.class);
private static Connection getOpenedConnection(String host, String username,
String password) {
if (logger.isDebugEnabled()) {
logger.debug("connecting to " + host + " with user " + username
+ " and pwd " + password);
}
Connection conn = new Connection(host);
boolean isOpen = true;
try {
conn.connect(); // make sure the connection is opened
} catch (IOException e) {
logger.info("服务器:" + host + "未开机");
isOpen = false;
}
if (isOpen) {
boolean isAuthenticated = true;
try {
isAuthenticated = conn.authenticateWithPassword(username,
password);
} catch (IOException e) {
isAuthenticated = false;
}
if (!isAuthenticated) {
logger.info("用户名或密码错误");
conn = null;
}
} else {
conn = null;
}
return conn;
}
public static List<String> runSshCmd(String host, String username,
String password, String cmd) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("running SSH cmd [" + cmd + "]");
}
List<String> list = new ArrayList<String>();
Connection conn = getOpenedConnection(host, username, password);
if (conn != null) {
Session sess = conn.openSession();
sess.execCommand(cmd);
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(
new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null)
break;
list.add(trim(line));
if (logger.isDebugEnabled()) {
logger.debug(line);
}
}
sess.close();
conn.close();
}
return list;
}
/**
* @Description: 将字符间空白转换为一个空格
* @param s
* @return String
* @throws
*/
private static String trim(String s) {
if (s == null) {
return null;
}
return s.trim().replaceAll("\\s{2,}", " ");
}
// 测试
public static void main(String[] args) throws IOException, Exception {
List<String> l = SSHConnection.runSshCmd("192.168.1.200", "hadoop",
"hadoop", "pwd");
for (String string : l) {
System.out.println(string);
}
}
}
2、CommonUtils.java
package com.run.common;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
public class CommonUtils {
public static List readXml(String url) throws Exception {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File(url));
return doc.selectNodes("//machine");
}
/**
* 读取xml中某个节点的信息
* @param url
* @param node
* @return
* @throws Exception
*/
public static List resolveXml(String url,String node) throws Exception {
SAXReader saxReader = new SAXReader();
InputStream in = CommonUtils.class.getResourceAsStream(url);
Document doc = saxReader.read(in);
return doc.selectNodes(node);
}
/**
* 读取properties配置文件
* @param path
* @param key
* @return
* @throws Exception
*/
public static String getValue(String path,String key) throws Exception {
InputStream in = CommonUtils.class.getClassLoader().getResourceAsStream(path);
Properties prop = new Properties();
prop.load(in);
return prop.getProperty(key);
}
/**
* 读取commonconfig.properties配置文件
* @param key
* @return
* @throws Exception
*/
public static String getValue(String key) throws Exception {
return getValue("commonconfig.properties",key);
}
public static URL getResource(String path) throws Exception {
return CommonUtils.class.getClassLoader().getResource(path);
}
}
3、Main.java程序入口
package com.run.common;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Element;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class);
public static void main(String[] args) {
try {
List list = CommonUtils.resolveXml("/shutdown.xml", "//machine");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
String host = element.element("ipAddress").getText();
String userName = element.element("username").getText();
String password = element.element("password").getText();
Element cmds = element.element("commands");
Iterator it = cmds.elementIterator("command");
while (it.hasNext()) {
Element cmdEl = (Element) it.next();
String cmd = cmdEl.getText();
logger.info(host + " " + userName + " " + password + " "
+ cmd);
try {
SSHConnection.runSshCmd(host, userName, password, cmd);
} catch (IOException ex) {
logger.debug("服务器:" + host + "关闭异常", ex);
}
}
if (!iter.hasNext()) {
logger.debug("所有服务器关机完毕!");
}
}
// Thread.sleep(Long.valueOf(CommonUtils.getValue("sleep_time")));
// System.exit(0);
} catch (Exception e) {
logger.debug("解析xml文件异常", e);
}
}
}
shutdown.xml
<?xml version="1.0" encoding="UTF-8"?>
<machines>
<!-- 关闭hadoop服务 -->
<machine>
<ipAddress>192.168.1.200</ipAddress>
<username>hadoop</username>
<password>111111</password>
<commands>
<command>sh /home/hadoop/hbase/bin/stop-hbase.sh</command>
<command>sh /home/hadoop/hadoop/bin/stop-all.sh</command>
</commands>
</machine>
<!-- 关机 -->
<machine>
<ipAddress>192.168.1.200</ipAddress>
<username>root</username>
<password>1111111</password>
<commands>
<command>halt -p</command>
</commands>
</machine>
<!-- 关oracle实例 -->
<machine>
<ipAddress>192.168.1.207</ipAddress>
<username>root</username>
<password>111111</password>
<commands>
<command>su - oracle -c "sqlplus / as sysdba @/home/oracle/shutdown.sql"</command>
<command>halt -p</command>
</commands>
</machine>
……………………
</machines>
commonconfig.properties
sleep_time=5000
由于是公司开发环境需要考虑电费成本,如果是生产环境,这套方案意义不是太大。
- 关于Linux服务器集群的开机自启动和有序关闭应用的实现
- 如何关闭Linux的某个开机自启动服务
- 【linux服务器】永久关闭防火墙 vsftp 开机自启动
- 关于APP开机自启动的简单实现
- 关于linux的开机自运行和登陆自运行
- Linux 下的 Redis 安装 && 启动 && 关闭 && 卸载&&开机自启动
- 开机自启动应用的方法
- Linux驱动和应用程序的开机自启动
- Linux下关闭开机自启动程序
- android实现应用程序的开机自启动
- Android实现应用程序的开机自启动
- android实现应用程序的开机自启动
- android实现项目的开机自启动
- Linux下的应用程序开机自启动
- Linux开机自启动脚本的总结
- 在ubuntu下关闭apache服务的开机自启动
- ORACLE --RAC 集群的启动和关闭
- ORACLE RAC 集群的启动和关闭
- #第六天 笔记 7/28
- OCP 1Z0 053 224
- tomcat加入log功能
- C++技术问题总结-第1篇 内存布局
- Windows驱动开发(二)
- 关于Linux服务器集群的开机自启动和有序关闭应用的实现
- poj-2352
- 线程同步与异步
- Oracle体系结构介绍
- 求一个字符串中连续出现次数最多的子串
- Travis Karr: 我们就是不断在改变,这是我们的宿命(一)
- Android Service 服务(一)—— Service
- 的房过户东方红刚
- 让View旋转