2009年3月 归档

awstats按日报表日历界面

| 暂无评论 | 暂无引用通告
脚本:perl_calendar.pl
使用模块:HTML::Calendar::Simple
安装模块:
cpan HTML::Calendar::Simple
如手动,需安装以下两个包:
Date-Simple
HTML-Calendar-Simple
将其存放在cgi目录中添加可执行权限

#!/usr/bin/perl -w
#Script Name: perl_calendar.pl
#author: supersun@gmail.com
#url: http://supersun.biz
use strict;

use HTML::Calendar::Simple;
use CGI qw/:all/;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
my $awstats_path="/awstats/awstats.pl";
print header(-charset=>'gbk');
if(param('host')){
        $year+=1900;
        $mon++;
        my $host=param("host");
        my $link_tag=1;

        print start_html("awstats for $host");
        print h1({-align=>"center"},"awstats for $host"),hr({-align=>"center"});

        print "<table border='1' align='center'>\n";
        print "<tr>\n";
        foreach my $month (01..12)
        {
                print "<tr>\n" if($month =~ /5|9/);
                print "<td valign='top'>";
                my $cal = HTML::Calendar::Simple->new({ 'month' => $month, 'year'  => $year});

                foreach my $day (1..31){
                        my $location=$awstats_path."?config=$host&databasebreak=day&day=$day&month=$month";
                        if($month eq $mon && $day eq $mday ){
                                $link_tag=0;
                                $cal->daily_info({'day'      => $day, 'pin_up'=>'now'});
                        }
                        if( $link_tag eq 1){
                                $cal->daily_info({'day'      => $day, 'day_link' => $location});
                        }else{
                                $cal->daily_info({'day'      => $day });
                        }
                }
                my $html = $cal->calendar_month;

                print $html;
                print "</td>\n";
                print "</tr>\n" if($month =~ /4|8/ );

        }
        print "</tr></table>\n";
}else{
        print "You must input a host: ",start_form,"Host: ",textfield('host'),submit,end_form;
}
print end_html;
界面如下:
perl_calander.jpg的缩略图
使用awstats按日分析参考:
http://www.chedong.com/blog/archives/001293.html


    使用Net::FTP从FTP服务器上下载文件,发现部分文件下载后大小有出入,可能是使用ASCII传输方式造成的。

查一下FTP的两种传输模式的区别:

ASCII模式和BINARY模式的区别是回车换行的处理,binary模式不对数据进行任何处理,asci模式将回车换行转换为本机的回车字符,比如
Unix下是\n,Windows下是\r\n,Mac下是\r

ascii模式下会转换文件
不能说是不同系统对回车换行解释不同
而是不同的系统有不同的行结束符
unix系统下行结束符是一个字节,即十六进制的0A
而ms的系统是两个字节,即十六进制的0D0A
所以当你用ascii方式从unix的ftp  server下载文件时(不管是二进制或者文本文件),每检测到一个

字节是0A,就会自动插入一个0D,所以如果你的文件是二进制文件比如可执行文件、压缩包什么的,就肯

定不能用了。如果你的文件就是unix下的文本文件,你用ascii模式是正确的,要是误用了binary模式,

你在windows上看这个文件是没有换行的,里面是一个个的黑方块。
一般来说,我们最好都用binary方式,这样可以保证不出错。如果有文本格式转换的问题,即unix格式

的文本和dos格式的文本之间的转换,

所以,可以采用这样的习惯:
所有的FTP传输都采用binary方式,但是在windows下面编辑的文件需要事先转换成Unix模式,这个可以使用
UltraEdit的conversion功能,也可以在UltraEdit下面另存为Unix Terminators-LF的格式。

原文见:
http://blog.chinaunix.net/u1/56521/showart_1183437.html
百度百科:FTP
http://baike.baidu.com/view/369.htm

inotify监控文件的更改

| 暂无评论 | 暂无引用通告
      脚本读取目录列表(脚本所在目录下的db.conf),将目录添加到inotify监控中,被监控目录下新创建的目录也会被添加到监控中去.被监控目录下创建的文件或文件内容被更改后,脚本以日志的方式文件名发送给本机的syslog。脚本发送日志时使用的facility为local2,priority为info,进程名为inotifyd。
      脚本的内容如下:
#!/usr/bin/perl

use strict;
use Event;
use Linux::Inotify2;
use POSIX;
use IO::File;
use Sys::Syslog qw(:DEFAULT setlogsock);

my $script_dir="/opt/";
our $CONF_FILE=$script_dir."db.conf";


#For Daemon
###################################################
sub daemonize {
        chdir "/";
        open STDIN, "</dev/null";
        open STDOUT, ">/dev/null";
        open STDERR, ">&STDOUT";
        defined(my $pid=fork) or die "Can't fork: $!\n";
        exit if($pid);
        POSIX::setsid() or die "Can't start a new session: $!";
        umask 0;
}

sub add_watch {
        my ($inotify,$log_file)=@_;
        open FH,$log_file;
        while(<FH>){
                my $file=$_;
                chomp $file;
                $inotify->watch ($file, IN_ALL_EVENTS, \&mywatch)
        }
        close FH;
}

####################################################
                         
&daemonize;

my $ident="inotify";
my $logopt="ndelay";
my $facility="local2";
my $priority="info";
openlog $ident, $logopt, $facility;       # don't forget this


our $INOTIFY_OBJECT=new Linux::Inotify2;
Event->io (fd => $INOTIFY_OBJECT->fileno, poll => 'r', repeat => 1, cb => sub { $INOTIFY_OBJECT->poll });

Event->signal (signal => 'HUP',cb => \&re_conf);

add_watch($INOTIFY_OBJECT,$CONF_FILE);

Event::loop;

sub re_conf{
        add_watch($INOTIFY_OBJECT,$CONF_FILE);
        Event::loop;
}


sub mywatch {
        my $e = shift;
        my $PATH= $e->fullname;
        my $MASK= $e->mask;
  
  
        if($e->IN_ISDIR){
         if($e->IN_CREATE){
            $INOTIFY_OBJECT->watch($PATH,IN_ALL_EVENTS, \&mywatch);
                    return;
        }
        }else{
            if($e->IN_MODIFY || $e->IN_CREATE){
                    syslog($priority,$PATH);
                    return
            }
    }
}

例如,我们需要监控/data下的目录:

将脚本保存在/opt下命名为inotifyd.pl
安装脚本使用的模块:
cpan Event
cpan Linux::Inotify2
cpan Sys::Syslog
设定脚本所在目录
vi /opt/inotifyd.pl
my $script_dir="/opt/";
检测脚本是否可运行:

[supersun@supersun.biz opt]$ perl -c inotifyd.pl
inotifyd.pl syntax OK


配置syslog.conf,添加以下的行:
local2.*                                                /var/log/inotify.log
或将日志发送给日志主机loghost
local2.*                                                @loghost

更改message行:
auth,authpriv,cron,daemon,kern,lpr,news,syslog,user.info;mail.none;authpriv.none;cron.none              /var/log/messages
不然的话inotify的日志也会发送至/var/log/messages中去

创建需要监控的目录的列表:
find /data -type d >/opt/db.conf

启动脚本
perl inotifyd.pl

脚本接收HUP信号,重新读加监控目录列表,将上次未加载的录添加到监控中。

如果目录列表很长的话,使用top查看进程状态,态状为R时,inotifyd.pl正在加载目录列表,状态S时,脚本已开始监控。

查看/var/log/inotify.log(根椐自己/etc/syslog.conf中的配置)中的日志输出。

Mar 20 15:36:28 supersun.biz inotify: /data/hello.txt

syslog使用的udp协议发送日志,如何使syslog监听udp的514端口可以参考将防火墙的日志发送给日志主机
syslog-ng可以使用tcp协议发送日志,也可以通过加密的方式发送日志

 

Perl:将日志发送给syslog

| 暂无评论 | 暂无引用通告
使用模块Sys::Syslog

将日志发送给本机

#!/usr/bin/perl -w
use strict;

use Sys::Syslog qw(:DEFAULT setlogsock);

my $ident="inotify";  #进程命
my $logopt="ndelay";
my $facility="local2";  #设备名
my $priority="info";    #日志级别
openlog $ident, $logopt, $facility;       
syslog($priority,"hello"); #记入日志
closelog;


将日志发送到日志主机

#!/usr/bin/perl -w
use strict;

use Sys::Syslog qw(:DEFAULT setlogsock);

my $ident="inotify";  #进程命
my $logopt="ndelay";
my $facility="local2";  #设备名
my $priority="info";    #日志级别
setlogsock('udp','log.supersun.biz');
syslog($priority,"hello"); #记入日志
closelog;

如果使用tcp协议传输日志:
setlogsock('tcp','log.supersun.biz');
同时,更改/etc/services
将514/tcp的服务名改为syslog

log option的几个可设定的值

    * cons - This option is ignored, since the failover mechanism will drop down to the console automatically if all other media fail.
    * ndelay - Open the connection immediately (normally, the connection is opened when the first message is logged).
    * nofatal - When set to true, openlog() and syslog() will only emit warnings instead of dying if the connection to the syslog can't be established.
    * nowait - Don't wait for child processes that may have been created while logging the message. (The GNU C library does not create a child process, so this option has no effect on Linux.)
    * perror - Write the message to standard error output as well to the system log.
    * pid - Include PID with each message.

syslog记入的日志格式

Mar 18 13:17:13 host1.supersun.biz inotify: test message
时间  接收到日志的时间
主机名  发送日志的主机
进程名 前面定义的$ident
日志内容

相关文章

使用syslog-ng配置日志主机
将防火墙的日志发送给日志主机

使用syslog-ng配置日志主机

| 暂无评论 | 暂无引用通告
syslog-ng的主页:
http://www.balabit.com/network-security/syslog-ng/

需要的软件包:
eventlog-0.2.7-1.el5.x86_64.rpm
syslog-ng-2.1.3-2.el5.x86_64.rpm

你可以到http://rpm.pbone.net/下载与系统对应的rpm包

安装:
rpm -ivh eventlog-0.2.7-1.el5.x86_64.rpm
rpm -ivh syslog-ng-2.1.3-2.el5.x86_64.rpm

server:192.168.11.2
client:192.168.11.1
client上的应用程序将日志发送给本机的syslog,client上的syslog-ng将日志记入本地文件/var/log/inotify.log同时发送一份给server,server将日志保存为var/log/inotify.1.log

syslog-ng的配置文件为:
/etc/syslog-ng/syslog-ng.conf
server端的配置
创建一个源,监听tcp的514端口,最大连接数10
#for network listening
source s_tcp { tcp(ip(192.168.11.2) port(514) max-connections(10)); };

创建一个目的端
destination d_inotify_1 { file("/var/log/inotify/inotify.1.log");};
创建一个过滤器
filter f_inotify_1 { facility(local2) and (host("host1") or host("host1.supersun.biz") or host("192.168.11.1")); };
记入日志
log { source(s_tcp); filter(f_inotify_1);destination(d_inotify_1); };

为了在日志中显示远程主机的IP地址,而不是主机名,可以
options段
keep_hostname (no);
注意不要被下面的配置给覆盖了。


client端的配置:
创建两个目的端,一个日志主机,一个本地文件。
destination d_remote { tcp("192.168.11.2" port(514)); };
destination d_inotify { file("/var/log/inotify.log"); };
创建一个过滤器,过滤local2的日志
filter f_inotify    { facility(local2); };
将local2的日志写到本地和日志主机
log { source(s_sys); filter(f_inotify); destination(d_inotify); };
log { source(s_sys); filter(f_inotify); destination(d_remote); };
此处的传日志给日志主机是基于tcp,因此,需要更改/etc/services
syslog          514/tcp
将tcp的514端口改为syslog

启动syslog-ng
service syslog-ng restart
将syslog-ng添加到开机启动中
chkconfig syslog-ng on
chkconfig syslog off

在client端运行以下命令测试一下:
logger -p local2.debug "this is a test"

其他配置示例:
http://www.campin.net/syslog-ng.conf

此处的示例是在信任网络,如果觉得这样不安全,可能使用stunnel进行加密传输,可参考这篇文章:
使用Stunnel保护syslog-ng服务器
http://server.it168.com/server/2007-11-15/200711140845734.shtml

syslog-ng介绍




相关文章

将防火墙的日志发送给日志主机


cacti中部分图片出现断层,在日志中发现以下错误

WARNING: Poller Output Table not Empty.  Potential Data Source Issues for Data Sources....好多数据.

    通常情况下,poll.php每五分钟执行一次,在启动poll.php时,5分钟之前启动的进程还没有结束,就会这出这种现象,可能是部分主机反应慢或已不再使用,导致poll.php执行时间过长,要记得在cacti中删除不再使用的主机 和监控.

原文地址:
http://www.ibm.com/developerworks/cn/aix/library/analyze_aix/index.html

下载地址:
http://www.ibm.com/developerworks/wikis/display/WikiPtype/nmon

unzip nmon_x86_64_rhel4.zip
mv nmon_x86_64_rhel4 nmon
cp nmon /usr/bin/


很好用的性能分析工具。

bash上小数计算

| 暂无评论 | 暂无引用通告
使用bc进行计算,如:

result=$(echo "37 * 0.5" |bc)

对结果取整的话:

result_int=${result%.*}

也可以通过sed截去小数点后的值:

echo $result|sed 's/\..*//g'

最新资源

  • perl_calander.jpg

关于此归档

这里是2009年3月的所有日记,它们按照时间从新到老排序。

上一篇日记2009年2月

下一篇日记2009年4月

首页归档页可以看到最新的日记和所有日记。