DevTech101

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

Monitoring remote Solaris client’s by using Java sockets.

The below configuration shows how to configure a host (Solaris or Linux), to expose local performance states in JSON using Java sockets, the stats are then collected by a remote system to crunch (graph/chart) the data.
The full capture pipeline process is outline below. Note: If losing client stat data is a concern, Flume or Logstah combined with Kafka can be used, for more details how to read here and here.
  • Capture the local stats about every second to a local SQLite DataBase
  • On the client, expose the latest stat with Json format
  • Connect to the client (about) every second to capture the latest stats
  • Append the latest stats to the local DB
  • By user request graph or chart the stored data (can be in real time if needed)
Note: This is the first part, how to capture the stats locally. Click here on how to publish these stats.

Creating a Java socket for local capture.

First lets create the java capture process. This java program will capture local system resources usage and store it in a local SQLite3 DB.
The Java capture process program Note: The Java capture program uses Sigar a C compiled program to capture some of the system resources. The CaptureSysStats.java (main)
package capturesysstats;

//import java.util.*;
import java.io.IOException;
import java.sql.SQLException;
import org.hyperic.sigar.SigarException;

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

    public static void main(String[] args) throws SQLException, SigarException, InterruptedException, IOException {
        for (;;) {
            String tableName = "statsTable";
            Sql sql = new Sql();
            if (sql.tableExists(tableName)) {
                //System.out.println("Opened database successfully");
                //System.out.println(tableName + " Exists, updaing records");
                sql.updateTable(tableName);
                Thread.sleep(1000);
            } else {
                // Table doesn't exist.
                //System.out.println("Opened database successfully");
                //System.out.println(tableName + " Doesn't exist, Lets create the table...");
                sql.createTable(tableName);
                Thread.sleep(1000);
                //sql.updateTable(tableName);            
            }
        }
    }

}
The program will get the resource usage i.e CPU, Mem, etc The GetResStats.java
package capturesysstats;

import java.lang.*;
import org.hyperic.sigar.*;

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

    public static Sigar sigar = new Sigar();
    Mem mem;
    Swap swap;
    //Cpu cpu;
    //IO disk;
    //Uptime uptime;
    //double uptime = sigar.getUptime().getUptime();

    public GetResStats() throws SigarException {
        this.mem = sigar.getMem();
        this.swap = sigar.getSwap();
        //this.cpu. = sigar.getCpuPerc();
        //CpuPerc cpuPerc = sigar.getCpuPerc();
        //this.disk = sigar.getDiskUsage(0);
        //this.uptime = sigar.getUptime();
    }

    public Long getTotMem() throws SigarException {
        //Mem mem = sigar.getMem();        
        return (mem.getTotal() / 1024 / 1024);
    }

    public Long getUsdMem() throws SigarException {
        return (mem.getActualUsed() / 1024 / 1024);
    }

    public Long getFreeMem() throws SigarException {
        return (mem.getActualFree() / 1024 / 1024);
    }

    public Long getSysFreeMem() throws SigarException {
        return (mem.getFree() / 1024 / 1024);
    }

    public Long getTotSwap() throws SigarException {
        return (swap.getTotal() / 1024);
    }

    public Long getFreeSwap() throws SigarException {
        return (swap.getFree() / 1024);
    }

    public Long getUsdSwap() throws SigarException {
        return (swap.getUsed() / 1024);
    }

    public Long getSwapPageIn() throws SigarException {
        return (swap.getPageIn() / 1024);
    }

    public Long getSwapPageOut() throws SigarException {
        return (swap.getPageOut() / 1024);
    }

    public String getUptime(int t) throws SigarException {
        double[] avg = sigar.getLoadAverage();
        return (String.format("%.2f", avg[t]));
    }
   
    CpuPerc cpuPerc = sigar.getCpuPerc();
    public String getCpuCombo() throws SigarException {
        return (String.format("%.2f", cpuPerc.getCombined() * 100));
    }

    public String getCpuUsr() throws SigarException {
        return (String.format("%.2f", cpuPerc.getUser() * 100));
    }
    
    public String getCpuSys() throws SigarException {
        return (String.format("%.2f", cpuPerc.getSys()* 100));
    }    
    
    public String getCpuIdle() throws SigarException {
        return (String.format("%.2f", cpuPerc.getIdle()* 100));
    }

    public String getCpuWait() throws SigarException {
        return (String.format("%.2f", cpuPerc.getWait()* 100));
    }    

    public Long getNetIf(int t) throws SigarException {
        //String[] netIf = sigar.getNetInterfaceList();
        NetInterfaceStat netStat = sigar.getNetInterfaceStat("localnet0");
        long rxCurrent = netStat.getTxBytes();
        return (rxCurrent);
    }

    public static void getNetStat() throws SigarException, InterruptedException {

        String[] netIf = sigar.getNetInterfaceList();
        NetInterfaceStat netStat = sigar.getNetInterfaceStat("localnet0");
        long rxCurrent = netStat.getRxBytes();
        System.out.println("rxCurrent: " + rxCurrent);

        System.out.println(netIf[1]);
        System.out.println(netIf[2]);
        System.out.println(netIf[3]);
        System.out.println(netIf[4]);
    }

    public static void getDiskStat() throws SigarException, InterruptedException {
        //double uptime = sigar.getUptime().getUptime();
        //int disk = sigar.getDiskUsage(0);
        //disk.
    }

}
Now lets capture all the stats in SQLite local data for latter retrieval
package capturesysstats;

import java.sql.*;

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

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

    public void connect() {
        if (conn != null) {
            return;
        }
        try {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + dbName);
        } catch (ClassNotFoundException | SQLException e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    public boolean tableExists(String tableName) {
        connect();

        try {
            DatabaseMetaData md = conn.getMetaData();
            ResultSet rs = md.getTables(null, null, tableName, null);
            if (rs.next()) {
                //Table Exist
                //System.out.println(tableName + " Exist");
            }
            return rs.getRow() > 0;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            //Logger.getLogger(SQLite.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public void createTable(String tableName) throws SQLException {
        connect();
        Statement stmt = null;
        String sql = "CREATE TABLE " + tableName
                + " (ID INT PRIMARY KEY     NOT NULL,"
                + " DATE_TIME      TEXT    NOT NULL, "
                + " CPU_TOT        INT     NOT NULL, "
                + " CPU_USR        INT     NOT NULL, "
                + " CPU_SYS        INT     NOT NULL, "
                + " CPU_IDL        INT     NOT NULL, "
                + " CPU_WAIT       INT     NOT NULL, "                
                + " UPT_ONE        INT     NOT NULL, "
                + " UPT_FIVE       INT     NOT NULL, "
                + " UPT_FIFTEEN    INT     NOT NULL, "
                + " MEM_USD        INT     NOT NULL, "
                + " MEM_FREE       INT     NOT NULL, "
                + " MEM_TOT        INT     NOT NULL)";
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            stmt.close();
            //conn.close();
        } catch (SQLException e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
        //System.out.println("Table created successfully");
    }

    public void updateTable(String tableName) throws SQLException {
        connect();
        try {
            conn.setAutoCommit(false);            

            GetResStats getResStat = new GetResStats();         
            
            Long DATE_TIME = System.currentTimeMillis() / 1000L;

            try (PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO "
                    + tableName
                    + " (ID, DATE_TIME, CPU_TOT, CPU_USR, CPU_SYS, CPU_IDL, CPU_WAIT, UPT_ONE, "
                    + "UPT_FIVE, UPT_FIFTEEN, MEM_USD, MEM_FREE, MEM_TOT) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {

                stmt.setInt(1, 0);
                stmt.setLong(2, DATE_TIME);
                stmt.setString(3, getResStat.getCpuCombo());                
                stmt.setString(4, getResStat.getCpuUsr());
                stmt.setString(5, getResStat.getCpuSys());
                stmt.setString(6, getResStat.getCpuIdle());                
                stmt.setString(7, getResStat.getCpuWait());
                stmt.setString(8, getResStat.getUptime(0));
                stmt.setString(9, getResStat.getUptime(1));
                stmt.setString(10, getResStat.getUptime(2));
                stmt.setLong(11, getResStat.getUsdMem());
                stmt.setLong(12, getResStat.getFreeMem());
                stmt.setLong(13, getResStat.getTotMem());
                stmt.executeUpdate();
            }
            conn.commit();
            conn.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            //System.exit(0);
        }
        //System.out.println("Records updated successfully");
    }
}
Note: Due to Solaris limitation’s I am not using all resources like Network, disk. but this can easily be extended on other OS’s.
For how to publish the captured stats, please click here – Java sockets – system monitoring / server publisher process.
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: