AJ wrote:
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.
Use preg_match_all()
http://www.php.net/preg_match_all
Remember the correct modifier for multiline mode!
Then I want to take all the characters between the : and the F and display
those.
that is also part of preg_match_all()
It would be nice if I could display a count of how many viruses had been
caught so far.
Save the count in a array with virus names as indexes, eg:
$array['Worm.SomeFool.B-petite'] = 18;
Lastly, I really only want to display, say, 30 lines at a time.
Best is a for loop
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.
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_contents('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($viruses[$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 =--