2014年10月7日星期二

Centos 6下通过nicstat进行网络流量统计

1. 用途说明

nicstat is to network interfaces as “iostat” is to disks, or “prstat” is to processes.

nicstat原本是Solaris平台下显示网卡流量的工具,Tim Cook将它移植到linux平台,官方网站见 这里

相比netstat, 他有以下关键特性:
  • Reports bytes in & out as well as packets.
  • Normalizes these values to per-second rates.
  • Reports on all interfaces (while iterating)
  • Reports Utilization (rough calculation as of now)
  • Reports Saturation (also rough)
  • Prefixes statistics with the current time

2. 安装

2.1 获取源码包

wget http://cznic.dl.sourceforge.net/project/nicstat/nicstat-1.95.tar.gz

2.2 修改Makefile

因为默认编译环境为32位,需要对Makefile进行修改以支持CentOS 6.x的64位系统。

具体查看diff结果:
$ diff Makefile Makefile.Linux
23c23
< CMODEL =  -m64
---
> CMODEL =  -m32
33,34c33,34
< OSREL =       6
< CPUTYPE = x86_64
---
> OSREL =       5
> CPUTYPE = i386

2.3 安装

tar -xzf nicstat-1.95.tar.gz
cd nicstat-1.95
cp Makefile.Linux Makefile # 此时按照上面的diff值修改Makefile文件
make
make install # 安装需要root权限

3. 用法


# 以KBytes大小查看网卡流量
$ nicstat 1
    Time      Int   rKB/s   wKB/s   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:49:32       lo    1.02    1.02   17.62   17.62   59.10   59.10  0.00   0.00
11:49:32     eth0   12.32   441.1   67.51   347.6   186.8  1299.3  0.00   0.00
    Time      Int   rKB/s   wKB/s   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:49:33       lo    0.00    0.00    0.00    0.00    0.00    0.00  0.00   0.00
11:49:33     eth0    2.58    2.85   36.95   35.96   71.62   81.31  0.00   0.00
    Time      Int   rKB/s   wKB/s   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:49:34       lo    0.55    0.55   10.00   10.00   56.70   56.70  0.00   0.00
11:49:34     eth0    3.83    4.07   52.99   52.99   73.94   78.57  0.00   0.00

# 以Mbits大小查看网卡流量
$ nicstat -M 1
    Time      Int   rMbps   wMbps   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:53:17       lo    0.01    0.01   17.62   17.62   59.10   59.10  0.00   0.00
11:53:17     eth0    0.10    3.45   67.51   347.6   186.8  1299.3  0.00   0.00
    Time      Int   rMbps   wMbps   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:53:18       lo    0.01    0.01   19.97   19.97   58.15   58.15  0.00   0.00
11:53:18     eth0    0.04    0.04   70.91   69.91   71.69   76.54  0.00   0.00
    Time      Int   rMbps   wMbps   rPk/s   wPk/s    rAvs    wAvs %Util    Sat
11:53:19       lo    0.01    0.01   20.00   20.00   58.25   58.25  0.00   0.00
11:53:19     eth0    0.03    0.03   56.99   56.99   71.81   77.18  0.00   0.00

# 统计tcp连接的情况
$ nicstat -t 1
11:55:19    InKB   OutKB   InSeg  OutSeg Reset  AttF %ReTX InConn OutCon Drops
TCP         0.00    0.00   77.95   77.44  0.04  0.04 0.000   1.76   13.3  0.00
11:55:20    InKB   OutKB   InSeg  OutSeg Reset  AttF %ReTX InConn OutCon Drops
TCP         0.00    0.00   84.87   84.87  0.00  0.00 0.000   2.00   14.0  0.00
11:55:21    InKB   OutKB   InSeg  OutSeg Reset  AttF %ReTX InConn OutCon Drops
TCP         0.00    0.00   84.96   86.96  0.00  0.00 0.000   3.00   14.0  0.00

# 统计udp连接的情况
$ nicstat -u 1
11:55:57                    InDG   OutDG     InErr  OutErr
UDP                         0.50   286.8      0.00    0.00
11:55:58                    InDG   OutDG     InErr  OutErr
UDP                         0.00    0.00      0.00    0.00
11:55:59                    InDG   OutDG     InErr  OutErr
UDP                         0.00    0.00      0.00    0.00

2014年10月3日星期五

通过homebrew安装 nginx-1.6 + php-5.5 + mysql-5.6【方法二】

  1. 安装homebrew
  2. 安装nginx
  3. 安装php
  4. 安装mysql
  5. nginx/php/mysql的启动、关闭、重启命令
  6. 问题解决

1. homebrew的安装

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

2. 安装nginx

brew install nginx

配置文件目录:/usr/local/etc/nginx

3. 安装php

brew tap homebrew/dupes
brew tap homebrew/php

brew install --without-apache --with-fpm --with-mysql php55
配置文件目录:/usr/local/etc/php

4. 安装mysql

brew install cmake
brew install mysql
配置文件目录:/usr/local/Cellar/mysql/5.6.21/my.cnf

5. nginx/php/mysql的启动、关闭、重启命令

在.bashrc文件中添加如下行:
# nginx
alias nginx-start="sudo /usr/local/bin/nginx"   
alias nginx-stop="sudo /usr/local/bin/nginx -s stop"
alias nginx-reload="sudo /usr/local/bin/nginx -s reload"
alias nginx-t="sudo /usr/local/bin/nginx -t"
alias nginx-restart="nginx-stop ; nginx-start"
# php
alias php-start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-stop="killall php-fpm"
alias php-restart="php-stop && php-start"
# mysql
alias mysql-start="/usr/local/bin/mysql.server start"
alias mysql-stop="/usr/local/bin/mysql.server stop"
alias mysql-restart="/usr/local/bin/mysql.server restart"

6. 问题解决

6.1 错误一: 端口问题

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
解决: 启动添加sudo,只有root用户才能使用1024以下端口的问题

6.2 错误二: 文件描述符限制

nginx: [warn] 10240 worker_connections exceed open file resource limit: 256
解决:
sudo launchctl limit maxfiles 16384 16384
ulimit -n 16384

Mac OS X 通过 homebrew 安装和配置nginx、mysql、php-fpm【方法一】

一. 安装nginx

1.1 通过brew安装nginx

brew install nginx
如果需要安装其他 nginx 的版本,可以 “brew edit nginx” 打开修改 nginx 的安装信息包 formula,默认会用 vi 打开,在文件开头处修改 nginx 相应版本的下载地址就行。

1.2 配置nginx

安装完后, nginx的配置文件为/usr/local/etc/nginx/nginx.conf, 按需要进行修改

1.3 开机启动nginx

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/nginx/1.4.7_1/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
##因为不是超级用户登陆,而是普通用户登陆,并且监听1024以下的端口(80端口),nginx 开机是启动不了。  
##所以要 nginx 开机启动的话,需要给予它管理员权限:
sudo chown root:wheel /usr/local/Cellar/nginx/1.4.7_1/bin/nginx  
sudo chmod u+s /usr/local/Cellar/nginx/1.4.7_1/bin/nginx

1.4 启动停止的nginx脚本

$ sudo vim /etc/init.d/nginx

#!/bin/bash
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

rh_start() {
  sudo  $DAEMON || echo -n " already running"
}

rh_stop() {
  sudo $DAEMON -s stop || echo -n " not running"
}

rh_reload() {
  sudo $DAEMON -s reload || echo -n " can't reload"
}

rh_testconfig() {
  sudo $DAEMON -t || echo -n " test failed "
}

case "$1" in
  start)
        echo "Starting $DESC: $NAME"
        rh_start
        ;;
  stop)
        echo "Stopping $DESC: $NAME"
        rh_stop
        ;;
  reload)
        echo "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  t)
        echo "Test $DESC configuration..."
        rh_testconfig
  ;;
  restart)
        echo "Restarting $DESC: $NAME"
        rh_stop
        sleep 2
        rh_start
        ;;
  *)
         echo "Usage: nginx {start|stop|restart|reload|t}" >&2
         exit 3
        ;;
esac
exit 0

二. 安装mysql

2.1 用brew安装mysql

homebrew安装的版本为当前最新的稳定版. 安装好后, 实际上数据的初始化已经做好.

2.2 启动mysql

## 在登录时自动启动mysql
To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
## 想让该启动立即生效
Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
## 不使用苹果自带的方式启动, 可以直接使用启动脚本
Or, if you don't want/need launchctl, you can just run:
    mysql.server start

## 连接数据库
To connect:
mysql -uroot

2.3 修改mysql的初始密码

$ /usr/local/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

三. 安装php

3.1 用brew安装php

brew install homebrew/php/php54 --with-fpm --with-gmp --with-mcrypt

安装php-5.4.27的过程中出现了错误, 访问cn2.php.net发现没有该文件, 出现sha不正确的情况.  
于是从其他节点下载好 php-5.4.27.tar.bz2 文件后改名为 php54-5.4.27, 复制到/Library/Caches/Homebrew/目录, 再进行安装可以顺利进行.

3.2 配置php

php的配置: /usr/local/etc/php/5.4//php.ini
php-fpm的配置文件: /usr/local/etc/php/5.4/php-fpm.conf

3.3 开机启动php

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/php54/5.4.27/homebrew.mxcl.php54.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php54.plist

启动 php-fpm 的话就直接在终端里执行 “php-fpm”,默认打开 php-fpm 会显示一个状态 shell 出来  
把 php-fpm 的配置文件里的 “daemonize = no” 改为 “daemonize = yes”,就会以后台守护进程的方式启动  
对于刚修改的配置文件,可以执行 “php-fpm -t” 来检测配置有没有问题。

3.4 启动和关闭脚本

$ vim /etc/init.d/php-fpm  
#!/bin/bash
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=/usr/local/Cellar/php54/5.4.27/sbin/$NAME
# 配置文件路径
CONFIGFILE=/usr/local/etc/php/5.4/$NAME.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=/var/run/$NAME.pid

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

rh_start() {
  sudo  $DAEMON -y $CONFIGFILE || echo -n " already running"
}

rh_stop() {
  sudo kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

rh_reload() {
  sudo kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
  start)
        echo "Starting $DESC: $NAME"
        rh_start
        #echo "\n"
        ;;
  stop)
        echo "Stopping $DESC: $NAME"
        rh_stop
        #echo "\n"
        ;;
  reload)
        echo "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo "Restarting $DESC: $NAME"
        rh_stop
        sleep 2
        rh_start
        #echo "\n"
        ;;
  *)
         echo "Usage: php-fpm {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0