在网上找到一个阿里云建VPN的一键脚本

#!/bin/bash

function installVPN(){
	echo "begin to install VPN services";
	#check wether vps suppot ppp and tun
	
	yum remove -y pptpd ppp
	iptables --flush POSTROUTING --table nat
	iptables --flush FORWARD
	rm -rf /etc/pptpd.conf
	rm -rf /etc/ppp
	
	arch=`uname -m`
	
	wget http://www.hi-vps.com/downloads/dkms-2.0.17.5-1.noarch.rpm
	wget http://wty.name/linux/sources/kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
	wget http://www.hi-vps.com/downloads/kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
	wget http://www.hi-vps.com/downloads/pptpd-1.3.4-2.el6.$arch.rpm
	wget http://www.hi-vps.com/downloads/ppp-2.4.5-17.0.rhel6.$arch.rpm


	yum -y install make libpcap iptables gcc-c++ logrotate tar cpio perl pam tcp_wrappers
	rpm -ivh dkms-2.0.17.5-1.noarch.rpm
	rpm -ivh kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
	rpm -qa kernel_ppp_mppe
	rpm -Uvh ppp-2.4.5-17.0.rhel6.$arch.rpm	
	rpm -ivh pptpd-1.3.4-2.el6.$arch.rpm

	mknod /dev/ppp c 108 0 
	echo 1 > /proc/sys/net/ipv4/ip_forward 
	echo "mknod /dev/ppp c 108 0" >> /etc/rc.local
	echo "echo 1 > /proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
	echo "localip 172.16.36.1" >> /etc/pptpd.conf
	echo "remoteip 172.16.36.2-254" >> /etc/pptpd.conf
	echo "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd
	echo "ms-dns 8.8.4.4" >> /etc/ppp/options.pptpd

	pass=`openssl rand 6 -base64`
	if [ "$1" != "" ]
	then pass=$1
	fi

	echo "vpn pptpd ${pass} *" >> /etc/ppp/chap-secrets

	iptables -t nat -A POSTROUTING -s 172.16.36.0/24 -j SNAT --to-source `ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk 'NR==1 { print $1}'`
	iptables -A FORWARD -p tcp --syn -s 172.16.36.0/24 -j TCPMSS --set-mss 1356
	service iptables save

	chkconfig iptables on
	chkconfig pptpd on

	service iptables start
	service pptpd start

	echo "VPN service is installed, your VPN username is vpn, VPN password is ${pass}"
	
}

function repaireVPN(){
	echo "begin to repaire VPN";
	mknod /dev/ppp c 108 0
	service iptables restart
	service pptpd start
}

function addVPNuser(){
	echo "input user name:"
	read username
	echo "input password:"
	read userpassword
	echo "${username} pptpd ${userpassword} *" >> /etc/ppp/chap-secrets
	service iptables restart
	service pptpd start
}

echo "which do you want to?input the number."
echo "1. install VPN service"
echo "2. repaire VPN service"
echo "3. add VPN user"
read num

case "$num" in
[1] ) (installVPN);;
[2] ) (repaireVPN);;
[3] ) (addVPNuser);;
*) echo "nothing,exit";;
esac

注:这个脚本最后一步进行iptables转发时,会指向为阿里云的内网地址,需要手工操作一下,修改为公网IP。

nginx 日志切割

#!/bin/bash
## 零点执行该脚本
## Nginx 日志文件所在的目录
LOGS_PATH=/usr/local/nginx/logs
## 获取昨天的 yyyy-MM-dd
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
## 移动文件
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log

tar cvfz ${LOGS_PATH}/access_${YESTERDAY}.log.tar.gz ${LOGS_PATH}/access_${YESTERDAY}.log

## 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

日志按大小进行分割

#!/bin/bash
## 按文件大小切割日志文件
## 注:实际测试中发现,对log文件进行切割后,rsyslogd短时间内会继续往mv后的文件写日志

## 文件日志所在的目录
LOG_PATH=/opt/logs/router-log

## 日志文件超过多少字节后,进行切割,最多进行100份切割,超过后,按时间为后缀
## 100M : 104857600
## 500M : 524288000
## 1G   : 1073741824

LOG_MAX_SIZE=5240

currentDate1=`date "+%Y%m%d_%H%M"`
currentLogDate=`date "+%Y-%m-%d"`

echo "start log_rotate.sh $currentDate1"
echo "LOG_PATH:$LOG_PATH"
echo "LOG_MAX_SIZE:$LOG_MAX_SIZE"

sendHUP=0

for name in $LOG_PATH/*
do
        if [ -f "$name" ]; then
                if [ "${name##*.}" == "log" ]; then
                        filesize=$(stat -c '%s' $name)
                        if [[ $filesize -ge $LOG_MAX_SIZE ]]; then
                        rotating=0

                        for((i=1;i<=100;i++))
                        do
                                if [ ! -f "$name.$i" ]; then
                                        echo "mv $name $name.$i"
                                        mv $name $name.$i
                                        rotating=1
                                        sendHUP=1
                                        break
                                fi


                        done

                        if [ $rotating -eq 0 ]; then
                                currentDate=`date "+%Y%m%d_%H%M"`
                                mv $name $name.$currentDate
                                sendHUP=1
                        fi
                        fi
                fi

        fi

        if [ -d "$name" ]; then

                if [ -d "$name/$currentLogDate" ]; then

                        for name2 in $name/$currentLogDate/*
                        do
                                if [ -f "$name2" ]; then
                                        if [ "${name2##*.}" == "log" ]; then
                                        filesize=$(stat -c '%s' $name2)
                                        if [[ $filesize -ge $LOG_MAX_SIZE ]]; then
                                                rotating=0

                                                for((i=1;i<=100;i++))
                                                do
                                                        if [ ! -f "$name2.$i" ]; then
                                                                echo "mv $name2 $name2.$i"
                                                                mv $name2 $name2.$i
                                                                rotating=1
                                                                sendHUP=1
                                                                break
                                                        fi


                                                done

                                                if [ $rotating -eq 0 ]; then
                                                        currentDate=`date "+%Y%m%d_%H%M"`
                                                        mv $name2 $name2.$currentDate
                                                        sendHUP=1
                                                fi
                                        fi
                                        fi
                                fi
                        done
                fi

        fi
done

if [ $sendHUP -eq 1 ]; then
        echo "send HUP to rsyslogd.pid:$(cat /var/run/syslogd.pid)"
        kill -HUP $(cat /var/run/syslogd.pid)
else
        echo "not send HUP"
fi

svn批量添加文件

svn批量添加文件:

svn st | awk '{if($1 == "?"){print $2}}'|xargs svn add

好吧,一个makefile的初学者学习skynet中

1、第一步就是项目的组织方式。skynet经常要更新,所以要尽量避免在自己项目共用同一个git。从群中和云风的文章中了解到,使用submodule引用skynet是一种比较理想的方式。我一个makefile基本为0的人要理解submodule的编写很不容易,而且也不知道过程对不对,但现在至少已经可以初步达到目标了。

# hello game
#

.PHONY : all clean skynet

CC ?= gcc
PLAT ?=linux

LUA_CLIB_PATH=/opt/git/hellogame/luaclib
CSERVICE_PATH=/opt/git/hellogame/cservice

SKYNET_BUILD_PATH ?=/opt/git/hellogame

all : skynet

skynet :
        cd module_skynet && $(MAKE) CC=$(CC) SKYNET_BUILD_PATH=$(SKYNET_BUILD_PATH) LUA_CLIB_PATH=$(LUA_CLIB_PATH) CSERVICE_PATH=$(CSERVICE_PATH) linux

clean :
        cd module_skynet && $(MAKE) CC=$(CC) SKYNET_BUILD_PATH=$(SKYNET_BUILD_PATH) LUA_CLIB_PATH=$(LUA_CLIB_PATH) CSERVICE_PATH=$(CSERVICE_PATH) clean

写的很差,先用着吧。module_skynet是引用的skynet

2、复制module_skynet/example/config到本地目录。

然后修改config中的lua地址:

root = "./"
thread = 8
logger = nil
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
start = "main"  -- main script
bootstrap = "snlua bootstrap"   -- The service for bootstrap
standalone = "0.0.0.0:2013"
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;".."module_skynet/service/?.lua;"..root.."module_skynet/test/?.lua;"..root.."module_skynet/examples/?.lua"
lualoader = "lualib/loader.lua"
-- preload = "./examples/preload.lua"   -- run preload.lua before every lua service run
snax = root.."examples/?.lua;"..root.."test/?.lua"
cpath = root.."cservice/?.so"
-- daemon = "./skynet.pid"

只修改其中 luaservice的地址。

3、启动 ./skynet config,提示找不到lualib/loader.lua,又建立了一个软连接指向module_skynet/lualib,可以顺利解决问题。

4、以上方式不保证是最合适的,但至少可以进行开发了。

mysql 中的date 0 处理

jdbc mysql遇到日期为0时即报异常。

这里需要在jdbc.url上多传一个参数 zeroDateTimeBehavior=convertToNull

用来告诉jdbc驱动,遇到date为0是设置为null。

安装ganglia

#安装ganglia

yum install zlib-devel freetype-devel libart_lgpl-devel libpng-devel rrdtool expat apr apr-util gcc-c++ rrdtool-devel  apr-devel apr-util-devel

#安装libconfuse

wget http://download.savannah.gnu.org/releases/confuse/confuse-2.7.tar.gz
tar xvfz confuse-2.7.tar.gz
cd confuse-2.7
./configure CFLAGS=-fPIC
make
make install

#安装ganglia
./configure –with-gmetad –with-python –enable-gexec –with-libconfuse=/usr/local/confuse –with-libexpat=/usr/local/expat  –sysconfdir=/etc/ganglia
make
make install

#创建rrdtool数据目录:

mkdir -p /var/lib/ganglia/rrds
mkdir -p /var/lib/ganglia/dwoo
chown -R apache:apache /var/lib/ganglia

#修改/etc/ganglia/gmetad.conf文件:
vim /etc/ganglia/gmetad.conf
data_source “suc” localhost
setuid_username “apache”

#启动 gmetad
cp -f gmetad/gmetad.init /etc/init.d/gmetad
cp -f /usr/local/sbin/gmetad /usr/sbin/gmetad
chkconfig –add gmetad
service gmetad start

#通过telnet localhost 8651验证gmetad是否正常

#客户端配置 gmond节点
cp -f gmond/gmond.init /etc/init.d/gmond
cp -f /usr/local/sbin/gmond /usr/sbin/gmond
chkconfig –add gmond
gmond –default_config > /etc/ganglia/gmond.conf
#修改/etc/ganglia/gmond.conf配置文件:
cluster {
name=”suc”
owner=”apache”
latlong=”unspecified”
url=”unspecified”
}

service gmond start
通过telnet localhost 8649验证gmond是否正常

ganglia-web可以允许在web中浏览数据,是使用php写的,网上大多数文章写的放在apache httpd中运行,实际上nginx一样可以运行。

脚本远程上传文件

#!/bin/bash
local_dir=$1
target_dir=$1
user=root
password=password
serverip=ip

lftp -c "open -u $user,$password sftp://$serverip ; mirror -R --delete $local_dir $target_dir"

今天想测试map的读取key的性能,结果遇到无法解答的问题。

代码如下:

如果注释掉test2(),启动test2_1(),则test()消耗的时间大幅减少,不知原因。

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public class Test4 {
	private static Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
	private static Map<String, String> map2 = new HashMap<String, String>();
	private static Map<String, String> map3 = new IdentityHashMap<String, String>();
	private static String testKey = "helloworld";
	private static long time;
	private static long time2;
	private static Integer a = 1500;

	public static void main(String[] args) {
		map3.put(testKey, testKey);
		for (int m = 1000; m < 1010; m++) {
			map1.put(m, m);
			map2.put("" + m, "" + m);
			map3.put("" + m, "" + m);
		}
		for (int mm = 0; mm < 100; mm++) {
			test1();
			test2();
			// test2_1();
		}
	}

	public static void test1() {
		time = System.nanoTime();
		for (int m = 0; m < 1000000000; m++) {
			map1.get(a);
		}
		time2 = System.nanoTime();
		System.out.println("数字:" + (time2 - time));
	}

	public static void test2() {
		time = System.nanoTime();
		for (int m = 0; m < 1000000000; m++) {
			map2.get("hello");
		}
		time2 = System.nanoTime();
		System.out.println("字符:" + (time2 - time));
	}

	public static void test2_1() {
		time = System.nanoTime();
		for (int m = 0; m < 1000000000; m++) {
			map2.get("hello222222222222222222222222222222222222222222222222222222222222222222222222222222222222222");
		}
		time2 = System.nanoTime();
		System.out.println("字符:" + (time2 - time));
	}

	public static void test3() {
		time = System.nanoTime();
		for (int m = 0; m < 1000000000; m++) {
			map3.get(testKey);
		}
		time2 = System.nanoTime();
		System.out.println("字符3:" + (time2 - time));
	}
}

loganalyzer3.6.5的一个bug

从昨天开始搞loganalyzer,一直无法显示自己mysql中的日志。

日志表是自定义的。

+---------+---------------------+------+-----+-------------------+----------------+
| Field   | Type                | Null | Key | Default           | Extra          |
+---------+---------------------+------+-----+-------------------+----------------+
| logId   | bigint(20) unsigned | NO   | PRI | NULL              | auto_increment | 
| account | char(64)            | NO   |     |                   |                | 
| server  | int(10) unsigned    | NO   |     | 0                 |                | 
| device  | char(64)            | NO   |     |                   |                | 
| error   | blob                | YES  |     | NULL              |                | 
| uID     | int(11)             | YES  |     | 0                 |                | 
| date    | timestamp           | NO   |     | CURRENT_TIMESTAMP |                | 
+---------+---------------------+------+-----+-------------------+----------------+

自己定义datasource,field,view,dbmapping,一切都OK。但就是显示有问题。

如下图所示少了uID那一列的数据。

google了N多资料都无解。

网上也有一个相同现象的帖子,也没有找到原因:http://kb.monitorware.com/reading-custom-mysql-table-t11919.html

一气之外感觉自己debug可能能找到原因。

在classes/logstreamdb.class.php 中

修改readnext方法如下:

 if ( $content['MaxExecutionTime'] > 0 && $scriptruntime > ($content['MaxExecutionTime']-2) )
                        {
                                // This may display a warning message, so the user knows we stopped reading records because of the script timeout.
                                $content['logstream_warning'] = "false";

                                // Run optional Message Parsers now
                                if ( isset($arrProperitesOut[SYSLOG_MESSAGE]) )
                                {
                                        $retParser = $this->_logStreamConfigObj->ProcessMsgParsers($arrProperitesOut[SYSLOG_MESSAGE], $arrProperitesOut);

                                        // Check if we have to skip the message!
                                        if ( $retParser == ERROR_MSG_SKIPMESSAGE )
                                                $ret = $retParser;
                                }

                                // Set uID to the PropertiesOut! //DEBUG -> $this->_currentRecordNum;
                                echo "<br/>";
                                echo $dbmapping[$szTableType];
                                foreach($dbmapping[$szTableType] as $key => $val) {
                                   echo "|".$key."|".$val, '<br>';
                                }
                                foreach($dbmapping[$szTableType]['DBMAPPINGS'] as $key => $val) {
                                   echo "|_|".$key."|".$val, '<br>';
                                }

                                foreach($this->bufferedRecords[$this->_currentRecordNum] as $key => $val) {
                                   echo "|||".$key."|".$val,"<br/>";
                                }

                                echo "SYSLOG_UID:".SYSLOG_UID."<br/>";
                                echo "hello ". $dbmapping[$szTableType]['DBMAPPINGS']['uID']."world";
                                echo "hello ". $this->bufferedRecords[$this->_currentRecordNum]['uid']."world";
                                echo "<br/>";
                                $uID = $arrProperitesOut[SYSLOG_UID] = $this->bufferedRecords[$this->_currentRecordNum][$dbmapping[$szTableType]['DBMAPPINGS'][SYSLOG_UID]];

                                // Increment $_currentRecordNum
                                $this->_currentRecordNum++;
                        }

把debug信息输出后,才发现问题。

uID对应数据中的logId字段,但在this->bufferedRecords中的key全是小写的。你妹的,各个文档上谁也没写要注意这么一个东西。dbmapping直接修改uID改为logid小写后,日志查看bug解决了。

正常的显示: