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();
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 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){
197 $message = $entry['FromHost'].": ".$entry['Message'];
198 $source .= "
199 <item>
200 <title>{$message}</title>
201 <description>{$message}</description>
202 <pubDate>2010-10-25T14:27:39Z</pubDate>
203 </item>";
204 }
205 $source .= "\n</rdf:RDF>";
206 return($source);
207 }
211 function get_result()
212 {
213 $result = array();
214 $result['entries'] = array();
215 $result['status'] = "ok";
216 $result['count'] = 0;
217 $result['error'] = "";
219 // Check whether the selected server exists.
220 if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
221 $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
222 $this->servers["unknown"]["cn"] = "-";
223 return($result);
224 }
226 // Check database connectivity
227 $name = $this->selected_server;
231 $server = $this->servers[$name];
232 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
233 if ($link === FALSE){
234 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
235 $this->servers[$name]['status'] = "connectfailed";
236 $this->servers[$name]['error'] = @mysql_error();
237 }elseif (! @mysql_select_db($server['Database'])){
238 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
239 $this->servers[$name]['status'] = "dbmissing";
240 $this->servers[$name]['error'] = @mysql_error();
241 }else{
242 $this->servers[$name]['status'] = "ok";
243 $this->servers[$name]['error'] = "";
244 }
246 // Get log entries matching the selected filter
247 if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
249 $host = "%%";
250 if($this->selected_host != "all") $host = $this->selected_host;
251 $prio = "%%";
252 if($this->selected_priority != "all") $prio = $this->selected_priority;
254 $filter = "FROM SystemEvents WHERE 1=1 ";
255 if($host != "%%"){
256 $filter.= " AND FromHost like '{$host}' ";
257 }
258 if($prio != "%%"){
259 $filter.= "AND Priority like '{$prio}' ";
260 }
261 $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";
262 $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";
264 if(!empty($this->search_for)){
265 $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR ( Message like '%".$this->search_for."%' ) ) ";
266 }
268 // Detect amount of matching entries
269 $query = "SELECT count(ID) as `amount` ".$filter;
270 $res = @mysql_query($query, $link);
271 if($res && $attrs = @mysql_fetch_assoc($res)){
272 $result['count'] = $attrs['amount'];
273 }else{
274 $this->servers[$name]['status'] = "query_failed";
275 $this->servers[$name]['error'] = @mysql_error();
276 }
278 if($result['count']){
280 if($result['count'] < $this->page) $this->page = 0;
282 $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
283 if($this->limits[$this->limit]!= "-"){
284 $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
285 }else{
286 $this->page = 0;
287 }
289 $query = "SELECT * ".$filter;
290 $res = mysql_query($query, $link);
291 if($res){
292 while($attrs = @mysql_fetch_assoc($res)){
293 $attrs['DeviceReportedTime']= $this->time2local($attrs['DeviceReportedTime']);
294 $attrs['Facility']= $this->facility2string($attrs['Facility']);
295 $attrs['Priority']= $this->severity2string($attrs['Priority']);
296 $attrs['SysLogTag']= preg_replace("/:$/", "", $attrs['SysLogTag']);
298 foreach($attrs as $key => $val){
299 $attrs[$key] = htmlentities($val, ENT_COMPAT, 'UTF-8');
300 }
302 $result['entries'][] = $attrs;
303 }
304 $this->servers[$name]['status'] = "ok";
305 $this->servers[$name]['error'] = "";
306 }else{
307 $this->servers[$name]['status'] = "query_failed";
308 $this->servers[$name]['error'] = @mysql_error();
309 }
310 }
311 }
312 $result['status'] =$this->servers[$name]['status'];
313 $result['error'] =$this->servers[$name]['error'];
314 return($result);
315 }
318 function save_object()
319 {
320 // Get timestamps
321 foreach(array("stopTime","startTime") as $attr){
322 if(isset($_POST[$attr])) $this->$attr = get_post($attr);
323 }
325 // Get server from post
326 if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){
327 $this->selected_server = $_POST['selected_server'];
328 }
330 // Get Host
331 if( isset($this->servers[$this->selected_server]['Hosts']) && isset($_POST['selected_host']) &&
332 ($_POST['selected_host'] == "all" ||
333 in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
334 $this->selected_host = $_POST['selected_host'];
335 }
337 // Get priority from post
338 if( isset($this->servers[$this->selected_server]['Priorities']) && isset($_POST['selected_priority']) &&
339 ($_POST['selected_priority'] == "all" ||
340 in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
341 $this->selected_priority = $_POST['selected_priority'];
342 }
344 // Get serach string
345 if(isset($_POST['search_for'])){
346 $this->search_for = trim(get_post('search_for'));
347 }
349 // Get page navigation attributes
350 if(isset($_GET['start'])) $this->page = $_GET['start'];
351 if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
352 $this->limit = $_POST['limit'];
353 }
355 // Get sorting attributes
356 if(isset($_GET['sort_value']) && in_array($_GET['sort_value'],
357 array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
358 $sort_value = $_GET['sort_value'];
359 if($this->sort_value == $sort_value){
360 if($this->sort_type == "ASC"){
361 $this->sort_type="DESC";
362 }else{
363 $this->sort_type="ASC";
364 }
365 }
366 $this->sort_value=$sort_value;
367 }
368 }
371 function severity2string($severity)
372 {
373 $map= array( 0 => _("Emergency"),
374 1 => _("Alert"),
375 2 => _("Critical"),
376 3 => _("Error"),
377 4 => _("Warning"),
378 5 => _("Notice"),
379 6 => _("Informational"),
380 7 => _("Debug") );
382 if (isset($map[$severity])) {
383 return $map[$severity];
384 }
386 return sprintf(_("Unknown (%s)"), $severity);
387 }
390 function facility2string($facility)
391 {
392 $map= array( 0 => _("Kernel"),
393 1 => _("User level"),
394 2 => _("Mail system"),
395 3 => _("System daemon"),
396 4 => _("Security"),
397 5 => _("Internal"),
398 6 => _("Printing"),
399 7 => _("News"),
400 8 => _("UUCP"),
401 9 => _("Cron"),
402 10 => _("Security"),
403 11 => _("FTP"),
404 12 => _("NTP"),
405 13 => _("Log audit"),
406 14 => _("Log alert"),
407 15 => _("Clock"),
408 16 => "Local0",
409 17 => "Local1",
410 18 => "Local2",
411 19 => "Local3",
412 20 => "Local4",
413 21 => "Local5",
414 22 => "Local6",
415 23 => "Local7" );
417 if (isset($map[$facility])) {
418 return $map[$facility];
419 }
421 return sprintf(_("Unknown (%s)"), $facility);
422 }
425 function time2local($str)
426 {
427 list($date, $time)= explode(" ", $str);
428 list($y, $m, $d)= explode("-", $date);
429 return "$d.$m.$y $time";
430 }
432 }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>