linux环境下监听nginx状态页面,判断tomcat是否down机,给管理员发短信和邮件

来源:互联网 发布:轻松赚软件怎么样 编辑:程序博客网 时间:2024/06/04 20:38
package com.www.ads.webWatch;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;







import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.www.ads.cqhySms.SendSmsRunnable;
import com.www.ads.sms.HttpService;
import com.www.ads.sms.HttpServiceLB;
import com.www.ads.sms.HttpServiceMS;
import com.www.ads.sms.HttpServiceST;
import com.www.ads.sms.HttpServiceUMS;
import com.www.ads.sms.HttpServiceXYC;
import com.www.ads.util.tool.TimeUtil;
import com.www.ads.util.tool.TimeUtil_1;
import com.www.ads.web.model.SmsRecord;

public class WatchTomcat {
private static final Logger logger = LoggerFactory.getLogger(WatchTomcat.class);
private  Tomcatconfig tc = new Tomcatconfig();
private String []  strs = tc.getFromKey();
private  String dir = strs[10];
 private  String sendMobiles = strs[9];
 private  String channel = strs[11];
 private  String user = strs[2];;
 private  String to = strs[1];
 public   Config conf=null; 
 public  com.www.ads.sms.HttpServiceXYC httpServiceXYC;
 public com.www.ads.sms.HttpServiceUMS httpServiceUMS;
 public com.www.ads.sms.HttpService httpService;
public static void main(String[] args){
if (args.length != 2) {
System.out.println("Usage: java watchTomcat spring.xml sms.conf tomcatlistening.conf");
return;
}
System.out.println("启动--------------------------------");
WatchTomcat watch = new  WatchTomcat(args[0]);
watch.watch();
//watch();
}
public WatchTomcat(String args){
 conf=new Config();
 conf.init(args);  
}
private  void watch(){
int i = 0;
Document doc =null;
//  File input = new File("/data/ads/bin/webWatch/tomcatstatus.html");
//  Document doc = null;
try {
doc = Jsoup.connect("http://localhost/wwwTomcatStatus").get();
} catch (Exception e) {
//如果访问失败,尝试三次
while(i<=3){
try {
doc = Jsoup.connect("http://localhost/wwwTomcatStatus").get();
break;
} catch (Exception e1) {
e1.printStackTrace();
logger.info("==============http://localhost/wwwTomcatStatus  页面访问失败");
}
i++;
}
int sum = readCount();
//nginx程序异常
if(sum<3){
if(i>3){
String msg = "http://localhost/wwwTomcatStatus  页面访问失败!";
sendMail(msg);
sendMessage();
int count = readCount();
count(count+1);
}
}
e.printStackTrace();
}
if(doc==null)
logger.debug("dos=null********");
// else 
// logger.debug("doc=********"+doc.toString());
 Elements elements =  doc.select("table").first().select("tr");
 Iterator<Element> it=elements.iterator();
 StringBuilder sb =new StringBuilder();
 it.next();
 while(it.hasNext()){
 Elements td=it.next().select("td");
//遍历页元素,取得down机的tomcat信息sb.append(td.get(0).text()).append(",").append(td.get(1).text()).append(",").append(td.get(2).text()).append(",").append(td.get(3).text()).append("\n");
 if(td.get(3).text().equalsIgnoreCase("down")){
sb.append(td.get(0).text()).append(",").append(td.get(1).text()).append(",").append(td.get(2).text()).append(",").append(td.get(3).text()).append("\n");
 }
 }
 System.out.println("----------"+sb.toString());
 String msg = sb.toString();
//有down机的tomcat就给管理员发短信和邮件,晚上2点到早上6点不发短信,在问题被处理之前最多发三次信息
 if(!msg.isEmpty()){
 int sum = readCount();
if(sum<3){
SimpleDateFormat df = new SimpleDateFormat("HHmm");//设置日期格式
String date = df.format(new Date());
int dat = Integer.parseInt(date);
if(!(200<=dat&&dat<=600) ){
 sendMessage();
}
 sendMail(msg);
 int count = readCount();
count(count+1);
}
 }else{
//如果没有问题,计数器归零
 count(0);
 }
}
private  void count(int sum){

try {  
File f = new File(dir);
   FileOutputStream os = new FileOutputStream(f) ;
   os.write((""+sum).getBytes());
os.flush();
os.close();
  } catch (IOException e) {  
   e.printStackTrace();  
  }
}
private  int readCount(){
FileReader  fr=null;
BufferedReader br = null;
String sb = new String();
try {
File f = new File(dir);
if(!f.exists()){
f.createNewFile();
}
fr = new FileReader(f); 
br = new BufferedReader(fr);
sb = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Integer.parseInt(sb==null||sb.isEmpty()?"0":sb);
}
private  void sendMail(String msg){
SendMail  mail = new SendMail();
mail.setAddress(user,to.split(","),"tomcat挂了"); 
mail.send(msg);
}
private void sendMessage(){
SmsRecord smsre = new SmsRecord();
logger.info("##########短信通道"+channel);
smsre.setChannel(Integer.parseInt(channel));
SimpleDateFormat df = new SimpleDateFormat("HHmm");//设置日期格式
smsre.setContext("您的动态密码为“0"+df.format(new Date())+"”,有效期1天。"); 
smsre.setMobile(sendMobiles);
httpServiceXYC=new HttpServiceXYC(conf.service_url_xyc,conf.xycEnterpriseID, conf.xycLoginName, conf.xycPassword);
httpServiceUMS = new HttpServiceUMS(conf.service_url_ums, conf.umsSpCode, conf.umsLoginName, conf.umsPassword);
httpService = new HttpService(conf.service_url, conf.SEQ, conf.USERNAME, conf.PASSWORD, conf.EXTNO,conf.ECODE);
int channel=smsre.getChannel();
int value=9999;//短信通道不对
System.out.println("##########"+smsre.channel);
if(channel==5){//欣易辰短信通道
value=sendSms_XYC(smsre);
}else if(channel==2||channel==8){//用lxt 短信通道
value=sendSms_lxt(smsre);
}else if(channel==9){//广东联通
value=sendSms_UMS(smsre);
}
logger.info("=========报警短信 发送状态"+value);
}
private   int sendSms_XYC(SmsRecord sms){
int value =9998;
httpServiceXYC.requestParam.put("mobiles", sms.getMobile());
httpServiceXYC.requestParam.put("content", sms.getContext());
value=httpServiceXYC.post();
return value;
}
private  int sendSms_lxt(SmsRecord sms){
int value =9998;
httpService.requestParam.put("MOBILE", sms.getMobile());
httpService.requestParam.put("CONTENT", sms.getContext());
value=httpService.post();
return value;
}
private  int sendSms_UMS(SmsRecord sms){
int value =9998;
httpServiceUMS.requestParam.put("UserNumber", sms.getMobile());
httpServiceUMS.requestParam.put("MessageContent", sms.getContext());
value=httpServiceUMS.post();
return value;
}
}

nginx
0 0
原创粉丝点击