로그파일(file)을 읽어서 분단위로 특정시간(time)부터 몇시간(until)까지 읽어 화면에 찍는다. 이를 엑셀로 확인하여 아파치 리쿼스트 갯수를 확인할 수 있다.

#!/usr/bin/perl

my $filename;
my $time;
my $until;

if($#ARGV < 0) {
    printUsage();
} else {
    while($#ARGV >= 0) {
        $filename = shift(@ARGV);
        $time = shift(@ARGV);
        $until = shift(@ARGV);
    }
}

sub printUsage() {
    print "Usage : getReqCount.pl filename time(hh:mm) until(until hour)\n";
    print "ex) getReqCount.pl access_https.log 1700 2\n";
    print "\n";
}

#print "filename : $filename\n";
#print "time : $time\n";
#print "until : $until\n";

$digits = $time;
@timeinfo = $digits =~ /(\d\d)/g;

for ($j = 0 ; $j < $until ; $j++) {
    $m = @timeinfo[0] + $j;
    for ($i = 0; $i < 60 ; $i++) {
        $i = @timeinfo[1] + $i;
        if ($i < 10) {
            $i = "0".$i;
        }
        print "$m:$i\t";
        print `grep "$m:$i" $filename | wc -l`
    }
}

'web' 카테고리의 다른 글

DDos 공격의 또 다른 패턴을 잡기.  (0) 2007.10.21
Apache request중 abusing IP 확인하기  (0) 2007.10.19
L4 이야기  (0) 2007.09.29
L4 사용여부.  (0) 2007.09.29
L7 스위치 개념 및 동작원리  (0) 2007.09.29
Posted by '김용환'
,

펄 해쉬 이야기 #2

perl 2007. 10. 19. 00:10

 

출처

 

http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/


Perl Hash Howto

This how-to comes with no guaratees other than the fact that these code segments were copy/pasted from code that I wrote and ran successfully.

Initialize (clear, or empty) a hash

Assigning an empty list is the fastest method.

Solution

    my %hash = ();

Note

People have asked how to initialize a hash reference (aka hash ref). It's just like any scalar variable; you can use my alone, or you can assign a value.

    my $hash_ref;
    my $hash_ref = 0;  # zero

Add a key/value pair to a hash

In the solutions below, quotes around the keys can be omitted when the keys are identifiers.

Hash:

Solution

    $hash{ 'key' } = 'value';    # hash

    $hash{ $key } = $value;      # hash, using variables

Hash reference:

Solution

    $href->{ 'key' } = 'value';  # hash ref

    $href->{ $key } = $value;    # hash ref, using variables

Add several key/value pairs to a hash

Solution

The following statements are equivalent, though the second one is more readable:

    %hash = ( 'key1', 'value1', 'key2', 'value2', 'key3', 'value3' );

    %hash = (
        key1 => 'value1',
        key2 => 'value2',
        key3 => 'value3',
    );

Copy a hash

Solution

    my %hash_copy = %hash;  # copy a hash

    my $href_copy = $href;  # copy a hash ref

Delete a single key/value pair

The solution differs for a hash and a hash reference, but both cases can use the delete function.

Solution

Hash:

    delete $hash{$key};

Hash reference:

    delete $hash_ref->{$key};

Perform an action on each key/value pair in a hash

The actions below print the key/value pairs.

Solution

Use each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.

    while ( my ($key, $value) = each(%hash) ) {
        print "$key => $value\n";
    }

A hash reference would be only slightly different:

    while ( my ($key, $value) = each(%$hash_ref) ) {
        print "$key => $value\n";
    }

Solution

Use keys with a for loop.

    for my $key ( keys %hash ) {
        my $value = $hash{$key};
        print "$key => $value\n";
    }

Example

    my $file = $ARGV[0] || "-";

    my %from = ();

    open FILE, "< $file" or die "Can't open $file : $!";

    while( <FILE> ) {
        if (/^From: (.*)/) { $from{$1}++ }  # count recurrences of sender
    }

    close FILE;

    for my $sender ( sort keys %from ) {
        print "$sender: $from{$sender}\n";
    }

Get the size of a hash

Solution

    print "size of hash:  " . keys( %hash ) . ".\n";

Solution

    my $i = 0;

    $i += scalar keys %$hash_ref;  # method 1: explicit scalar context
    $i += keys %$hash_ref;         # method 2: implicit scalar context

Use hash references

Solution

    sub foo
    {
        my $hash_ref;

        $hash_ref->{ 'key1' } = 'value1';
        $hash_ref->{ 'key2' } = 'value2';
        $hash_ref->{ 'key3' } = 'value3';

        return $hash_ref;
    }

    my $hash_ref = foo();

    print "the keys... ", sort keys %$hash_ref, "...\n";

Function to build a hash of hashes; return a reference

Solution

    sub foo
    {
        my ( $login, $p, $uid, $gid, $gecos, $dir, $s );

        my %HoH = ();

        my $file = '/etc/passwd';
        open( PASSWD, "< $file" ) or die "Can't open $file : $!";

        while( <PASSWD> ) {
            ( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );

            $HoH{ $login }{ 'uid' } = $uid;
            $HoH{ $login }{ 'gid' } = $gid;
            $HoH{ $login }{ 'dir' } = $dir;
        }

        close PASSWD;

        return \%HoH;
    }

Access and print a reference to a hash of hashes

Solution

    my $rHoH = foo();

    my( $uid, $gid, $dir );

    for my $login ( keys %$rHoH ) {

        $uid =       $rHoH->{ $login }->{ 'uid' };   # method 1  most readable
        $gid =    ${ $rHoH->{ $login } }{ 'gid' };   # method 2
        $dir = ${ ${ $rHoH }{ $login } }{ 'dir' };   # method 3 least readable

        print "uid: $uid, gid: $gid, dir, $dir.\n";
    }

Solution

    my $rHoH = foo();

    for my $k1 ( sort keys %$rHoH ) {
        print "k1: $k1\n";
        for my $k2 ( keys %{$rHoH->{ $k1 }} ) {
            print "k2: $k2 $rHoH->{ $k1 }{ $k2 }\n";
        }
    }

Function to build a hash of hashes of hashes; return a reference

Solution

    sub foo
    {
        my %HoHoH = ();

        while( ... ) {

            if( /LOCATION:/ ) {

                ...

            } elsif( /MODULE:/ ) {

                $HoHoH{ $loc }{ $module_type }{ MODULE_NAME } = $module_name;

            } elsif( $ARGS_ALLOWED ) {

                $HoHoH{ $loc }{ $module_type }{ $arg_name } = $arg_value;

            }

        }

        return \%HoHoH;
    }

Access and print a reference to a hash of hashes of hashes

Solution

    my $rHoHoH = foo();

    for my $k1 ( sort keys %$rHoHoH ) {
        print "$k1\n";

        for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
            print "\t$k2\n";

            for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
                print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
            }
        }
    }

Print the keys and values of a hash, given a hash reference

Solution

    while( my ($k, $v) = each %$hash_ref ) {
        print "key: $k, value: $v.\n";
    }

Determine whether a hash value exists, is defined, or is true

Solution

    print "Value EXISTS, but may be undefined.\n" if exists  $hash{ $key };
    print "Value is DEFINED, but may be false.\n" if defined $hash{ $key };
    print "Value is TRUE at hash key $key.\n"     if         $hash{ $key };

Example

Let's say we execute an sql query where some of the resulting values may be NULL. Before attempting to use any of the values we should first check whether they are defined, as in the following code. Note that the subroutine sql_fetch_hashref() takes care of connecting to the database, preparing the statement, executing it, and returning the resulting row as a hash reference using DBI's fetchrow_hashref() method.

    my $answers = 'a,b,c,d,e';

    my $sql = "select max_time, $answers from questions " .
              'where question_number=?';
    my $hash_ref = sql_fetch_hashref( $sql, $q );

    my @answers = split ',', $answers;

    my $max_time = $hash_ref->{max_time} || '60';

    my $hash_ref_ans;
    for my $letter ( @answers ) {
        $hash_ref_ans->{ $letter } = $hash_ref->{ $letter }
            if defined $hash_ref->{ $letter };
    }

The for loop made a new hash of only defined key/value pairs.


AUTHOR

Alex BATKO <abatko AT cs.mcgill.ca>

Thanks to all those who have written with suggestions and comments.


SEE ALSO

http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/

http://exitloop.ca/abatko/computers/programming/perl/howto/

'perl' 카테고리의 다른 글

Perl의 String 관련 함수  (0) 2008.03.22
bash는 float 변수 연산이 안된다.  (0) 2007.10.21
펄 Hash 관련 정보 #1  (0) 2007.10.19
패턴 매칭  (0) 2007.09.23
패턴 매칭  (0) 2007.09.23
Posted by '김용환'
,

펄 Hash 관련 정보 #1

perl 2007. 10. 19. 00:09

 

출처 :http://www.ebb.org/PickingUpPerl/pickingUpPerl_6.html

Associative Arrays (Hashes)

This chapter will introduce the third major Perl abstract data type, associative arrays. Also known as hashes, associative arrays provide native language support for one of the most useful data structures that programmers implement--the hash table.

What Is It?

Associative arrays, also frequently called hashes, are the third major data type in Perl after scalars and arrays. Hashes are named as such because they work very similarly to a common data structure that programmers use in other languages--hash tables. However, hashes in Perl are actually a direct language supported data type.

Variables

We have seen that each of the different native data types in Perl has a special character that identify that the variable is of that type. Hashes always start with a %.

Accessing a hash works very similar to accessing arrays. However, hashes are not subscripted by numbers. They can be subscripted by an arbitrary scalar value. You simply use the {} to subscript the value instead of [] as you did with arrays. Here is an example:

use strict;
my %table;
$table{'schmoe'} = 'joe';
$table{7.5}  = 2.6;

In this example, our hash, called, %table, has two entries. The key 'schmoe' is associated with the value 'joe', and the key 7.5 is associated with the value 2.6.

Just like with array elements, hash elements can be used anywhere a scalar variable is permitted. Thus, given a @hash{%table} built with the code above, we can do the following:

print "$table{'schmoe'}\n";    # outputs "joe\n"
--$table{7.5};                 # $table{7.5} now contains 1.6

Another interesting fact is that all hash variables can be evaluated in the list context. When done, this gives a list whose odd elements are the keys of the hash, and whose even elements are the corresponding values. Thus, assuming we have the same %table from above, we can execute:

my @tableListed = %table;  # @tableListed is qw/schmoe joe 7.5 1.6/

If you happen to evaluate a hash in scalar context, it will give you undef if no entries have yet been defined, and will evaluate to true otherwise. However, evaluation of hashes in scalar context is not recommended. To test if a hash is defined, use defined(%hash).

Literals

"Hash literals" per se do not exist. However, remember that when we evaluate a hash in the list context, we get the pairs of the hash unfolded into the list. We can exploit this to do hash literals. We simply write out the list pairs that we want placed into the hash. For example:

use strict;
my %table = qw/schmoe joe 7.5 1.6/;

would give us the same hash we had in the previous example.

Functions

You should realize that any function you already know that works on arrays will also work on hashes, since you can always evaluate a hash in the list context and get the pair list. However, there are a variety of functions that are specifically designed and optimized for use with hashes.

Keys and Values

When we evaluate a hash in a list context, Perl gives us the paired list that can be very useful. However, sometimes we may only want to look at the list of keys, or the list of values. Perl provides two optimized functions for doing this: keys and values.

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my @lastNames  = keys %table;    # @lastNames is: qw/schmoe smith simpson/
my @firstNames = values %table;  # @firstNames is: qw/joe john bart/

Each

The each function is one that you will find particularly useful when you need to go through each element in the hash. The each function returns each key-value pair from the hash one by one as a list of two elements. You can use this function to run a while across the hash:

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my($key, $value);  # @cc{Declare two variables at once}
while ( ($key, $value) = each(%table) ) {
    # @cc{Do some processing on @scalar{$key} and @scalar{$value}}
}

This while terminates because each returns undef when all the pairs have been exhausted. However, be careful. Any change in the hash made will "reset" the each function for that hash.

So, if you need to loop and change values in the hash, use the following foreach across the keys:

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
foreach my $key (keys %table) {
    # Do some processing on $key and $table{$key}
}

Slices

It turns out you can slice hashes just like you were able to slice arrays. This can be useful if you need to extract a certain set of values out of a hash into a list.

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my @friends = @table{'schmoe', 'smith'};   # @friends has qw/joe john/

Note the use of the @ in front of the hash name. This shows that we are indeed producing a normal list, and you can use this construct in any list context you would like.

Context Considerations

We have now discussed all the different ways you can use variables in list and scalar context. At this point, it might be helpful to review all the ways we have used variables in different contexts. The table that follows identifies many of the ways variables are used in Perl.

Expression Context Variable Evaluates to
$scalar scalar $scalar, a scalar the value held in $scalar
@array list @array, an array the list of values (in order) held in @array
@array scalar @array, an array the total number of elements in @array (same as $#array + 1)
$array[$x] scalar @array, an array the ($x+1)th element of @array
$#array scalar @array, an array the subscript of the last element in @array (same as @array -1)
@array[$x, $y] list @array, an array a slice, listing two elements from @array (same as ($array[$x], $array[$y]))
"$scalar" scalar (interpolated) $scalar, a scalar a string containing the contents of $scalar
"@array" scalar (interpolated) @array, an array a string containing the elements of @array, separated by spaces
%hash list %hash, a hash a list of alternating keys and values from %hash
$hash{$x} scalar %hash, a hash the element from %hash with the key of $x
@hash{$x, $y} list %hash, a hash a slice, listing two elements from %hash (same as ($hash{$x}, $hash{$y})


Go to the first, previous, next, last section, table of contents.

If you find this book useful, and you play online poker, consider signing up for a poker site using the affiliate links below. These raise revenue to help Bradley pay for the cost of hosting this book draft:

'perl' 카테고리의 다른 글

bash는 float 변수 연산이 안된다.  (0) 2007.10.21
펄 해쉬 이야기 #2  (0) 2007.10.19
패턴 매칭  (0) 2007.09.23
패턴 매칭  (0) 2007.09.23
Expect (shell)문제 및 해결  (0) 2007.08.24
Posted by '김용환'
,

 둘이 똑같은 명령어이다.


:/home/www/backup/apachelog/a]# grep -c \"-\" access_https.071016
21109
:/home/www/backup/apachelog/a]# grep  \"-\" access_https.071016 | wc -l
21109

'unix and linux' 카테고리의 다른 글

[bash] 로깅 시간 출력  (0) 2007.11.14
netstat 과 tcp 상태  (0) 2007.10.31
crond 문제  (0) 2007.10.17
bash 배열 선언 및 처리하기  (0) 2007.10.16
DNS 서버 보기  (0) 2007.10.10
Posted by '김용환'
,

텍스트 함수를 쓰면 됩니다.

아래 그림과 같이 A2셀에 있는 내용 중에 필요한 문자만 B2셀에 추출하고자 하면

B2셀에 커서를 위치하고 =+LEFT(A2,3)을 입력하면 됩니다.

의미는 A2셀의 문자중에 왼쪽부터 3글자만 표시하라는 것입니다.

A열에 데이터가 계속된다면 B2셀 값을 복사하면 적용이 되겠지요.

 

Posted by '김용환'
,

crond 문제

unix and linux 2007. 10. 17. 02:43

 

 crontab이 안돌아가는 문제가 있었다.

 

/var/log/cron 파일을 열어보니..

 

10월 9일부터 crond 데몬이 동작되지 않았다.

 

 

 Oct  9 07:57:01 nhn346 crond[28706]: (root) CMD (/usr/lib/sa/sa1 1 1)
Oct 12 18:10:22 nhn346 crontab[10389]: (root) BEGIN EDIT (root)
Oct 12 18:10:36 nhn346 crontab[10389]: (root) END EDIT (root)

 

 

아무리 /etc/rc.d/init.d/crond restart를 해도 재시작이 안되고, kill -9 crond 을 해도 죽지 않아.

리눅스 재부팅 시작~~

'unix and linux' 카테고리의 다른 글

netstat 과 tcp 상태  (0) 2007.10.31
grep -c 와 wc -l은 똑같은 명령어이다.  (0) 2007.10.18
bash 배열 선언 및 처리하기  (0) 2007.10.16
DNS 서버 보기  (0) 2007.10.10
crontab 이야기  (0) 2007.10.10
Posted by '김용환'
,

 #!/bin/bash

files[1]="/etc/rc.local"
files[2]="/etc/rsyncd.conf"
files[3]="/etc/xinetd.d/rsync"

element_count=${#files[@]}

index=0

while [ "$index" -lt "$element_count" ]
do
    let "index = $index + 1"
    if [ -f "${files[index]}" ]; then
        size=`ls -nl ${files[index]} | awk ' {print $5 }'`
        if [ $size = "0" ] ; then
            echo "${files[index]}" is broken
        fi

    else
        echo "${files[index]}" is broken
    fi

done

 

 

참고하세용

'unix and linux' 카테고리의 다른 글

grep -c 와 wc -l은 똑같은 명령어이다.  (0) 2007.10.18
crond 문제  (0) 2007.10.17
DNS 서버 보기  (0) 2007.10.10
crontab 이야기  (0) 2007.10.10
bash shell script substitution  (0) 2007.10.09
Posted by '김용환'
,

 

* 현상

이클립스에서 웹서버를 server를 실행할때, 다음과 같이 에러가 나는 경우가 있다.

com.sun.tools.javac.Main 을 찾으려 하는데, classpath에 있지 않아서 에러가 난다.

 

Exception:
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
at org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:106)
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:935)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:764)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:282)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:328)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:427)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:142)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:536)

 

 

* 해결방법

tools.jar를 jdk 또는 톰캣의 lib  디렉토리에 tools.jar를 복사한다.

Posted by '김용환'
,

ERR=12505 에러

DB 2007. 10. 11. 04:36

 

DB 연결이 안되는 장애가 생겼다.

 

Caused by: java.sql.SQLException: IO 예외 상황:  Connection refused(DESCRIPTION=(TMP=)(VSNNUM=153094144)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))

 

 

원인은 ERR=12505 였다.

 

하지만, 이와 함께 여러 Exception이 발견되었다. 이 것만 봤었으면 인증문제인줄로 생각할 수도 있겠지만, 그보다 더 근본원인을 찾아야 한다.

 

java.lang.RuntimeException: Cannot create DataSource jdbc:oracle:thin:@1111.1.24:1606:google
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied

 

 

12505, 00000, "TNS:listener could not resolve SID given in connect descriptor"
// *Cause:  The SID in the CONNECT_DATA was not found in the listener's tables.
// *Action: Check to make sure that the SID specified is correct.
// The SIDs that are currently registered with the listener can be obtained by
// typing "LSNRCTL SERVICES ".  These SIDs correspond to SID_NAMEs
// in TNSNAMES.ORA, or db_names in INIT.ORA.
// *Comment: This error will be returned if the database instance has not
// registered with the listener; the instance may need to be started.

 

원인은 다음과 같다. 오라클 SID가 자바클라이언트와 오라클 서버간의 SID가 서로 다르기 때문이다.

 

ORA9로 변경하니 SID가 서로 맞아서 문제 해결이 되었다..

 

DBA에게 sid를 확인해 달라고 한다.

다음의 명령어를 오라클 서버에서 해달라고 부탁한다.

ps -ef |  grep smon

 

Posted by '김용환'
,

http://blog.naver.com/rinatear?Redirect=Log&logNo=120039007390

 

위의 링크에 가보면 한글 패치 파일이 있다.

'general computer' 카테고리의 다른 글

Spring Batch JobInstanceAlreadyCompleteException 내용  (0) 2012.07.12
검색 공부  (0) 2010.01.29
시너지 - Synergy  (0) 2007.08.10
Posted by '김용환'
,