Tuesday, May 12, 2015

How To Use both HTTP and Socks Proxy In Java

The simplest way is use global setting, for example you can specify the proxy in command line.
java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
-DsocksProxyHost=socks.example.com GetURL

You can also configure proxy in you java code.
System.setProperty("http.proxyHost", "webcache.example.com");
System.setProperty("http.proxyPort", "8080");

By the ways above, you can use one proxy only. If you want to connect proxy requires username and password, or select proxy for each connection, please refer to java sample code below.

Note

1.       If you use HTTP proxy, you can use many proxies in your single java process. For example, Thread A connect to Proxy P1, and Thread B connect to Proxy P2.
2.       If you use Socks proxy, the socks proxy setting is global setting. In other words, only one proxy is supported. So if you change proxy, the setting will take effect in all threads.

Sample java code that use multi-proxy with authentication.

The EasyHA supports  both Http and Socks proxy. Perhaps you want to upload data to cloud, and your server is behind proxy, so you need to connect to the Http Proxy. You may want to monitor a Linux server behind a firewall, and EasyHA cannot connect to the server directly, so you have to configure socks proxy. There are many free socks5 proxies. For example: 3proxy.

Here is the sample code in EasyHA:

java.net.Proxy proxy=null;
String Proxy_Authorization_headerValue=null;
if(item.getProxyId()!=null){
      final ProxyConf conf=MonitorDaemon.getInstance().getProxyConfById(item.getProxyId());
      if(conf!=null){
            SocketAddress sa=InetSocketAddress.createUnresolved(conf.getHost(), conf.getPort());
            if( conf.getProxyType().equals("http") ){
                  proxy=new Proxy(Proxy.Type.HTTP, sa);
            if(conf.getUserName()!=null&&conf.getUserName().length()>0
                  &&conf.getPassword()!=null&&conf.getPassword().length()>0){
                        Proxy_Authorization_headerValue= "Basic " + Base64.encode( (conf.getUserName()+":"+conf.getPassword()).getBytes("UTF-8")) ; 
                        }
            }else{
                  proxy=new Proxy(Proxy.Type.SOCKS, sa);
            if(conf.getUserName()!=null&&conf.getUserName().length()>0
                        &&conf.getPassword()!=null&&conf.getPassword().length()>0){
                      java.net.Authenticator authenticator = new java.net.Authenticator() {
                        @Override
                             protected java.net.PasswordAuthentication getPasswordAuthentication() {
                                  return new java.net.PasswordAuthentication(conf.getUserName(), conf.getPassword().toCharArray());
                             }
                             };
                             java.net.Authenticator.setDefault(authenticator);
                  }
            }
      }
}
if(proxy==null){
   httpUrlConn = (java.net.HttpURLConnection) url.openConnection();
}else{
   httpUrlConn = (java.net.HttpURLConnection) url.openConnection(proxy);
   if(Proxy_Authorization_headerValue!=null){
   String headerKey = "Proxy-Authorization"
   httpUrlConn.setRequestProperty(headerKey, Proxy_Authorization_headerValue); 
   }
}
if(cookie!=null){
     httpUrlConn.setRequestProperty( "cookie" , cookie);
}
httpUrlConn.setConnectTimeout(1000*3);

Saturday, May 9, 2015

Tablespace growth and usage monitoring

Tablespace growth and usage monitoring by EasyHA

I want to create a mechanism to monitor growth of our tablespaces and how much space is used in them. 

Step by Step Guide

1.       Provided you have install the free version of EasyHA, before you monitor your databases and business data (like tablespace usage), you should please create JDBC database connection to your Oracle database.

To create JDBC connection, please click “Setup” menu, and then click “Databases Overview” button. In the “Jdbcs” page, please click “Add Jdbc” button.

The “New Jdbc” form appears, it is very easy to fill in the form. You can also click “Help” link if you don’t know how to fill in URL box.


Now I have created a connection named gnbsprd-ozq. The settings of the connection is listed below:




2.       Create item that will monitor your tablespace usage.
a.       Click “Items” menu.
b.       Now you will see the list of items.
c.        Click “Add Item” button in right upper corner.
d.       Select “Biz Data Monitor” from item type list.
e.       Select DB and fill in the SQL statement.
f.        Fill other fields like interval and description of field #1.
g.       Submit the form and review the item. The item looks like this:


The SQL statement is very critical.  This is sample SQL.

SELECT 
  D.TOT_GROOTTE_MB "SIZE MB",
  D.TOT_GROOTTE_MB - F.TOTAL_BYTES "USED SPACE MB"
  FROM (SELECT TABLESPACE_NAME,
  ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
  ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
  FROM SYS.DBA_FREE_SPACE
  GROUP BY TABLESPACE_NAME) F,
  (SELECT DD.TABLESPACE_NAME,
   ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
  FROM SYS.DBA_DATA_FILES DD
    WHERE dd.TABLESPACE_NAME='TABLESPACE1'
  GROUP BY DD.TABLESPACE_NAME) D
  WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
If you want to monitor specified table, for example named “emp”, the sql like:

SELECT SUM(bytes)/1024/1024 Mbytese
  FROM dba_segments
 WHERE tablespace_name ='TABLESPACE1'
   AND SEGMENT_NAME='EMP'
    ORDER BY Mbytese DESC

3.       View the tablespace growth a few days later.
a.       Click “View” manu.
b.       Click “Graph View” button.
c.        Select item, and fill in date range, and click submit button.
The graph looks like: