DevTech101

DevTech101
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

This is part two – on how to capture and publish Solaris client performance stats.

To read part one please click here.
This program will retrieve the last system stats from the local SQLite DB and make it available over a socket or http connection for remote fetching.
Once the program is running, just telnet to the port (in this case 19999) and you will get a JSON dump of the system performance.
The SysMonRes.java runs the socket server and publishes the latest status on port 19999.
package sysmonres;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;

import org.json.simple.JSONObject;

/**
 *
 * @author Eli Kleinman
 */
public class SysMonRes {

    public static void main(String[] args) throws IOException, SQLException {
        int port = 19999;        
        String tableName = "statsTable";
        
        ServerSocket s1 = new ServerSocket(port);
        while (true) {
            Socket ss = s1.accept();
            Sql sql = new Sql();
            
            JSONObject obj = sql.getStats(tableName);
                        
            PrintStream p = new PrintStream(ss.getOutputStream());
            p.println(obj);
            ss.close();
        }
    }

}
The Sql.java retrieves the latest stats from the local SQLite DB.
package sysmonres;

import java.sql.*;

import org.json.simple.JSONObject;

/**
 *
 * @author Eli Kleinman
 */
public class Sql {

    protected Connection conn = null;
    protected String dbName = "/statsDB.db";

    public JSONObject getStats(String tableName) throws SQLException {
        try {
            
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + dbName);
            
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
            
            ResultSetMetaData rsmd = rs.getMetaData();
            
            //JSONArray stats = new JSONArray();                                   
            JSONObject obj = new JSONObject();
            
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String name = rsmd.getColumnName(i);               
                int value = rs.getInt(name);
                
                obj.put(name, value);
                
                //System.out.println(name + ": " + value);
            }
            //System.out.print(obj);
                        
            //Long DATE_TIME = System.currentTimeMillis() / 1000L;
            //System.out.println("\n"+DATE_TIME);
            stmt.close();
            return obj;

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());            
        }
        return null;        
    } 
}
The next (and last) part of this series - talks about capturing multiple stats in parallel capturing multiple stats in parallel
To read about how to create a python socket server click here.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: