diff --git a/README.md b/README.md index 06c884f..bc5873f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -WebSysLog +WebSysLog 2.0 ========= This is a simple Syslog viewer written in PHP. Your syslog server must be configured to push syslog messages to a MySQL database. @@ -7,5 +7,11 @@ Instructions for writing syslog to MySQL can be found here: [[http://www.rsyslog Edit config.php to enter your database information and basic setup. +Version 2.0: +![Screenshot](https://github.com/SnipeLike/WebSysLog/blob/master/screenshot_v2.png) + +---- + +Version 1.0 (by github.com/tmanternach): ![Screenshot](https://raw.github.com/tmanternach/WebSysLog/master/screenshot.png) diff --git a/config.php b/config.php index a32c8ed..7a56ca5 100644 --- a/config.php +++ b/config.php @@ -1,26 +1,49 @@ of your page and also the h1 header at the top of the page. + $hostname = "localhost"; //Change this to IP or hostname of your database server. Probably localhost. + $username = "user"; //This is a MySQL user that has access to the Syslog database. + $password = "pass"; //Password for above user. + $database_name = "syslog"; //The name of the database for the Syslog events. + $search_limit = 200; + + # COLORS + $background_color = "#111111"; + $header_color = "#111111"; + $date_color = "#009E52"; + $host_color = "#FFFA9E"; + $tag_color = "#0081C2"; + $msg_color = "#ffffff"; + $warning_color = "#AA0000"; -$title = "Syslog Viewer"; //This appears as the of your page and also the h1 header at the top of the page. -$hostname = "localhost"; //Change this to IP or hostname of your database server. Probably localhost. -$username = "root"; //This is a MySQL user that has access to the Syslog database. -$password = "password"; //Password for above user. -$database_name = "Syslog"; //The name of the database for the Syslog events. -$search_limit = 300; -# COLORS -$background_color = "rgb(17,17,17)"; -$header_color = "rgb(17,17,17)"; -$date_color = "#009E52"; -$host_color = "#FFFA9E"; -$tag_color = "#0081C2"; + // DB-Table + $tbl_name = "syslog"; //The Table where Syslog events are stored. + //$tbl_name = "SystemEvents"; -# DO NOT EDIT BELOW THIS LINE + //Translation-Table for the various columnNames + // Hint: Change only the right values! Left-Values are the indexes! + $columnNames = array( + "id" => "id", + "ReceivedAt" => "timestamp", + "FromHost" => "host", + "SysLogTag" => "tag", + "Priority" => "level", + "Message" => "msg", + ); + /* + $columnNames = array( + "id" => "ID", + "ReceivedAt" => "ReceivedAt", + "FromHost" => "FromHost", + "SysLogTag" => "SysLogTag", + "Priority" => "Priority", + "Message" => "Message", + ); + */ -$con = mysql_connect($hostname,$username,$password); -if (!$con) - { - die('Could not connect: ' . mysql_error()); - } + # ---DO NOT EDIT BELOW THIS LINE--- + require_once('dbhelper.php'); -mysql_select_db($database_name, $con); + $database = new DBObject($hostname, $username, $password, $database_name); + $tbl = new TableObject($database, $tbl_name, $columnNames); ?> diff --git a/dbhelper.php b/dbhelper.php new file mode 100644 index 0000000..5a789e0 --- /dev/null +++ b/dbhelper.php @@ -0,0 +1,104 @@ +<?php + +/* +Simple Database Object which holds all data required for connecting to a database. +- Such Object automatically opens a connection if it's needed +- When this Object is deconstructed, an open mysql-connection will be automatically closed! + */ +class DBObject { + private $hostname; + private $username; + private $password; + + private $con = NULL; + + private $database; + + public function getDBName(){ + return $this->database; + } + + public function getUserName(){ + return $this->username; + } + + public function getDBCon(){ + if(!isset($this->con)){ + $this->connect(); + } + return $this->con; + } + + + private function connect(){ + $con = mysql_connect( + $this->hostname, + $this->username, + $this->password + ); + if(!$con){ + die('Could not connect: ' . mysql_error() + . debug_print_backtrace()); + } else { + $this->con = $con; + set_time_limit(300); + mysql_select_db($this->database, $con); + } + } + + function __construct($hostname, $username, $password, $database){ + $this->hostname = $hostname; + $this->username = $username; + $this->password = $password; + $this->database = $database; + } + + function __destruct(){ + if(isset($this->con)){ + mysql_close($this->con); + } + } +} + + +class TableObject { + private $DBObject; + + private $TableName; + + private $ColumnTranslations; + + public function __construct($DBObject, $TableName, $ColumnTranslations){ + if(!is_object($DBObject) || !(get_class($DBObject) == 'DBObject')){ + die("First argument isn't a DBObject!". debug_print_backtrace()); + } + if(isset($ColumnTranslations) && !is_array($ColumnTranslations)){ + die("ColumnTranslation isn't an array!" . debug_print_backtrace()); + } + $this->DBObject = $DBObject; + $this->TableName = $TableName; + if(isset($ColumnTranslations)){ + $this->ColumnTranslations = $ColumnTranslations; + } else { + $this->ColumnTranslations = NULL; + } + //var_dump($this->ColumnTranslations); + } + + public function tableName(){ + return $this->DBObject->getDBName() . '.' . $this->TableName; + } + + public function long_columnName($columnIdentifier){ + return $this->tableName() . '.' . $this->columnName($columnIdentifier); + } + + public function columnName($columnIdentifier){ + if(array_key_exists($columnIdentifier, $this->ColumnTranslations)){ + return $this->ColumnTranslations[$columnIdentifier]; + } + return $columnIdentifier; + } +} + +?> diff --git a/distinct.php b/distinct.php index b05154f..7673f23 100644 --- a/distinct.php +++ b/distinct.php @@ -1,14 +1,14 @@ <?php -require_once("config.php"); -$sql="SELECT DISTINCT FromHost FROM SystemEvents ORDER BY FromHost DESC"; -$result = mysql_query($sql); -$arr = array(); -while($row = mysql_fetch_array($result)) -{ - //if (strpos($row['FromHost'],$_GET['term']) !== false) { - $arr[] = $row['FromHost']; - //} -} -echo json_encode($arr); -mysql_close($con); + require_once("config.php"); + $sql="SELECT DISTINCT " $tbl->long_columnName('FromHost') . " FROM " . $tbl->TableName() . " ORDER BY " . $tbl->long_columnName('FromHost') . " DESC"; + $result = mysql_query($sql, $database->getDBcon()); + $arr = array(); + + while($row = mysql_fetch_array($result)) { + //if (strpos($row[$tbl->columnName('FromHost')],$_GET['term']) !== false) { + $arr[] = $row[$tbl->columnName('FromHost')]; + //} + } + + echo json_encode($arr); ?> diff --git a/function.php b/function.php new file mode 100644 index 0000000..622a1ae --- /dev/null +++ b/function.php @@ -0,0 +1,32 @@ +<?php + //Include + require_once("config.php"); + + //Function for no results found + function noResFound() { + echo "<span class=\"warning\">No results found! ¯\_(ツ)_/¯</span><br>"; + } + + //Function for SELECT in search.php, 4 Days Archive + function selectFromArchive4DaysDBM($db, $date, $query, $limit) { + $sql="SELECT * FROM ( + SELECT * FROM ".$db." WHERE timestamp LIKE '".$date."%' AND (timestamp LIKE '%".$query."%' OR host LIKE '%".$query."%' OR msg LIKE '%".$query."%') + ORDER BY id DESC + ) + sub + ORDER BY timestamp DESC LIMIT 500"; + $result = mysql_query($sql); + + return $result; + } + + //Function for SELECT in search.php + function selectFromArchiveDBM($db, $date, $query, $limit) { + $sql="SELECT id,timestamp,host,facility,level,tag,msg FROM ".$db." WHERE timestamp LIKE '".$date."%' AND (timestamp LIKE '%".$query."%' OR host LIKE '%".$query."%' + OR msg LIKE '%".$query."%') + ORDER BY timestamp DESC LIMIT 500"; + $result = mysql_query($sql); + + return $result; + } +?> diff --git a/get_mysql_query.php b/get_mysql_query.php new file mode 100644 index 0000000..3698b74 --- /dev/null +++ b/get_mysql_query.php @@ -0,0 +1,18 @@ +<?php + require_once("config.php"); + + //Get Proc list + $sql="SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER LIKE \"" . $database.getUserName() . "\" AND COMMAND LIKE 'Query' AND TIME > 600"; + $result = mysql_query($sql, $database->getDBcon()); + + while($row = mysql_fetch_array($result)) { + $id = $row['ID']; + $rtime = $row['TIME']; + + echo $id ." ". $rtime ."<br>"; + + //Kill + $kill_proc = "KILL $id"; + mysql_query($kill_proc, $database->getDBcon()); + } +?> diff --git a/index.php b/index.php index fbf2f80..ec72f82 100644 --- a/index.php +++ b/index.php @@ -1,300 +1,481 @@ -<?php require_once("config.php"); ?> +<?php + require_once("config.php"); + + + //Get Var + $searchtext = $_GET['search']; + $date = $_GET['date']; +?> + <html> <head> -<title><?=$title; ?> - - - - - + <?=$title;?> + + + + + + + + -