Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced stats and graphs #23

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions api.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php
$domains_being_blocked = exec("wc -l /etc/pihole/gravity.list | awk '{print $1}'");
$dns_queries_today = exec("cat /var/log/pihole.log | awk '/query/ {print $6}' | wc -l");
$ads_blocked_today = exec("cat /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l");
$ads_percentage_today = $ads_blocked_today / $dns_queries_today * 100;
$arr = array(
'domains_being_blocked' => $domains_being_blocked,
'dns_queries_today' => $dns_queries_today,
'ads_blocked_today' => $ads_blocked_today,
'ads_percentage_today' => $ads_percentage_today
);
include('data.php');

header('Content-type: application/json');
echo json_encode($arr);
echo json_encode($data);
?>
105 changes: 105 additions & 0 deletions data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
$domains = file("/etc/pihole/gravity.list");
$log = file("/var/log/pihole.log");
$domains_being_blocked = count($domains);

$dns_queries = array_filter($log, "findQueries");
$dns_queries_today = count($dns_queries);

$ads_blocked= array_filter($log, "findAds");
$ads_blocked_today = count($ads_blocked);

$ads_percentage_today = $ads_blocked_today / $dns_queries_today * 100;

$domains_over_time = overTime($dns_queries);
$ads_over_time = overTime($ads_blocked);
alignTimeArrays($ads_over_time, $domains_over_time);

$topAds = topItems($ads_blocked);
$topQueries = topItems($dns_queries, $topAds);

function topItems($queries, $exclude = array()) {
$splitQueries = array();
foreach ($queries as $query) {
$exploded = explode(" ", $query);
$domain = trim($exploded[5]);
if (!isset($exclude[$domain])) {
if (isset($splitQueries[$domain])) {
$splitQueries[$domain]++;
}
else {
$splitQueries[$domain] = 1;
}
}
}
arsort($splitQueries);
return array_slice($splitQueries, 0, 10);
}

function overTime($entries) {
$byTime = array();
foreach ($entries as $entry) {
$time = date_create(substr($entry, 0, 16), new DateTimeZone('GMT'))->SetTimeZone(new DateTimeZone(date_default_timezone_get()));
$hour = $time->format('G');

if (isset($byTime[$hour])) {
$byTime[$hour]++;
}
else {
$byTime[$hour] = 1;
}
}
return $byTime;
}

function alignTimeArrays(&$times1, &$times2) {
foreach (array_keys($times1) as $time) {
if (!isset($times2[$time])) {
$times2[$time] = 0;
}
}
foreach (array_keys($times2) as $time) {
if (!isset($times1[$time])) {
$times1[$time] = 0;
}
}
ksort($times1);
ksort($times2);
}

function getRecent($queries, $qty){
$recent = array();
foreach (array_slice($queries, -$qty) as $query) {
$queryArray = array();
$exploded = explode(" ", $query);
$time = date_create(substr($query, 0, 16), new DateTimeZone('GMT'))->SetTimeZone(new DateTimeZone(date_default_timezone_get()));

$queryArray['time'] = $time->format('h:m:s');
$queryArray['domain'] = trim($exploded[5]);
$queryArray['ip'] = trim($exploded[7]);
array_push($recent, $queryArray);
}
return array_reverse($recent);
}

function findQueries($var) {
return strpos($var, "query") != false;
}

function findAds($var) {
return strpos($var, "gravity.list") != false;
}

$data = array(
'domains_being_blocked' => $domains_being_blocked,
'dns_queries_today' => $dns_queries_today,
'ads_blocked_today' => $ads_blocked_today,
'ads_percentage_today' => $ads_percentage_today,
'top_queries' => $topQueries,
'top_ads' => $topAds,
'domains_over_time' => $domains_over_time,
'ads_over_time' => $ads_over_time,
'recent_queries' => getRecent($dns_queries, 20),
);

?>
154 changes: 142 additions & 12 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
$domains_being_blocked = exec("wc -l /etc/pihole/gravity.list | awk '{print $1}'");
$dns_queries_today = exec("cat /var/log/pihole.log | awk '/query/ {print $6}' | wc -l");
$ads_blocked_today = exec("cat /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l");
$ads_percentage_today = $ads_blocked_today / $dns_queries_today * 100;
# adds $data variable
include('data.php');
?>
<!DOCTYPE html>
<html>
Expand All @@ -23,6 +21,35 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<script type="text/javascript">
var chartData = {
labels: [<?php foreach($data['domains_over_time'] as $time=>$qty) {echo $time . ", ";} ?>],
datasets: [
{
label: "All Queries",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(0, 166, 90,.8)",
data: [<?= implode(', ', $data['domains_over_time']) ?>],
},
{
label: "Ad Queries",
fillColor: "rgba(243,156,18,0.5)",
strokeColor: "rgba(243,156,18,1)",
pointColor: "rgba(243,156,18,1)",
data: [<?= implode(', ', $data['ads_over_time']) ?>],
}
]
};

$(document).ready(function() {
var ctx = document.getElementById("queryOverTimeChart").getContext("2d");
var myLineChart = new Chart(ctx).Line(chartData, {pointDot : false });
});


</script>
</head>
<body class="skin-blue sidebar-mini">
<div class="wrapper">
Expand Down Expand Up @@ -125,60 +152,163 @@
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
<h3><?php echo number_format($ads_blocked_today) ?></h3>
<h3><?php echo number_format($data['ads_blocked_today']) ?></h3>
<p>Ads Blocked Today</p>
</div>
<div class="icon">
<i class="ion ion-android-hand"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-green">
<div class="inner">
<h3><?php echo number_format($dns_queries_today) ?></h3>
<h3><?php echo number_format($data['dns_queries_today']) ?></h3>
<p>DNS Queries Today</p>
</div>
<div class="icon">
<i class="ion ion-earth"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-yellow">
<div class="inner">
<h3><?php echo number_format($ads_percentage_today, 2, '.', '') ?><sup style="font-size: 20px">%</sup></h3>
<h3><?php echo number_format($data['ads_percentage_today'], 2, '.', '') ?><sup style="font-size: 20px">%</sup></h3>
<p>Of Today's Traffic Is Ads</p>
</div>
<div class="icon">
<i class="ion ion-pie-graph"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-red">
<div class="inner">
<h3><sup style="font-size: 30px"><?php echo number_format($domains_being_blocked) ?></sup></h3>
<h3><sup style="font-size: 30px"><?php echo number_format($data['domains_being_blocked']) ?></sup></h3>
<p>Domains Being Blocked</p>
</div>
<div class="icon">
<i class="ion ion-ios-list"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
</div>
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Queries over time</h3>
</div>
<div class="box-body">
<div class="chart">
<canvas id="queryOverTimeChart" style="height: 247px; width: 466px;" width="932" height="494"></canvas>
</div>
</div>
<!-- /.box-body -->
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Top Domains</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody><tr>
<th>Domain</th>
<th>Hits</th>
<th>Frequency</th>
</tr>
<?php foreach($data['top_queries'] as $key=>$value): ?>
<tr>
<td><?php echo $key ?></td>
<td><?php echo $value ?></td>
<td>
<div class="progress progress-sm">
<div class="progress-bar progress-bar-green" style="width: <?php echo number_format(($value/$data['dns_queries_today']), 2, '.', '') * 100?>%"></div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
<div class="col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Top Advertisers</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody><tr>
<th>Domain</th>
<th>Hits</th>
<th>Frequency</th>
</tr>
<?php foreach($data['top_ads'] as $key=>$value): ?>
<tr>
<td><?php echo $key ?></td>
<td><?php echo $value ?></td>
<td>
<div class="progress progress-sm">
<div class="progress-bar progress-bar-yellow" style="width: <?php echo number_format(($value/$data['ads_blocked_today']), 2, '.', '') * 100?>%"></div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Recent Queries</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody><tr>
<th>Time</th>
<th>Domain</th>
<th>Source Ip</th>
</tr>
<?php foreach($data['recent_queries'] as $key=>$value): ?>
<tr>
<td><?php echo $value['time'] ?></td>
<td><?php echo $value['domain'] ?></td>
<td><?php echo $value['ip'] ?></td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
</section>
<!-- /.content -->
</div>
Expand Down