From 106aba7efce18e26daac2150650426388a02a810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=A0ijanec?= Date: Mon, 20 May 2019 22:01:18 +0200 Subject: uploaded the actual program yes --- server-status-logger.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 server-status-logger.php diff --git a/server-status-logger.php b/server-status-logger.php new file mode 100644 index 0000000..b1fa4e2 --- /dev/null +++ b/server-status-logger.php @@ -0,0 +1,147 @@ +#!/usr/bin/env php +# file updated 20. may 2019, 22:00 from http://razor.arnes.si/~asija3/files/server-status-logger.php +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() { + $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 + )'); + + // var_dump($table); // debug + // exit(); // debug + +$countTable = $dbConnect->querySingle("SELECT COUNT(*) as count FROM ".$tablename.""); +$countTable = true; +while($countTable) { // while true basically + $table = tablefromhtml($url); + end($table); + $lastkey = key($table); + for($i=1;$i <= $lastkey;$i++) { + // echo $table[$i]["Client"] . " "; + $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); + // var_dump($row); // debug + 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 + $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 = $smt->execute(); + if($query) { + echo "."; + } else { + echo "x"; + } + } else { + echo "whoops?\n"; + $dbConnect->lastErrorMsg(); + } + } +} + +// echo build_table($table); // debug +// header("Content-type: text/plain"); // debug +// var_dump($table); // debug + + + +function build_table($array){ // not needed, maybe for debugging + // start table + $html = ''; + // header row + $html .= ''; + foreach($array[0] as $key=>$value){ + $html .= ''; + } + $html .= ''; + + // data rows + foreach( $array as $key=>$value){ + $html .= ''; + foreach($value as $key2=>$value2){ + $html .= ''; + } + $html .= ''; + } + + // finish table and return it + + $html .= '
' . htmlspecialchars($key) . '
' . htmlspecialchars($value2) . '
'; + return $html; +} + +?> -- cgit v1.2.3