Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.50 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.Date;
  4. import java.util.Properties;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7.  
  8. public class SqlJobMon {
  9.  
  10.    public static void main(String[] args) {
  11.  
  12.       int count = 0;
  13.       String connectionUrl;
  14.       String datetime;
  15.       String datequery;
  16.       String jobname;
  17.       String test;
  18.       DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  19.       DateFormat dateQuery = new SimpleDateFormat("yyyyMMdd");
  20.       Date date = new Date();
  21.       FileOutputStream out = null; // declare a file output object
  22.       PrintStream p = null; // declare a print stream object
  23.       datetime = dateFormat.format(date);
  24.       datequery = dateQuery.format(date);
  25.  
  26.       String style = "<style type=\"text/css\"> #gradient-style"
  27.             + "{"
  28.             + "   font-family: \"Lucida Sans Unicode\", \"Lucida Grande\", Sans-Serif;"
  29.             + "font-size: 12px;"
  30.             + "margin: 0px;"
  31.             + "width: 1000px;"
  32.             + "text-align: left;"
  33.             + "border-collapse: collapse;"
  34.             + "}"
  35.             + "#gradient-style th"
  36.             + "{"
  37.             + "font-size: 13px;"
  38.             + "font-weight: normal;"
  39.             + "padding: 8px;"
  40.             + "background: #b9c9fe;"
  41.             + "border-top: 2px solid #d3ddff;"
  42.             + "border-bottom: 1px solid #fff;"
  43.             + "color: #039;"
  44.             + "}"
  45.             + "#gradient-style td"
  46.             + "{"
  47.             + "padding: 8px;"
  48.             + "border-bottom: 1px solid #fff;"
  49.             + "color: #669;"
  50.             + "border-top: 1px solid #fff;"
  51.             + "background: #e8edff;"
  52.             + "}"
  53.             + "#gradient-style tfoot tr td"
  54.             + "{"
  55.             + "background: #e8edff;"
  56.             + "font-size: 12px;"
  57.             + "color: #99c;"
  58.             + "}"
  59.             + "#gradient-style tbody tr:hover td"
  60.             + "{"
  61.             + "background: #d0dafd;"
  62.             + "color: #339;"
  63.             + "}"
  64.             + "#gradient-style caption"
  65.             + "{"
  66.             + "font-family: \"Lucida Sans Unicode\", \"Lucida Grande\", Sans-Serif; font-weight: bold; font-size: 13px;color: #039;"
  67.             + "}</style>";
  68.  
  69.       connectionUrl = "jdbc:sqlserver://" + args[0]
  70.             + ":1433;databaseName=msdb;integratedSecurity=false;user="
  71.             + args[1] + ";password=" + args[2];
  72.  
  73.       // Declare the JDBC objects.
  74.       Connection con = null;
  75.       Statement stmt = null;
  76.       ResultSet rsHistory = null, rsRecent = null;
  77.  
  78.       try {
  79.          // Reading values from property file
  80.          String link = new SqlJobMon().readPropertiesFile("HTML_LINK");
  81.          String currentDirectory = new SqlJobMon()
  82.                .readPropertiesFile("HTML_DIR");
  83.          File file = new File(currentDirectory + "sqllog");
  84.          boolean exist = file.exists();
  85.          if (!exist) {
  86.             boolean success = (new File(currentDirectory + "sqllog"))
  87.                   .mkdir();
  88.          }
  89.          out = new FileOutputStream(currentDirectory + "sqllog/" + args[0]
  90.                + "-" + datetime + ".html");
  91.          p = new PrintStream(out);
  92.          // Establish the connection.
  93.          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  94.          con = DriverManager.getConnection(connectionUrl);
  95.  
  96.          // Create and execute an SQL statement For getting history and
  97.          // recent job failed.
  98.  
  99.          String sqlHistory = "select sj.name, sjh.step_name, sjh.run_date, sjh.run_time, sjh.run_status, sjh.message, sjh.server "
  100.                + "from msdb.dbo.sysjobhistory sjh inner join msdb.dbo.sysjobs sj on sjh.job_id = sj.job_id "
  101.                + "inner join msdb.dbo.sysjobsteps sjs on sj.job_id = sjs.job_id and sjh.step_id = sjs.step_id "
  102.                + "where sjh.run_status = 0"
  103.                + "and sj.name ='" + args[3] + "'";
  104.  
  105.          stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  106.                ResultSet.CONCUR_READ_ONLY);
  107.          rsHistory = stmt.executeQuery(sqlHistory);
  108.  
  109.          String sqlRecent = "select top 1 sj.name, sjh.step_name, sjh.run_date, sjh.run_time, sjh.run_status, sjh.message, sjh.server "
  110.                + "from msdb.dbo.sysjobhistory sjh inner join msdb.dbo.sysjobs sj on sjh.job_id = sj.job_id "
  111.                + "inner join msdb.dbo.sysjobsteps sjs on sj.job_id = sjs.job_id and sjh.step_id = sjs.step_id "
  112.                + "and sj.name ='" + args[3] + "'"
  113.                + "and sjh.run_date ='" + datequery + "'"
  114.                + "ORDER BY run_time DESC";
  115.          stmt = null;
  116.          stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  117.                ResultSet.CONCUR_READ_ONLY);
  118.          rsRecent = stmt.executeQuery(sqlRecent);
  119.  
  120. while (rsRecent.next()) {
  121.          if (rsRecent.getString("run_status").equals("0")){
  122.             rsRecent.beforeFirst();
  123.             p
  124.                   .println("<html>\n<head>"
  125.                         + style
  126.                         + "<title>Failed SQL Job </title>"
  127.                         + "</head><table border=1 id=\"gradient-style\">\n<CAPTION>Recent SQL Job Failed For "
  128.                         + args[3]
  129.                         + datetime
  130.                         + "</CAPTION>\n"
  131.                         + "<th>Date</th>\n<th>Time</th>\n<th>Server</th>\n<th>Job_Name</th>\n<th>Step_Name</th>\n<th>Run_Status"
  132.                         + "</th>\n<th>Message</th>\n");
  133.             // Iterate through the data in the result set and display it.
  134.             while (rsRecent.next()) {
  135.                p.println("<tr>");
  136.                p.print("<td>");
  137.                p.print(rsRecent.getString("run_date"));
  138.                p.print("</td>\n");
  139.                p.print("<td>");
  140.                p.print(rsRecent.getString("run_time"));
  141.                p.print("</td>\n");
  142.                p.print("<td>");
  143.                p.print(rsRecent.getString("server"));
  144.                p.print("</td>\n");
  145.                p.print("<td>");
  146.                p.print(rsRecent.getString("name"));
  147.                p.print("</td>\n");
  148.                p.print("<td>");
  149.                p.print(rsRecent.getString("step_name"));
  150.                p.print("</td>\n");
  151.                p.print("<td>");
  152.                p.print(rsRecent.getString("run_status"));
  153.                p.print("</td>\n");
  154.                p.print("<td>");
  155.                p.print(rsRecent.getString("message"));
  156.                p.print("</td>\n");
  157.                p.println("</tr>");
  158.             }
  159.             p.println("</table>");
  160.             p.println("<a href=JavaScript:history.back();>[ back ]</a>");
  161.             System.out.print("<a href=" + link + "/" + args[0] + "-"
  162.                   + datetime + ".html target=main>");
  163.             System.out.print(" CRITICAL: Job Failed" );
  164.             System.out.print("</a>\n");
  165.          } else {
  166.             if (rsRecent.getString("run_status").equals("1")){
  167.                rsRecent.beforeFirst();
  168.                      p
  169.                            .println("<html>\n<head>"
  170.                                  + style
  171.                                  + "<title>Failed SQL Job </title>"
  172.                                  + "</head><table border=1 id=\"gradient-style\">\n<CAPTION>Recent SQL Job Failed For "
  173.                                  + args[3]
  174.                                  + datetime
  175.                                  + "</CAPTION>\n"
  176.                                  + "<th>Date</th>\n<th>Time</th>\n<th>Server</th>\n<th>Job_Name</th>\n<th>Step_Name</th>\n<th>Run_Status"
  177.                                  + "</th>\n<th>Message</th>\n");
  178.  
  179.                      System.out.print("<a href=" + link + "/" + args[0] + "-"
  180.                            + datetime + ".html target=main>");
  181.                      System.out.print(" OK: Nothing Failed");
  182.                      System.out.print("</a>\n");
  183.                      System.out.flush();
  184.                      p.println("No Failed Jobs");
  185.                      p.println("</table>");
  186.                      p.println("<a href=JavaScript:history.back();>[ back ]</a>");
  187.          }
  188.     }
  189. }
  190. }
  191.  
  192.       // Handle any errors that may have occurred.
  193.       catch (Exception e) {
  194.          e.printStackTrace();
  195.       }
  196.  
  197.       finally {
  198.          if (out != null)
  199.             try {
  200.                p.close();
  201.                out.close();
  202.             } catch (Exception e) {
  203.             }
  204.          if (rsRecent != null)
  205.             try {
  206.                rsRecent.close();
  207.             } catch (Exception e) {
  208.             }
  209.  
  210.          if (rsHistory != null)
  211.             try {
  212.                rsHistory.close();
  213.             } catch (Exception e) {
  214.             }
  215.          if (stmt != null)
  216.             try {
  217.                stmt.close();
  218.             } catch (Exception e) {
  219.             }
  220.          if (con != null)
  221.             try {
  222.                con.close();
  223.             } catch (Exception e) {
  224.             }
  225.  
  226.       }
  227.    }
  228.  
  229.    /*
  230.     * To read the property file and return the value for key which is stored in
  231.     * the property file
  232.     */
  233.    public String readPropertiesFile(String key) {
  234.       String value = null;
  235.       try {
  236.          String path = null;
  237.          Properties prop = new Properties();
  238.          File dir1 = new File(".");
  239.          /* To get the current working directory */
  240.          path = dir1.getCanonicalPath();
  241.          prop.load(new FileInputStream(path + "/url.properties"));
  242.          value = prop.getProperty(key);
  243.       } catch (Exception e) {
  244.          e.printStackTrace();
  245.       }
  246.       return value;
  247.    }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement