Code

Updated rSyslog template
[gosa.git] / gosa-plugins / rsyslog / addons / rsyslog / class_rsyslog.inc
1 <?php
3 class rsyslog extends plugin
4 {
5   var $plHeadline= "System logs";
6   var $plDescription= "View system logs";
8   var $servers = array();
9   var $selected_server = "";
10   var $selected_host = "all";
11   var $selected_priority = "all";
13   var $startTime = "";
14   var $stopTime  = "";
16   var $search_for ="";
18   var $sort_value  = "DeviceReportedTime";
19   var $sort_type  = "DESC";
20   var $limit  = "0";
21   var $limits = array(20,50,100,200,500,1000,'-');
22   var $page   = 0;
24   function rsyslog (&$config, $dn= NULL)
25   {
26     $this->config= &$config;
27     $this->ui = get_userinfo();
29     $this->startTime = date("d.m.Y", (time() - 7*24*60*60));
30     $this->stopTime  = date("d.m.Y", time());
32     // Get list of rsyslog servers 
33     $ldap = $this->config->get_ldap_link();
34     $ldap->cd($this->config->current['BASE']);
35     $ldap->search("objectClass=goLogDBServer",array("cn","goLogAdmin","gosaLogDB","goLogPassword"));
36     while($attrs = $ldap->fetch()){
37       if(empty($this->selected_server)) $this->selected_server = $attrs['cn'][0];
38       $s = array('cn' => $attrs['cn'][0], 'dn' => $attrs['dn'], 'Password' => '', 'status' => 'ok');
39       $s['User'] = $attrs['goLogAdmin'][0]; 
40       $s['Database'] = $attrs['gosaLogDB'][0]; 
41       $s['Hosts'] = array();
42       $s['Priorities'] = array();
43       $s['ACL'] = "";
44       if(isset($attrs['goLogPassword'])) $s['Password'] = $attrs['goLogPassword'][0];
45       $this->servers[$attrs['cn'][0]] = $s;
46     }
48     // Check for installed mysql extension, if missing abort
49     $this->mysql_extension_installed = is_callable("mysql_connect");
50     if(!$this->mysql_extension_installed) return;
52     // Test server connetivity 
53     $ui = get_userinfo();
54     foreach($this->servers as $name => $server)  
55     {
56       $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
57       if ($link === FALSE){
58         new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
59         $this->servers[$name]['status'] = "connectfailed";
60         $this->servers[$name]['error']  = @mysql_error();
61         continue;
62       }elseif (! @mysql_select_db($server['Database'])){
63         new log("debug","gosa_logging","selectdb",array(),@mysql_error());
64         $this->servers[$name]['status'] = "dbmissing";
65         $this->servers[$name]['error']  = @mysql_error();
66         continue;
67       }else{
68         $this->servers[$name]['status'] = "ok";
69         $this->servers[$name]['error']  = "";
71         // Detect ACLs
72         $this->servers[$name]['ACL'] =  $ui->get_permissions($server['dn'], 'server/rSyslogServer','viewEntries');
74         // Get list of Hosts using this log server..
75         if(preg_match('/r/', $this->servers[$name]['ACL'])){
76           $query = "SELECT distinct(FromHost) FROM SystemEvents;";
77           $res = @mysql_query($query, $link);
78           if($res){
79             while($attrs = @mysql_fetch_assoc($res)){
80               $this->servers[$name]['Hosts'][$attrs['FromHost']] = $attrs['FromHost'];  
81             }
82           }else{
83             $this->servers[$name]['status'] = "query_failed";
84             $this->servers[$name]['error']  = @mysql_error();
85           }
86           $query = "SELECT distinct(Priority) FROM SystemEvents ORDER BY Priority;";
87           $res = @mysql_query($query, $link);
88           if($res){
89             while($attrs = @mysql_fetch_assoc($res)){
90               $this->servers[$name]['Priorities'][$attrs['Priority']] = $attrs['Priority'];  
91             }
92           }else{
93             $this->servers[$name]['status'] = "query_failed";
94             $this->servers[$name]['error']  = @mysql_error();
95           }
96         }
97       }
98     }
99   }
101   function execute()
102   {
103     plugin::execute();
105     $smarty = get_smarty();
106     $smarty->assign("usePrototype", "true");
107     $ui = get_userinfo();
108     
109     if(!isset($this->servers[$this->selected_server]['Hosts']) || !count($this->servers[$this->selected_server]['Hosts'])){
110       $hosts = array('-' => _("-"));
111     }else{
112       $hosts = array('all' => _("All"));
113       $hosts = array_merge($hosts,$this->servers[$this->selected_server]['Hosts']);
114     }
115     if(!isset($this->servers[$this->selected_server]['Priorities']) || !count($this->servers[$this->selected_server]['Priorities'])){
116       $priorities = array('-' => _("-"));
117     }else{
118       $priorities = array('all' => _("All"));
119       foreach($this->servers[$this->selected_server]['Priorities'] as $id => $name){
120         $priorities[$id] = $this->severity2string($name);
121       }
122     }
124     $result =$this->get_result();
126     $smarty->assign("servers",$this->servers);
127     $smarty->assign("hosts",$hosts);
128     $smarty->assign("priorities",$priorities);
129     $smarty->assign("selected_server",$this->selected_server);
130     $smarty->assign("selected_host",$this->selected_host);
131     $smarty->assign("selected_priority",$this->selected_priority);
132     $smarty->assign("search_for",$this->search_for);
133     $smarty->assign("startTime",  $this->startTime);
134     $smarty->assign("stopTime",    $this->stopTime);
135     $smarty->assign("sort_type",    $this->sort_type);
136     $smarty->assign("sort_value",   $this->sort_value);
137     $smarty->assign("limits",   $this->limits);
138     $smarty->assign("limit",    $this->limit);
139     $smarty->assign("page",    $this->page);
140     $smarty->assign("plug_id", $_GET['plug']);
141     $smarty->assign("downimg", image('images/lists/sort-down.png'));
142     $smarty->assign("upimg", image('images/lists/sort-up.png'));
143     $smarty->assign("result", $result);
144     $smarty->assign("matches", sprintf(_("%s entries match the filter"), $result["count"]));
145     if($this->limits[$this->limit] != '-'){
146       $smarty->assign("page_sel", range_selector($result['count'],$this->page,$this->limits[$this->limit]));
147     }else{
148       $smarty->assign("page_sel", "");
149     }
151     return($smarty->fetch(get_template_path("rSyslog.tpl", TRUE)));
152   }
154   function get_result()
155   {
156     $result = array();
157     $result['entries'] = array();
158     $result['status']  = "ok";
159     $result['count']   = 0;
160     $result['error']   = "";
162     // Check whether the selected server exists.
163     if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
164       $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
165       $this->servers["unknown"]["cn"] = "-";
166       return($result);
167     }
169     // Check database connectivity    
170     $name = $this->selected_server;
174     $server = $this->servers[$name];
175     $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
176     if ($link === FALSE){
177       new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
178       $this->servers[$name]['status'] = "connectfailed";
179       $this->servers[$name]['error']  = @mysql_error();
180     }elseif (! @mysql_select_db($server['Database'])){
181       new log("debug","gosa_logging","selectdb",array(),@mysql_error());
182       $this->servers[$name]['status'] = "dbmissing";
183       $this->servers[$name]['error']  = @mysql_error();
184     }else{
185       $this->servers[$name]['status'] = "ok";
186       $this->servers[$name]['error']  = "";
187     }
189     // Get log entries matching the selected filter
190     if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
192       $host = "%%";
193       if($this->selected_host != "all") $host = $this->selected_host;
194       $prio = "%%";
195       if($this->selected_priority != "all") $prio = $this->selected_priority;
197       $filter = "FROM SystemEvents WHERE 1=1 ";
198       if($host != "%%"){
199         $filter.= " AND FromHost like '{$host}' ";
200       }
201       if($prio != "%%"){
202         $filter.= "AND Priority like '{$prio}' ";
203       }
204       $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";     
205       $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";     
207       if(!empty($this->search_for)){
208         $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR  ( Message like '%".$this->search_for."%' ) ) ";
209       }
211       // Detect amount of matching entries 
212       $query = "SELECT count(ID) as `amount` ".$filter;
213       $res = @mysql_query($query, $link);
214       if($res && $attrs = @mysql_fetch_assoc($res)){ 
215         $result['count'] = $attrs['amount'];
216       }else{
217         $this->servers[$name]['status'] = "query_failed";
218         $this->servers[$name]['error']  = @mysql_error();
219       }
221       if($result['count']){
223         if($result['count'] < $this->page) $this->page = 0;
225         $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
226         if($this->limits[$this->limit]!= "-"){
227           $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
228         }else{
229           $this->page = 0;
230         }
232         $query = "SELECT * ".$filter;
233         $res = mysql_query($query, $link);
234         if($res){
235           while($attrs = @mysql_fetch_assoc($res)){
236             $attrs['DeviceReportedTime']= $this->time2local($attrs['DeviceReportedTime']);
237             $attrs['Facility']= $this->facility2string($attrs['Facility']);
238             $attrs['Priority']= $this->severity2string($attrs['Priority']);
239             $attrs['SysLogTag']= preg_replace("/:$/", "", $attrs['SysLogTag']);
241             foreach($attrs as $key => $val){
242               $attrs[$key] = htmlentities($val, ENT_COMPAT, 'UTF-8');
243             }
245             $result['entries'][] = $attrs;
246           }
247           $this->servers[$name]['status'] = "ok";
248           $this->servers[$name]['error']  = "";
249         }else{
250           $this->servers[$name]['status'] = "query_failed";
251           $this->servers[$name]['error']  = @mysql_error();
252         }
253       }
254     }
255     $result['status'] =$this->servers[$name]['status'];
256     $result['error'] =$this->servers[$name]['error'];
257     return($result);
258   }
261   function save_object()
262   {
263     // Get timestamps
264     foreach(array("stopTime","startTime") as $attr){
265       if(isset($_POST[$attr])) $this->$attr = get_post($attr);
266     }
267     
268     // Get server from post
269     if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){ 
270       $this->selected_server = $_POST['selected_server'];
271     }
273     // Get Host
274     if( isset($this->servers[$this->selected_server]['Hosts']) &&  isset($_POST['selected_host']) && 
275         ($_POST['selected_host'] == "all" || 
276          in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
277       $this->selected_host = $_POST['selected_host'];
278     }
280     // Get priority from post
281     if( isset($this->servers[$this->selected_server]['Priorities']) &&  isset($_POST['selected_priority']) && 
282         ($_POST['selected_priority'] == "all" || 
283          in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
284       $this->selected_priority = $_POST['selected_priority'];
285     }
286       
287     // Get serach string
288     if(isset($_POST['search_for'])){
289       $this->search_for = trim(get_post('search_for'));
290     } 
292     // Get page navigation attributes  
293     if(isset($_GET['start'])) $this->page = $_GET['start'];
294     if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
295       $this->limit = $_POST['limit'];
296     }
298     // Get sorting attributes 
299     if(isset($_GET['sort_value']) && in_array($_GET['sort_value'], 
300           array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
301       $sort_value = $_GET['sort_value'];
302       if($this->sort_value == $sort_value){
303         if($this->sort_type == "ASC"){
304           $this->sort_type="DESC"; 
305         }else{
306           $this->sort_type="ASC"; 
307         }
308       } 
309       $this->sort_value=$sort_value; 
310     } 
311   }
314   function severity2string($severity)
315   {
316     $map= array( 0 => _("Emergency"),
317                  1 => _("Alert"),
318                  2 => _("Critical"),
319                  3 => _("Error"),
320                  4 => _("Warning"),
321                  5 => _("Notice"),
322                  6 => _("Informational"),
323                  7 => _("Debug") );
325     if (isset($map[$severity])) {
326       return $map[$severity];
327     }
329     return sprintf(_("Unknown (%s)"), $severity);
330   }
333   function facility2string($facility)
334   {
335     $map= array( 0 => _("Kernel"),
336                  1 => _("User level"),
337                  2 => _("Mail system"),
338                  3 => _("System daemon"),
339                  4 => _("Security"),
340                  5 => _("Internal"),
341                  6 => _("Printing"),
342                  7 => _("News"),
343                  8 => _("UUCP"),
344                  9 => _("Cron"),
345                 10 => _("Security"),
346                 11 => _("FTP"),
347                 12 => _("NTP"),
348                 13 => _("Log audit"),
349                 14 => _("Log alert"),
350                 15 => _("Clock"),
351                 16 => "Local0",
352                 17 => "Local1",
353                 18 => "Local2",
354                 19 => "Local3",
355                 20 => "Local4",
356                 21 => "Local5",
357                 22 => "Local6",
358                 23 => "Local7" );
360     if (isset($map[$facility])) {
361       return $map[$facility];
362     }
364     return sprintf(_("Unknown (%s)"), $facility);
365   }
368   function time2local($str)
369   {
370     list($date, $time)= explode(" ", $str);
371     list($y, $m, $d)= explode("-", $date);
372     return "$d.$m.$y $time";
373   }
376 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
377 ?>