Table of Contents
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(); } }
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;
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.
Reduce TIME_WAIT on linux
sysctl net.ipv4.tcp_tw_reuse=1 sysctl net.ipv4.tcp_tw_recycle=1
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!
apparmor and mysql on ubuntu
The default data directory is /var/lib/mysql. How to change to a new directory, such as /data?
- stop mysql
- move /var/lib/mysql /data
- modify /etc/apparmor.d/usr.sbin.mysqld, /var/lib/mysql to /data/mysql
- restart apparmor
- start mysql
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.
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", "");
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 ''?'''
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
Installed blog plugin
Spent a few minutes to install the plugin:blog