001    package org.hackystat.systemstatus.pingmail;
002    
003    import java.util.Date;
004    import java.util.Properties;
005    
006    import javax.mail.Message;
007    import javax.mail.Session;
008    import javax.mail.Transport;
009    import javax.mail.internet.InternetAddress;
010    import javax.mail.internet.MimeMessage;
011    
012    import org.hackystat.sensorbase.client.SensorBaseClient;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
014    import org.hackystat.utilities.stacktrace.StackTrace;
015    
016    /**
017     * Demonstration class to show a simple form of service monitoring. 
018     * Provides a main class that is invoked with a mail server, a SensorBase URL, and an email
019     * address. The hackystat server is pinged and an email is generated indicating whether the ping was
020     * successful. If so, the server is up and accepting requests.  
021     * 
022     * @author Philip Johnson
023     */
024    public class PingMail {
025    
026      /**
027       * When invoked, this main class pings the passed server and sends an email indicating whether the
028       * ping was successful or not to the passed email address via the passed mail server.
029       * 
030       * @param args Takes three string arguments, a hackystat server, a mail server, and an email.
031       */
032      public static void main(String[] args) {
033        
034        // Make sure we have correct number of args. 
035        if (!(args.length == 3)) {
036          System.out.println("java -jar pingmail.jar <sensorbase> <mail server> <email>");
037          return;
038        }
039        // Assign the args to variables for readability. 
040        String sensorbase = args[0];
041        String mailServer = args[1];
042        String email = args[2];
043        // Echo what we're going to do. 
044        System.out.println("Contacting sensorbase server at: " + sensorbase);
045        System.out.println("Emailing results to " + email);
046        System.out.println("Using mail server " + mailServer);
047        // For demo purposes, use the pre-defined test user and password. 
048        String user = "TestUser@hackystat.org";
049        String password = "TestUser@hackystat.org";
050        
051        // Start doing some checks. 
052        boolean isHost = SensorBaseClient.isHost(sensorbase);
053        boolean isRegistered = SensorBaseClient.isRegistered(sensorbase, user, password);
054        SensorBaseClient client = new SensorBaseClient(sensorbase, user, password);
055        SensorDataIndex index;
056        boolean hasData;
057        try {
058          index = client.getSensorDataIndex(user);
059          hasData = (index.getSensorDataRef().size() > 0);
060        }
061        catch (Exception e) {
062          hasData = false;
063        }
064        // Create report on results. 
065        String hostReport = "Attempt to contact " + sensorbase + (isHost ? " succeeds" : " fails");
066        String userReport = "Attempt to login " + user + (isRegistered ? " succeeds" : " fails");
067        String dataReport = "Attempt to get data " + (hasData ? "succeeds" : "fails");
068        String report = hostReport + "\n" + userReport + "\n" + dataReport;
069        System.out.println(report);
070        sendMail(mailServer, email, "Hackystat PingMail (" + (hasData ? "OK" : "FAIL") + ")" , report);
071      }
072    
073      /**
074       * Sends email to the userEmail.
075       * @param mailServer The mail server. 
076       * @param userEmail The userEmail.
077       * @param subject The subject of the email.
078       * @param body The body of the email.
079       */
080      private static void sendMail(String mailServer, String userEmail, String subject, String body) {
081        Properties props = new Properties();
082        props.put("mail.smtp.host", mailServer);
083        Session session = Session.getInstance(props);
084        try {
085          Message msg = new MimeMessage(session);
086          InternetAddress userAddress = new InternetAddress(userEmail);
087          InternetAddress[] adminAddressArray = { userAddress };
088          msg.setFrom(userAddress);
089          msg.setReplyTo(adminAddressArray);
090          msg.setRecipient(Message.RecipientType.TO, userAddress);
091          msg.setSubject(subject);
092          msg.setSentDate(new Date());
093          msg.setText(body);
094          Transport.send(msg);
095          System.out.println("Email regarding system status sent to " + userEmail);
096        }
097        catch (Exception e) {
098          String msg = "Error sending mail. Perhaps JavaMail is not installed correctly? " +
099          "See http://code.google.com/p/hackystat/wiki/InstallingJavaMail for instructions." +
100          StackTrace.toString(e);
101          System.err.println(msg);
102        }
103      }
104    }