summaryrefslogtreecommitdiffstats
path: root/server-status-logger.php
blob: 74c97f1d16cf9d8013b5ee3ee2dd70d0abb357b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env php
#file updated from http://razor.arnes.si/~asija3/files/server-status-logger.php at 1. June 2019 at 17:53
#!/usr/bin/env php
<?php
if(empty($argv[1])) {
	exit("server-status-logger.php v0.1 2019 Anton Šijanec astiril/server-status-logger\n
	$argv[0] server-status-url [dbfile]\n");
}
if(empty($argv[2])) {
	$dbfile = 'log.db';
} else {
	$dbfile = $argv[2];
}
$url = $argv[1];
function tablefromhtml($urltoget) {
	$dom = new DOMDocument();  
	//load the html  
	$html = $dom->loadHTMLFile($urltoget);  
	  //discard white space   
	$dom->preserveWhiteSpace = false;
	  //the table by its tag name  
	$tables = $dom->getElementsByTagName('table');   
		//get all rows from the table  
	$rows = $tables->item(0)->getElementsByTagName('tr');   
	  // get each column by tag name  
	$cols = $rows->item(0)->getElementsByTagName('th');   
	$row_headers = NULL;
	foreach ($cols as $node) {
		//print $node->nodeValue."\n";   
		$row_headers[] = $node->nodeValue;
	}   
	$table = array();
	  //get all rows from the table  
	$rows = $tables->item(0)->getElementsByTagName('tr');   
	foreach ($rows as $row)   
	{   
	   // get each column by tag name  
		$cols = $row->getElementsByTagName('td');   
		$row = array();
		$i=0;
		foreach ($cols as $node) {
			# code...
			//print $node->nodeValue."\n";   
			if($row_headers==NULL)
				$row[] = $node->nodeValue;
			else
				$row[$row_headers[$i]] = $node->nodeValue;
			$i++;
		}   
		$table[] = $row;
	}
	return $table;
}

   class MyDB extends SQLite3 {
      function __construct() {
		  global $dbfile;
         $this->open($dbfile);
      }
   }
   $dbConnect = new MyDB();
   if(!$dbConnect) {
	  exit($dbConnect->lastErrorMsg());
   } else {
      echo "Opened database successfully\n";
   }
   $tablename = "dnevnik";
   
   $result = $dbConnect->query('CREATE TABLE IF NOT EXISTS '.$tablename.' (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "timestamp" INTEGER,
    "worker" INTEGER,
    "client" TEXT,
	"vhost" TEXT,
	"request" TEXT
	)');
   
   
$countTable = $dbConnect->querySingle("SELECT COUNT(*) as count FROM ".$tablename."");
$countTable = true;
while($countTable) { // if table is ok while true basically
	echo "r";
	$table = tablefromhtml($url);
	end($table);
	$lastkey = key($table);
	for($i=1;$i <= $lastkey;$i++) {
		$unique = false;
		$smt = $dbConnect->prepare("SELECT `client`, `request` FROM ".$tablename." WHERE `worker` = :worker ORDER BY `timestamp` DESC LIMIT 1");
		$smt->bindValue(':worker', explode('-', $table[$i]["Srv"]), SQLITE3_TEXT)[0];
		@$query = $smt->execute(); // string>array notice
		$row = $query->fetchArray(SQLITE3_ASSOC);
		if(!is_array($row) || empty(array_diff($row, array($table[$i]["Client"], $table[$i]["Request"])))) {
			$unique = true;
		}
		if($table[$i]["Client"] != "::1" && $unique) { // ::1 because localhost is always querying for some reason via IPv6 on my target server
			$smt = $dbConnect->prepare("INSERT INTO ".$tablename." (timestamp, worker, client, vhost, request) VALUES ( :timestamp, :worker, :client, :vhost, :request )");
			$smt->bindValue(':timestamp', date("U"), SQLITE3_TEXT);
			$smt->bindValue(':worker', explode('-', $table[$i]["Srv"])[0], SQLITE3_TEXT);
			$smt->bindValue(':client', $table[$i]["Client"], SQLITE3_TEXT);
			$smt->bindValue(':vhost', $table[$i]["VHost"], SQLITE3_TEXT);
			$smt->bindValue(':request', $table[$i]["Request"], SQLITE3_TEXT);
			$query = false;
			while(!$query) {
				$query = @$smt->execute();
				if($query) {
					echo ".";
				} else {
					echo "x";
				}
			}
		}
	}
}
?>