使用Logrotate来管理系统日志(转贴)

原文:http://tech.ccidnet.com/art/737/20070914/1212669_1.html

发布时间:2007.09.15 06:39     来源:赛迪网    作者:kid

对于Linux 的系统安全来说,日志文件是极其重要的工具。系统管理员可以使用logrotate 程序用来管理系统中的最新的事件。

对于Linux 的系统安全来说,日志文件是极其重要的工具。

系统管理员可以使用logrotate 程序用来管理系统中的最新的事件。logrotate 还可以用来备份日志文件,本篇将通过以下几部分来介绍

日志文件的管理:

1、logrotate 配置

2、缺省配置 logrotate

3、使用include 选项读取其他配置文件

4、使用include 选项覆盖缺省配置

5、为指定的文件配置转储参数

一、logrotate 配置

logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行。

logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。

logrotate 的配置文件是 /etc/logrotate.conf。主要参数如下表:

参数 功能

compress 通过gzip 压缩转储以后的日志

nocompress 不需要压缩时,用这个参数

copytruncate 用于还在打开中的日志文件,把当前日志备份并截断

nocopytruncate 备份日志文件但是不截断

create mode owner group 转储文件,使用指定的文件模式创建新的日志文件

nocreate 不建立新的日志文件

delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩

nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。

errors address 专储时的错误信息发送到指定的Email 地址

ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。

notifempty 如果是空文件的话,不转储

mail address 把转储的日志文件发送到指定的E-mail 地址

nomail 转储时不发送日志文件

olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统

noolddir 转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行

postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

daily 指定转储周期为每天

weekly 指定转储周期为每周

monthly 指定转储周期为每月

rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份

tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~

size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

二、缺省配置 logrotate

logrotate 缺省的配置募?/etc/logrotate.conf。

Red Hat Linux 缺省安装的文件内容是:

# see “man logrotate” for details

# rotate log files weekly

weekly

# keep 4 weeks worth of backlogs

rotate 4

# send errors to root

errors root

# create new (empty) log files after rotating old ones

create

# uncomment this if you want your log files compressed

#compress

1

# RPM packages drop log rotation information into this directory

include /etc/logrotate.d

# no packages own lastlog or wtmp –we’ll rotate them here

/var/log/wtmp {

monthly

create 0664 root utmp

rotate 1

}

/var/log/lastlog {

monthly

rotate 1

}

# system-specific logs may be configured here

缺省的配置一般放在logrotate.conf 文件的最开始处,影响整个系统。在本例中就是前面12行。

第三行weekly 指定所有的日志文件每周转储一次。

第五行 rotate 4 指定转储文件的保留 4份。

第七行 errors root 指定错误信息发送给root。

第九行create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和

原来的文件一样的权限。

第11行 #compress 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。

三、使用include 选项读取其他配置文件

include 选项允许系统管理员把分散到几个文件的转储信息,集中到一个

主要的配置文件。当 logrotate 从logrotate.conf 读到include 选项时,会从指定文件读入配置信息,就好像他们已经在/etc/logrotate.conf 中一样。

第13行 include /etc/logrotate.d 告诉 logrotate 读入存放在/etc/logrotate.d 目录中的日志转储参数,当系统中安装了RPM 软件包时,使用include 选项十分有用。RPM 软件包的日志转储参数一般存放在/etc/logrotate.d 目录。

include 选项十分重要,一些应用把日志转储参数存放在 /etc/logrotate.d 。

典型的应用有:apache, linuxconf, samba, cron 以及syslog。

这样,系统管理员只要管理一个 /etc/logrotate.conf 文件就可以了。

四、使用include 选项覆盖缺省配置

当 /etc/logrotate.conf 读入文件时,include 指定的文件中的转储参数将覆盖缺省的参数,如下例:

# linuxconf 的参数

/var/log/htmlaccess.log

{ errors jim

notifempty

nocompress

weekly

prerotate

/usr/bin/chattr -a /var/log/htmlaccess.log

endscript

postrotate

/usr/bin/chattr +a /var/log/htmlaccess.log

endscript

}

/var/log/netconf.log

{ nocompress

monthly

}

在这个例子中,当 /etc/logrotate.d/linuxconf 文件被读入时,下面的参数将覆盖/etc/logrotate.conf中缺省的参数。

Notifempty

errors jim

五、为指定的文件配置转储参数

经常需要为指定文件配置参数,一个常见的例子就是每月转储/var/log/wtmp。为特定文件而使用的参数格式是:

# 注释

/full/path/to/file

{

option(s)

}

下面的例子就是每月转储 /var/log/wtmp 一次:

#Use logrotate to rotate wtmp

/var/log/wtmp

{

monthly

rotate 1

}

六、其他需要注意的问题

1、尽管花括号的开头可以和其他文本放在同一行上,但是结尾的花括号必须单独成行。

2、使用 prerotate 和 postrotate 选项

下面的例子是典型的脚本 /etc/logrotate.d/syslog,这个脚本只是对

/var/log/messages 有效。

/var/log/messages

{

prerotate

/usr/bin/chattr -a /var/log/messages

endscript

postrotate

/usr/bin/kill -HUP syslogd

/usr/bin/chattr +a /var/log/messages

endscript

}

第一行指定脚本对 /var/log messages 有效

花ê哦阅诓康慕疟驹诵杏? /var/log/messages

prerotate 命令指定转储以前的动作/usr/bin/chattr -a 去掉/var/log/messages文件的“只追加”属性 endscript 结束 prerotate 部分的脚本postrotate 指定转储后的动作

/usr/bin/killall -HUP syslogd

用来重新初始化系统日志守护程序 syslogd

/usr/bin/chattr +a /var/log/messages

重新为 /var/log/messages 文件指定“只追加”属性,这样防治程序员或用户覆盖此文件。

最后的 endscript 用于结束 postrotate 部分的脚本

3、logrotate 的运行分为三步:

判断系统的日志文件,建立转储计划以及参数,通过cron daemon 运行下面的代码是 Red Hat Linux 缺省的crontab 来每天运行logrotate。

#/etc/cron.daily/logrotate

#! /bin/sh

/usr/sbin/logrotate /etc/logrotate.conf

4、/var/log/messages 不能产生的原因:

这种情况很少见,但是如果你把/etc/services 中的 514/UDP 端口关掉的话,这个文件就不能产生了。

小结:本文通过对Red Hat 系统上典型的logrotate 配置例子的介绍,详细说明了logrotate 程序的应用方法。希望对所有Linux 系统管理员有所帮助。管理好,分析好日志文件是系统安全的第一步,在以后的文章里FreeLAMP还会介绍另外一个检查日志的好东东 logcheck。

我们来聊聊syslogd和logrotate吧(转贴)

原文:http://www.chinaunix.net/jh/4/323489.html

————-
文档目录
————-
1 什么是syslogd
2 配置syslogd的说明
3 syslogd和系统中服务配置中日志的关系
4 什么是logrotate
5 配置logrotate的说明

————-
文档正文
————-
1 什么是syslogd
syslogd 可以简单地被称为记录系统活动的一个daemons。比如可以记录谁,在什么时间,在哪里,做了什么事情(像是在写记叙文啊);也可以记录您的系统曾经发 生过什么事情,比如什么时候重新引导过、软硬件的错误信息等;当然也记录着您系统上运行着的服务的信息。
很多时候,有朋友上来就问, “我的X不能启动了是怎么回事啊?!”问号和叹号这样使用在语文规范上是不允许的,但是我们可以获知,他的心情是急迫的。可是为什么您不先考虑一下您做了 什么更改,为什么不先看看是否有错误输出,为什么不看看日志文件?起码,日志为解决问题提供了很好的参考啊。很多人讲自己的某个设备不工作,dmesg的 信息您是否认真参考了?
syslogd做的是琐碎的工作,但却是相当重要的工作。很多朋友为了提高系统的性能,节省那一点点资源就决定把这个daemons停掉。我认为,这是不可取的。
syslogd记录的日志一般在/var/log/下,当然也有存储在另外的服务器上的。因为syslogd记录的信息实在是太重要了,所以还要涉及日志安全的问题。
一般系统中日志信息:

    /var/log/secure: 记录系统的安全信息,比如ssh、ftp、pop3等;

    /var/log/wtmp: 记录谁曾经登陆过系统,由于本日志被编码过,所以只能用last命令查看;

    /var/log/boot.log: 顾名思义,记录开启或者关闭系统及武夫的信息;

    /var/log/message:系统发生的错误信息都会记录在这个日志中,比如iptables中您使用log功能的日志;

    /var/log/mail:

    /var/log/httpd/

    /var/log/mysqld.d 等,记录的就是这些服务的日志。

2 配置syslogd的说明
2.1 启动syslogd服务
首先,您要确定您的系统是否运行着这个服务。

service [color=red]syslog[/color] status

或者:

ps -aux|grep syslog

如果您的系统中并没有运行这个服务,您可以打开它。方法很多啦。

service syslog start

如果您希望系统在下次启动的时候就运行syslogd,您可以在setup中的服务中添加。如果确定您的默认引导级别,比如3,那么您也可以在 /etc/rc.d/rc3.d/下添加以 S 开头的软连接。或者,您使用[color=green] chkconfig 2345 syslog on[/color]来添加。
2.2 syslogd服务的配置文件
syslogd的配置文件一般在/etc/syslog.conf中。这个文件依然遵循你所见过的其它配置文件的规则,比如 # 是注释。您可以看看您的syslogd都在帮助您记录着什么。这是我的syslog.conf中的一部分:
引用:
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don’t log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  /var/log/maillog

# Log cron stuff
cron.*                                                  /var/log/cron

2.3 syslog.conf的配置规则

服务名称.信息等级	存放或者显示地点

这个语法很简单呢。呵呵。逐个解释一下。

[color=red]服务名称[/color]
mail http at cron kern 等等。

[color=red]信息等级[/color]

    info: 一些提示信息资料;

    notice: 需要您注意的信息;

    warn或者waring: 警告信息;

    [color=black]上面三个信息虽然是提醒您注意,但是却还没有到错误的情况。下面的信息就要注意了。[/color]

    error或者err: 错误信息。您需要仔细检查发生错误的原因了;

    crit: 很严重的错误,到达临界点了;

    alert: 警告! 是否想起了“Red Alert”?不过,在这里这可是相当严重的错误啊;

    emerg或者panic:系统混乱,重做吧;

    特别的:

    debug: 将显示很多信息;

    none: 顾名思义,什么信息也不记录。

[color=red]存放或者显示地点[/color]

    日志的绝对路径: 比如/var/log;

    您的一个用户 ;

    网络上的主机: @log.company.com

    打印机: /dev/lp0

2.4 应用举例

    mail.info	/var/log/maillog

大于等于info的信息都会写到/var/log/maillog中。

    mail.*;cron.* /var/log/mailcron mail.=warn;cron.=warn /var/log/mailcronwarn

等级为warn的信息,写进/var/log/mailcronwarn,其它的信息写进/var/log/mailcron。

    *.*;mail,cron.none /var/log/message
    *.*;mail.none;cron.none /var/log/message

记录除去mail和cron之外的所有服务的所有信息。

3 syslogd和系统中服务配置中日志的关系
上面已经说了,syslogd是为系统提供日志服务的。那么,我们在配置文件中定义的日志信息和syslog.conf有什么关系呢?
或者您也注意到了这段:

# Log anything (except mail) of level info or higher.

# Don't log private authentication messages!

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

这段话是什么意思呢?我认为是在/var/log/messages中记录除去mail、authpriv和cron之外的所有系统信息。也就是说它会记 录您自己的http信息。是否这样呢?我自己安装了一个httpd,并且指定日志到另外一个文件,比如 /usr/website/log/httplog。我虽然可以在/usr/website/log/httpdlog中看到我的httpd的日志信息, 但是我执行

# cat /var/log/messages|grep HTTP

却得到空。
另外,就这个问题我曾经请教一个朋友。他说syslogd不会记录你没有要求它记录的信息,虽然有上面的*.info但是这个*是不包括你自己的服务的。 我问,那么iptables也没有在我的syslog.conf中要求,那为什么它会记录到/var/log/messages呢?或者是 iptables自己定义了吧。
[color=blue]所以[/color],关于这段的理解我只有这样的认识。如果有错误,请您指出。:D

4 什么是logrotate
logrotate 是对日志文件做轮换。就是把现在的log命名为log.1,然后继续写log。如果存在log.1就命名log.1为log.2然后命名log为 log.1,依此类推,但并非没有尽头。这个尽头就是您在logrotate的配置文件中的定义,我的系统默认的是到4。那么对log.4做什么操作呢? 删除。

syslogd是daemons方式运行的;
logrotate是按计划运行的。

5 配置logrotate的说明
5.1 配置文件的位置

/etc/logrotate.conf
/etc/logrotate.d

其中,/etc/logrotate.conf是主要配置文件,/etc/logrotate.d中的文件会被/etc/logrotate.conf读 取。如果您在/etc/logrotate.d中的配置文件没有规定具体的参数,则这些参数由/etc/logrotate来决定。
5.2 logrotate的配置规则
正如在上面5.1中所说的,logrotate的主要配置在/etc/logrotate.conf中设置,而/etc/logrotate.d中的文件 是对/etc/logrotate.conf的补充。或者可以看作为了不使/etc/logrotate.conf过大而设置:D。logrotate的 写法:

    把logfile(s)写在前面,包含文件的绝对路径,可以使用空白字元分隔多个log,也可以使用统配符置换;

    用 { } 包含所有设定;

    一般包括:

    prerotate 在启动 logrotate 之前执行的命令,比如 /usr/bin/charrt -a /var/log/logfile;

    postrotate 在执行了 logrotate 之后执行的命令,比如 /usr/bin/charrt +a /var/log/logfile;

    您可以设定执行如上两个动作,也可以不设定,这依赖于您的需要。

    在prerotate与postrotate之间的动作有:

    weeky                                  #每个星期执行一次

    rotate 4                               #保留四个日志

    create                                 #logratoe之后再建立日志

    compress                               #rotate之后的日志是否压缩

    include /etc/logrotate.d                #包含/etc/logrotate.d目录下面的轮换设置

如果您自己在/etc/logrotate.d下写了自己的轮换设置可以使用

logrotate -f yourfile

来测试。

5.3 鉴于这个文档的设置比较明了所以不在此举例。您可以参考您的 /etc/logrotate.conf 和 /etc/logrotate.d 下面的文档看看。:D

好了,这个文档写到这里就算是结束了。正如我在文档的开始所说,这是我假期看书的一点体会,写出来是为了加深自己的理解;同时也是为了发现问题。如果您发现问题请指正。:D
关于楼主讲的关于为什么自己装的httpd的log没有记录到/var/log/message中去的问题
关于*号的匹配问题
我查了下文档
是这样的
auth  由 pam_pwdb 报告的认证活动。
authpriv 包括特权信息如用户名在内的认证活动
cron  与 cron 和 at 有关的信息。
daemon 与 inetd 守护进程有关的信息。
kern  内核信息,首先通过 klogd 传递。
lpr   与打印服务有关的信息。
mail  与电子邮件有关的信息
mark  syslog 内部功能用于生成时间戳
news  来自新闻服务器的信息
syslog  由 syslog 生成的信息
user   由用户程序生成的信息
uucp   由 uucp 生成的信息
local0—-local7   与自定义程序使用,例如使用 local5 做为 ssh 功能
*   通配符代表除了 mark 以外的所有功能

这里的local0-local7可以让用户(程序)自定义使用
其实可以通过
man syslog
来看
引用:iptables自己定义了吧。
所以,关于这段的理解我只有这样的认识。如果有错误,请您指出

是的

引用:日志的绝对路径: 比如/var/log;
您的一个用户 ;
网络上的主机: @log.company.com
打印机: /dev/lp0

我解釋你沒解釋之處:
用戶: 寫 user account , 這個動作系統將以 write 指令,將 syslog 訊息丟給他
如果寫成 * , 那就會是傳給每個人 (wall)

主機:
你原來的 syslogd.conf 不要動它,在最後加一行:

*.*   @FQDN
建議您使用 FQDN ,也就是 DNS 查得到的名字,不要使用 /etc/hosts,
如果你像我一樣有n 部要做日常管理(N>;40),log server(後述) 改個 IP 或有
什麼狀況,你每台都要去改也太累人了,所以要用 DNS 可解的名稱來做,
要你不改原來的東西原因在於,如此你就會有兩份 syslogd,一份存本機(依你原來
的定義),一份存遠端,至於像 apache/named/… 這種 log 你只要定義好
他們的 log 要寫到 syslog 的那一個 facility  (建議使用 User facility,
Local0-7 )

在遠端的 syslogd 主機,啟動參數要加 -r , 自己去改一下 rc.d 下的東西
syslogd 預設是走 udp 514,Firewall 要放行

至於設定檔什麼都不用改,只改啟動參數即可,但建議將  local0-7 都補上
因若你有 Sun 或其他的機器時,他們常會用到某幾個
(註: sun 中的 syslogd.conf 寫了 @log.server.domain , 還得再跑 snoop 將 udp port 514 打開,如此 syslog 才送的出來)

如果你真有很多機器,建議你 syslogd.conf 一定要細分,每天優先檢視最 critical
的,當然,像我這種情況,光一天我就有超過 1000000 行 syslog , 根本看不完
(LogWatch 對我來說用處不大,因為 syslog 的主機太多, OS 也不同,還有
Firewall , Cisco …等都寫進來)

,建議你可以改用 syslog-ng

http://www.linuxsecurity.com/feature_stories/feature_story-138.html

基本上設定完全相容,但是你可以改 log 到 pgsql/mysql
看得熟了,還可以做到像這樣

將 syslog 轉成 mysql/pgsql ,
功能強大,網頁寫的不夠多,但download 下來的文件有寫,請先看文件!!

# PASS only logs NOT containing the string ’connection reset by peer’ to next mo
dule
*.emerg %regex -v -m ’connection reset by peer’ %classic root

# PASS only logs containing the string ’disk full’ to next module
*.emerg %regex -m ’disk full’ %classic root

# PASS ONLY logs with host matching ’www’
*.emerg %regex -h ’www’ %classic /var/log/webserver

# PASS ONLY logs with host from 8pm to 9am (20:00:00 to 09:00:00)
# and also matching ’root’
auth.info %regex -v -t ’^1′ %regex -m ’root’ %classic /var/log/webserver

有興趣的人可自己去看看,尤其機器多時,滙整,分析,就會變的很重要…
上面那個 link 都還有 php 做介面  Event View 的介面(雖然很陽春 :em20: )…

另外,有人問看什麼書…我不知樓主看什麼書,我看的是 man page
把 See Also 相關的都看過,就很清楚了

syslog to remote 還有一個小小心得,建議你將 local0-7 中找一個
只有你自己會用的,但不會被別的程式佔走的,你若要什麼東西要做統計
就用 logger 這個指令寫進來這個 facility  , 如此就可以很方便的在
log server 上跑一些監測軟體,因為你只要 parser 出你定義的格式就可以了

man syslogd and See Also Section
man logger

看完後,比我或樓主的解釋清楚太多了
俺也凑点热闹,发个以前写过的帖子,涉及一些系统日值管理的一段。主要是syslog的。。

http://chinaunix.net/jh/14/71557.html

6.1 系统日志
unix 系统能够跟踪系统中发生的事件并将每一个事件的所有消息记录到系统的日志文件中。日志对于安全来说,非常重要,他记录了系统每天发生的各种各样的事情,你 可以通过他来检查错误发生的原因,或者受到攻击时攻击者留下的痕迹。日志主要的功能有:审计和监测。他还可以实时的监测系统状态,监测和追踪侵入者等等。 作为一个管理员,你应该每天至少扫描一遍日志,来监视系统或安全问题。在Linux系统中,有三个主要的日志子系统.
6.1.1连接时间日志
连 接时间日志–由多个程序执行,把纪录写入到/var/log/wtmp和/var/run/utmp,login等程序更新wtmp和utmp文件,使 系统管理员能够跟踪谁在何时登录到系统。wtmp和utmp文件都是二进制文件,他们不能被诸如tail命令剪贴或合并(使用cat命令)。用户需要使用 who、w、users、last和ac来使用这两个文件包含的信息。
who:who命令查询utmp文件并报告当前登录的每个用户。Who的缺省输出包括用户名、终端类型、登录日期及远程主机。例如:who(回车)显示(表1)

chyang pts/0 Aug 18 15:06 (192.168.1.3)
ynguo pts/2 Aug 18 15:32  (192.168.1.3)
ynguo pts/3 Aug 18 13:55  (192.168.1.3)
lewis pts/4 Aug 18 13:35  (192.168.1.3)
ynguo pts/7 Aug 18 14:12  (192.168.1.3)
ylou pts/8 Aug 18 14:15  (192.168.1.3)

如果指明了wtmp文件名,则who命令查询所有以前的纪录。命令who /var/log/wtmp将报告自从wtmp文件创建或删改以来的每一次登录。

w:w命令查询utmp文件并显示当前系统中每个用户和它所运行的进程信息。例如:w(回车)显示(表2):3:36pm up 1
day, 22:34, 6 users, load average: 0.23, 0.29, 0.27

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
chyang pts/0 202.38.68.242 3:06pm 2:04 0.08s 0.04s -bash
ynguo pts/2 202.38.79.47 3:32pm 0.00s 0.14s 0.05 w
lewis pts/3 202.38.64.233 1:55pm 30:39 0.27s 0.22s -bash
lewis pts/4 202.38.64.233 1:35pm 6.00s 4.03s 0.01s sh /home/users/
ynguo pts/7 simba.nic.ustc.e 2:12pm 0.00s 0.47s 0.24s telnet mail
ylou pts/8 202.38.64.235 2:15pm 1:09m 0.10s 0.04s -bash

users: users用单独的一行打印出当前登录的用户,每个显示的用户名对应一个登录会话。如果一个用户有不止一个登录会话,那他的用户名将显示相同的次数。例 如:users(回车)显示:chyang lewis lewis ylou ynguo ynguo

last:last命令往回搜索wtmp来显示自从文件第一次创建以来登录过的用户。例如(表3):

chyang pts/9 202.38.68.242 Tue Aug 1 08:34 - 11:23 (02:49)
cfan pts/6 202.38.64.224 Tue Aug 1 08:33 - 08:48 (00:14)
chyang pts/4 202.38.68.242 Tue Aug 1 08:32 - 12:13 (03:40)
lewis pts/3 202.38.64.233 Tue Aug 1 08:06 - 11:09 (03:03)
lewis pts/2 202.38.64.233 Tue Aug 1 07:56 - 11:09 (03:12)

如果指明了用户,那么last只报告该用户的近期活动,例如:last ynguo(回车)显示(表4):

ynguo pts/4 simba.nic.ustc.e Fri Aug 4 16:50 - 08:20 (15:30)
ynguo pts/4 simba.nic.ustc.e Thu Aug 3 23:55 - 04:40 (04:44)
ynguo pts/11 simba.nic.ustc.e Thu Aug 3 20:45 - 22:02 (01:16)
ynguo pts/0 simba.nic.ustc.e Thu Aug 3 03:17 - 05:42 (02:25)
ynguo pts/0 simba.nic.ustc.e Wed Aug 2 01:04 - 03:16 1+02:12)
ynguo pts/0 simba.nic.ustc.e Wed Aug 2 00:43 - 00:54 (00:11)
ynguo pts/9 simba.nic.ustc.e Thu Aug 1 20:30 - 21:26 (00:55)

ac:ac命令根据当前的/var/log/wtmp文件中的登录进入和退出来报告用户连结的时间(小时),如果不使用标志,则报告总的时间。例如:ac(回车)显示:total 5177.47

ac -d(回车)显示每天的总的连结时间 (表5):

Aug 12 total 261.87
Aug 13 total 351.39
Aug 14 total 396.09
Aug 15 total 462.63
Aug 16 total 270.45
Aug 17 total 104.29
Today total 179.02

ac -p (回车)显示每个用户的总的连接时间 (表6):

ynguo 193.23
yucao 3.35
rong 133.40
hdai 10.52
zjzhu 52.87
zqzhou 13.14
liangliu 24.34
total 5178.24

lastlog: lastlog文件在每次有用户登录时被查询。可以使用lastlog命令来检查某特定用户上次登录的时间,并格式化输出上次登录日志 /var/log/lastlog的内容。它根据UID排序显示登录名、端口号(tty)和上次登录时间。如果一个用户从未登录过,lastlog显示 “**Never logged**。注意需要以root运行该命令,例如(表7):

rong 5 202.38.64.187 Fri Aug 18 15:57:01 +0800 2000
dbb **Never logged in**
xinchen **Never logged in**
pb9511 **Never logged in**
xchen 0 202.38.64.190 Sun Aug 13 10:01:22 +0800 2000

另外,可一加一些参数,例如,last -u 102将报告UID为102的用户;last -t 7表示限制上一周的报告。
6.1.2 进程统计日志
进程统计–由系统内核执行。当一个进程终止时,为每个进程往进程统计文件(pacct或acct)中写一个纪录。进程统计的目的是为系统中的基本服务提供命令使用统计。
UNIX 可以跟踪每个用户运行的每条命令,如果想知道昨晚弄乱了哪些重要的文件,进程统计子系统可以告诉你。它对还跟踪一个侵入者有帮助。与连接时间日志不同,进 程统计子系统缺省不激活,它必须启动。在Linux系统中启动进程统计使用accton命令,必须用root身份来运行。Accton命令的形式 accton file,file必须先存在。先使用touch命令来创建pacct文件:
# touch /var/log/pacct
然后运行accton:
# accton /var/log/pact
一旦accton被激活,就可以使用lastcomm命令监测系统中任何时候执行的命令。若要关闭统计,可以使用不带任何参数的accton命令。

lastcomm命令报告以前执行的文件。不带参数时,lastcomm命令显示当前统计文件生命周期内纪录的所有命令的有关信息。包括命令名、用户、tty、命令花费的CPU时间和一个时间戳。如果系统有许多用户,输入则可能很长。下面的例子(表8):
——————————————————————————
crond F root ?? 0.00 secs Sun Aug 20 00:16
promisc_check.s S root ?? 0.04 secs Sun Aug 20 00:16
promisc_check root ?? 0.01 secs Sun Aug 20 00:16
grep root ?? 0.02 secs Sun Aug 20 00:16
tail root ?? 0.01 secs Sun Aug 20 00:16
sh root ?? 0.01 secs Sun Aug 20 00:15
ping S root ?? 0.01 secs Sun Aug 20 00:15
ping6.pl F root ?? 0.01 secs Sun Aug 20 00:15
sh root ?? 0.01 secs Sun Aug 20 00:15
ping S root ?? 0.02 secs Sun Aug 20 00:15
ping6.pl F root ?? 0.02 secs Sun Aug 20 00:15
sh root ?? 0.02 secs Sun Aug 20 00:15
ping S root ?? 0.00 secs Sun Aug 20 00:15
ping6.pl F root ?? 0.01 secs Sun Aug 20 00:15
sh root ?? 0.01 secs Sun Aug 20 00:15
ping S root ?? 0.01 secs Sun Aug 20 00:15
——————————————————————————
进 程统计的一个问题是pacct文件可能增长的十分迅速。这时需要交互式的或经过cron机制运行sa命令来保持日志数据在系统控制内。sa命令报告、清理 并维护进程统计文件。它能把/var/log/pacct中的信息压缩到摘要文件/var/log/savacct和/var/log/usracct 中。这些摘要包含按命令名和用户名分类的系统统计数据。sa缺省情况下先读它们,然后读pacct文件,使报告能包含所有的可用信息。sa的输出有下面一 些标记项(表9):
—————————————————————————-
avio–每次执行的平均I/O操作次数
cp–用户和系统时间总和,以分钟计
cpu–和cp一样
k–内核使用的平均CPU时间,以1k为单位
k*sec–CPU存储完整性,以1k-core秒
re–实时时间,以分钟计
s–系统时间,以分钟计
tio–I/O操作的总数
u–用户时间,以分钟计
——————————————————————————-
例如(表10):
——————————————————————————-
842 173.26re 4.30cp 0avio 358k
2 10.98re 4.06cp 0avio 299k find
9 24.80re 0.05cp 0avio 291k ***other
105 30.44re 0.03cp 0avio 302k ping
104 30.55re 0.03cp 0avio 394k sh
162 0.11re 0.03cp 0avio 413k security.sh*
154 0.03re 0.02cp 0avio 273k ls
56 31.61re 0.02cp 0avio 823k ping6.pl*
2 3.23re 0.02cp 0avio 822k ping6.pl
35 0.02re 0.01cp 0avio 257k md5sum
97 0.02re 0.01cp 0avio 263k initlog
12 0.19re 0.01cp 0avio 399k promisc_check.s
15 0.09re 0.00cp 0avio 288k grep
11 0.08re 0.00cp 0avio 332k awk
——————————————————————————
用户还可以根据用户而不是命令来提供一个摘要报告。例如sa -m显示如下(表11):

885 173.28re 4.31cp 0avk
root 879 173.23re 4.31cp 0avk
alias 3 0.05re 0.00cp 0avk
qmailp 3 0.01re 0.00cp 0avk
6.1.3 错误日值
错误日志–由syslogd执行。各种系统守护进程、用户程序和内核通过syslog向文件/var/log/messages报告值得注意的事件。另外有许多UNIX程序创建日志。像HTTP和FTP这样提供网络服务的服务器也保持详细的日志。
Syslog已被许多日志函数采纳,它用在许多保护措施中–任何程序都可以通过syslog 纪录事件。Syslog可以纪录系统事件,可以写到一个文件或设备中,或给用户发送一个信息。它能纪录本地事件或通过网络纪录另一个主机上的事件。
Syslog 设备依据两个重要的文件:/etc/syslogd(守护进程)和/etc/syslog.conf配置文件,习惯上,多数syslog信息被写到 /var/adm或/var/log目录下的信息文件(messages.*)。一个典型的syslog纪录包括生成程序的名字和一个文本信息。它还包括 一个设备和一个优先级范围(但不在日之中出现)。
每个syslog消息被赋予下面的主要设备之一(表12):
————————————————————————–
LOG_AUTH–认证系统:login、su、getty等
LOG_AUTHPRIV–同LOG_AUTH,但只登录到所选择的单个用户可读的文件中
LOG_CRON–cron守护进程
LOG_DAEMON–其他系统守护进程,如routed
LOG_FTP–文件传输协议:ftpd、tftpd
LOG_KERN–内核产生的消息
LOG_LPR–系统打印机缓冲池:lpr、lpd
LOG_MAIL–电子邮件系统
LOG_NEWS–网络新闻系统
LOG_SYSLOG–由syslogd(8)产生的内部消息
LOG_USER–随机用户进程产生的消息
LOG_UUCP–UUCP子系统
LOG_LOCAL0~LOG_LOCAL7–为本地使用保留
——————————————————————————-
Syslog为每个事件赋予几个不同的优先级(表13):
——————————————————————————
LOG_EMERG–紧急情况
LOG_ALERT–应该被立即改正的问题,如系统数据库破坏
LOG_CRIT–重要情况,如硬盘错误
LOG_ERR–错误
LOG_WARNING–警告信息
LOG_NOTICE–不是错误情况,但是可能需要处理
LOG_INFO–情报信息
LOG_DEBUG–包含情报的信息,通常旨在调试一个程序时使用
——————————————————————————
syslog.conf 文件指明syslogd程序纪录日志的行为,该程序在启动时查询配置文件。该文件由不同程序或消息分类的单个条目组成,每个占一行。对每类消息提供一个选 择域和一个动作域。这些域由tab隔开:选择域指明消息的类型和优先级;动作域指明syslogd接收到一个与选择标准相匹配的消息时所执行的动作。每个 选项是由设备和优先级组成。当指明一个优先级时,syslogd将纪录一个拥有相同或更高优先级的消息。所以如果指明”crit”,那所有标为crit、 alert和emerg的消息将被纪录。每行的行动域指明当选择域选择了一个给定消息后应该把他发送到哪儿。
例如,如果想把所有邮件消息纪录到一个文件中,如下(表14):
——————————————————————————
#Log all the mail messages in one place
mail.* /var/log/maillog

其他设备也有自己的日志。UUCP和news设备能产生许多外部消息。它把这些消息存到自己的日志(/var/log/spooler)中并把级别限为”err”或更高。例如:

# Save mail and news errors of level err and higher in aspecial file.
uucp,news.crit /var/log/spooler

当一个紧急消息到来时,可能想让所有的用户都得到。也可能想让自己的日志接收并保存。

#Everybody gets emergency messages, plus log them on anther machine
*.emerg *
*.emerg @linuxaid.com.cn

alert消息应该写到root和tiger的个人账号中:

#Root and Tiger get alert and higher messages
*.alert root,tiger

有时syslogd将产生大量的消息。例如内核(”kern”设备)可能很冗长。用户可能想把内核消息纪录到/dev/console中。下面的例子表明内核日志纪录被注释掉了:

#Log all kernel messages to the console
#Logging much else clutters up the screen
#kern.* /dev/console

用户可以在一行中指明所有的设备。下面的例子把info或更高级别的消息送到/var/log/messages,除了mail以外。级别”none”禁止一个设备:

#Log anything(except mail)of level info or higher
#Don\’t log private authentication messages!
*.info:mail.none;authpriv.none /var/log/messages
—————————————————————————–
在有些情况下,可以把日志送到打印机,这样网络入侵者怎么修改日志都没有用了。通常要广泛纪录日志。Syslog设备是一个攻击者的显著目标。一个为其他主机维护日志的系统对于防范服务器攻击特别脆弱,因此要特别注意。

有个小命令logger为syslog(3)系统日志文件提供一个shell命令接口,使用户能创建日志文件中的条目。用法:logger 例如:logger This is a test!

它将产生一个如下的syslog纪录:Aug 19 22:22:34 tiger: This is a test!

注意不要完全相信日志,因为攻击者很容易修改它的。
6.1.4 程序日志
许多程序通过维护日志来反映系统的安全状态。su命令允许用户获得另一个用户的权限,所以它的安全很重要,它的文件为sulog。同样的还有sudolog。另外,象Apache有两个日志:access_log和error_log。
这里用了大量的篇章来说了系统日志,是必要的。如果不能保证主机的安全,也谈不上服务了。下面,我们将详细讲一下mail日志。

6.2.1 qmail的替代日志程序
长久以来,针对标准的syslogd程序的效率,已经有很多争议了。一个消息发送给syslogd,病不能保证消息被真正的写道日志中,另外,他的写的速度并不快。
下面是slogger遵循的几个条件:
1、 每一条消息都有时间戳,时间戳被附加到消息中。
2、 每一条消息都要对关键字aler:或者warning:进行检查。如果其中有一个出现了,就为消息选定一个适当的优先级水平。
3、 消息中的不可打印的字符被转换成问号(?)。
4、 不记录空白行。
5、 超过800个字符的消息被分割成800个字符的多行消息。分割的行在时间戳后用一个加号标识。
针对以上,qmail的创始人dan bernstein开发了splogger程序,它包含在qmail软件包中。
用 它来替代系统的syslog.是用splogger程序作为日志程序,将qmail记录重新定向给splogger程序,在将记录转发给linux的 syslog程序。Mail日志的位置取决于/etc/syslog.conf文件设定的值,在上面我们详细将过了怎样设置。

Force iptables to log messages to a different log file (Paste)

原文:http://www.cyberciti.biz/tips/force-iptables-to-log-messages-to-a-different-…

According to man page:
Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. Several different tables may be defined. Each table contains a number of built-in chains and may also contain user defined chains.

By default, Iptables log message to a /var/log/messages file. However you can change this location. I will show you how to create a new logfile called /var/log/iptables.log. Changing or using a new file allows you to create better statistics and/or allows you to analyze the attacks.

Iptables default log file

For example, if you type the following command, it will display current iptables log from /var/log/messages file:
# tail -f /var/log/messages
Output:

Oct  4 00:44:28 debian gconfd (vivek-4435): Resolved address "xml:readonly:/etc/gconf/gconf.xml.defaults" to a read-only configuration source at position 2

Oct  4 01:14:19 debian kernel: IN=ra0 OUT= MAC=00:17:9a:0a:f6:44:00:08:5c:00:00:01:08:00 SRC=200.142.84.36 DST=192.168.1.2 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=18374 DF PROTO=TCP SPT=46040 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0

Oct  4 00:13:55 debian kernel: IN=ra0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:18:de:55:0a:56:08:00 SRC=192.168.1.30 DST=192.168.1.255LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=13461 PROTO=UDP SPT=137 DPT=137 LEN=58

Procedure to log the iptables messages to a different log file

Open your /etc/syslog.conf file:
# vi /etc/syslog.conf
Append following line
kern.warning /var/log/iptables.log
Save and close the file.

Restart the syslogd (Debian / Ubuntu Linux):# /etc/init.d/sysklogd restartOn the other hand, use following command to restart syslogd under Red Hat/Cent OS/Fedora Core Linux:# /etc/init.d/syslog restart

Now make sure you pass the log-level 4 option with log-prefix to iptables. For example:
# DROP everything and Log it
iptables -A INPUT -j LOG –log-level 4
iptables -A INPUT -j DROP

For example, drop and log all connections from IP address 64.55.11.2 to your /var/log/iptables.log file:
iptables -A INPUT -s 64.55.11.2 -m limit --limit 5/m --limit-burst 7 -j LOG –log-prefix ‘** HACKERS **’ --log-level 4
iptables -A INPUT -s 64.55.11.2 -j DROP

Where,

  • –log-level 4: Level of logging. The level # 4 is for warning.
  • –log-prefix ‘*** TEXT ***’: Prefix log messages with the specified prefix (TEXT); up to 29 letters long, and useful for distinguishing messages in the logs.

You can now see all iptables message logged to /var/log/iptables.log file:
# tail -f /var/log/iptables.log

Tomorrow you will learn how to use this new log file to analyze logs.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in…

Discussion on This Article:

  1. Paul Says: March 7th, 2007 at 12:54 am I have absolutely no idea what you are talking about when you get to this part:

    “Now make sure you pass the log-level 4 option with log-prefix to iptables.”

    What is a “log-level 4 option”? What is a “log-prefix”? Why do I want to do this?

    This is a huge problem in the Linux community, and the main reason most people steer clear of Linux: the people with all the know-how talk over the heads of everyone else, and won’t explain what the reason for doing something is, it’s “just do it, because I said to”.

  2. Joel Says: March 19th, 2007 at 7:23 pm Question: If I was to add

    kern.warning /var/log/iptables.log

    to ‘/etc/syslog.conf’ as stated above, does it then append anything at the “warning” level in the file ‘/var/log/iptables.log’, regardless of whether or not it has to do with IPTables?

    Thanks.

  3. Algol Says: March 19th, 2007 at 9:04 pm Paul,

    I don’t think this article was written as a ’step-by-step-iptables-how-to’… So the author might assume some familiarity with iptables.

    I invite you to read
    http://www.linuxguruz.com/iptables/howto/iptables-HOWTO-6.html#ss6.3
    Specially the section
    “Extensions to iptables: New Targets”
    There is the answer to your questions :)

  4. sciron Says: June 1st, 2007 at 11:47 pm Now make sure you pass the log-level 4 option with log-prefix to iptables. For example:
    # DROP everything and Log it
    iptables -A INPUT -j LOG –log-level 4

    No, it is just that some of us can actually read. The rest should steer clear of Linux.

  5. Shaun P Says: September 18th, 2007 at 5:40 pm Just to let you all know. Even though you are setting this to log to /var/log/iptables.log, it still logs into /var/log/messages.

    I am working on a fix for this.

  6. a.h.s. boy Says: November 9th, 2007 at 6:02 pm Shaun –

    There is usually a syslog.conf rule that sends *.info to /var/log/messages

    On my system (Fedora), it reads
    *.info;mail.none;authpriv.none;cron.none /var/log/messages

    I changed it to
    *.info;kern.!=warning;mail.none;authpriv.none;cron.none /var/log/messages

    and it stopped logging iptables stuff to the messages log.

    Note, however, that it will now put ANY “warning” level kernel messages into the iptables.log

配置文件/etc/syslog.conf的实例解析(转贴)

//把info或更高级别的消息送到/var/log/messages,除了mail以外。

//其中*是通配符,代表任何设备;none表示不对任何级别的信息进行记录。

*.info;mail.none;authpriv.none /var/log/messages

//把authpirv设备的任何级别的信息记录到/var/log/secure文件中,这主要是一些和认、权限使用相关的信息。

authpriv.* /var/log/secure

//把mail设备中的任何级别的信息记录到/var/log/maillog文件中,这主要是和电子邮件相关的信息。

mail.* /var/log/maillog

//把cron设备中的任何级别的信息记录到/var/log/cron文件中,这主要是和系统中定期执行的任务相关的 信息。cron.* /var/log/cron//把任何设备的emerg级别的信息发送给所有正在系统上的用户。

*.emerg *

//把uucp和news设备的crit级别的信息记录到/var/log/spooler文件中。

uucp,news.crit /var/log/spooler

//把和系统启动相关的信息记录到/var/log/boot.log文件中。

local7.* /var/log/boot.log

[转贴] Traffic accounting with iptables

Traffic accounting with iptables

From OpenVZ Wiki

Jump to: navigation, search

Suppose you need to know how much traffic your containers eat. It can be easily done using iptables.

Contents

[hide]

//

[edit] Situation description

Let’s consider the very simple situation: one container with one IP address on the Hardware Node with only one network interface. To be more exact, assume that container ID is 200, the IP address of the HN is 192.168.0.56, the network interface name is eth0, and the IP address of the container is 192.168.0.117.

You wish to know how many bytes container 200 eats. One more assumption is that there are no iptables rules on HN now. All these assumption are only for clarity!

[edit] Solution

Almost any traffic that goes to and from a container can be catched by FORWARD chain of iptables module in container0, thus we add such rules:

# iptables -A FORWARD -s 192.168.0.117
# iptables -A FORWARD -d 192.168.0.117

It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted. To obtain current traffic usage of container you can issue the command:

# iptables -nv -L FORWARD
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)
 pkts bytes target     prot opt in     out     source               destination
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117

Bytes column is the column we need. It’s worth saying, that restarting a container doesn’t affect accounting, it remains right. But if you restart your hardware node, all the rules and consequently statistics are dropped. So it is recommended to

  • run some cron job that dumps statistics to some file
  • add init script that creates iptables rules on HN start.

If you want to process the results with a script it is useful to use the “-x” or “–exact” option of iptables

# iptables -nvx -L FORWARD

You will get the exact value of the packet and byte counters, instead of only the rounded number in K’s (multiples of 1000) M’s (multiples of 1000K) or G’s (multiples of 1000M).

As is easy to see, it’s not per-container statistic, but rather per-IP statistic. Thus you must be careful then changing container IP addresses, otherwise you’ll get mess of results.

By saying almost any traffic I mean that traffic between a container and container0 is not accounted by rules above. Not sure if it can be useful for anybody, but to account such traffic these rules are needed:

iptables -I INPUT 1 -i venet0 -d 192.168.0.117
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117

To observe results:

# iptables -nvx -L INPUT
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)
 pkts bytes target     prot opt in     out     source               destination
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117
# iptables -nvx -L OUTPUT
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)
 pkts bytes target     prot opt in     out     source               destination
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0

If you need to zero counters this command works:

# iptables -Z

The disadvantage is that by doingit this way you zero all counters in all rules. If it is not what you need, you can just replace the rule with the same rule:

# iptables -nvx -L FORWARD
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)
 pkts bytes target     prot opt in     out     source               destination
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117
# iptables -R FORWARD 1 -s 192.168.0.117
# iptables -nvx -L FORWARD
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117

[edit] More complicated cases

Well, now, when we know how to work in the easiest case, we’ll try to understand what to do in more complicated situations.

More than one container on the node
Just add the rules like above for each container IP.
More than one IP per container.
For each IP add the rules like above. When counting the complete traffic of a container you have to summarize over all IPs that this container owns.
More interfaces on the HN.
Nothing to do! :)

[edit] Scripting

Here are some scripting ideas

[edit] Get CTIDs of all running containers

host2:~/bin# cat vz-all-running
vzlist -H -oveid | sed 's/ //g;'

[edit] Get all IPs of running containers

host2:~/bin# cat vz-all-running-ip
vzlist -H -o ip

[edit] Set up all needed iptables rules

host2:~/bin# cat vz-iptables-create-rules
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done >/dev/null 2>&1
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done >/dev/null 2>&1

[edit] Generate a traffic.log

Please use crontab to run this script once per hour or day to collect your traffic statistics.

(Warning, the counters can overflow if there is too much traffic within that period. Would recommend 15 minute intervals if you expect a lot of traffic)

host2:~/bin# cat vz-generate-traffic-log
trafficlog="/var/log/vz-traffic.log"
for i in `./vz-all-running-ip` ;
 do
  echo -n `date "+%Y-%m-%d %H:%M:%S"` >> $trafficlog
  echo -n " $i " >> $trafficlog
  echo `iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` >> $trafficlog
 done
 # reset the counter
 iptables -Z
 # update the iptables rules if there is a any change in containers
 ./vz-iptables-create-rules

 # copy the trafficlog file to a webserver where users can see their traffic

 # please mind to use
 # ssh-keygen -t rsa
 # to generate ssh keys
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS
 # in order for the below scp command to not ask for root password
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic

 # clear the copied trafficlog
 cp /dev/null $trafficlog
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS
 # please mind to use .htaccess to secure this
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null

[edit] Sample php script to store the trafficlog in a database

Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS

HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php
<?
 $MySQL_Host="INSERT-YOUR-MYSQL-HOST-HERE";
 $MySQL_User="INSERT-YOUR-MYSQL-USER-HERE";
 $MySQL_Passw="INSERT-YOUR-MYSQL-PASSWORD-HERE";
 
 mysql_connect("$MySQL_Host","$MySQL_User","$MySQL_Passw");
 
 $HN=trim(addslashes($_GET["HN"])); // Hardware Node
 
 $handle = fopen ("tmp/$HN-traffic","r");
 while (!feof($handle)) {
   $line = fgets($handle, 4096);
   list($date,$time,$ip,$traffic)=explode(" ",$line);
   if($traffic>0) {mysql($db,"insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')");}
 }
 fclose($handle);
?>

[edit] A SQL query to get the traffic for the last 30 days

SELECT sum(bytes)
FROM Traffic
WHERE ip = 'INSERT-YOUR-IP-HERE'
AND measuringtime > ( NOW() - INTERVAL 1 MONTH)
GROUP BY ip

[edit] Notes

As you see this way can be time-consuming in case of a big number of containers.

So if anybody has scripts that automate all the process — you are welcome!

[edit] See also

在 XP 中安装 SATA AHCI 驱动

新买的笔记本 SATA 设成 AHCI 模式就不能安装 XP,试了一些方法制作带 SATA 驱动的 XP 安装盘都没有成功,安装过程就出蓝屏。只好先在 ATA 模式下装好系统,再装驱动,步骤如下:

  1. 在 BIOS 中设置 SATA 为 ATA 模式,安装 XP
  2. 下载联想的 SATA 驱动并安装(默认安装路径是 C:\DRIVERS\WIN\IMSM):7zim64ww.exe
  3. 执行 C:\DRIVERS\WIN\IMSM\PREPARE\install.cmd
  4. 重启电脑,进入 BIOS 将 SATA 该为 AHCI 模式
  5. 进入 XP 系统,会提示找到新硬件,安装相应驱动就可以了。

参考:

[转贴] 国外常用的免费DNS域名解析服务器

转贴自:http://idc.cnw.com.cn/Xuni/htm2010/20100105_188830.shtml

在国内注册的域名默认使用的是国内域名注册商提供的DNS服务器,国内的DNS服务器可能受政策的影响停止解析域名,网络上传说以后没有备案的域名国内将不给解析。

为了避免国内的这些政策,建议使用国外的域名服务:

如果您还没有注册域名,请不要在国内注册域名,我们直接代理enom.com 的域名,使用enom分布在全球各地的DNS服务器,推荐各位使用。

如果您已经在国内注册了域名,建议把您的域名转移到国外,点击这里查看怎样转移域名。

如果您已经在国内注册了域名,但由于国内注册商赖皮,不给您转移密码,您还可以使用国外的免费DNS服务器。

国外免费DNS服务器

国外免费DNS服务器有除了everydns.com还有很多。

例如:

ZoneEdit:只支持5个域名的免费解析服务(但要求域名流量不能太大),也提供动态的域名解析。现在似乎又做起来域名销售的服务。

Edit DNS:提供从DNS域名解析服务,并支持修改A, CNAME, MX, NS, TXT, PTR, and AAAA records等,支持免费的子域名,域名重定向等服务。

PowerDNS:提供免费的DSN解析,也有收费的服务。免费的服务的功能较少。

MyDomain:其上主要还是做域名和虚拟主机销售服务,有提供免费的DNS服务,免费的域名和E-mail转向,修改A/MX记录、支持多个子域名服务。

除了上面几个国人用得较多外,其它的还有granitecanyon.com、hn.org、dynu.com、24link.com、 yi.org、dyndns.org、no-ip.com、dnsmadeeasy.com等,以及最近谷歌提供的Google Public DNS服务。

你可以上他们的网站查询他们DNS服务器分布的情况,也可以在dnsreport上查询域名DNS解析服务的具体状态报告。

最后再介绍个重头戏,OpenDNS,非常棒的域名解析服务,为什么选用它呢?因为其有最大的三个特点:安全、快速、自动纠错。

安全就在于它能阻止一些网站盗取你的重要信息,能很好地保护你的私人信息;

快速就是在于它采用了大量智能缓存技术,先是通过缓存进行解析,如果缓存不存在,则转换到最近的DNS服务器进行解析,再存入缓存;另外一点就是它有着高效多位的分布网络(在USA、EUROPE有多个公布网点、而且正在筹备英国和香港的网点)。

自动纠错功能,就如你输入baidu.cmo时,它会自动帮你纠正为baidu.com,并做好解析。

解析方法:

这里以推荐的国外everydns.com免费的DNS服务器来解析国内注册的域名为例说明解析方法

第一步,先到 everydns.com 去注册一个用户,然后登录进去,在Add new domain: (basic)部分填写自己的域名,然后点击(basic)按钮添加自己的域名到everydns,这时候屏幕左上角就会出现刚添加的域名了,点击一下这 个域名,就可以在右侧给自己的域名添加解析了。

第二步,登录到国内域名的后台,直接修改DNS服务器为everydns的服务器,everydns一共有4个DNS服务器,分别 是:ns1.everydns.net, ns2.everydns.net, ns3.everydns.net, ns4.everydns.net。国内一般可以写两个DNS服务器,从上面这4个中随便挑2个就可以了

然后,等待大概24小时,新的DNS服务器就应该生效了。

[转贴] 教你在Ubuntu下保存屏幕亮度设置

转贴自:http://www.redbots.cn/ubuntu/2009/02/28/8178.htm

本文保留屏幕亮度方法适合使用笔记本的朋友,大家都知道,过亮的屏幕不但刺眼而且缩短LCD屏幕寿命,不幸的是,Ubuntu默认关机后并不保存当前屏幕亮度配置数据,每次开机都要重新设置亮度,很不方便。本文介绍的方法可以彻底解决这个问题!

1.修改/etc/default/acpi-support

ENABLE_LAPTOP_MODE=true

2.修改 /etc/laptop-mode/laptop-mode.conf

交流电模式下使用 laptop mode
#
# Enable laptop mode when on AC power.
#
ENABLE_LAPTOP_MODE_ON_AC=1
#电池那也可设为1

使用 laptop mode 进行LCD亮度控制
#
# Should laptop mode tools control LCD brightness?
#
CONTROL_BRIGHTNESS=1

#
# Commands to execute to set the brightness on your LCD
#
BATT_BRIGHTNESS_COMMAND=”echo 0″
LM_AC_BRIGHTNESS_COMMAND=”echo 9″
NOLM_AC_BRIGHTNESS_COMMAND=”echo 9″
BRIGHTNESS_OUTPUT=”/sys/class/backlight/acpi_video1/brightness”

如果你不明白上面的设置的意思,可以参考下面的说明:

***********************************
# * If your system has the file “/proc/acpi/video/VID/LCD/brightness” (VID may
# be VID1 or similar), use this file as BRIGHTNESS_OUTPUT, and use
# the command “echo “. The possible values can be listed using the
# command:
#
# cat /proc/acpi/video/VID/LCD/brightness

# * If you have a file /sys/class/backlight/…/brightness, then you can use
# that file as BRIGHTNESS_OUTPUT, and the command “echo “.
#
# As far as I understand it the values are between 0 and
# the value contained in the file /sys/class/backlight/…/max_brightness.
**********************************

注:

我在查找调整屏幕亮度的方法时查到了以上的文章。在其它地方(如:

http://forum.ubuntu.org.cn/viewtopic.php?f=155&t=230876&start=0

)有说调整使用的命令是:

echo -n 80 > /proc/acpi/video/DGFX/LCD/brightness

但我试过,首先能找到 brightness 的地方是 /proc/acpi/video/GFX0/DD01/brightness,与命令中路径有一定差别;其次是 brightness 显示大小为 0,但 cat 与例子中的 echo 都会提示错误。看来还是前面转贴的文章靠谱。

解决开机时 upstart 错误提示

今天打开 ubuntu 时发现有下图中的错误提示:

检查发现文件 /etc/init/network-manager.conf 中设置有 exec NetworkManager,但文件 NetworkManager 是不存在的(我用 xfce,删去了很多 gnome 相关的东西)。

一不做二不休,将 network-manger 也删掉,重开机。哈哈!两个错误提示都没有了!暂时没有发现网络有什么问题。

[转贴] Nginx重定向[Rewrite]配置 for wordpress & Discuz

转贴自:http://srsman.com/2008/08/nginx-rewrite-for-wordpress-and-discuz/

首先Apache的Rewite规则差别不是很大,但是Nginx的Rewrite规则比Apache的简单灵活多了
Nginx可以用if进行条件匹配,语法规则类似C

注:如果发现设置 rewite 有问题,可以试试设置
server_name_in_redirect off;


if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}

官方文档请点击这里

Rewrite的Flags
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301

WordPress的重定向规则:

if (!-e $request_filename) {
rewrite ^/(index|atom|rsd)\.xml$ http://feed.shunz.net last;
rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
rewrite ^ /index.php last;
}

以下为Discuz完整的Rewrite for Nginx规则

if (!-f $request_filename) {
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
}

正则正则表达式匹配注解

~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
2、文件及目录匹配,其中:

-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

Follow

Get every new post delivered to your Inbox.