Advertisement
Guest User

Untitled

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