[checkmk-commits] Check_MK Git: check_mk: livestatus iReport: now uses KeepAlive on:

git version control git at mathias-kettner.de
Thu Nov 15 14:17:41 CET 2012


Module: check_mk
Branch: master
Commit: e6c25f0a87fe8605fe0d024f811c3e229b57f4ae
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e6c25f0a87fe8605fe0d024f811c3e229b57f4ae

Author: Andreas Boesl <ab at mathias-kettner.de>
Date:   Thu Nov 15 14:16:13 2012 +0100

livestatus iReport: now uses KeepAlive on:

---

 doc/jasperreports/livestatus.jar                   |  Bin 7609 -> 9737 bytes
 doc/jasperreports/src/LivestatusDatasource.java    |   12 ++++-
 .../src/LivestatusFieldsProvider.java              |    9 ++--
 doc/jasperreports/src/LivestatusQueryExecuter.java |   40 +++++++++++++------
 .../src/LivestatusQueryExecuterFactory.java        |   26 +++++++++++-
 5 files changed, 63 insertions(+), 24 deletions(-)

diff --git a/doc/jasperreports/livestatus.jar b/doc/jasperreports/livestatus.jar
index 84fe8d8..1f6e9c7 100644
Binary files a/doc/jasperreports/livestatus.jar and b/doc/jasperreports/livestatus.jar differ
diff --git a/doc/jasperreports/src/LivestatusDatasource.java b/doc/jasperreports/src/LivestatusDatasource.java
index aa8d561..b64c026 100644
--- a/doc/jasperreports/src/LivestatusDatasource.java
+++ b/doc/jasperreports/src/LivestatusDatasource.java
@@ -29,7 +29,10 @@ public class LivestatusDatasource implements JRDataSource, JRRewindableDataSourc
 
 	protected String getFieldDescription(String fieldname){
 		try{ 
-			return map_fielddescr.get(fieldname);
+			if (map_fielddescr.containsKey(fieldname))
+				return map_fielddescr.get(fieldname);
+			else
+				return "";
 		}catch(Exception ex){
 			return "";
 		}
@@ -37,7 +40,10 @@ public class LivestatusDatasource implements JRDataSource, JRRewindableDataSourc
 	
 	protected String getFieldType(String fieldname){
 		try{ 
-			return map_fieldtypes.get(fieldname);
+			if (map_fieldtypes.containsKey(fieldname))
+				return map_fieldtypes.get(fieldname);
+			else
+				return "";			
 		}catch(Exception ex){
 			return "";
 		}
@@ -66,7 +72,6 @@ public class LivestatusDatasource implements JRDataSource, JRRewindableDataSourc
 			}else if(fieldtype.equals("list")){
 				ArrayList<String> res_list = new ArrayList<String>();
 				String[] tokens = value.split("\\|");
-				System.out.println("token value " + value);
 				for( int i = 0; i<tokens.length; i++){
 					res_list.add(tokens[i]);
 				}
@@ -77,6 +82,7 @@ public class LivestatusDatasource implements JRDataSource, JRRewindableDataSourc
 		return value;
 	}
 
+	
 	public boolean next() throws JRException {
 		m_nIdx++;
 		return (m_nIdx < data.size());
diff --git a/doc/jasperreports/src/LivestatusFieldsProvider.java b/doc/jasperreports/src/LivestatusFieldsProvider.java
index 164bf16..e935378 100644
--- a/doc/jasperreports/src/LivestatusFieldsProvider.java
+++ b/doc/jasperreports/src/LivestatusFieldsProvider.java
@@ -29,7 +29,7 @@ public class LivestatusFieldsProvider implements FieldsProvider{
 	@SuppressWarnings("rawtypes")
 	public JRField[] getFields(IReportConnection con, JRDataset dataset, Map parameters)
 			throws JRException, UnsupportedOperationException {
-
+		
 		JRField[] ret = null;
 		try{
 			Vector<JRDesignField> jr_vector = new Vector<JRDesignField>();
@@ -42,18 +42,17 @@ public class LivestatusFieldsProvider implements FieldsProvider{
 			int header_count = headers.length;
 			String descr;
 			String type;
+						
 			for( int i = 0; i < header_count; i++ ){
 				// Set field name
 				JRDesignField tmp_field = new JRDesignField();
 				tmp_field.setName(headers[i]);
 				
-				
 				// Add field description, if available
 				descr = data.getFieldDescription(headers[i]);
 				if( ! descr.equals("") )
 					tmp_field.setDescription(descr);
 
-				
 				// Add field class type
 				type  = data.getFieldType(headers[i]);
 				if( ! type.equals("") ){
@@ -81,10 +80,10 @@ public class LivestatusFieldsProvider implements FieldsProvider{
 			ret = new JRField[jr_vector.size()];
 			jr_vector.toArray(ret);
 		}catch(JRException jrex){
-			throw jrex;
+			throw new JRException("Jasper Error evaluating query" + jrex.getMessage());
 		}
 		catch(Exception ex){
-			throw new JRException("Error evaluating query");
+			throw new JRException("Error evaluating query:" + ex.getMessage());
 		}
 		return ret;
 	}
diff --git a/doc/jasperreports/src/LivestatusQueryExecuter.java b/doc/jasperreports/src/LivestatusQueryExecuter.java
index 14a389f..c39308b 100644
--- a/doc/jasperreports/src/LivestatusQueryExecuter.java
+++ b/doc/jasperreports/src/LivestatusQueryExecuter.java
@@ -13,10 +13,12 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
+import net.sf.jasperreports.engine.JRDataSource;
 import net.sf.jasperreports.engine.JRDataset;
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.query.JRQueryExecuter;
 
+
 public class LivestatusQueryExecuter implements JRQueryExecuter{
 	// Connection parameters
 	private String    			jasper_query;     // Query from jasperreports incl. connection details
@@ -24,7 +26,7 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 	private String    			server;           // server name
 	private int       			server_port;      // server port
 	private int 				timeoutInMs = 10*1000;  // 10 seconds
-	private String    			fixed_appendix = "\nColumnHeaders: on\nResponseHeader: fixed16\nOutputFormat: csv\n\n";
+	private String    			fixed_appendix = "\nColumnHeaders: on\nResponseHeader: fixed16\nSeparators: 10 1 44 124\nOutputFormat: csv\n\n";
 
 	// Socket parameters
 	private Socket    			socket;           // socket to server
@@ -36,10 +38,17 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 	public static void main(String[] args){
 		try {
 			//new LivestatusQueryExecuter("localhost 6561\nGET services\nColumns: host_name check_command").createDatasource();
-		
-			String query = "localhost 6561\nGET statehist\nColumns: state host_name service_description\nFilter: time >= 1344195720\nFilter: time <= 1344196820\nFilter: service_description = CPU load\nFilter: host_name = localhost\nStats: sum duration_ok\nStats: sum duration_warning\nStats: sum duration_critical";
-			new LivestatusQueryExecuter(query).createDatasource();
-			
+			String query = "localhost 6561\n"+
+			"GET statehist\n" +
+			"Columns: host_name service_description\n"+
+			"Filter: time >= 1344195720\n"+
+			"Filter: time <= 1344195776\n"+
+			"Filter: host_name = localhost\n"+
+			"Stats: sum duration_ok\n"+
+			"Stats: sum duration_warning\n"+
+			"Stats: sum duration_critical";
+						
+			JRDataSource sourci = new LivestatusQueryExecuter(query).createDatasource();
 		} catch (JRException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
@@ -69,10 +78,8 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 	}
 
 	public boolean cancelQuery() throws JRException {
-		System.out.println("ABORT QUERY");
 		try {
 			if( socket.isConnected() ){
-				System.out.println("STOPPED !");
 				socket.close();
 				return true;
 			}
@@ -117,9 +124,11 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 			// setup socket and in/output streams
 			setupSocket();
 			
+			
 			// query the column types and their descriptions
 			String table_name = livestatus_query.split("\n")[0].split(" ")[1];
-			String desc_query = String.format("GET columns\nFilter: table = %s\nColumnHeaders: off\nResponseHeader: fixed16\nOutputFormat: csv\nKeepAlive: on\n\n", table_name);
+			String desc_query = String.format("GET columns\nFilter: table = %s\nColumnHeaders: off\nResponseHeader: fixed16\nOutputFormat: csv\nSeparators:  10 1 44 124\nKeepAlive: on\n\n", table_name);
+			
 			sockOutput.write(desc_query.getBytes(), 0, desc_query.getBytes().length); 
 			
 			// read description information
@@ -129,13 +138,18 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 			String line;
 			line = in.readLine(); // Skip column headers
 			offset += line.getBytes().length + 1;
+			String[] tokens;
 			while( offset < response_size ){
-				line = in.readLine();
+				line = in.readLine();				
 				if( line == null )
 					break;
 				offset += line.getBytes().length + 1;
-				map_fielddesc.put(line.split(";")[1],line.split(";")[0]);
-				map_fieldtype.put(line.split(";")[1],line.split(";")[3]);
+				tokens = line.split("\001");
+				map_fielddesc.put(tokens[1],tokens[0]);
+				if( tokens[1].startsWith("stats_") )
+					map_fieldtype.put(tokens[1],"float");
+				else
+					map_fieldtype.put(tokens[1],tokens[3]);
 			}
 
 			// send livestatus query to socket
@@ -163,12 +177,12 @@ public class LivestatusQueryExecuter implements JRQueryExecuter{
 			// read content
 			offset = 0;
 			offset += line.getBytes().length + 1;
-			while( offset < response_size ){
+			while( true ){
 				line = in.readLine();
 				if( line == null )
 					break;
 				ArrayList<String> tmp_array = new ArrayList<String>();
-				for( String field : line.split(";") ){
+				for( String field : line.split("\001") ){
 					tmp_array.add(field);
 				}
 				livestatus_data.add(tmp_array);
diff --git a/doc/jasperreports/src/LivestatusQueryExecuterFactory.java b/doc/jasperreports/src/LivestatusQueryExecuterFactory.java
index ff6824c..db0fb84 100644
--- a/doc/jasperreports/src/LivestatusQueryExecuterFactory.java
+++ b/doc/jasperreports/src/LivestatusQueryExecuterFactory.java
@@ -3,15 +3,35 @@ package livestatus;
 import java.util.Map;
 import net.sf.jasperreports.engine.JRDataset;
 import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JRValueParameter;
 import net.sf.jasperreports.engine.query.JRQueryExecuter;
 import net.sf.jasperreports.engine.query.JRQueryExecuterFactory;
 
+ at SuppressWarnings("deprecation")
 public class LivestatusQueryExecuterFactory implements JRQueryExecuterFactory{
-	@SuppressWarnings("rawtypes")
-	public JRQueryExecuter createQueryExecuter(JRDataset dataset, Map parameters)
-			throws JRException {
+	public JRQueryExecuter createQueryExecuter(JRDataset dataset, Map<String, 
+			? extends JRValueParameter> parameters) throws JRException {
+		// TODO Auto-generated method stub
+		logFile("Create livestatusQueryExecut0r!\n");
 		return new LivestatusQueryExecuter(dataset, parameters);
+		
 	}
+static	public void logFile(String info){
+//		FileWriter writer;
+//		File file;
+//
+//		file = new File("/tmp/ireport.txt");
+//		try {
+//			writer = new FileWriter(file, true);
+//			writer.write(info);
+//			writer.write("\n");
+//			writer.flush();
+//			writer.close();
+//		}catch (IOException e) {
+//			e.printStackTrace();
+//		}
+	}
+
 
 	public Object[] getBuiltinParameters() {
 		return null;



More information about the checkmk-commits mailing list