1、需求
之前一直是手動(dòng)的巡檢,然后貼圖,最近服務(wù)器數(shù)量大增,有點(diǎn)忙不過(guò)來(lái)了。因?yàn)橐恢庇玫膉ava,對(duì)shell腳本不是特別了解,所以這次用java寫了個(gè)小項(xiàng)目,實(shí)現(xiàn)對(duì)多服務(wù)器,多任務(wù)的巡檢,巡檢結(jié)果有故障的會(huì)通過(guò)郵件通知。
2、功能和效果
巡檢的項(xiàng)目主要是服務(wù),硬盤,內(nèi)存等,命令可配置,巡檢結(jié)果以日期和服務(wù)器為基準(zhǔn)輸出文件,錯(cuò)誤信息通過(guò)郵件通知管理運(yùn)維人員。
3、代碼
action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package com.save.action; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.hashset; import java.util.iterator; import java.util.list; import java.util.map; import java.util.set; import java.util.regex.matcher; import java.util.regex.pattern; import org.apache.commons.lang3.stringutils; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.alibaba.fastjson.json; import com.save.pojo.cmd; import com.save.until.mailutil; import com.save.until.propertiesutil; import com.save.until.sshcommutil; import com.save.until.writeuntil; /** * 巡檢任務(wù) * @author zhangzhuo * */ public class inspaction { final static logger logger = loggerfactory.getlogger(inspaction. class ); /* public static void main(string[] args) { inspaction n = new inspaction(); try { n.execute(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); logger.error("dd"); } }*/ /** * 執(zhí)行巡檢任務(wù) * @param args */ public void execute() throws exception{ list<cmd> list = this.handlerdata(); set<string> mail = new hashset<string>(); for (cmd cmd : list) { string ip = cmd.getip(); int port = 22; string localip = null; int localport = 0; int timeout = 6000; string username = cmd.getusername(); string password = cmd.getpassword(); string server = cmd.getserver(); string[] cmds = cmd.getcmds(); string[] result = null; logger.info(ip+"執(zhí)行巡檢任務(wù)開始"); try { result = sshcommutil.execshellcmdbyssh(ip, port, localip, localport, timeout, username, password, cmds); } catch (exception e) { e.printstacktrace(); logger.error(ip+"巡檢,服務(wù)器連接不上"); mail.add(ip+" "+"巡檢,服務(wù)器連接不上"); } date date = new date(); simpledateformat formatter = new simpledateformat("yyyy/mm/dd"); string datestring = formatter.format(date); //1、服務(wù)存活驗(yàn)證 2、硬盤占用驗(yàn)證 3、巡檢結(jié)果寫入文件 if (result != null) { for (string string : result) { if (string.contains("ps -ef|grep java")||string.contains("ps -ef|grep mongo")||string.contains("ps -ef|grep redis")) { if (!string.contains(server)) { mail.add(ip+" "+server+"服務(wù)不存在"); } } if (string.contains("df -h")) { string patt = "^[5]\\d{1}\\%|[5-9]\\d{1}\\%|\\d{3,}\\%$"; string group = null; pattern p = pattern.compile(patt); matcher m = p.matcher(string); while (m.find()) { group = m.group(); } if (!stringutils.isblank(group)) { mail.add(ip+" "+"硬盤占用超出預(yù)警線"); } } writeuntil.createfile("e:\\save", datestring, "\\"+ip+".txt", string); } logger.info(ip+"巡檢結(jié)束"); } } //發(fā)送故障郵件通知 if (!mail.isempty()||mail.size()!=0) { mailutil.getinstance().sendmail(mail); } } /** * 數(shù)據(jù)處理 * @return */ private list<cmd> handlerdata(){ logger.info( "開始加載需要巡檢的服務(wù)器數(shù)據(jù)" ); cmd cmd = null ; list<cmd> list = new arraylist<cmd>(); map map = propertiesutil.getinstance().getallproperty(); iterator<map.entry<string, string>> it = map.entryset().iterator(); while (it.hasnext()) { map.entry<string, string> entry = it.next(); cmd = new cmd(); cmd.setip(entry.getkey()); cmd cmd2 = json.parseobject(entry.getvalue(), cmd. class ); string[] cmds = cmd2.getshell().split( "," ); cmd.setcmds(cmds); cmd.setserver(cmd2.getserver()); cmd.setusername(cmd2.getusername()); cmd.setpassword(cmd2.getpassword()); list.add(cmd); } logger.info( "數(shù)據(jù)加載完畢" ); return list; } } |
pojo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.save.pojo; public class cmd { private string ip; private string username; private string password; private string shell; private string[] cmds; private string server; public string getserver() { return server; } public void setserver(string server) { this .server = server; } public string getshell() { return shell; } public void setshell(string shell) { this .shell = shell; } public string getip() { return ip; } public void setip(string ip) { this .ip = ip; } public string getusername() { return username; } public void setusername(string username) { this .username = username; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } public string[] getcmds() { return cmds; } public void setcmds(string[] cmds) { this .cmds = cmds; } } |
工具類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.save.until; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.socket; import java.net.unknownhostexception; import java.util.properties; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.jcraft.jsch.jsch; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; import com.jcraft.jsch.socketfactory; /** * ssh創(chuàng)建與服務(wù)器連接工具類 * @author 張卓 * 2017-4-21 */ public class jschutil { final static logger logger = loggerfactory.getlogger(jschutil. class ); private static jsch jsch = new jsch(); /** * 創(chuàng)建session,并打開session連接 * */ public static session createsession(string dstip, int dstport, final string localip, final int localport, string username, string password, final int timeout) throws jschexception { //jsch.setknownhosts("/home/foo/.ssh/known_hosts"); logger.info( "開始連接:" +dstip); // 建立一個(gè)ssh連接 session session = jsch.getsession(username, dstip, dstport); session.setpassword(password); properties sshconfig = new properties(); sshconfig.put( "stricthostkeychecking" , "no" ); //跳過(guò)主機(jī)檢查 session.setconfig(sshconfig); // 此socket工廠用于創(chuàng)建目標(biāo)主機(jī)的socket, // 并創(chuàng)建我們使用的這個(gè)socket字節(jié)流 session.setsocketfactory( new socketfactory() { public outputstream getoutputstream(socket socket) throws ioexception { return socket.getoutputstream(); } public inputstream getinputstream(socket socket) throws ioexception { return socket.getinputstream(); } public socket createsocket(string host, int port) throws ioexception, unknownhostexception { socket socket = new socket(); if (localip != null ) { socket.bind( new inetsocketaddress(inetaddress .getbyname(localip), localport)); } socket.connect( new inetsocketaddress(inetaddress.getbyname(host), port), timeout); return socket; } }); session.connect(timeout); return session; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package com.save.until; import java.util.properties; import java.util.set; import javax.mail.authenticator; import javax.mail.message; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.sun.mail.util.mailsslsocketfactory; /** * 郵件發(fā)送 * @author zhangzhuo * */ public class mailutil { final static logger logger = loggerfactory.getlogger(mailutil. class ); private static mailutil instance = new mailutil(); private mailutil (){} public static mailutil getinstance() { return instance; } public void sendmail(set<string> mail) { string host = "smtp.qq.com" ; // 指定發(fā)送郵件的主機(jī)smtp.qq.com(qq)|smtp.163.com(網(wǎng)易) properties properties = new properties(); properties.setproperty( "mail.smtp.host" , host); // 設(shè)置郵件服務(wù)器 properties.setproperty( "mail.smtp.auth" , "true" ); // 打開認(rèn)證 try { //qq郵箱需要下面這段代碼,163郵箱不需要 mailsslsocketfactory sf = new mailsslsocketfactory(); sf.settrustallhosts( true ); properties.put( "mail.smtp.ssl.enable" , "true" ); properties.put( "mail.smtp.ssl.socketfactory" , sf); // 1.獲取默認(rèn)session對(duì)象 session session = session.getdefaultinstance(properties, new authenticator() { public passwordauthentication getpasswordauthentication() { } }); // 2.創(chuàng)建郵件對(duì)象 message message = new mimemessage(session); message.setfrom( new internetaddress(from)); message.setsubject( "巡檢故障通知" ); stringbuffer sb = new stringbuffer(); for (string string : mail) { sb.append( "<div>" +string+ "</div><br/><hr/>" ); } string content = sb.tostring(); message.setcontent(content, "text/html;charset=utf-8" ); transport.send(message); logger.info( "故障郵件發(fā)送成功" ); } catch (exception e) { e.printstacktrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.save.until; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.net.uri; import java.util.enumeration; import java.util.hashmap; import java.util.map; import java.util.properties; /** * 讀取文件工具類 * @author zhangzhuo * */ public class propertiesutil { private properties props; private uri uri; private static propertiesutil ourinstance = new propertiesutil( "/config.properties" ); public static propertiesutil getinstance() { return ourinstance; } public propertiesutil(string filename){ readproperties(filename); } private void readproperties(string filename) { try { props = new properties(); inputstream fis =getclass().getresourceasstream(filename); inputstreamreader re= new inputstreamreader(fis, "utf-8" ); props.load(re); } catch (exception e) { e.printstacktrace(); } } /** * 獲取某個(gè)屬性 */ public string getproperty(string key){ return props.getproperty(key); } /** * 獲取所有屬性,返回一個(gè)map,不常用 * 可以試試props.putall(t) */ public map getallproperty(){ map map= new hashmap(); enumeration enu = props.propertynames(); while (enu.hasmoreelements()) { string key = (string) enu.nextelement(); string value = props.getproperty(key); map.put(key, value); } return map; } /** * 在控制臺(tái)上打印出所有屬性,調(diào)試時(shí)用。 */ public void printproperties(){ props.list(system.out); } /** * 寫入properties信息 */ public void writeproperties(string key, string value) { try { outputstream fos = new fileoutputstream( new file(uri)); props.setproperty(key, value); // 將此 properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流 props.store(fos, "『comments』update key:" + key); } catch (exception e) { e.printstacktrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package com.save.until; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.jcraft.jsch.channel; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; /** * 執(zhí)行shell工具類 * @author zhangzhuo * */ public class sshcommutil { final static logger logger = loggerfactory.getlogger(sshcommutil. class ); /** * shh連接linux shell,返回結(jié)果 */ public static string[] execshellcmdbyssh(string dstip, int dstport, string localip, int localport, int timeout, string username, string password, string... cmds) throws exception { session session = null ; channel channel = null ; inputstream is = null ; outputstream os = null ; try { session = jschutil.createsession(dstip, dstport, localip, localport, username, password, timeout); logger.info( "開始創(chuàng)建channel通道!" ); //創(chuàng)建一個(gè)channel類型的通道 channel = session.openchannel( "shell" ); // enable agent-forwarding. // ((channelshell)channel).setagentforwarding(true); // choose the pty-type "vt102". // ((channelshell)channel).setptytype("vt102"); // set environment variable "lang" as "ja_jp.eucjp". // ((channelshell)channel).setenv("lang", "ja_jp.eucjp"); channel.connect(); is = channel.getinputstream(); os = channel.getoutputstream(); string[] result = new string[cmds.length]; for ( int i = 0 ; i < cmds.length; i++) { result[i] = sendcommand(is, os, cmds[i]); } return result; } catch (jschexception e) { if (e.getmessage().contains( "auth fail" )) { logger.error(dstip+ "服務(wù)器驗(yàn)證失敗" ); throw new exception( "auth error" ); } else { logger.error(dstip+ "服務(wù)器連接失敗" ); throw new exception( "connect error" ); } } catch (exception e) { throw e; } finally { try { is.close(); } catch (ioexception e) { } try { os.close(); } catch (ioexception e) { } channel.disconnect(); session.disconnect(); } } /** *執(zhí)行shell腳本?并返回結(jié)果 * */ private static string sendcommand(inputstream is, outputstream os, string cmd) throws ioexception { logger.info( "開始執(zhí)行腳本!" ); os.write(cmd.getbytes()); os.flush(); stringbuffer sb = new stringbuffer(); int beat = 0 ; while ( true ) { if (beat > 3 ) { break ; } if (is.available() > 0 ) { byte [] b = new byte [is.available()]; is.read(b); sb.append( new string(b)); beat = 0 ; } else { if (sb.length() > 0 ) { beat++; } try { thread.sleep(sb.tostring().trim().length() == 0 ? 1000 : 300 ); } catch (interruptedexception e) { } } } return sb.tostring(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
package com.save.until; 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 com.jcraft.jsch.channelexec; import com.jcraft.jsch.jsch; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; /** * ssh工具類 * */ public class sshexcutecommandhelper { session session = null ; channelexec openchannel = null ; /** * @param host 主機(jī)ip * @param name 用戶名 * @param pwd 密碼 * @param port ssh端口 */ public sshexcutecommandhelper(string host, string user, string pwd, int port) { jsch jsch = new jsch(); try { session = jsch.getsession(user, host, port); java.util.properties config = new java.util.properties(); config.put( "stricthostkeychecking" , "no" ); session.settimeout( 1000 ); session.setconfig(config); session.setpassword(pwd); } catch (jschexception e) { e.printstacktrace(); } } /** * 是否連接成功,調(diào)用如果不需要調(diào)用execcommand方法那么必須調(diào)用 disconnect方法關(guān)閉session * @return */ public boolean canconnection(){ try { session.connect(); return true ; } catch (jschexception e) { e.printstacktrace(); return false ; } } /** * 關(guān)閉連接 */ public void disconnect(){ if (openchannel != null && !openchannel.isclosed()) { openchannel.disconnect(); } if (session != null && session.isconnected()) { session.disconnect(); } } /** * 執(zhí)行命令 * @param command * @return */ public string execcommand(string command) { stringbuffer result = new stringbuffer(); try { if (!session.isconnected()){ session.connect(); } openchannel = (channelexec) session.openchannel( "exec" ); openchannel.setcommand(command); //int exitstatus = openchannel.getexitstatus(); openchannel.connect(); inputstream in = openchannel.getinputstream(); bufferedreader reader = new bufferedreader( new inputstreamreader(in)); string tmpstr = "" ; while ((tmpstr = reader.readline()) != null ) { result.append( new string(tmpstr.getbytes( "gbk" ), "utf-8" )).append( "\n" ); } } catch (exception e) { e.printstacktrace(); result.append(e.getmessage()); } finally { disconnect(); } return result.tostring(); } /** * 解析 * @param result * @return */ public list<list<string>> parseresult(string result){ list<list<string>> parseresult = new arraylist<list<string>>(); list<string> list = null ; // for (string line : result.split( "\n" )) { list = new arraylist<string>(); string[] columns = {}; //這個(gè)是針對(duì)df命令的 [mounted on] 其實(shí)就一個(gè),如果用空格就會(huì)分割出兩個(gè) if (line.contains( "mounted " )){ columns = line.replace( "mounted " , "mounted-" ).split( " " ); } else { columns = line.split( " " ); } for (string column : columns) { if (! " " .equals(column) && ! "" .equals(column)) { list.add(column); } } parseresult.add(list); } return parseresult; } //測(cè)試 /* public static void main(string args[]) { sshexcutecommandhelper execute = new sshexcutecommandhelper("192.168.175.128", "root", "123456", 22); system.out.println("是否連接成功"+execute.canconnection()); string s = execute.execcommand("free -m"); system.out.println("解析前"); system.out.println(s); system.out.println("解析后"); list<list<string>> parseresult = execute.parseresult(s); for (list<string> l : parseresult) { system.out.println(l); } }*/ } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package com.save.until; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.text.simpledateformat; import java.util.date; import org.slf4j.logger; import org.slf4j.loggerfactory; import com.save.action.inspaction; /** * * @author zhangzhuo * */ public class writeuntil { final static logger logger = loggerfactory.getlogger(writeuntil. class ); /** * 新建文件夾,并創(chuàng)建文件寫入數(shù)據(jù) */ public static void createfile(string basepath,string filepath, string filename, string input) { string[] dirs = filepath.split( "/" ); string temppath = basepath; for (string dir : dirs) { if ( null == dir || "" .equals(dir)) continue ; temppath += "\\" + dir; } //文件夾判斷 file dir = new file(temppath); //"d:\\test_dir" if (dir.exists()) { if (dir.isdirectory()) { logger.info( "文件夾存在" ); } else { logger.info( "同名文件存在,無(wú)法創(chuàng)建目錄" ); } } else { logger.info( "文件夾不存在,開始創(chuàng)建" ); dir.mkdirs(); } //文件判斷 file file = new file(temppath+filename); //"d:\\test_file.txt" if (file.exists()) { logger.info(filename+ "已存在" ); } else { logger.info(filename+ "文件不存在,開始創(chuàng)建" ); try { file.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } } //寫入數(shù)據(jù) //方法一、每次寫入覆蓋之前的 /* try { fileoutputstream fos = new fileoutputstream(temppath+filename); fos.write(input.getbytes()); fos.close(); } catch (exception e) { e.printstacktrace(); }*/ try { fileoutputstream fos = new fileoutputstream(temppath+filename, true); fos.write(input.getbytes()); fos.close(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } //測(cè)試 /* public static void main(string[] args) { //createfile("e:\\log", "2014/16/2/", "\\2020.txt", "hahha"); date date = new date(); simpledateformat formatter = new simpledateformat("yyyy/mm/dd"); string datestring = formatter.format(date); system.out.println(datestring); }*/ } |
applicationcontext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<beans xmlns= "http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置作業(yè)類 --> <bean id= "inspaction" class = "org.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean" > <property name= "targetobject" > <bean class = "com.save.action.inspaction" /> </property> <property name= "targetmethod" value= "execute" /> <property name= "concurrent" value= "false" /><!-- 作業(yè)不并發(fā)調(diào)度 --> </bean> <!-- 配置觸發(fā)器 --> <bean id= "crontrigger" class = "org.springframework.scheduling.quartz.crontriggerbean" > <property name= "jobdetail" ref= "inspaction" /> <!-- 每天 7 : 00 運(yùn)行一次 --> <property name= "cronexpression" value= "0 0 07 * * ?" /> </bean> <!-- 配置調(diào)度工廠 --> <bean class = "org.springframework.scheduling.quartz.schedulerfactorybean" > <property name= "triggers" > <list> <ref bean= "crontrigger" /> </list> </property> </bean> </beans> |
config.properties
1
2
3
4
|
#測(cè)試用服務(wù)器 192.168 . 175.128 ={ "username" : "root" , "password" : "123456" , "shell" : "ps -ef|grep mongo\n,df -h\n, free -m\n, top\n" , "server" : "mongod" } 192.168 . 175.129 ={ "username" : "root" , "password" : "123456" , "shell" : "ps -ef|grep redis\n,df -h\n, free -m\n, top\n" , "server" : "mongod" } log4j.properties |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#指定根logger,及日志輸出級(jí)別 #大于等于該級(jí)別的日志將被輸出( debug < info < warn < error < fatal ),設(shè)為off可以關(guān)閉日志 log4j.rootlogger=info, a1,a2 #指定log輸出目的,這里設(shè)為輸出日志到指定目錄的文件my.log中 log4j.appender.a1=org.apache.log4j.fileappender log4j.appender.a1.file=e:\\save\\log\\xj.log #指定日志信息的格式 log4j.appender.a1.layout=org.apache.log4j.patternlayout log4j.appender.a1.layout.conversionpattern=%r %d{yyyy-mm-dd hh:mm:ss} %c %p -%m%n #把a(bǔ)2輸出到控制臺(tái) log4j.appender.a2=org.apache.log4j.consoleappender log4j.appender.a2.layout=org.apache.log4j.patternlayout log4j.appender.a2.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss} %c %p -%m%n |
pom.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.save</groupid> <artifactid>save-xj</artifactid> <packaging>war</packaging> <version> 0.0 . 1 -snapshot</version> <name>save-xj maven webapp</name> <url>http: //maven.apache.org</url> <dependencies> <!-- ssh連接 --> <dependency> <groupid>com.jcraft</groupid> <artifactid>jsch</artifactid> <version> 0.1 . 53 </version> </dependency> <!-- json轉(zhuǎn)換 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2 . 31 </version> </dependency> <!-- spring的包 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version> 3.1 . 1 .release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version> 3.1 . 1 .release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version> 3.1 . 1 .release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version> 3.0 . 5 .release</version> </dependency> <!-- 定時(shí)任務(wù) --> <dependency> <groupid>org.quartz-scheduler</groupid> <artifactid>quartz</artifactid> <version> 1.8 . 5 </version> </dependency> <!-- 日志的包 --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version> 1.2 . 17 </version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version> 1.7 . 21 </version> </dependency> <!-- 郵件 --> <dependency> <groupid>com.sun.mail</groupid> <artifactid>javax.mail</artifactid> <version> 1.4 . 4 </version> </dependency> <dependency> <groupid>javax.mail</groupid> <artifactid>mail</artifactid> <version> 1.4 </version> </dependency> <!-- https: //mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version> 3.4 </version> </dependency> </dependencies> <build> <plugins> <!-- 配置tomcat插件 --> <plugin> <groupid>org.apache.tomcat.maven</groupid> <artifactid>tomcat7-maven-plugin</artifactid> <version> 2.2 </version> <configuration> <port> 8080 </port> <path>/</path> </configuration> </plugin> </plugins> </build> </project> |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。