haproxy的命令行管理以及同一用户的server stick

haproxy虽然没有提供单独的管理工具,但是实际上可以通过一个unix socket进行实时的命令控制,比如简单的查询,权重设置,disable/enable某个服务器 clear counters : clear max statistics counters (add ‘all’ for all counters)clear table : remove an entry from a tablehelp : this messageprompt : toggle interactive mode with promptquit : disconnectshow info : report information about the running processshow stat : report counters for each proxy and servershow errors : report last request and response errors for each proxyshow sess [id] : report the list of current sessions or dump this sessionshow table [id]: report table usage stats or dump this table’s contentsget weight : report a server’s current weight set weight : change a server’s weight set timeout : change a timeout setting set maxconn : change a maxconn setting set rate-limit : change a rate limiting valuedisable : put a server or frontend in maintenance modeenable : re-enable a server or frontend which is in maintenance modeshutdown : kill a session or a frontend (eg:to release listening ports)要使用这个unix socket需要在global段配置stats socket /opt/haproxy/etc/haproxy.stats level admin 然后就能通过 ...

July 17, 2012 · 2 min · pm

/bin/sh和/bin/bash的区别

经常我们写脚本的时候是直接 #!/bin/bash 这样写,但是运行的时候是直接sh xx.sh这样运行的。细心一点会发现有的脚本使用bash xx.sh能允许但是不能使用sh xxx.sh允许。比如shellhacks里面的 [shellhacks](http://nakedape.cc/wiki/ShellHacks)1.6.1. Logging and Monitoring with Tee Sometimes you want to capture all standard output to a log file while monitoring the output yourself. We can use the exec I/O redirection to do this also along with process substitution: exec > >(tee -a ${0##*/}.log) If you wanted to redirect stderr to a different file: exec 2> >(tee -a ${0##*/}.err) 这是我们可以在脚本开头set -o 打印出两种运行情况的区别。sh xx.sh的时候是开启了posix兼容,而禁用了bash的一些基本特性。可以在脚本开头set +o posix,强制关闭posix兼容。这样在两种情况下脚本允许的结果都是相同的。不过我在debian下测试有点问题,主要是debian下的sh实际是链接到dash去了。

July 14, 2012 · 1 min · pm

使用stunnel进行ssl加密

stunnel是个功能很简单的软件,就是进行ssl加密。可以帮助我们把http加密为https,也可以对普通的tcp链接进行ssl加密。stunnel的安装非常简单,就想详细写了。在centos下是只用用yum安装的,debian下就直接aptitude安装了。使用stunnel的配置如果不对客户端进行证书校验的话那么主要是2方面的配置。 1.配置ssl证书。stunnel配置证书有两种方式。老的方式是使用http://www.stunnel.org/static/stunnel.html介绍的先把key放最前面,然后依次放证书链。类似 -----BEGIN RSA PRIVATE KEY----- [encoded key] -----END RSA PRIVATE KEY----- [empty line] -----BEGIN CERTIFICATE----- [encoded certificate] -----END CERTIFICATE----- [empty line] 这种形式,如果服务器证书不是由根CA签发的,那么就需要类型nginx配置证书链那样把后面中间证书都逆序依次追加在后面。每个证书之间预留一个空行(不过我自己测试时不需要空行也是可以的)。这时的配置文件如下: ; Sample stunnel configuration file by Michal Trojnara 2002-2006; Some options used here may not be adequate for your particular configuration; Please make sure you understand them (especially the effect of chroot jail) ; Some debugging stuff useful for troubleshooting;debug = 7output = stunnel.log ; Use it for client mode;client = yes ...

July 14, 2012 · 2 min · pm

常用perl模块的使用

1. Net::Ping,perl的ping模块,范例: #!/usr/bin/perl -wuse strict; use Net::Ping; sub ping_check{my $dest=shift; my $mp = Net::Ping->new("icmp"); if($mp->ping($dest,2)){print "$dest is alive\n"; }else {print "$dest is dead\n"; } } 2.File::Copy 主要提供了copy和move函数 #!/usr/bin/perluse strict; use warnings; use File::Copy; my $filein=$ARGV[0]; my $fileout=$ARGV[1]; copy($filein,$fileout) or die "copy $filein to $fileout failed\n"; move($fileout,"$fileout.test") or die "mv $fileout to $fileout.txt failed\n"; 3.File::Rsync; #!/usr/bin/perluse strict; use warnings; use File::Rsync; my $filein=$ARGV[0]; my $fileout=$ARGV[1]; &rsync_file($filein,$fileout); sub rsync_file{my $localdir=shift; my $remotedir=shift; print "rsync file from $localdir to $remotedir\n"; my $obj = File::Rsync->new( { archive => 1, compress => 1 ,del=>1} ); $obj->exec( { src => $localdir, dest => $remotedir } ) or warn "rsync failed\n"; } ...

July 1, 2012 · 1 min · pm

perl正则匹配时的环视和命名捕获

perl的正则匹配里有几个有用的匹配方式 1.非捕获型匹配 [perl]#!/usr/bin/perluse warnings; use strict; my $line="123d4f5g7h8"; if($line=~/(?:5)(\w)/){print "match $1\n"; }[/perl] 这样就表示捕获5后面的一个字母或者数字,由于(?:5)是不占用空间的,所以我们还是用$1对捕获的字符串进行引用. 2.命名捕获 #!/usr/bin/perluse warnings; use strict; my $line="123d4f5g7h8"; if($line=~/(?:5)(?\w)/){print "match $+{var1}\n"; } 我们可以使用(?xxx)来把保存匹配的字符串存放在制定的变量里面.引用的时候需要使用$+{NAME}来进行引用. 3.顺序环视 (?=xxx) *******|xxx******** (?=xxx)匹配xxx前面的位置。而(?!xxx)就是匹配除了xxx前面位置的其他所有地方。 4.逆序环视 ```perl (?<=xxx) 匹配 *xxx|,也就是xxx以后的那个位置,同理(?另外,在匹配时可以指定的几个常用选项是/i 忽略字母的大小写/x 忽略中间的空格 /\d{2} ([\W]) \d{2} \1 \d{2}/x等价于/\d{2}([\W])\d{2}\1\d{2}//s 将串视为单行,”.”可以匹配换行符 。 /a.*bc/s匹配字符串axxxxx \nxxxxbc,但/a.*bc/则不匹配该字符串。/m 多行匹配。 在此情况下,^符号匹配字符串的起始或新的一行的起始;$符号匹配任意行的末尾。/o 只编译一次,注意有内插变量的时候谨慎使用/g 匹配所有可能的模式 另外,在匹配时可以指定的几个常用选项是 /i 忽略字母的大小写 /x 忽略中间的空格 /\d{2} ([\W]) \d{2} \1 \d{2}/x等价于/\d{2}([\W])\d{2}\1\d{2}/ /s 将串视为单行,”.”可以匹配换行符 。 /a.*bc/s匹配字符串axxxxx \nxxxxbc,但/a.*bc/则不匹配该字符串。 /m 多行匹配。 在此情况下,^符号匹配字符串的起始或新的一行的起始;$符号匹配任意行的末尾。 /o 只编译一次,注意有内插变量的时候谨慎使用 /g 匹配所有可能的模式

June 30, 2012 · 1 min · pm

找出IO消耗较高的进程

经常碰见服务器IO比较慢,这个是最不能忍受的,因为基本的命令行操作都会非常慢。网上搜了下,可以按照这个方式直接找出耗IO最高的进程名。 1.先停掉syslog然后打开block dumpservice syslog stop echo 1 > /proc/sys/vm/block_dump2.从dmesg的中找到消耗IO的进程dmesg | grep -E “READ|WRITE|dirtied” | grep -E -o ‘([a-zA-Z]*)’ | sort |uniq -c|sort -k1 -gr 排前的比较占用io。如果只想关系到读或者写的也可以直接只搜READ和WRITE。 3.恢复系统。 ```bash echo 0 > /proc/sys/vm/block_dumpservice syslog start

June 27, 2012 · 1 min · pm

linux上的rdp客户端

由于工作需要,经常得在linux下登陆windows的远程桌面.找到了一个比较好的客户端 aptitude intall remmina即可..

June 26, 2012 · 1 min · pm

OOM续

最近维护测试服务器越来越多出现OOM。每次都是改改内核参数,貌似有点用处。但是这个治标不治本,源头没有找到。 自己先了解了一下一些基础性的东西。每次OOM后查看messages日志都能看到 Jun 18 17:10:23 free-72-222 kernel: oom-killer: gfp_mask=0xd0Jun 18 17:10:23 free-72-222 kernel: Mem-info:Jun 18 17:10:23 free-72-222 kernel: DMA per-cpu:Jun 18 17:10:23 free-72-222 kernel: cpu 0 hot: low 2, high 6, batch 1Jun 18 17:10:23 free-72-222 kernel: cpu 0 cold: low 0, high 2, batch 1Jun 18 17:10:23 free-72-222 kernel: cpu 1 hot: low 2, high 6, batch 1Jun 18 17:10:23 free-72-222 kernel: cpu 1 cold: low 0, high 2, batch 1Jun 18 17:10:23 free-72-222 kernel: cpu 2 hot: low 2, high 6, batch 1Jun 18 17:10:27 free-72-222 kernel: cpu 2 cold: low 0, high 2, batch 1Jun 18 17:10:27 free-72-222 kernel: cpu 3 hot: low 2, high 6, batch 1Jun 18 17:10:27 free-72-222 kernel: cpu 3 cold: low 0, high 2, batch 1Jun 18 17:10:27 free-72-222 kernel: Normal per-cpu:Jun 18 17:10:27 free-72-222 kernel: cpu 0 hot: low 32, high 96, batch 16Jun 18 17:10:27 free-72-222 kernel: cpu 0 cold: low 0, high 32, batch 16Jun 18 17:10:27 free-72-222 kernel: cpu 1 hot: low 32, high 96, batch 16Jun 18 17:10:27 free-72-222 kernel: cpu 1 cold: low 0, high 32, batch 16Jun 18 17:10:27 free-72-222 kernel: cpu 2 hot: low 32, high 96, batch 16Jun 18 17:10:27 free-72-222 kernel: cpu 2 cold: low 0, high 32, batch 16…Jun 20 14:46:44 free-72-222 kernel: cpu 2 cold: low 0, high 32, batch 16Jun 20 14:46:44 free-72-222 kernel: cpu 1 cold: low 0, high 32, batch 16Jun 20 14:46:44 free-72-222 kernel: cpu 2 hot: low 32, high 96, batch 16Jun 20 14:46:44 free-72-222 kernel: cpu 2 cold: low 0, high 32, batch 16Jun 20 14:46:44 free-72-222 kernel: cpu 3 hot: low 32, high 96, batch 16Jun 20 14:46:44 free-72-222 kernel: cpu 3 cold: low 0, high 32, batch 16Jun 20 14:46:44 free-72-222 kernel:Jun 20 14:46:44 free-72-222 kernel: Free pages: 35748kB (24320kB HighMem)Jun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: Normal free:3304kB min:3336kB low:6672kB high:10008kB active:617956kB inactive:0kB present:729088kB pages_scanned:1293 all_unreclaimable? noJun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: HighMem free:24320kB min:512kB low:1024kB high:1536kB active:2836904kB inactive:486976kB present:3358720kB pages_scanned:0 all_unreclaimable? noJun 20 14:46:44 free-72-222 kernel: protections[]: 0 0 0Jun 20 14:46:44 free-72-222 kernel: DMA: 34kB 28kB 616kB 432kB 564kB 1128kB 1256kB 2512kB 21024kB 22048kB 04096kB = 8124kBJun 20 14:46:44 free-72-222 kernel: Normal: 04kB 18kB 016kB 132kB 164kB 1128kB 0256kB 0512kB 11024kB 12048kB 04096kB = 3304kBJun 20 14:46:44 free-72-222 kernel: HighMem: 59424kB 58kB 016kB 032kB 064kB 0128kB 0256kB 1512kB 01024kB 02048kB0*4096kB = 24320kBJun 20 14:46:44 free-72-222 kernel: 428935 pagecache pagesJun 20 14:46:44 free-72-222 kernel: Swap cache: add 0, delete 0, find 0/0, race 0+0Jun 20 14:46:44 free-72-222 kernel: 0 bounce buffer pagesJun 20 14:46:44 free-72-222 kernel: Free swap: 0kBJun 20 14:46:44 free-72-222 kernel: 1026048 pages of RAMJun 20 14:46:44 free-72-222 kernel: 839680 pages of HIGHMEMJun 20 14:46:44 free-72-222 kernel: 10594 reserved pagesJun 20 14:46:44 free-72-222 kernel: 413640 pages sharedJun 20 14:46:44 free-72-222 kernel: 0 pages swap cachedJun 20 14:46:44 free-72-222 kernel: Out of Memory: Killed process 19148 (java). 这样的日志,对于里面的 ...

June 20, 2012 · 5 min · pm

openssl签发证书时设置subjectAltName

很简单地表述一下需求,就是希望一个证书能给多个域名使用(非通配符证书). 直接上脚本吧,第一个是生成根CA的证书,因为是在以前的基础上完成的,所以实际是将就自己以前写的一个多级CA签发证书的脚本做的。原文见这里。 [这里](http://blog.chinaunix.net/uid-20553497-id-3163297.html)pm@debian:~/test/ca$ cat makerootca.sh #!/bin/bashDI R=`pwd`mkdir -p $DIR/demoCA/privatemkdir -p $DIR/demoCA/newcertsmkdir -p $DIR/autogettouch $DIR/demoCA/index.txt echo 01 > $DIR/demoCA/serial openssl genrsa -des3 -out $DIR/demoCA/private/cakey.pem 2048 openssl req -new -x509 -days 3650 -key $DIR/demoCA/private/cakey.pem -out $DIR/demoCA/careq.pem 然后是签发二级CA的脚本 pm@debian:~/test/ca$ cat no2ca.sh #!/bin/bashNAM E=$ 1DIR=$(pwd)/autoget openssl genrsa -des3 -out $DIR/$NAME.key 2048 openssl rsa -in $DIR/$NAME.key -out $DIR/$NAME.key openssl req -new -days 3650 -key $DIR/$NAME.key -out $DIR/$NAME.csr openssl ca -extensions v3_ca -in $DIR/$NAME.csr -confi g ./openssl.cnf -days 3000 -out $DIR/$NAME.crt -cert $DIR/../demoCA/careq.pem -keyfi le $DIR/../demoCA/private/cakey.pem 对应的配置文件是 ...

June 5, 2012 · 6 min · pm

OOM-killer

今天碰见个应用。一启动就被干掉了,jboss没有打出什么日志,然后看了下messages里面有OOM-killer. Jun 1 10:09:15 test kernel: Out of Memory: Killed process 10645 (java).Jun 1 10:12:28 test kernel: oom-killer: gfp_mask=0x4d0Jun 1 10:12:28 test kernel: Mem-info: 然后自己第一想到的是直接把OOM-killer关闭掉 echo "0" > /proc/sys/vm/oom-kill 启动应用后就惨了,直接把机器整来hang住了等不进去了,还是用虚拟机,直接强制重启,修复磁盘…… 然后就 /etc/sysctl.conf 里添加 vm.lower_zone_protection=300 重新启动应用终于好了。 参考:http://www.redhat.com/archives/taroon-list/2007-August/msg00006.html

June 1, 2012 · 1 min · pm