歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux 端Tomcat監控程序(JAVA版)

1.一個判斷服務是否啟動的監控類

  1. package com.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.LineNumberReader;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. publicclass RequestUtil {
  12. /**
  13. * 網站是否正常運轉
  14. * @return
  15. */
  16. publicstaticboolean isWebSiteNormal(String urlStr){
  17. try {
  18. if(urlStr != null && urlStr.indexOf("://") <= 0 ){
  19. urlStr = "http://"+urlStr;
  20. }
  21. URL url = new URL(urlStr);
  22. URLConnection con = url.openConnection();
  23. BufferedReader in = new BufferedReader(new java.io.InputStreamReader(con
  24. .getInputStream()));
  25. con.setConnectTimeout(2000);
  26. con.setReadTimeout(6000);
  27. String s = "";
  28. while ((s = in.readLine()) != null) {
  29. if (s.length() > 0) {
  30. returntrue;
  31. }
  32. }
  33. in.close();
  34. } catch (Exception e) {
  35. LogUtil.writerE(e);
  36. }
  37. returnfalse;
  38. }
  39. /**
  40. * 查看Tomcat7服務是否正在運行
  41. * @return
  42. */
  43. publicstaticboolean isTomcatServiceAlive(){
  44. try {
  45. Process process = Runtime.getRuntime().exec("lsof -i tcp:80");
  46. InputStreamReader ir = new InputStreamReader(process.getInputStream());
  47. LineNumberReader input = new LineNumberReader(ir);
  48. String line;
  49. while ((line = input.readLine()) != null) {
  50. if(line != null && !"".equals(line)){
  51. returntrue;
  52. }
  53. }
  54. } catch (java.io.IOException e) {
  55. LogUtil.writerE(e);
  56. }
  57. returnfalse;
  58. }
  59. /**
  60. * 獲取tomcat監聽的進程
  61. * @return
  62. */
  63. publicstatic String getTomcatTcpIpListener(){
  64. StringBuffer str = new StringBuffer();
  65. try {
  66. Process process = Runtime.getRuntime().exec("lsof -i tcp:80");
  67. InputStreamReader ir = new InputStreamReader(process.getInputStream());
  68. LineNumberReader input = new LineNumberReader(ir);
  69. String line;
  70. while ((line = input.readLine()) != null) {
  71. if(line != null && !"".equals(line)){
  72. str.append(line).append("\n");
  73. }
  74. }
  75. } catch (java.io.IOException e) {
  76. LogUtil.writerE(e);
  77. }
  78. return str.toString();
  79. }
  80. /**
  81. * 獲取tomcat監聽的進程
  82. * @param content
  83. * @return
  84. */
  85. publicstatic Set<Integer> getUids(String content){
  86. Pattern pattern = Pattern.compile("java\\s*[0-9]*");
  87. Matcher matcher = pattern.matcher(content);
  88. Pattern pattern2 = Pattern.compile("[0-9]+");
  89. Set<Integer> uidSet = new HashSet<Integer>();
  90. while(matcher.find()){
  91. Matcher matcher2 = pattern2.matcher(matcher.group());
  92. if(matcher2.find()){
  93. if(matcher2.group() != null && !"".equals(matcher2.group())){
  94. uidSet.add(Integer.parseInt(matcher2.group()));
  95. }
  96. }
  97. }
  98. return uidSet;
  99. }
  100. publicstaticboolean isWebCanVisite(String url) {
  101. if(url != null && url.lastIndexOf("://") <= 0 ){
  102. url = "http://"+url;
  103. }
  104. LogUtil.writerINF("Check Url:"+url);
  105. String content = HttpRequestProxy.doGet(url,"utf-8");
  106. // Pattern pattern = Pattern.compile("<title>\\s*[^<>][^<>]*\\s*</title>");
  107. // Pattern pattern2 = Pattern.compile("[^x00-xff]*");
  108. if(content != null){
  109. returntrue;
  110. // Matcher matcher = pattern.matcher(content);
  111. // String temp = "";
  112. // while(matcher.find()){
  113. // temp = matcher.group();
  114. // temp = temp.trim();
  115. // if(temp.length() > 16){
  116. // temp = temp.substring(7, temp.length()-8);
  117. // Matcher matcher2=pattern2.matcher(temp);
  118. // if(matcher2.find()){
  119. // return true;
  120. // }
  121. // }
  122. // }
  123. }
  124. returnfalse;
  125. }
  126. }

2.一個Tomcat服務控制工具類

  1. package com.util;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. publicclass TomcatUtil {
  5. /**
  6. * 關閉tomcat7服務
  7. * @return
  8. */
  9. publicstaticboolean stopTomcat() {
  10. try {
  11. String commandstr = "/usr/local/tomcat7/bin/shutdown.sh";
  12. Process p;
  13. LogUtil.writerINF("Tomcat7 ShutDown");
  14. p = Runtime.getRuntime().exec(commandstr);
  15. if (p.waitFor() == 0){
  16. LogUtil.writerINF("Tomcat7 ShutDown over");
  17. returntrue;
  18. }else{
  19. LogUtil.writerINF("Tomcat7 ShutDown fail");
  20. returnfalse;
  21. }
  22. } catch (Exception e) {
  23. LogUtil.writerINF("Tomcat7 ShutDown fail");
  24. LogUtil.writerE(e);
  25. returnfalse;
  26. }
  27. }
  28. /**
  29. * 開啟服務
  30. * @return
  31. */
  32. publicstaticboolean startTomcat() {
  33. try {
  34. String commandstr = "/usr/local/tomcat7/bin/startup.sh";
  35. Process p;
  36. LogUtil.writerINF("Tomcat7 startup");
  37. p = Runtime.getRuntime().exec(commandstr);
  38. if (p.waitFor() == 0){
  39. LogUtil.writerINF("Tomcat7 startup over");
  40. returntrue;
  41. }else{
  42. LogUtil.writerINF("Tomcat7 startup fail");
  43. returnfalse;
  44. }
  45. } catch (Exception e) {
  46. LogUtil.writerINF("Tomcat7 startup fail");
  47. LogUtil.writerE(e);
  48. returnfalse;
  49. }
  50. }
  51. /**
  52. * 殺掉進程
  53. * @param uidSet
  54. * @return
  55. */
  56. publicstaticint killUid(Set<Integer> uidSet){
  57. Iterator<Integer> it = uidSet.iterator();
  58. int successCount = 0 ;
  59. while(it.hasNext()){
  60. int uid = it.next();
  61. try {
  62. String commondStr = "kill -9 "+uid;
  63. Process p = Runtime.getRuntime().exec(commondStr);
  64. if (p.waitFor() == 0){
  65. LogUtil.writerINF("java "+uid+" be Killed");
  66. successCount ++;
  67. }else{
  68. LogUtil.writerINF("java "+uid+" be Kill Failed");
  69. }
  70. } catch (Exception e) {
  71. LogUtil.writerE(e);
  72. }
  73. }
  74. return successCount;
  75. }
  76. }
Copyright © Linux教程網 All Rights Reserved