Code

Added severity/facility translation
[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/rsyslog','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     $smarty = get_smarty();
104     $smarty->assign("usePrototype", "true");
105     $ui = get_userinfo();
106     
107     if(!isset($this->servers[$this->selected_server]['Hosts']) || !count($this->servers[$this->selected_server]['Hosts'])){
108       $hosts = array('-' => _("-"));
109     }else{
110       $hosts = array('all' => _("All"));
111       $hosts = array_merge($hosts,$this->servers[$this->selected_server]['Hosts']);
112     }
113     if(!isset($this->servers[$this->selected_server]['Priorities']) || !count($this->servers[$this->selected_server]['Priorities'])){
114       $priorities = array('-' => _("-"));
115     }else{
116       $priorities = array('all' => _("All"));
117       foreach($this->servers[$this->selected_server]['Priorities'] as $id => $name){
118         $priorities[$id] = $this->severity2string($name);
119       }
120     }
122     $result =$this->get_result();
124     $smarty->assign("servers",$this->servers);
125     $smarty->assign("hosts",$hosts);
126     $smarty->assign("priorities",$priorities);
127     $smarty->assign("selected_server",$this->selected_server);
128     $smarty->assign("selected_host",$this->selected_host);
129     $smarty->assign("selected_priority",$this->selected_priority);
130     $smarty->assign("search_for",$this->search_for);
131     $smarty->assign("startTime",  $this->startTime);
132     $smarty->assign("stopTime",    $this->stopTime);
133     $smarty->assign("sort_type",    $this->sort_type);
134     $smarty->assign("sort_value",   $this->sort_value);
135     $smarty->assign("limits",   $this->limits);
136     $smarty->assign("limit",    $this->limit);
137     $smarty->assign("page",    $this->page);
138     $smarty->assign("plug_id", $_GET['plug']);
139     $smarty->assign("downimg", "<img src='images/lists/sort-down.png' class='center' alt='\\/' border='0'>");
140     $smarty->assign("upimg", "<img src='images/lists/sort-up.png' class='center' alt='\\/' border='0'>");
141     $smarty->assign("result", $result);
142     $smarty->assign("matches", sprintf(_("%s entries match the filter"), $result["count"]));
143     if($this->limits[$this->limit] != '-'){
144       $smarty->assign("page_sel", range_selector($result['count'],$this->page,$this->limits[$this->limit]));
145     }else{
146       $smarty->assign("page_sel", "");
147     }
149     return($smarty->fetch(get_template_path("rSyslog.tpl", TRUE)));
150   }
152   function get_result()
153   {
154     $result = array();
155     $result['entries'] = array();
156     $result['status']  = "ok";
157     $result['count']   = 0;
158     $result['error']   = "";
160     // Check whether the selected server exists.
161     if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
162       $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
163       $this->servers["unknown"]["cn"] = "-";
164       return($result);
165     }
167     // Check database connectivity    
168     $name = $this->selected_server;
172     $server = $this->servers[$name];
173     $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
174     if ($link === FALSE){
175       new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
176       $this->servers[$name]['status'] = "connectfailed";
177       $this->servers[$name]['error']  = @mysql_error();
178     }elseif (! @mysql_select_db($server['Database'])){
179       new log("debug","gosa_logging","selectdb",array(),@mysql_error());
180       $this->servers[$name]['status'] = "dbmissing";
181       $this->servers[$name]['error']  = @mysql_error();
182     }else{
183       $this->servers[$name]['status'] = "ok";
184       $this->servers[$name]['error']  = "";
185     }
187     // Get log entries matching the selected filter
188     if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
190       $host = "%%";
191       if($this->selected_host != "all") $host = $this->selected_host;
192       $prio = "%%";
193       if($this->selected_priority != "all") $prio = $this->selected_priority;
195       $filter = "FROM SystemEvents WHERE FromHost like '{$host}' ";
196       $filter.= "AND Priority like '{$prio}' ";
197       $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";     
198       $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";     
200       if(!empty($this->search_for)){
201         $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR  ( Message like '%".$this->search_for."%' ) ) ";
202       }
204       // Detect amount of matching entries 
205       $query = "SELECT count(ID) as `amount` ".$filter;
206       $res = @mysql_query($query, $link);
207       if($res && $attrs = @mysql_fetch_assoc($res)){ 
208         $result['count'] = $attrs['amount'];
209       }else{
210         $this->servers[$name]['status'] = "query_failed";
211         $this->servers[$name]['error']  = @mysql_error();
212       }
214       if($result['count']){
216         if($result['count'] < $this->page) $this->page = 0;
218         $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
219         if($this->limits[$this->limit]!= "-"){
220           $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
221         }else{
222           $this->page = 0;
223         }
225         $query = "SELECT * ".$filter;
226         $res = mysql_query($query, $link);
227         if($res){
228           while($attrs = @mysql_fetch_assoc($res)){
229             $attrs['Facility']= $this->facility2string($attrs['Facility']);
230             $attrs['Priority']= $this->severity2string($attrs['Priority']);
231             $result['entries'][] = $attrs;
232           }
233           $this->servers[$name]['status'] = "ok";
234           $this->servers[$name]['error']  = "";
235         }else{
236           $this->servers[$name]['status'] = "query_failed";
237           $this->servers[$name]['error']  = @mysql_error();
238         }
239       }
240     }
241     $result['status'] =$this->servers[$name]['status'];
242     $result['error'] =$this->servers[$name]['error'];
243     return($result);
244   }
247   function save_object()
248   {
249     // Get timestamps
250     foreach(array("stopTime","startTime") as $attr){
251       if(isset($_POST[$attr])) $this->$attr = get_post($attr);
252     }
253     
254     // Get server from post
255     if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){ 
256       $this->selected_server = $_POST['selected_server'];
257     }
259     // Get Host
260     if( isset($this->servers[$this->selected_server]['Hosts']) &&  isset($_POST['selected_host']) && 
261         ($_POST['selected_host'] == "all" || 
262          in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
263       $this->selected_host = $_POST['selected_host'];
264     }
266     // Get priority from post
267     if( isset($this->servers[$this->selected_server]['Priorities']) &&  isset($_POST['selected_priority']) && 
268         ($_POST['selected_priority'] == "all" || 
269          in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
270       $this->selected_priority = $_POST['selected_priority'];
271     }
272       
273     // Get serach string
274     if(isset($_POST['search_for'])){
275       $this->search_for = trim(get_post('search_for'));
276     } 
278     // Get page navigation attributes  
279     if(isset($_GET['start'])) $this->page = $_GET['start'];
280     if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
281       $this->limit = $_POST['limit'];
282     }
284     // Get sorting attributes 
285     if(isset($_GET['sort_value']) && in_array($_GET['sort_value'], 
286           array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
287       $sort_value = $_GET['sort_value'];
288       if($this->sort_value == $sort_value){
289         if($this->sort_type == "ASC"){
290           $this->sort_type="DESC"; 
291         }else{
292           $this->sort_type="ASC"; 
293         }
294       } 
295       $this->sort_value=$sort_value; 
296     } 
297   }
300   function severity2string($severity)
301   {
302     $map= array( 0 => _("Emergency"),
303                  1 => _("Alert"),
304                  2 => _("Critical"),
305                  3 => _("Error"),
306                  4 => _("Warning"),
307                  5 => _("Notice"),
308                  6 => _("Informational"),
309                  7 => _("Debug") );
311     if (isset($map[$severity])) {
312       return $map[$severity];
313     }
315     return sprintf(_("Unknown (%s)"), $severity);
316   }
319   function facility2string($facility)
320   {
321     $map= array( 0 => _("Kernel"),
322                  1 => _("User level"),
323                  2 => _("Mail system"),
324                  3 => _("System daemon"),
325                  4 => _("Security"),
326                  5 => _("Internal"),
327                  6 => _("Printing"),
328                  7 => _("News"),
329                  8 => _("UUCP"),
330                  9 => _("Cron"),
331                 10 => _("Security"),
332                 11 => _("FTP"),
333                 12 => _("NTP"),
334                 13 => _("Log audit"),
335                 14 => _("Log alert"),
336                 15 => _("Clock"),
337                 16 => _("Local0"),
338                 17 => _("Local1"),
339                 18 => _("Local2"),
340                 19 => _("Local3"),
341                 20 => _("Local4"),
342                 21 => _("Local5"),
343                 22 => _("Local6"),
344                 23 => _("Local7") );
346     if (isset($map[$facility])) {
347       return $map[$facility];
348     }
350     return sprintf(_("Unknown (%s)"), $facility);
351   }
356 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
357 ?>