2009年2月19日
#
#!/usr/bin/env python
import string, os, sys
dir = '/home/waterye/works/'
files = os.listdir(dir)
for f in files:
if not os.path.isfile(dir+os.sep+f): continue
linecount = 0
fp = open(dir+os.sep+f,'r')
for line in fp:
linecount += 1
fp = open(dir+os.sep+f,'r')
for i,line in enumerate(fp):
if (i==linecount-1 and line.endswith('\n')):
print f
print "--------------------------------"
* bash太难理解,还是用py看上去舒服,虽然不专业,但能完成任务就行
2008年12月4日
#
"perldoc DateTime" would have told you how to format milliseconds.
use DateTime;
use Time::HiRes qw(time);
my $dt = DateTime->from_epoch( epoch => time() );
print $dt->strftime('%Y-%m-%d %H-%M-%S-%3N'), "\n";
* sudo apt-get install libdatetime-perl
perl真麻烦
2008年10月31日
#
1. http://www.whalin.com/memcached/ v2.0.1
2. http://code.google.com/p/spymemcached/ v2.2
a. 使用whalin版会导致File Descriptor leak,而使用spy版则不会,原因是whalin版没有使用selector管理socketchannel.
Thread.sleep(1000 * 30);
System.out.println("begin
");
Selector selector = null;
SocketChannel channel = null;
try {
String host = "192.168.0.74";
int port = 11211;
int timeout = 1000 * 60;
selector = Selector.open();
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(new InetSocketAddress(host, port));
channel.register(selector, channel.validOps());
try {
selector.select();
} catch (IOException e) {
e.printStackTrace();
}
Iterator it = selector.selectedKeys().iterator();
int i = 0;
while (it.hasNext()) {
i++;
System.out.println(i);
SelectionKey selKey = (SelectionKey) it.next();
it.remove();
try {
processSelectionKey(selKey);
} catch (IOException e) {
e.printStackTrace();
selKey.cancel();
}
}
System.out.println("unclose
");
Thread.sleep(1000 * 30);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (selector!=null) {
selector.close(); // not fd leak
}
}
System.out.println("end
");
Thread.sleep(1000 * 30 * 1);
通过lsof -p pid | grep pipe可以观察是否有fd leak.
b. w版set 1000000 object 需要600s左右,s版只需150s左右
2008年10月21日
#
192.168.0.74 [root avi]$ python /root/memcached/cacti-memcached-1.0/memcached.py localhost
total_items:2002344 get_hits:3 uptime:50291 cmd_get:3 time:1224521129 bytes:58888890 curr_connections:1 connection_structures:8 bytes_written:16167008 limit_maxbytes:402653184 cmd_set:2002344 curr_items:1000000 rusage_user:101.730357 get_misses:0 rusage_system:199.092442 bytes_read:63849044 total_connections:314
2008年10月5日
#
1. iftop
sudo apt-get install iftop
sudo iftop -i ppp0
2. slurm
sudo apt-get install slurm
slurm -i ppp0
3. vnstat
sudo apt-get install vnstat
sudo chmod o+x /usr/bin/vnstat
sudo chmod o+wx /var/lib/vnstat/
vnstat -u -i ppp0
vnstat -i ppp0
vnstat -l -i ppp0
因为不是server就没有用复杂的mrtg
2008年9月28日
#
vi,gedit会在最后一行加上\n,实在找不到好的解决方法,只好自己写个shell script删掉
#!/usr/bin/env python
import sys
oldfile=sys.argv[1]
newfile=sys.argv[2]
print oldfile,newfile,
print
linecount = 0
str = ''
f = open(oldfile,'r')
for line in f:
linecount += 1
f = open(oldfile,'r')
for i,line in enumerate(f):
if (i==linecount-1):
str += line.replace('\n','')
# print line.replace('\n','')
else:
str += line
# print line,
#print len(str),str
f2 = open(newfile, 'w')
f2.write(str)
quit()
2008年9月24日
#
free -lmt
cat /proc/meminfo
dmesg |grep [mM][eE][mM]
cat /proc/pid/status
2008年9月22日
#
1. find . -type f -exec rm -v {} \;
2. nice -n 19 rm -rf directory
3. ls | xargs rm
4. find . -mtime +90 -type f -exec rm -v {} \;
采用第四种最好,把一些旧的文件先删除
2008年9月12日
#
shell
mysql -h 192.168.0.136 -P 3306 -N -u root -p'abcd' -e 'show full processlist' | egrep -v 'Sleep|show full processlist'
jsp
String sql = " show full processlist ";
rs = stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("Id");
String user = rs.getString("User");
String host = rs.getString("Host");
String db = rs.getString("db");
String command = rs.getString("Command");
String info = rs.getString("Info");
String state = rs.getString("State");
if (command.equalsIgnoreCase("Sleep")) continue;
if (info.equalsIgnoreCase("show full processlist")) continue;
if (info.length()==0 && state.length()==0) continue;
int time = rs.getInt("Time");
// print
}
2008年9月2日
#
nbtstat.exe -a 192.168.0.171
nbtscan-1.0.35.exe -f 192.168.0.1/24
http://www.unixwiz.net/tools/nbtscan.html