Parsing Webalizer or/and AWstats html file

1.5k views Asked by At

I've got CPanel hosting with a few subdomains. Each one tracks its own stats using AWstats.

Is there any way to parse the HTML tables that are generated to get the bandwidth used?

Thanks for all.

2

There are 2 answers

0
FokeyJoe On

An alternative to reading the HTML directly is to directly use the stats files that awstats uses to render the HTML. cpanel should store them somewhere accessible (on my hosting they're in tmp/). It's a simple text file, with well marked sections that are easily found, and within the section is space-separated CSV representing the tables. This page on parsing the files with PHP is a good place for information on how to do it with code. I'm sure with a bit of linux know-how you can use sed and awk to respectively chop the file and return the columns you need to gather up the stats you need from these summaries.

1
Paul Sweatte On

Use a DOMXPath query the get the text of each row in the "kB F" column, such as the following for the first row:

$doc = new DOMDocument;

//Path to Webalizer or AWStats file
$doc->Load('stats.html');

$xpath = new DOMXPath($doc);

// Get the first bandwidth record in the table
$query = "//tr[7]/td[7]/font/text()";

$bandwidth1 = $xpath->query($query);

References