Linxu & Unix的存档

curl DNS cache

2010-04-09 10:55 am

最近有一台服务器无法解析域名了,第一想到dns服务器,原来dns服务器被同事换成了上海网通的,用

  1. 1
  1. dig sogou.com

无法得到结果,后添加两个dns服务器:

  1. 1
  2. 2
  3. 3
  1. nameserver 210.22.70.3
  2. nameserver 202.106.0.20
  3. nameserver 8.8.8.8

再次dig,仍然无法解析,但是ping sogou.com 已经通了,原来dig不会像ping一样去尝试多个dns,

只会用到第一个,可以指定dns:

  1. 1
  1. dig @8.8.8.8 sogou.com

这样就可以得到这个dns给你的解析结果了,另外发现8.8.8.8还是不太稳定,当然因为我们是中国用户,呵呵

另外更换dns后,发现php的curl程序仍然无法得到结果,其实curl有个dns缓存,

  1. 1
  1. curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30);

CURLOPT_DNS_CACHE_TIMEOUT 指dns缓存时间,默认为120s ,或者你重启apache也是一样的,
可以重新载入dns缓存。

推荐(0)
收藏

Ruby 1.9.x中文问题

2010-02-03 1:04 pm

编译安装了Ruby官方推荐的1.9.1.x,却发现处理多字节会有问题,找了不少网文,发现
并没有具体和有效的解决,有修改ruby源码的等等,发现为什么处理这个不合适,考虑稳定性,
还是切换回来1.8.x,发现正常,看到ruby官方有这个bug,但是并未解决,等待吧。

推荐(0)
收藏

Gearmand启动脚本

2009-12-29 6:45 pm

每次启动gearmand都要敲很多参数,干脆写到一起,直接start和stop就可以了,

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  1. #! /bin/sh
  2.  
  3. #----------------------------------------------------------------
  4. # Startup script for the server of Gearmand
  5. #----------------------------------------------------------------
  6.  
  7. # configuration variables
  8. prog="Gearmand"
  9. cmd="/home/tanbin/g/gearmand"
  10. basedir="/home/tanbin/g/a"
  11. port="4730"
  12. pidfile="$basedir/pid"
  13. logfile="$basedir/log"
  14.  
  15. retval=0
  16.  
  17. # setting environment variables
  18. LANG=C
  19. LC_ALL=C
  20. PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
  21. export LANG LC_ALL PATH
  22.  
  23. # start the server
  24. start(){
  25. printf 'Starting the server of Gearmandn'
  26. mkdir -p "$basedir"
  27. if [ -z "$basedir" ] [ -z "$port" ] [ -z "$pidfile" ] then
  28. printf 'Invalid configurationn'
  29. retval=1
  30. elif ! [ -d "$basedir" ] then
  31. printf 'No such directory: %sn' "$basedir"
  32. retval=1
  33. elif [ -f "$pidfile" ] then
  34. pid=`cat "$pidfile"`
  35. printf 'Existing process: %dn' "$pid"
  36. retval=1
  37. else
  38. cmd="$cmd -d -p $port -P $pidfile"
  39. if [ -n "$logfile" ] then
  40. cmd="$cmd -l $logfile"
  41. fi
  42. printf "Executing: %sn" "$cmd"
  43. cmd="$cmd $dbname"
  44. $cmd
  45. if [ "$?" -eq 0 ] then
  46. printf 'Donen'
  47. else
  48. printf 'The server could not startedn'
  49. retval=1
  50. fi
  51. fi
  52. }
  53.  
  54.  
  55. # stop the server
  56. stop(){
  57. printf 'Stopping the server of Tokyo Tyrantn'
  58. if [ -f "$pidfile" ] then
  59. pid=`cat "$pidfile"`
  60. printf "Sending the terminal signal to the process: %sn" "$pid"
  61. kill -TERM "$pid"
  62. c=0
  63. while true do
  64. sleep 0.1
  65. if [ -f "$pidfile" ] then
  66. c=`expr $c + 1`
  67. if [ "$c" -ge 100 ] then
  68. printf 'Hanging process: %dn' "$pid"
  69. retval=1
  70. break
  71. fi
  72. else
  73. printf 'Donen'
  74. break
  75. fi
  76. done
  77. else
  78. printf 'No process foundn'
  79. retval=1
  80. fi
  81. }
  82.  
  83. # send HUP to the server for log rotation
  84. hup(){
  85. printf 'Sending HUP signal to the server of Tokyo Tyrantn'
  86. if [ -f "$pidfile" ] then
  87. pid=`cat "$pidfile"`
  88. printf "Sending the hangup signal to the process: %sn" "$pid"
  89. kill -HUP "$pid"
  90. printf 'Donen'
  91. else
  92. printf 'No process foundn'
  93. retval=1
  94. fi
  95. }
  96.  
  97. # check permission
  98. if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1
  99. then
  100. printf 'Permission deniedn'
  101. exit 1
  102. fi
  103. rm -f "$basedir/$$"
  104.  
  105.  
  106. # dispatch the command
  107. case "$1" in
  108. start)
  109. start
  110. stop)
  111. stop
  112. restart)
  113. stop
  114. start
  115. hup)
  116. hup
  117. *)
  118. printf 'Usage: %s {startstoprestarthup}n' "$prog"
  119. exit 1
  120. esac
  121.  
  122. # exit
  123. exit "$retval"
  124.  
  125. # END OF FILE

脚本非常简单,其实看过ttservctl的启动脚本的人会发现这就是copy过来了,呵呵。

推荐(0)
收藏

linux抓包工具tcpdump

2009-12-25 2:50 pm

今天在尝试TTserver的Http协议接口,为了确定走的协议是否为Http,需要抓包确定一下,
在windows下有很多工具很方便的抓包,linux下只能通过命令行来操作了,下面简单记录一下
tcpdump工具的使用:

  1. 1
  2. 2
  3. 3
  4. 4
  1. //抓包,
  2. tcpdump -nX -s 0 -w cap.raw host 123.123.123.123 and port 2978
  3. //读包
  4. tcpdump -r cap.raw -A

tcpdump有很多参数选项,man就可看到,

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  1. EXAMPLES
  2. To print all packets arriving at or departing from sundown:
  3. tcpdump host sundown
  4.  
  5. To print traffic between helios and either hot or ace:
  6. tcpdump host helios and \( hot or ace \)
  7.  
  8. To print all IP packets between ace and any host except helios:
  9. tcpdump ip host ace and not helios
  10.  
  11. To print all traffic between local hosts and hosts at Berkeley:
  12. tcpdump net ucb-ether
  13.  
  14. To print all ftp traffic through internet gateway snup: (note that the
  15. expression is quoted to prevent the shell from (mis-)interpreting the
  16. parentheses):
  17. tcpdump 'gateway snup and (port ftp or ftp-data)'
  18.  
  19. To print traffic neither sourced from nor destined for local hosts (if
  20. you gateway to one other net, this stuff should never make it onto your
  21. local net).
  22. tcpdump ip and not net localnet
  23.  
  24. To print the start and end packets (the SYN and FIN packets) of each
  25. TCP conversation that involves a non-local host.
  26. tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'
  27.  
  28. To print all IPv4 HTTP packets to and from port 80, i.e. print only
  29. packets that contain data, not, for example, SYN and FIN packets and
  30. ACK-only packets. (IPv6 is left as an exercise for the reader.)
  31. tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<>2)) != 0)'
  32.  
  33. To print IP packets longer than 576 bytes sent through gateway snup:
  34. tcpdump 'gateway snup and ip[2:2] > 576'
  35.  
  36. To print IP broadcast or multicast packets that were not sent via Eth-
  37. ernet broadcast or multicast:
  38. tcpdump 'ether[0] & 1 = 0 and ip[16] >= 224'
  39.  
  40. To print all ICMP packets that are not echo requests/replies (i.e., not
  41. ping packets):
  42. tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'

注:需要root的权限才能运行tcpdump命令
说实话,tcpdump虽然强大,但是毕竟操作起来不如windows下一些图形软件方便。

推荐(0)
收藏

免安装版tokyo_tyrant

2009-12-16 4:45 pm

安装php tokyo_tyrant扩展的系统要求为:

  1. 1
  1. This extension requires Tokyo Cabinet, Tokyo Tyrant and PHP version 5.2.0+.

如果你只是作为client来使用ttserver,而不想安装tokyo cabinet和tokyo tyrant,因为仅仅是作为client而已,
没必要增加安装的复杂度,所以制作了免安装版的php tokyo_tyrant扩展(其实只是so文件打包而已,没有技术含量)
安装要求至少服务器操作系统一致,php是编译安装的,我使用的是centos系统,好了,发上包:

下载

注:如果安装出了问题可以用ldd tokyo_tyrant.so查看一下各个库的链接情况即可自己解决了,呵呵

推荐(0)
收藏

搭建lua环境

2009-12-05 3:10 am

lua是最快的脚本语言,也是各种语言的粘合剂,如mysql proxy,tokyo tyrant程序都用到lua
灵活配置应用等,具体的可以查看官方http://www.lua.org
安装lua需要readline,nucrese,一个是支持编辑的库和图形库,
我用编译安装,其实直接用yum安装是最方便的:

  1. 1
  2. 2
  1. yum install readline-devel
  2. yum install nucrese-devel

然后编译安装lua:

  1. 1
  2. 2
  3. 3
  4. 4
  1. tar -xvzf lua5.1.4.tar.gz
  2. cd lua5.1.4
  3. make linux (这个是你使用的platform)
  4. make install

非常简单就安装好了,然后试试:lua

  1. 1
  1. print("hello world")

一切就ok了。

推荐(0)
收藏

centos 升级php

2009-10-31 11:20 pm

本人是非常喜欢ubuntu的,社区活跃,操作方便,但是公司用的centos,没办法,也得用,
作为一个开发人员,我不想过多的把时间放在各种软件的编译中去,指向快速稳定的部署和测试,
所以apt和yum这样的工具实在是一个好帮手,但是我安装了centos5.4后发现php的版本竟然还是
php5.1.6真是有点失望,看来那些包很久没人去更新了,自己来编译是可以,但是如果所以软件都
自己编译的话会费不少时间,所以还是找个更新的源就得了(测试机器,所以对稳定性要求不高)

找到一个国外的源:

#rpm --import http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
#vi /etc/yum.repos.d/utterramblings.repo
推荐(0)
收藏

memcachedb使用

2009-10-22 3:29 pm

以前就听说过memcachedb了,但是一直也没有直接的使用上,最近项目中要用到,所以写点使用的总结,

1.介绍:

说这个肯定是废话,还是一句话说一下,使用memcache协议,分布能力来使用Berkeley db,也不知道这样准不准确。

1.安装:

官方中说的很清楚,摘录一下就可以,另外,如果ubuntu安装libenvent可以使用

apt-get install libevent1

其他的就需要编译安装了:

Berkeley DB 4.7 or later
-------------------------
http://www.oracle.com/database/berkeley-db/db/index.html

How to install BerkekeyDB:

$tar xvzf db-4.7.25.tar.gz
$cd db-4.7.25/
$cd build_unix/
$../dist/configure
$make
$sudo make install

libevent 1.3e or later
-------------------------
http://monkey.org/~provos/libevent/

How to install libevent:

$tar xvzf libevent-1.3e.tar.gz
$cd libevent-1.3e
$./configure
$make
$sudo make install

Load .so file by add two line in /etc/ld.so.conf:

  /usr/local/lib
  /usr/local/BerkeleyDB.4.7/lib

Then, run 'ldconfig'.

Installation
============
$tar xvzf memcachedb-X.Y.Z.tar.gz
$cd memcachedb-X.Y.Z
$./configure --enable-threads
$make
$sudo make install

这段是官方的,不需多解释了,安装好后启动了:

memcachedb -p21201 -d -r -u root -H ./env -N -v

解释一下,“./env”是data的目录,这个可以自己设定,这个数据是可以移植的,里面的log文件memcachedb会清理,数据文件会不会动的(废话)。
安装好后使用php的memcache扩展就可以了:

$mdb = new Memcache();
$mdb->connect('localhost', 21201);
$mdb->set($key, $result);
$value = $mdb->get($key);

这些基本的操作和对memcached的是一样,基本是memcached的协议,看到老版本对key有限制,我使用的1.2是没有这个限制了,和memcache一样,250字符够用了。和memcached不一样的地方,memcachedb(memcached默认限制1M)并没有对value的大小做限制(当然很大的话没有测试),这个是因为存储是Berkeley db,另外有点奇怪的地方,我看到源码包里面的protocal里面有这么一段:

Expiration times
----------------

Some commands involve a client sending some kind of expiration time
(relative to an item or to an operation requested by the client) to
the server. In all such cases, the actual value sent may either be
Unix time (number of seconds since January 1, 1970, as a 32-bit
value), or a number of seconds starting from current time. In the
latter case, this number of seconds may not exceed 60*60*24*30 (number
of seconds in 30 days); if the number sent by a client is larger than
that, the server will consider it to be real Unix time value rather
than an offset from current time.

应该是没有expiration这个概念的啊,自己试了一下,发现的确没有expiration,再到官方discuss里面找,
作者回答其他人提问时也回答没有expire这个概念了。但是为什么官方文档中有这么一段呢?(可能是指memcache协议,但是放到这里会容易让人误解)。

推荐(0)
收藏

linux 文件系统 && inode cache

2009-10-10 3:52 pm

最近在跟踪apc.stat参数的效果的时候遇到很多问题,为了一弄究竟,一直跟踪下来了,先上一个测试报告,
对zend framework空框架测试,开启apc或者eaccelerator这样的opcode缓存会提升很大的性能(废话),这个并不是我们要研究的,空的zend框架大概需要include 38个文件左右,试过将38个文件中大部分合并成一个文件,再测试,发现性能提升近50%,这个是非常诱人的,不管有没有opcode的缓存是一样的。apc.stat参数来控制每次是否检测涉及到的文件修改过没有,但是在仍然不是很理想,如对include_once和require_once是无效的,也对zend框架的require_once做了优化(官方提供的),改成autoload形式,效果也不明显,(修改文件缓存不更新,但是删除文件会报错),如果不使用zend框架来测试,写几个php文件包含apc.stat是完全没问题的,但是发现效果并不明显。
所以我认为在opcode缓存的情况下,每次检查文件是否修改过的操作并不是瓶颈,用strace(freebsd 下是ktrace)跟踪即使fstat操作。为了证实这点,也顺便对linux的文件系统做一个简单的分析。

引用一些linux文件系统的基础知识:原文地址: http://www.ibm.com/developerworks/linux/library/l-linux-filesystem/?S_TACT=105AGX52&S_CMP=cn-a-l

这个图很好的概述了linux文件系统,

Linux 以一组通用对象的角度看待所有文件系统。这些对象是超级块(superblock)、inode、dentry 和文件。超级块在每个文件系统的根上,超级块描述和维护文件系统的状态。文件系统中管理的每个对象(文件或目录)在 Linux 中表示为一个 inode。inode 包含管理文件系统中的对象所需的所有元数据(包括可以在对象上执行的操作)。另一组结构称为 dentry,它们用来实现名称和 inode 之间的映射,有一个目录缓存用来保存最近使用的 dentry。dentry 还维护目录和文件之间的关系,从而支持在文件系统中移动。

从图中也可以看出,inode和dentry(directory cache)都有cache,会将最近使用到的文件更新inode等缓存,缓存是保存在内存中的,所以读取缓存中的inode是没有io操作的,也就是非常高效的,下面附上linux 内核对inode结构的部分定义,/kernel/include/linux/fs.h中716行:

716 struct inode {
 717     struct hlist_node   i_hash;
 718     struct list_head    i_list;
 719     struct list_head    i_sb_list;
 720     struct list_head    i_dentry;
 721     unsigned long       i_ino;
 722     atomic_t        i_count;
 723     unsigned int        i_nlink;
 727     u64         i_version;
 728     loff_t          i_size;
 729 #ifdef __NEED_I_SIZE_ORDERED
 730     seqcount_t      i_size_seqcount;
 731 #endif
 732     struct timespec     i_atime;
 733     struct timespec     i_mtime;
 734     struct timespec     i_ctime;
742     const struct inode_operations   *i_op;
 743     const struct file_operations    *i_fop; /* former ->i_op->default_file_ops */
}

能看出包括mtime和link等的元信息都在里面,所以要检查文件是否修改只需查看inode就足够了,inode 和目录缓存分别保存最近使用的 inode 和 dentry,且对于 inode 缓存中的每个 inode,在目录缓存中都有一个对应的 dentry。

其实 inode_operations 和 file_operations。这些结构表示可以在这个 inode 上执行的操作。inode_operations 定义直接在 inode 上执行的操作,而 file_operations 定义与文件和目录相关的方法(标准系统调用)。
dentry和super_block的结构在上面的文件中也有,不打出来了。

总结从以上的分析来看,有理由相信系统在判断一个文件是否修改过的操作上是花销是极小的,不会成为opcode检查中的瓶颈。 接下来继续往下跟踪,说明瓶颈可能出现在opcode的compile(include的开销)。

推荐(0)
收藏

使用webalizer分析日志

2009-09-14 6:04 pm

对日志的分析是很重要的,能及时的了解服务器的状况和用户的习惯等,
对日志的分析可以使用awstats,webalizer等等工具,我选择webalizer的原因是
因为它很简单,安装,配置简单,速度快,而且提供给我的数据能满足我的需求,如果想更
强大的分析程序,请google awstats。

首先去下载webalizer,如果使用xampp等套件的同学已经有webalizer了,
在windows下,webalizer在xampp目录下,或者你指定的安装目录,这里主要说linux下,因为你的
服务器应该是在linux环境下。

下载后tar ......; cd ......; 需要先说明的,因为需要生成图片,所以服务器上必须先安装:jpeg,png,zlib,freetype,gd等程序,这个一般你的php能跑到话这些都已经安装好了。

然后直接./configure;make;make install;
安装完成,然后找到sample.webalizer.conf文件(没有自己从源码里面copy一份)放到自己定义的路径下,
配置这个文件,如:

 29 LogFile        /usr/local/apache2/logs/access_log.20090913
 30
 31 # LogType defines the log type being processed.  Normally, the Webalizer
 32 # expects a CLF or Combined web server log as input.  Using this option,
 33 # you can process ftp logs (xferlog as produced by wu-ftp and others),
 34 # Squid native logs or W3C extended format web logs. Values can be 'clf',
 35 # 'ftp', 'squid' or 'w3c'.  The default is 'clf'.
 36
 37 LogType clf
 38
 39 # OutputDir is where you want to put the output files.  This should
 40 # should be a full path name, however relative ones might work as well.
 41 # If no output directory is specified, the current directory will be used.
 42
 43 OutputDir      /home/dongzhiguo/accesslog

每个配置前都有很详细的说明,这里也不多举了,然后cp webalizer文件到/usr/sbin/下(不cp也可以,只是为了放到包含路径可以方便些)。

最后,执行 webalizer -c /usr/local/etc/webalizer.conf (你自定义的配置文件),会在你指定的OutputDir目录下生成分析文件。有很多图标,具体的也不解释了。
如:

注意:执行webalizer和创建目录都要需要有权限。我在apache的log配置里面过滤了图片,js,css等,这样省去很多不必要的数据分析,文件数就可以认为是pv。

附apache的配置:

  1 LogLevel warn
  2
  3 LogFormat "%h %l %u %t "%r" %>s %b" common
  4 LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
  5 LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" "%{B}C" %v %I %O %D" combinedio
  6
  7 SetEnvIf Request_URI (.gif$|.jpg$|.swf$|.js$|.vbs$|.css$) log-ignore
  8
  9 ErrorLog logs/error_log
 10 CustomLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/access_log.%Y%m%d 86400 480" combinedio env=!log-ignore

如果你访问量比较大的话,日志会分割,就像上面的httpd.conf配置,如果要统计一个月的日志,需要把日志进行合并,
简单的办法是使用cat log1 log2 ....> logall ; 或者sort -m -t " " -k 4 -o logall log1 log2 ...;
这个语句指:对log1,log2里面的日志按第四列进行排序后合并成logall,我认为没有必要排序,webalizer这个已经能分别了。

另外说一下webaliezer.hist这个文件,这个文件会记录上次统计的结果,就是上个月的数据,这样就很好的连接成了连贯的数据了。

推荐(0)
收藏