A java class to get a compressed file from the server

Some guys often ask me to help them to get some files from the server, such as tomcat log. Everytime I must open ftp client to connect the server, download these file. The lazy man need a smart way to do this job. Now they can help themselves.

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.zip.*;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 public class ZGet {
     public void get(HttpServletRequest request, HttpServletResponse response, String filename) {
         try {
             // String fname = request.getParameter("xpath");
             fname = filename;
             File f = new File(fname);
             if (f.canRead()) {
                 response.setContentType("application/x-gzip");
                 response.setHeader("Location", f.getName());  
                 response.setHeader("Content-Disposition", "attachment; filename=" + f.getName() + ".gz");
                 get(new FileInputStream(filename), response.getOutputStream());
                 response.getOutputStream().flush();
                 response.getOutputStream().close();
              }
          } catch (Exception e) {
              // TODO Auto-generated catch block
              //e.printStackTrace();
          }  
      }
      private void get(InputStream in, OutputStream out) throws IOException {
          if (in == null || out == null) {
              return;
          }
          byte[] buffer = new byte[1024];
          int i = -1;
          GZIPOutputStream gzip = new GZIPOutputStream(out);
          while ((i = in.read(buffer)) != -1) {
              gzip.write(buffer, 0, i);
          }
          gzip.close();
      }
  }
2009/12/19 05:22 · 0 Comments · 0 Linkbacks

speed up mysql import

Generally, it is very slow to restore the data from mysqldump file. Here is a tip to speed up this work:

  % mysql -u someuser -p
  > SET AUTOCOMMIT=0;
  > SET UNIQUE_CHECKS=0;
  > SET FOREIGN_KEY_CHECKS=0;
  > source dump.sql;
  > SET FOREIGN_KEY_CHECKS=1;
  > UNIQUE_CHECKS=1;
  > COMMIT;
2009/12/17 16:12 · 0 Comments · 0 Linkbacks

Adjust Tomcat timezone under CentOS

Tomcat alway complains that it got wrong timezone, but the Linux time is right.
The problem is the timezone in the file /etc/sysconfig/clock is wrong. This timezone is not same as the timezone of the command 'date', so modified /etc/sysconfig/clock, make Java happy.

2009/12/10 15:08 · 0 Comments · 0 Linkbacks

Reduce TIME_WAIT on linux

sysctl net.ipv4.tcp_tw_reuse=1
sysctl net.ipv4.tcp_tw_recycle=1
2009/11/25 10:39 · 0 Linkbacks

Monitor the statistics of Nginx with Cacti

First, add the next lines to configuration file of nginx

 location /server_status {
    stub_status on;
    access_log off;
    # Only me can access this
    #allow 10.0.0.12;
    #deny all;
 }

Run nginx -t to make sure the configuration file is correct. Then run kill -1 nginx_pid, nginx will reload the configuration. After nginx reload it, try to access the url: http://server/server_status/, the browser will display like this:

 Active connections: 303 
 server accepts handled requests
  6314384 6314384 34931986 
 Reading: 3 Writing: 5 Waiting: 295 

Next, create the script to get the status data from the nginx server. I wrote a python tool to get these data.
It's better that putting this command in <path_cacti>/scripts/

 #!/usr/bin/env /usr/bin/python
 import sys,os,commands
 import re
 import urllib2,urllib
 p = re.compile('\n')
 st = []
 try:
     url = 'http://ttm.clogi.com/my_nginx_status'
     s = urllib.urlopen(url)
     s = s.read()
     s = s.split('\n')
     a = s[0].split(' ') 
     if sys.argv[1] == 'actconns':
         print(a[2])
     elif sys.argv[1] == 'requests':
         a = s[2].split(' ')
         print(a[3])
     elif sys.argv[1] == 'rqrw':
         a = s[2].split(' ')
         str = 'request:' + str(float(a[3]) / float(a[1]))
         a = s[3].split(' ')
         str = str + ' read:' + a[1] + ' write:' + a[3]
         print(str)
     else:
         print(0)
 except Exception, err:
     print(0)

Now, login cacti, to setup the new graph. Click Data Input Methods, add a new data input method


Then create Data Templates


Ok, let create Graph Templates

That' OK!

2009/11/21 07:01 · 0 Comments · 0 Linkbacks

apparmor and mysql on ubuntu

The default data directory is /var/lib/mysql. How to change to a new directory, such as /data?

  1. stop mysql
  2. move /var/lib/mysql /data
  3. modify /etc/apparmor.d/usr.sbin.mysqld, /var/lib/mysql to /data/mysql
  4. restart apparmor
  5. start mysql
2009/04/15 02:53 · 0 Comments · 0 Linkbacks

The easy way to add locale environment on Ubuntu

  sudo /usr/share/locales/install-language-pack zh_CN.GBK

install-language-pack will have nothing to do if the locale is invalid.

2009/04/14 03:15 · 0 Comments · 0 Linkbacks

sysctl string on FreeBSD

The way to 'sysctl' string in the kernel module program

The sysctl_proc function:

char sysctl_str[1024];
static int
sysctl_str_ctrl(SYSCTL_HANDLER_ARGS)
{
   int error;
   char *p;
   p = sysctl_str;
   error = sysctl_handle_string(oidp, p, sizeof(sysctl_str), req);
   if (!error && req->newptr) {
      //
   }
   error = SYSCTL_OUT(req, sysctl_str, sizeof(sysctl_str));
   return error;
}

Note: SYSCTL_OUT sends the value to user space, that is to update the MIB value.

SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(sysctl_root), OID_AUTO, "str", 
   CTLTYPE_STRING|CTLFLAG_RW, 0, sizeof(sysctl_str), sysctl_str_ctrl, "A", "");
2009/03/06 10:05 · 0 Comments · 0 Linkbacks

Shrink the larger log of MSSQL Server

1. BACKUP DATABASE database_name TO backup_device
2. BACKUP LOG database_name TO backup_device
3. DBCC SHRINKFILE (log_file_name)


list the size of tables

EXEC sp_msforeachtable 'sp_spaceused ''?'''
2009/02/12 03:58 · 0 Comments · 0 Linkbacks

hide delphi application from taskbar

I am newbie of dephi. I spent a few days to hide the main form from the taskbar. I wrote a application which creates security tunnel and then runs a special program, such as mstsc(MS remote desktop) to connect the remote server via this tunnel.

I tried several ways, none can help me. Even many guys said that application.showmainform shoud be added to the project, and application.restore to onshow. It's weird that the main form was hidden at the beginning, but it's show again after the second program was executed.

At last, one way is worked for me, adding timer to hide the main form after all of initializationes have been done, and the next line to FormCreate

SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW)
2009/01/31 15:01 · 0 Comments · 0 Linkbacks

Unlock svnsync

When svnsync reports: Failed to get lock on destination repos, currently held by 'xxxxx', use the next command to unlock svnsync:

svn propdel svn:sync-lock --revprop -r 0 file:///mirror
2009/01/08 04:20 · 0 Comments · 0 Linkbacks

Installed blog plugin

Spent a few minutes to install the plugin:blog

2008/12/30 08:40 · 0 Comments · 0 Linkbacks