Code

Updated Pager
[gosa.git] / gosa-plugins / rsyslog / addons / rsyslog / class_rsyslog.inc
index da20ef51139e69f57acacc39a1c02ec96f813a0d..4c7ed5bc6358949b6fc490251f719a84f89df9a3 100644 (file)
@@ -2,19 +2,22 @@
 
 class rsyslog extends plugin
 {
-  var $plHeadline= "rSyslog";
+  var $plHeadline= "System logs";
   var $plDescription= "View system logs";
 
   var $servers = array();
   var $selected_server = "";
-  var $selected_host = "";
+  var $selected_host = "all";
+  var $selected_priority = "all";
 
   var $startTime = "";
   var $stopTime  = "";
 
-  var $sort_value  = "FromHost";
+  var $search_for ="";
+
+  var $sort_value  = "DeviceReportedTime";
   var $sort_type  = "DESC";
-  var $limit  = 20;
+  var $limit  = "0";
   var $limits = array(20,50,100,200,500,1000,'-');
   var $page   = 0;
 
@@ -29,15 +32,16 @@ class rsyslog extends plugin
     // Get list of rsyslog servers 
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);
-    $ldap->search("objectClass=rSyslogServer",array("cn","rSyslogUser","rSyslogDatabase","rSyslogPassword"));
+    $ldap->search("objectClass=goLogDBServer",array("cn","goLogAdmin","gosaLogDB","goLogPassword"));
     while($attrs = $ldap->fetch()){
       if(empty($this->selected_server)) $this->selected_server = $attrs['cn'][0];
       $s = array('cn' => $attrs['cn'][0], 'dn' => $attrs['dn'], 'Password' => '', 'status' => 'ok');
-      $s['User'] = $attrs['rSyslogUser'][0]; 
-      $s['Database'] = $attrs['rSyslogDatabase'][0]; 
+      $s['User'] = $attrs['goLogAdmin'][0]; 
+      $s['Database'] = $attrs['gosaLogDB'][0]; 
       $s['Hosts'] = array();
+      $s['Priorities'] = array();
       $s['ACL'] = "";
-      if(isset($attrs['rSyslogPassword'])) $s['Password'] = $attrs['rSyslogPassword'][0];
+      if(isset($attrs['goLogPassword'])) $s['Password'] = $attrs['goLogPassword'][0];
       $this->servers[$attrs['cn'][0]] = $s;
     }
 
@@ -79,6 +83,16 @@ class rsyslog extends plugin
             $this->servers[$name]['status'] = "query_failed";
             $this->servers[$name]['error']  = @mysql_error();
           }
+          $query = "SELECT distinct(Priority) FROM SystemEvents ORDER BY Priority;";
+          $res = @mysql_query($query, $link);
+          if($res){
+            while($attrs = @mysql_fetch_assoc($res)){
+              $this->servers[$name]['Priorities'][$attrs['Priority']] = $attrs['Priority'];  
+            }
+          }else{
+            $this->servers[$name]['status'] = "query_failed";
+            $this->servers[$name]['error']  = @mysql_error();
+          }
         }
       }
     }
@@ -90,19 +104,30 @@ class rsyslog extends plugin
     $smarty->assign("usePrototype", "true");
     $ui = get_userinfo();
     
-    if(!isset($this->servers[$this->selected_server]) || !count($this->servers[$this->selected_server]['Hosts'])){
+    if(!isset($this->servers[$this->selected_server]['Hosts']) || !count($this->servers[$this->selected_server]['Hosts'])){
       $hosts = array('-' => _("-"));
     }else{
       $hosts = array('all' => _("All"));
       $hosts = array_merge($hosts,$this->servers[$this->selected_server]['Hosts']);
     }
+    if(!isset($this->servers[$this->selected_server]['Priorities']) || !count($this->servers[$this->selected_server]['Priorities'])){
+      $priorities = array('-' => _("-"));
+    }else{
+      $priorities = array('all' => _("All"));
+      foreach($this->servers[$this->selected_server]['Priorities'] as $id => $name){
+        $priorities[$id] = $name;
+      }
+    }
 
     $result =$this->get_result();
 
     $smarty->assign("servers",$this->servers);
     $smarty->assign("hosts",$hosts);
+    $smarty->assign("priorities",$priorities);
     $smarty->assign("selected_server",$this->selected_server);
     $smarty->assign("selected_host",$this->selected_host);
+    $smarty->assign("selected_priority",$this->selected_priority);
+    $smarty->assign("search_for",$this->search_for);
     $smarty->assign("startTime",  $this->startTime);
     $smarty->assign("stopTime",    $this->stopTime);
     $smarty->assign("sort_type",    $this->sort_type);
@@ -114,7 +139,8 @@ class rsyslog extends plugin
     $smarty->assign("downimg", "<img src='images/lists/sort-down.png' class='center' alt='\\/' border='0'>");
     $smarty->assign("upimg", "<img src='images/lists/sort-up.png' class='center' alt='\\/' border='0'>");
     $smarty->assign("result", $result);
-    if($this->limit != '-'){
+    $smarty->assign("matches", sprintf(_("%s entries match the filter"), $result["count"]));
+    if($this->limits[$this->limit] != '-'){
       $smarty->assign("page_sel", range_selector($result['count'],$this->page,$this->limits[$this->limit]));
     }else{
       $smarty->assign("page_sel", "");
@@ -132,14 +158,17 @@ class rsyslog extends plugin
     $result['error']   = "";
 
     // Check whether the selected server exists.
-    if(!isset($this->servers[$this->selected_server])){
-      $this->servers[$name]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
-      $this->servers[$name]['error']  = $this->servers[$this->selected_server]['error'];
+    if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
+      $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
+      $this->servers["unknown"]["cn"] = "-";
       return($result);
     }
 
     // Check database connectivity    
     $name = $this->selected_server;
+
+
+
     $server = $this->servers[$name];
     $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
     if ($link === FALSE){
@@ -156,15 +185,22 @@ class rsyslog extends plugin
     }
 
     // Get log entries matching the selected filter
-    if(preg_match('/r/', $this->servers[$name]['ACL'])){
+    if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
 
       $host = "%%";
       if($this->selected_host != "all") $host = $this->selected_host;
+      $prio = "%%";
+      if($this->selected_priority != "all") $prio = $this->selected_priority;
 
       $filter = "FROM SystemEvents WHERE FromHost like '{$host}' ";
+      $filter.= "AND Priority like '{$prio}' ";
       $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";     
       $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";     
 
+      if(!empty($this->search_for)){
+        $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR  ( Message like '%".$this->search_for."%' ) ) ";
+      }
+
       // Detect amount of matching entries 
       $query = "SELECT count(ID) as `amount` ".$filter;
       $res = @mysql_query($query, $link);
@@ -179,8 +215,8 @@ class rsyslog extends plugin
 
         if($result['count'] < $this->page) $this->page = 0;
 
-        $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type." ";
-        if($this->limit != "-"){
+        $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
+        if($this->limits[$this->limit]!= "-"){
           $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
         }else{
           $this->page = 0;
@@ -208,17 +244,44 @@ class rsyslog extends plugin
 
   function save_object()
   {
-    foreach(array("selected_host","selected_server","stopTime","startTime") as $attr){
+    // Get timestamps
+    foreach(array("stopTime","startTime") as $attr){
       if(isset($_POST[$attr])) $this->$attr = get_post($attr);
     }
+    
+    // Get server from post
+    if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){ 
+      $this->selected_server = $_POST['selected_server'];
+    }
+
+    // Get Host
+    if( isset($this->servers[$this->selected_server]['Hosts']) &&  isset($_POST['selected_host']) && 
+        ($_POST['selected_host'] == "all" || 
+         in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
+      $this->selected_host = $_POST['selected_host'];
+    }
+
+    // Get priority from post
+    if( isset($this->servers[$this->selected_server]['Priorities']) &&  isset($_POST['selected_priority']) && 
+        ($_POST['selected_priority'] == "all" || 
+         in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
+      $this->selected_priority = $_POST['selected_priority'];
+    }
+      
+    // Get serach string
+    if(isset($_POST['search_for'])){
+      $this->search_for = trim(get_post('search_for'));
+    } 
 
+    // Get page navigation attributes  
     if(isset($_GET['start'])) $this->page = $_GET['start'];
     if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
       $this->limit = $_POST['limit'];
     }
 
+    // Get sorting attributes 
     if(isset($_GET['sort_value']) && in_array($_GET['sort_value'], 
-          array("DeviceReportedTime","FromHost","Facility","Priority","Message"))){
+          array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
       $sort_value = $_GET['sort_value'];
       if($this->sort_value == $sort_value){
         if($this->sort_type == "ASC"){