Code

Some debugging changes
[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 systemlogs that got recorded with rsyslog";
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();
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   }
155     function logToRss()
156     {
157         $result = array();
158         foreach($this->servers as $name => $server){
159             $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
160             if ($link === FALSE){
161                 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
162                 $this->servers[$name]['status'] = "connectfailed";
163                 $this->servers[$name]['error']  = @mysql_error();
164             }elseif (! @mysql_select_db($server['Database'])){
165                 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
166                 $this->servers[$name]['status'] = "dbmissing";
167                 $this->servers[$name]['error']  = @mysql_error();
168             }else{
169                 $this->servers[$name]['status'] = "ok";
170                 $this->servers[$name]['error']  = "";
171             }
173             if($link){
174                 $query = "SELECT * FROM SystemEvents WHERE Message like '%GOsa%' ORDER BY ID DESC LIMIT 50";
175                 $res = mysql_query($query, $link);
176                 if($res){
177                     while($attrs = mysql_fetch_assoc($res)){
178                         $result[] = $attrs; 
179                     }
180                 }
181             }
182         }
183         $source =
184             '<?xml version="1.0" encoding="utf-8"?>'.
185             ' <rdf:RDF'.
186             '  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'.
187             '  xmlns="http://purl.org/rss/1.0/"'.
188             '  xmlns:dc="http://purl.org/dc/elements/1.1/">'.
189             ' <channel rdf:about="Gosa">'.
190             '  <description>GOsa</description>'.
191             '  <link>gosa-project.org</link>'.
192             '  <title>GOsa</title>'.
193             '  <dc:date>'.date('d.m.Y H:i:s').'</dc:date>'.
194             ' </channel>';
196         foreach($result as $entry){
198             $message = $entry['ReceivedAt']." - ".htmlentities($entry['FromHost'].": ".$entry['Message']);
199             $source .= "
200                 <item>
201                 <title>{$message}</title>
202                 <description>{$message}</description>
203                 <pubDate>2010-10-25T14:27:39Z</pubDate>
204                 </item>";
205         }
206         $source .= "\n</rdf:RDF>";
207         return($source);
208     }
212   function get_result()
213   {
214     $result = array();
215     $result['entries'] = array();
216     $result['status']  = "ok";
217     $result['count']   = 0;
218     $result['error']   = "";
220     // Check whether the selected server exists.
221     if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
222       $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
223       $this->servers["unknown"]["cn"] = "-";
224       return($result);
225     }
227     // Check database connectivity    
228     $name = $this->selected_server;
232     $server = $this->servers[$name];
233     $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
234     if ($link === FALSE){
235       new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
236       $this->servers[$name]['status'] = "connectfailed";
237       $this->servers[$name]['error']  = @mysql_error();
238     }elseif (! @mysql_select_db($server['Database'])){
239       new log("debug","gosa_logging","selectdb",array(),@mysql_error());
240       $this->servers[$name]['status'] = "dbmissing";
241       $this->servers[$name]['error']  = @mysql_error();
242     }else{
243       $this->servers[$name]['status'] = "ok";
244       $this->servers[$name]['error']  = "";
245     }
247     // Get log entries matching the selected filter
248     if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
250       $host = "%%";
251       if($this->selected_host != "all") $host = $this->selected_host;
252       $prio = "%%";
253       if($this->selected_priority != "all") $prio = $this->selected_priority;
255       $filter = "FROM SystemEvents WHERE 1=1 ";
256       if($host != "%%"){
257         $filter.= " AND FromHost like '{$host}' ";
258       }
259       if($prio != "%%"){
260         $filter.= "AND Priority like '{$prio}' ";
261       }
262       $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";     
263       $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";     
265       if(!empty($this->search_for)){
266         $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR  ( Message like '%".$this->search_for."%' ) ) ";
267       }
269       // Detect amount of matching entries 
270       $query = "SELECT count(ID) as `amount` ".$filter;
271       $res = @mysql_query($query, $link);
272       if($res && $attrs = @mysql_fetch_assoc($res)){ 
273         $result['count'] = $attrs['amount'];
274       }else{
275         $this->servers[$name]['status'] = "query_failed";
276         $this->servers[$name]['error']  = @mysql_error();
277       }
279       if($result['count']){
281         if($result['count'] < $this->page) $this->page = 0;
283         $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
284         if($this->limits[$this->limit]!= "-"){
285           $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
286         }else{
287           $this->page = 0;
288         }
290         $query = "SELECT * ".$filter;
291         $res = mysql_query($query, $link);
292         if($res){
293           while($attrs = @mysql_fetch_assoc($res)){
294             $attrs['DeviceReportedTime']= $this->time2local($attrs['DeviceReportedTime']);
295             $attrs['Facility']= $this->facility2string($attrs['Facility']);
296             $attrs['Priority']= $this->severity2string($attrs['Priority']);
297             $attrs['SysLogTag']= preg_replace("/:$/", "", $attrs['SysLogTag']);
299             foreach($attrs as $key => $val){
300               $attrs[$key] = htmlentities($val, ENT_COMPAT, 'UTF-8');
301             }
303             $result['entries'][] = $attrs;
304           }
305           $this->servers[$name]['status'] = "ok";
306           $this->servers[$name]['error']  = "";
307         }else{
308           $this->servers[$name]['status'] = "query_failed";
309           $this->servers[$name]['error']  = @mysql_error();
310         }
311       }
312     }
313     $result['status'] =$this->servers[$name]['status'];
314     $result['error'] =$this->servers[$name]['error'];
315     return($result);
316   }
319   function save_object()
320   {
321     // Get timestamps
322     foreach(array("stopTime","startTime") as $attr){
323       if(isset($_POST[$attr])) $this->$attr = get_post($attr);
324     }
325     
326     // Get server from post
327     if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){ 
328       $this->selected_server = $_POST['selected_server'];
329     }
331     // Get Host
332     if( isset($this->servers[$this->selected_server]['Hosts']) &&  isset($_POST['selected_host']) && 
333         ($_POST['selected_host'] == "all" || 
334          in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
335       $this->selected_host = $_POST['selected_host'];
336     }
338     // Get priority from post
339     if( isset($this->servers[$this->selected_server]['Priorities']) &&  isset($_POST['selected_priority']) && 
340         ($_POST['selected_priority'] == "all" || 
341          in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
342       $this->selected_priority = $_POST['selected_priority'];
343     }
344       
345     // Get serach string
346     if(isset($_POST['search_for'])){
347       $this->search_for = trim(get_post('search_for'));
348     } 
350     // Get page navigation attributes  
351     if(isset($_GET['start'])) $this->page = $_GET['start'];
352     if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
353       $this->limit = $_POST['limit'];
354     }
356     // Get sorting attributes 
357     if(isset($_GET['sort_value']) && in_array($_GET['sort_value'], 
358           array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
359       $sort_value = $_GET['sort_value'];
360       if($this->sort_value == $sort_value){
361         if($this->sort_type == "ASC"){
362           $this->sort_type="DESC"; 
363         }else{
364           $this->sort_type="ASC"; 
365         }
366       } 
367       $this->sort_value=$sort_value; 
368     } 
369   }
372   function severity2string($severity)
373   {
374     $map= array( 0 => _("Emergency"),
375                  1 => _("Alert"),
376                  2 => _("Critical"),
377                  3 => _("Error"),
378                  4 => _("Warning"),
379                  5 => _("Notice"),
380                  6 => _("Informational"),
381                  7 => _("Debug") );
383     if (isset($map[$severity])) {
384       return $map[$severity];
385     }
387     return sprintf(_("Unknown (%s)"), $severity);
388   }
391   function facility2string($facility)
392   {
393     $map= array( 0 => _("Kernel"),
394                  1 => _("User level"),
395                  2 => _("Mail system"),
396                  3 => _("System daemon"),
397                  4 => _("Security"),
398                  5 => _("Internal"),
399                  6 => _("Printing"),
400                  7 => _("News"),
401                  8 => _("UUCP"),
402                  9 => _("Cron"),
403                 10 => _("Security"),
404                 11 => _("FTP"),
405                 12 => _("NTP"),
406                 13 => _("Log audit"),
407                 14 => _("Log alert"),
408                 15 => _("Clock"),
409                 16 => "Local0",
410                 17 => "Local1",
411                 18 => "Local2",
412                 19 => "Local3",
413                 20 => "Local4",
414                 21 => "Local5",
415                 22 => "Local6",
416                 23 => "Local7" );
418     if (isset($map[$facility])) {
419       return $map[$facility];
420     }
422     return sprintf(_("Unknown (%s)"), $facility);
423   }
426   function time2local($str)
427   {
428     list($date, $time)= explode(" ", $str);
429     list($y, $m, $d)= explode("-", $date);
430     return "$d.$m.$y $time";
431   }
434 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
435 ?>