package introscope import java.text.SimpleDateFormat import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSetMetaData; import groovy.text.SimpleTemplateEngine class IntroscopeService { private static Map openConnections = Collections.synchronizedMap(new HashMap()); static { Class.forName("com.wily.introscope.jdbc.IntroscopeDriver"); } def void closeIntroscopeConnection(iscopeConnection) { println "Closing Introscope Connection" try { if (iscopeConnection != null) iscopeConnection.close() } catch(Exception e1) {} iscopeConnection = null } //-------------------------------------------------------------------- // // getIntroscope data // // retrieve data from Introscope // // // startDate // endDate // // agent='.*Custom.*' // metric='Alerts.*' // timestamp between '$start' and '$end' // period=60 // //-------------------------------------------------------------------- def List getData(IntroscopeServer svr, String agents, String metrics, Date startDate, Date endDate, int period, boolean allowdups) { //println "getOpenAlerts" boolean introscopeConnectFail = false String introscopeConnectFailMsg = "" //Connection con = null; Statement stmt = null; ResultSet rs = null; Connection iscopeConnection = null SimpleDateFormat formatter = null; String datePref = svr.datefmt if (datePref == "DDMM") formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss") else formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss") //java.util.Date now = new java.util.Date() //java.util.Date oneminago = new java.util.Date(now.getTime() - 60000); String start = formatter.format(startDate) String end = formatter.format(endDate) List data = [] //println "--- Get Introscope Alerts -- $start $end ----" try { // Using JDBC instead of Groovy SQL because Groovy uses Prepared statements and Iscope // jdbc driver doesnt like them iscopeConnection = openConnections.get(svr.source); if (iscopeConnection == null) { String iscopeHost = svr.host String iscopePort = svr.port String iscopeUser = svr.iuser String iscopePwd = svr.ipassword iscopeConnection = DriverManager.getConnection("jdbc:introscope:net//Admin:@${iscopeHost}:${iscopePort}", iscopeUser, iscopePwd); openConnections.put(svr.source,iscopeConnection) } stmt = iscopeConnection.createStatement(); //rs = stmt.executeQuery("select * from metric_data where agent='.*Custom.*' and metric='Alerts.*' and timestamp between '$start' and '$end' period=60"); String query = "select * from metric_data where agent='${agents}' and metric='${metrics}' and timestamp between '$start' and '$end' period=${period}" println "Poll Introscope with $query" rs = stmt.executeQuery(query); //ResultSetMetaData rsmd = rs.getMetaData() //int numberOfColumns = rsmd.getColumnCount() //println "no cols = $numberOfColumns" //for ( i in 1..numberOfColumns ) { // println " Column $i = ${rsmd.getColumnName(i)}" //} while (rs.next()) { //for ( i in 1..numberOfColumns ) { // println " Column $i = ${rsmd.getColumnName(i)} = ${rs.getString(i)}" //} Map datamap = [:] datamap["source"] = svr.source datamap["Host"] = "${rs.getString('Host')}" datamap["Domain"] = "${rs.getString('Domain')}" //datamap["Process"] = "${rs.getString('Process')}" //datamap["AgentName"] = "${rs.getString('AgentName')}" datamap["Resource"] = "${rs.getString('Resource')}" datamap["MetricName"] = "${rs.getString('MetricName')}".trim() if (rs.getString("Type") == "4101") { datamap["Value"] = "${rs.getString('String_Value')}" } else { datamap["Value"] = "${rs.getString('Value')}" datamap["Min"] = "${rs.getString('Min')}" datamap["Max"] = "${rs.getString('Max')}" } //saveAlert["Count"] = "${rs.getString('Count')}" if ( data.any { datamap["MetricName"] == it.MetricName } ) { if ( allowdups ) data << datamap } else data << datamap } try{if (rs != null) rs.close(); rs = null } catch(Exception e1){} try{if (stmt != null) stmt.close(); stmt = null } catch(Exception e1){} } catch(SQLException e) { introscopeConnectFail = true introscopeConnectFailMsg = "Error connecting to Introscope $e" println introscopeConnectFailMsg e.printStackTrace() try{ if (iscopeConnection != null) iscopeConnection.close();} catch(Exception e1){} iscopeConnection = null openConnections.remove(svr.source) } catch(ClassNotFoundException e) { introscopeConnectFail = true introscopeConnectFailMsg = "ClassNotFoundException - You need the IntroscopeJDBC.jar driver in the classpath $e" println introscopeConnectFailMsg e.printStackTrace(); } catch(Exception e) { introscopeConnectFail = true introscopeConnectFailMsg = "Unknown Exception $e" println "Unknown Exception $e "; e.printStackTrace(); try{if (iscopeConnection != null) iscopeConnection.close();} catch(Exception e1){} iscopeConnection = openConnections[svr.source] = null; iscopeConnection = null openConnections.remove(svr.source) } finally { try{if (rs != null) rs.close(); rs = null } catch(Exception e1){} try{if (stmt != null) stmt.close(); stmt = null } catch(Exception e1){} } println "FOUND ${data.size()} ROWS from ${svr.source} $start to $end " if (introscopeConnectFail) { println "Introscope problem - Exception received" return null } else { } //println data return data } }