private static void monitor() { long nowInMillis; long dailyTimeStampInMillis; init(); String subject = "HTTP Uptime monitor started: " + remoteHost + "."; String message = "Monitoring from " + Server.getHostName() + Names.NEW_LINE; message += "URL: " + url + Names.NEW_LINE; message += "remote host: " + remoteHost + Names.NEW_LINE; message += "local down command:" + downCommand + " " + remoteHost + Names.NEW_LINE; message += "local up command:" + upCommand + " " + remoteHost + Names.NEW_LINE; try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception e) { e.printStackTrace(); } dailyTimeStamp = getToday(); dailyTimeStampInMillis = dailyTimeStamp.getTimeInMillis(); int year, month, day; year = dailyTimeStamp.get(Calendar.YEAR); month = dailyTimeStamp.get(Calendar.MONTH); day = dailyTimeStamp.get(Calendar.DAY_OF_MONTH); dailyTimeStamp = new GregorianCalendar(year, month, day, 5, 0, 0); Calendar currentTimeStamp; int i = 0; while (true) { i++; try { int code = getResponseCode(); for (int x = 0; x < (i % 8); x++) System.out.print("."); { } // System.out.println("Http Response=" + HTTPCodes[code] + "[" +code + "] for " + url); if (code < 200 || code > 204) restartServer(code); } catch (Exception e) { e.printStackTrace(); restartServer(601); } Sleep.sleep(POLLING_INTERVAL * 1000); currentTimeStamp = new GregorianCalendar(); nowInMillis = currentTimeStamp.getTimeInMillis(); if (nowInMillis > (dailyTimeStampInMillis + (1000 * 60 * 60 * 24))) { dailyTimeStamp = getToday(); dailyTimeStampInMillis = dailyTimeStamp.getTimeInMillis(); subject = "HTTP Uptime monitor running: " + remoteHost + "."; message = "Monitoring from " + Server.getHostName() + Names.NEW_LINE; message += "URL: " + url + Names.NEW_LINE; message += "remote host: " + remoteHost + Names.NEW_LINE; message += "local down command:" + downCommand + " " + remoteHost + Names.NEW_LINE; message += "local up command:" + upCommand + " " + remoteHost + Names.NEW_LINE; try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception e) { e.printStackTrace(); } } } }
public static void main(String[] args) { // Create a URLConnection object for a URL // remotehost = "zws-nt4"; // String url = // "http://"+remotehost+":80/login.do?username=admin&password=admi&event=authenticate"; // String upCommand= "c:\\zws\\bin\\uptime\\bring-zws-up.bat"; // String downCommand = "c:\\zws\\bin\\uptime\\bring-zws-down.bat"; { } // System.out.println("args:" + args.length); url = StringUtil.trimQuotes(args[0]); remoteHost = StringUtil.trimQuotes(args[1]); downCommand = StringUtil.trimQuotes(args[2]); upCommand = StringUtil.trimQuotes(args[3]); smtpHost = StringUtil.trimQuotes(args[4]); String recipients = StringUtil.trimQuotes(args[5]); mailTo = EMail.parseAddresses(recipients); String d = smtpHost; while (d.indexOf('.') != d.lastIndexOf('.')) d = d.substring(d.indexOf('.') + 1); FROM += d; { } // System.out.println(args[0]); { } // System.out.println(args[1]); { } // System.out.println(args[2]); { } // System.out.println(args[3]); { } // System.out.println(args[4]); { } // System.out.println(args[5]); { } // System.out.println("________________________________________________________________________________"); { } // System.out.println(" "); { } // System.out.println("Zero Wait-State HTTP Uptime monitor started"); { } // System.out.println("________________________________________________________________________________"); { } // System.out.println("URL: " + url); { } // System.out.println("remote host:" + remoteHost); { } // System.out.println("local down command:" + downCommand + " " + remoteHost); { } // System.out.println("local up command:" + upCommand+ " " + remoteHost); { } // System.out.println("smtp host:" + smtpHost); { } // System.out.println("mail recipients:" + recipients); { } // System.out.println("================================================================================"); monitor(); }
private static void restartServer(int err) { String subject = "Restarting Server: " + remoteHost + " [" + err + "]!!"; String message = "Description of last Response:" + Names.NEW_LINE; message += "URL: " + url + Names.NEW_LINE; message += "Response: " + HTTPCodes[err] + "[" + err + "]" + Names.NEW_LINE; message += "Attempting to restart server: " + remoteHost + Names.NEW_LINE; try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception e) { e.printStackTrace(); } { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); { } // System.out.println("Attempting restart server: " + remoteHost); ExecShell down = new ExecShell(); down.setExecutable(downCommand); down.addCommandLineArgument(remoteHost, false); try { { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); { } // System.out.println("Bringing server down on " + remoteHost + " with " + downCommand); down.execute(); down.waitFor(); Sleep.sleep(SERVER_SHUTDOWN_DURATION * 1000); } catch (Exception e) { subject = "ERROR Restarting Server on " + remoteHost; message = "Could not execute bring down command: " + downCommand + Names.NEW_LINE; try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception ex) { ex.printStackTrace(); } { } // System.out.println("Could not execute bring down command: " + downCommand); { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } ExecShell up = new ExecShell(); up.setExecutable(upCommand); up.addCommandLineArgument(remoteHost, false); try { { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); { } // System.out.println("Bringing server up on " + remoteHost + " with " + upCommand); up.execute(); up.waitFor(); Sleep.sleep(SERVER_STARTUP_DURATION * 1000); int code = getResponseCode(); if (code > 199 && code < 206) { subject = "Server is back up: " + remoteHost + "[" + code + "]."; message = "Http Response=" + HTTPCodes[code] + "[" + code + "] for " + url; { } // System.out.println(subject); { } // System.out.println(message); try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception ex) { ex.printStackTrace(); } } else { subject = "Could not bring server back up" + " [" + code + "]!"; message = "Http Response=" + HTTPCodes[code] + "[" + code + "] for " + url; { } // System.out.println(subject); { } // System.out.println(message); EMail.send(FROM, mailTo, subject, message, smtpHost); } { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } catch (Exception e) { subject = "ERROR Restarting Server on " + remoteHost; message = "Could not execute bring up command: " + upCommand; { } // System.out.println(subject); { } // System.out.println(message); { } // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); try { EMail.send(FROM, mailTo, subject, message, smtpHost); } catch (Exception ex) { ex.printStackTrace(); } } }