常见 NAT 类型描述对应关系

在不同的设备上,对于各种类型的 NAT 有各自不同的描述。先列举一下目前遇到的说法:

PS4

NAT类型 显示PS4™与互联网的连接方式
使用游戏的通信功能等时,可确认与其它PS4™间的连接稳定性。
Type 1:与互联网直接连接。
Type 2:通过路由器与互联网连接。
Type 3:通过路由器与互联网连接。
显示Type 3时,可能会出现无法与其它PS4™顺利通信,使PS4™的网络功能受到限制的情形。详细请参阅

XBox

OPEN NAT MODERATE NAT STRICT NAT
With an OPEN NAT type, you’re able to chat with other people, as well as join and host multiplayer games with people who have any NAT type on their network. With a MODERATE NAT type, you’re able to chat and play multiplayer games with some people; however, you might not be able to hear or play with others, and normally you won’t be chosen as the host of a match. With a STRICT NAT type, you’re only able to chat and play multiplayer games with people who have an OPEN NAT type. You can’t be chosen as the host of a match.

PC

NatTypeTester

Full Cone

Restricted Cone

Port Restricted Cone

Symmetric

其中的对应关系为:

Platform Great Not Good Bad
PS4, PS3 NAT Type 1  NAT Type 2 NAT Type 3
Xbox One, 360 NAT Type Open NAT Type Moderate NAT Type Strict
PC Full Cone Restricted Cone

Port Restricted Cone

Symmetric

未完,后面会介绍具体的信息。

PS命令的STAT列含义

通常,ps命令的输出结果是这样的:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  27176  2960 ?        Ss   Sep20   0:02 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Sep20   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Sep20  13:05 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Sep20   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S<   Sep20   0:00 [kworker/u:0H]
root         8  0.0  0.0      0     0 ?        S    Sep20   2:16 [migration/0]
root         9  0.0  0.0      0     0 ?        S    Sep20   0:00 [rcu_bh]
root        10  0.0  0.0      0     0 ?        S    Sep20  20:08 [rcu_sched]
root        11  0.0  0.0      0     0 ?        S    Sep20   0:07 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    Sep20   0:05 [watchdog/1]
root        13  0.0  0.0      0     0 ?        S    Sep20   2:21 [ksoftirqd/1]
root        14  0.0  0.0      0     0 ?        S    Sep20   1:00 [migration/1]

其中STAT列的各个标识的含义如下:
PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process:
D uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:
< high-priority (not nice to other users) N low-priority (nice to other users) L has pages locked into memory (for real-time and custom IO) s is a session leader l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) + is in the foreground process group.

限制用户进程CPU和内存占用率的SHELL脚本

限制进程CPU占用率的问题,给出了一个shell脚本代码如下:

renice +10 `ps aux | awk ‘{ if ($3 > 0.8 && id -u $1 > 500) print $2}’`

其中用到ps获取进程信息,其实

ps中%CPU一列的意义是进程实际占有CPU时间和它存活时间的比值,这个值能反应进程对CPU的消耗,但不能准确反应进程所占CPU时间占整个系统CPU的百分比。

而top输出中的%CPU这一列正是进程所占CPU时间占整个系统CPU的百分比,用于限制进程CPU占用率更加合理,同时%MEM一列还反应了进程占用内存的百分比,可以用于限制进程内存占用率。

shell脚本代码如下:

#!/bin/sh

PIDS=`top -bn 1 | grep “^ *[1-9]” | awk ‘{ if($9 > 50 || $10 > 25 && id -u $2 > 500) print $1}’`

for PID in $PIDS

do

renice +10 $PID

echo “renice +10 $PID”

done

可以将这个脚本放到cron中运行,比如每分钟检查一次,只需以root身份添加crontab项:

#crontab -e

* * * * * limit.sh

以后每个一分钟就会检查一次,调整占用50%以上CPU或25%内存的进程的nice值,从而使这样的进程优先级变低,被调度的机会减少,同时会向root发邮件提示该进程被调整过。

不过,限制内存使用最好还是用PAM,RedHat可以在/etc/security/limits.conf中设置。