explode?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AJ

    explode?

    Hi All

    I've just had anti virus installed on my server. There is a log file that
    shows all the viruses that have been trapped. There are lots of different
    lines in the log file but the ones I'm interested in look like this:

    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND

    What I want to write a little PHP script that will display the results on a
    web page. So, my first task will be to extract only the lines that begine
    with /var/amavis, or even only those that end with FOUND.

    Then I want to take all the characters between the : and the F and display
    those.

    It would be nice if I could display a count of how many viruses had been
    caught so far.

    Lastly, I really only want to display, say, 30 lines at a time.

    Then I pop a refresh on the page and we can hopefully see the viruses as
    they come in. It's just so I can show customers how clever this is.

    I'm going to start digging myself through the PHP manual but if anyone could
    offer any guidance I'd be most grateful. I think it might be explode that I
    want to use, but I'm not sure. Nor am I sure how to select only the
    relevant lines and just display the last 30.

    Best

    Andy


  • Pedro Graca

    #2
    Re: explode?

    AJ wrote:[color=blue]
    > I've just had anti virus installed on my server. There is a log file that
    > shows all the viruses that have been trapped. There are lots of different
    > lines in the log file but the ones I'm interested in look like this:
    >
    > /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    >
    > What I want to write a little PHP script that will display the results on a
    > web page. So, my first task will be to extract only the lines that begine
    > with /var/amavis, or even only those that end with FOUND.[/color]

    Use preg_match_all( )


    Remember the correct modifier for multiline mode!
    [color=blue]
    > Then I want to take all the characters between the : and the F and display
    > those.[/color]

    that is also part of preg_match_all( )
    [color=blue]
    > It would be nice if I could display a count of how many viruses had been
    > caught so far.[/color]

    Save the count in a array with virus names as indexes, eg:
    $array['Worm.SomeFool. B-petite'] = 18;
    [color=blue]
    > Lastly, I really only want to display, say, 30 lines at a time.[/color]

    Best is a for loop
    [color=blue]
    > I'm going to start digging myself through the PHP manual but if anyone could
    > offer any guidance I'd be most grateful. I think it might be explode that I
    > want to use, but I'm not sure. Nor am I sure how to select only the
    > relevant lines and just display the last 30.[/color]


    SPOILER FOLLOWS -- try to make the script on your own



    ....







    ....







    ....







    ....







    ....







    ....







    ....











    <?php
    // get data
    # read from file instead
    $data = "
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    /amavis/amavis-012184/parts/part-00002: Worm.SomeFool.A-petite FOUND
    /amavis/amavis-012184/parts/part-00002: Worm.SomeFool.C-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.C-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite DELETED
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.A-petite DELETED
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.C-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.A-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.A-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.A-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    /var/amavis/amavis-012184/parts/part-00002: Worm.SomeFool.B-petite FOUND
    ";
    # for example:
    #$data = file_get_conten ts('amavis.log' );

    // find all lines matching specification
    preg_match_all( '@^/var/amavis/[^:]+: (.*) FOUND$@m', $data, $matches);

    // populate result array
    foreach ($matches[1] as $virus_name) {
    if (!isset($viruse s[$virus_name])) $viruses[$virus_name] = 0;
    // HACK!! :: decrement for asort() to work ascendingly
    $viruses[$virus_name]--;
    }

    asort($viruses) ;
    foreach ($viruses as $v=>$q) {
    # you might want to make this nicer HTML :)
    # I tested with the command-line PHP
    // HACK!! :: remember to reverse the sign
    echo $v, ': ', -$q, "\n";
    }
    ?>


    Result with that constant data:
    Worm.SomeFool.B-petite: 5
    Worm.SomeFool.A-petite: 3
    Worm.SomeFool.C-petite: 2
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    Working...