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(); } } } }
private File getWorkingDirectory(File f) { try { File workingDir = new File( Properties.get(Names.WORKING_DIR) + Names.PATH_SEPARATOR + getOpType() + Names.PATH_SEPARATOR + "work" + getNewCount()); if (!workingDir.exists()) workingDir.mkdirs(); FileUtil.deleteContents(workingDir); return workingDir; } catch (Exception ignore) { // ++ notify of error - maybe out of disk space ignore.printStackTrace(); return null; } }
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(); } } }