Skip to content

Commit

Permalink
Merge pull request #77 from pi-hole/devel
Browse files Browse the repository at this point in the history
Pi-Hole Admin UI v1.2
  • Loading branch information
PromoFaux committed Apr 16, 2016
2 parents 4c67f54 + e1e5e50 commit 5ac7557
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 99 deletions.
121 changes: 66 additions & 55 deletions data.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
$log = array();
$ipv6 = file_exists("/etc/pihole/.useIPv6");

$hosts = file_exists("/etc/hosts") ? file("/etc/hosts") : array();

/******* Public Members ********/
function getSummaryData() {
global $ipv6;
$log = readInLog();
$domains_being_blocked = readInBlockList() / ($ipv6 ? 2 : 1);
$domains_being_blocked = gravityCount() / ($ipv6 ? 2 : 1);

$dns_queries_today = count(getDnsQueries($log));

Expand Down Expand Up @@ -102,68 +103,71 @@ function getQuerySources() {
$sources = array();
foreach($dns_queries as $query) {
$exploded = explode(" ", $query);
$ip = trim($exploded[count($exploded)-1]);
$ip = hasHostName(trim($exploded[count($exploded)-1]));
if (isset($sources[$ip])) {
$sources[$ip]++;
}
else {
$sources[$ip] = 1;
}
}
return $sources;
arsort($sources);
array_slice($sources, 0, 10);
return Array(
'top_sources' => $sources
);
}

function getAllQueries() {
$allQueries = array("data" => array());
$log = readInLog();
$dns_queries = getDnsQueries($log);

$fileName = '/etc/pihole/gravity.list';
//Turn gravity.list into an array
$lines = explode("\n", file_get_contents($fileName));

//Create a new array and set domain name as index instead of value, with value as 1
foreach(array_values($lines) as $v){
$new_lines[trim(strstr($v, ' '))] = 1;
}
$dns_queries = getDnsQueriesAll($log);

foreach ($dns_queries as $query) {
$time = date_create(substr($query, 0, 16));
$exploded = explode(" ", trim($query));

//Is index of the domain name set?
if (isset($new_lines[$exploded[count($exploded)-3]])){
$extra = "Pi-holed";
$tmp = $exploded[count($exploded)-4];

if (substr($tmp, 0, 5) == "query"){
$type = substr($exploded[count($exploded)-4], 6, -1);
$domain = $exploded[count($exploded)-3];
$client = $exploded[count($exploded)-1];
$status = "";
}
else
{
$extra = "OK";
elseif (substr($tmp, 0, 9) == "forwarded" ){
$status="OK";
}
array_push($allQueries['data'], array(
elseif (substr($tmp, strlen($tmp) - 12, 12) == "gravity.list" && $exploded[count($exploded)-5] != "read"){
$status="Pi-holed";
}

if ( $status != ""){
array_push($allQueries['data'], array(
$time->format('Y-m-d\TH:i:s'),
substr($exploded[count($exploded)-4], 6, -1),
$exploded[count($exploded)-3],
$exploded[count($exploded)-1],
$extra,
));
$type,
$domain,
hasHostName($client),
$status,
));
}


}
return $allQueries;
}

/******** Private Members ********/
function readInBlockList() {
function gravityCount() {
//returns count of domains in blocklist.
$file="/etc/pihole/gravity.list";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
$gravity="/etc/pihole/gravity.list";
$swallowed = 0;
$NGC4889 = fopen($gravity, "r");
while ($stars = fread($NGC4889, 1024000)) {
$swallowed += substr_count($stars, "\n");
}
fclose($NGC4889);

fclose($handle);

return $linecount;
return $swallowed;

}
function readInLog() {
Expand All @@ -174,6 +178,9 @@ function readInLog() {
function getDnsQueries($log) {
return array_filter($log, "findQueries");
}
function getDnsQueriesAll($log) {
return array_filter($log, "findQueriesAll");
}
function getBlockedQueries($log) {
return array_filter($log, "findAds");
}
Expand Down Expand Up @@ -248,30 +255,34 @@ function getRecent($queries, $qty){
return array_reverse($recent);
}

function findQueriesAll($var) {
return strpos($var, ": query[") || strpos($var, "gravity.list") || strpos($var, ": forwarded") !== false;
}

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

function findAds($var) {
return strpos($var, "gravity.list") !== false;
$exploded = explode(" ", $var);
$tmp = $exploded[count($exploded)-4];
$tmp2 = $exploded[count($exploded)-5];
//filter out bad names and host file reloads:
return (substr($tmp, strlen($tmp) - 12, 12) == "gravity.list" && $tmp2 != "read") ;
}

function findForwards($var) {
return strpos($var, ": forwarded") !== 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),
);
*/
?>

function hasHostName($var){
global $hosts;
foreach ($hosts as $host){
$x = explode("\t", $host);
if ( $var == $x[0] ){
$var = $x[1] . "($var)";
}
}
return $var;
}
?>
9 changes: 8 additions & 1 deletion header.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@
</div>
<div class="pull-left info">
<p>Status</p>
<a href="#"><i class="fa fa-circle text-success"></i> Nominal</a>
<?php
$pistatus = exec('pgrep dnsmasq | wc -l');
if ($pistatus > "0") {
echo '<a href="#"><i class="fa fa-circle" style="color:#7FFF00"></i> Active</a>';
} else {
echo '<a href="#"><i class="fa fa-circle" style="color:#FF0000"></i> Offline</a>';
}
?>
</div>
</div>
<!-- sidebar menu: : style can be found in sidebar.less -->
Expand Down
81 changes: 38 additions & 43 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</div>

<div class="row">
<div class="col-md-4">
<div class="col-md-6">
<div class="box" id="query-types">
<div class="box-header with-border">
<h3 class="box-title">Query Types</h3>
Expand All @@ -102,23 +102,7 @@
<!-- /.box-body -->
</div>
</div>
<div class="col-md-4">
<div class="box" id="top-clients">
<div class="box-header with-border">
<h3 class="box-title">Top Clients</h3>
</div>
<div class="box-body">
<div class="chart">
<canvas id="topClientsChart" style="height: 247px; width: 466px;" width="932" height="494"></canvas>
</div>
</div>
<div class="overlay">
<i class="fa fa-refresh fa-spin"></i>
</div>
<!-- /.box-body -->
</div>
</div>
<div class="col-md-4">
<div class="col-md-6">
<div class="box" id="forward-destinations">
<div class="box-header with-border">
<h3 class="box-title">Forward Destinations</h3>
Expand All @@ -137,7 +121,7 @@
</div>

<div class="row">
<div class="col-md-6">
<div class="col-md-4">
<div class="box" id="domain-frequency">
<div class="box-header with-border">
<h3 class="box-title">Top Domains</h3>
Expand All @@ -160,7 +144,7 @@
<!-- /.box -->
</div>
<!-- /.col -->
<div class="col-md-6">
<div class="col-md-4">
<div class="box" id="ad-frequency">
<div class="box-header with-border">
<h3 class="box-title">Top Advertisers</h3>
Expand All @@ -185,6 +169,29 @@
<!-- /.box -->
</div>
<!-- /.col -->
<div class="col-md-4">
<div class="box" id="client-frequency">
<div class="box-header with-border">
<h3 class="box-title">Top Clients</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody><tr>
<th>Client</th>
<th>Requests</th>
<th>Frequency</th>
</tr>
</tbody></table>
</div>
<div class="overlay">
<i class="fa fa-refresh fa-spin"></i>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->

Expand Down Expand Up @@ -243,7 +250,7 @@
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};
var animate = !isMobile.any();
var animate = false;
var ctx = document.getElementById("queryOverTimeChart").getContext("2d");
timeLineChart = new Chart(ctx).Line(chartData,
{
Expand All @@ -268,20 +275,6 @@
animation : animate
}
);

var radarChartData = {
labels: [],
datasets: [
{
label: "Top Clients",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(0, 166, 90,.8)",
data: []
}
]
};
ctx = document.getElementById("topClientsChart").getContext("2d");
topClientsChart = new Chart(ctx).Radar(radarChartData, {animation : animate});
});

// Functions to oupdate data in page
Expand Down Expand Up @@ -328,14 +321,15 @@ function updateQueriesOverTime() {
}

function updateTopClientsChart() {
$.getJSON("api.php?getQuerySources", function(data) {
console.log(data);
$.each(data, function(key, value) {
topClientsChart.addData([value], key);
});

$('#top-clients .overlay').remove();
//$('#queries-over-time').append(timeLineChart.generateLegend());
$.getJSON("api.php?summaryRaw&getQuerySources", function(data) {
var clienttable = $('#client-frequency').find('tbody:last');
for (domain in data.top_sources) {
clienttable.append('<tr> <td>' + domain +
'</td> <td>' + data.top_sources[domain] + '</td> <td> <div class="progress progress-sm"> <div class="progress-bar progress-bar-blue" style="width: ' +
data.top_sources[domain] / data.dns_queries_today * 100 + '%"></div> </div> </td> </tr> ');
}

$('#client-frequency .overlay').remove();
});
}

Expand Down Expand Up @@ -375,6 +369,7 @@ function updateTopLists() {
$.getJSON("api.php?summaryRaw&topItems", function(data) {
var domaintable = $('#domain-frequency').find('tbody:last');
var adtable = $('#ad-frequency').find('tbody:last');

for (domain in data.top_queries) {
domaintable.append('<tr> <td>' + domain +
'</td> <td>' + data.top_queries[domain] + '</td> <td> <div class="progress progress-sm"> <div class="progress-bar progress-bar-green" style="width: ' +
Expand Down

0 comments on commit 5ac7557

Please sign in to comment.