1 <?php
3 class rsyslog extends plugin
4 {
5 var $plHeadline= "System logs";
6 var $plDescription= "View recorded systemlogs";
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->initTime = microtime(TRUE);
28 // Create statistic table entry
29 stats::log('plugin', $class = get_class($this), $category = array($this->acl_category), $action = 'open',
30 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
33 $this->config= &$config;
34 $this->ui = get_userinfo();
36 $this->startTime = date("d.m.Y", (time() - 7*24*60*60));
37 $this->stopTime = date("d.m.Y", time());
39 // Get list of rsyslog servers
40 $ldap = $this->config->get_ldap_link();
41 $ldap->cd($this->config->current['BASE']);
42 $ldap->search("objectClass=goLogDBServer",array("cn","goLogAdmin","gosaLogDB","goLogPassword"));
43 while($attrs = $ldap->fetch()){
44 if(empty($this->selected_server)) $this->selected_server = $attrs['cn'][0];
45 $s = array('cn' => $attrs['cn'][0], 'dn' => $attrs['dn'], 'Password' => '', 'status' => 'ok');
46 $s['User'] = $attrs['goLogAdmin'][0];
47 $s['Database'] = $attrs['gosaLogDB'][0];
48 $s['Hosts'] = array();
49 $s['Priorities'] = array();
50 $s['ACL'] = "";
51 if(isset($attrs['goLogPassword'])) $s['Password'] = $attrs['goLogPassword'][0];
52 $this->servers[$attrs['cn'][0]] = $s;
53 }
55 // Check for installed mysql extension, if missing abort
56 $this->mysql_extension_installed = is_callable("mysql_connect");
57 if(!$this->mysql_extension_installed) return;
59 // Test server connetivity
60 $ui = get_userinfo();
61 foreach($this->servers as $name => $server)
62 {
63 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
64 if ($link === FALSE){
65 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
66 $this->servers[$name]['status'] = "connectfailed";
67 $this->servers[$name]['error'] = @mysql_error();
68 continue;
69 }elseif (! @mysql_select_db($server['Database'])){
70 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
71 $this->servers[$name]['status'] = "dbmissing";
72 $this->servers[$name]['error'] = @mysql_error();
73 continue;
74 }else{
75 $this->servers[$name]['status'] = "ok";
76 $this->servers[$name]['error'] = "";
78 // Detect ACLs
79 $this->servers[$name]['ACL'] = $ui->get_permissions($server['dn'], 'server/rSyslogServer','viewEntries');
81 // Get list of Hosts using this log server..
82 if(preg_match('/r/', $this->servers[$name]['ACL'])){
83 $query = "SELECT distinct(FromHost) FROM SystemEvents;";
84 $res = @mysql_query($query, $link);
85 if($res){
86 while($attrs = @mysql_fetch_assoc($res)){
87 $this->servers[$name]['Hosts'][$attrs['FromHost']] = $attrs['FromHost'];
88 }
89 }else{
90 $this->servers[$name]['status'] = "query_failed";
91 $this->servers[$name]['error'] = @mysql_error();
92 }
93 $query = "SELECT distinct(Priority) FROM SystemEvents ORDER BY Priority;";
94 $res = @mysql_query($query, $link);
95 if($res){
96 while($attrs = @mysql_fetch_assoc($res)){
97 $this->servers[$name]['Priorities'][$attrs['Priority']] = $attrs['Priority'];
98 }
99 }else{
100 $this->servers[$name]['status'] = "query_failed";
101 $this->servers[$name]['error'] = @mysql_error();
102 }
103 }
104 }
105 }
106 }
108 function execute()
109 {
110 plugin::execute();
112 $smarty = get_smarty();
114 $ui = get_userinfo();
116 if(!isset($this->servers[$this->selected_server]['Hosts']) || !count($this->servers[$this->selected_server]['Hosts'])){
117 $hosts = array('-' => _("-"));
118 }else{
119 $hosts = array('all' => _("All"));
120 $hosts = array_merge($hosts,$this->servers[$this->selected_server]['Hosts']);
121 }
122 if(!isset($this->servers[$this->selected_server]['Priorities']) || !count($this->servers[$this->selected_server]['Priorities'])){
123 $priorities = array('-' => _("-"));
124 }else{
125 $priorities = array('all' => _("All"));
126 foreach($this->servers[$this->selected_server]['Priorities'] as $id => $name){
127 $priorities[$id] = $this->severity2string($name);
128 }
129 }
131 $result =$this->get_result();
133 $smarty->assign("servers", set_post($this->servers));
134 $smarty->assign("hosts", set_post($hosts));
135 $smarty->assign("priorities", set_post($priorities));
136 $smarty->assign("selected_server", set_post($this->selected_server));
137 $smarty->assign("selected_host", set_post($this->selected_host));
138 $smarty->assign("selected_priority",set_post($this->selected_priority));
139 $smarty->assign("search_for", set_post($this->search_for));
140 $smarty->assign("startTime", set_post($this->startTime));
141 $smarty->assign("stopTime", set_post($this->stopTime));
142 $smarty->assign("sort_type", set_post($this->sort_type));
143 $smarty->assign("sort_value", set_post($this->sort_value));
144 $smarty->assign("limits", set_post($this->limits));
145 $smarty->assign("limit", set_post($this->limit));
146 $smarty->assign("page", set_post($this->page));
147 $smarty->assign("plug_id", set_post($_GET['plug']));
148 $smarty->assign("downimg", image('images/lists/sort-down.png'));
149 $smarty->assign("upimg", image('images/lists/sort-up.png'));
150 $smarty->assign("result", $result);
151 $smarty->assign("matches", sprintf(_("%s entries match the filter"), $result["count"]));
152 if($this->limits[$this->limit] != '-'){
153 $smarty->assign("page_sel", range_selector($result['count'],$this->page,$this->limits[$this->limit]));
154 }else{
155 $smarty->assign("page_sel", "");
156 }
158 return($smarty->fetch(get_template_path("rSyslog.tpl", TRUE)));
159 }
162 function logToRss()
163 {
164 $result = array();
165 foreach($this->servers as $name => $server){
166 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
167 if ($link === FALSE){
168 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
169 $this->servers[$name]['status'] = "connectfailed";
170 $this->servers[$name]['error'] = @mysql_error();
171 }elseif (! @mysql_select_db($server['Database'])){
172 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
173 $this->servers[$name]['status'] = "dbmissing";
174 $this->servers[$name]['error'] = @mysql_error();
175 }else{
176 $this->servers[$name]['status'] = "ok";
177 $this->servers[$name]['error'] = "";
178 }
180 if($link){
181 $query = "SELECT * FROM SystemEvents WHERE Message like '%GOsa%' ORDER BY ID DESC LIMIT 50";
182 $res = mysql_query($query, $link);
183 if($res){
184 while($attrs = mysql_fetch_assoc($res)){
185 $result[] = $attrs;
186 }
187 }
188 }
189 }
190 $source =
191 '<?xml version="1.0" encoding="utf-8"?>'.
192 ' <rdf:RDF'.
193 ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'.
194 ' xmlns="http://purl.org/rss/1.0/"'.
195 ' xmlns:dc="http://purl.org/dc/elements/1.1/">'.
196 ' <channel rdf:about="Gosa">'.
197 ' <description>GOsa</description>'.
198 ' <link>gosa-project.org</link>'.
199 ' <title>GOsa</title>'.
200 ' <dc:date>'.date('d.m.Y H:i:s').'</dc:date>'.
201 ' </channel>';
203 foreach($result as $entry){
205 $message = $entry['ReceivedAt']." - ".htmlentities($entry['FromHost'].": ".$entry['Message']);
206 $source .= "
207 <item>
208 <title>{$message}</title>
209 <description>{$message}</description>
210 <pubDate>2010-10-25T14:27:39Z</pubDate>
211 </item>";
212 }
213 $source .= "\n</rdf:RDF>";
214 return($source);
215 }
219 function get_result()
220 {
221 $result = array();
222 $result['entries'] = array();
223 $result['status'] = "ok";
224 $result['count'] = 0;
225 $result['error'] = "";
227 // Check whether the selected server exists.
228 if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
229 $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
230 $this->servers["unknown"]["cn"] = "-";
231 return($result);
232 }
234 // Check database connectivity
235 $name = $this->selected_server;
239 $server = $this->servers[$name];
240 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
241 if ($link === FALSE){
242 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
243 $this->servers[$name]['status'] = "connectfailed";
244 $this->servers[$name]['error'] = @mysql_error();
245 }elseif (! @mysql_select_db($server['Database'])){
246 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
247 $this->servers[$name]['status'] = "dbmissing";
248 $this->servers[$name]['error'] = @mysql_error();
249 }else{
250 $this->servers[$name]['status'] = "ok";
251 $this->servers[$name]['error'] = "";
252 }
254 // Get log entries matching the selected filter
255 if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
257 $host = "%%";
258 if($this->selected_host != "all") $host = $this->selected_host;
259 $prio = "%%";
260 if($this->selected_priority != "all") $prio = $this->selected_priority;
262 $filter = "FROM SystemEvents WHERE 1=1 ";
263 if($host != "%%"){
264 $filter.= " AND FromHost like '".mysql_real_escape_string($host)."' ";
265 }
266 if($prio != "%%"){
267 $filter.= "AND Priority like '".mysql_real_escape_string($prio)."' ";
268 }
269 $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";
270 $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";
272 if(!empty($this->search_for)){
273 $filter.= "AND ( ( SysLogTag like '%".mysql_real_escape_string($this->search_for).
274 "%' ) OR ( Message like '%".mysql_real_escape_string($this->search_for)."%' ) ) ";
275 }
277 // Detect amount of matching entries
278 $query = "SELECT count(ID) as `amount` ".$filter;
279 $res = @mysql_query($query, $link);
280 if($res && $attrs = @mysql_fetch_assoc($res)){
281 $result['count'] = $attrs['amount'];
282 }else{
283 $this->servers[$name]['status'] = "query_failed";
284 $this->servers[$name]['error'] = @mysql_error();
285 }
287 if($result['count']){
289 if($result['count'] < $this->page) $this->page = 0;
291 $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
292 if($this->limits[$this->limit]!= "-"){
293 $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
294 }else{
295 $this->page = 0;
296 }
298 $query = "SELECT * ".$filter;
299 $res = mysql_query($query, $link);
300 if($res){
301 while($attrs = @mysql_fetch_assoc($res)){
302 $attrs['DeviceReportedTime']= $this->time2local($attrs['DeviceReportedTime']);
303 $attrs['Facility']= $this->facility2string($attrs['Facility']);
304 $attrs['Priority']= $this->severity2string($attrs['Priority']);
305 $attrs['SysLogTag']= preg_replace("/:$/", "", $attrs['SysLogTag']);
307 foreach($attrs as $key => $val){
308 $attrs[$key] = htmlentities($val, ENT_COMPAT, 'UTF-8');
309 }
311 $result['entries'][] = $attrs;
312 }
313 $this->servers[$name]['status'] = "ok";
314 $this->servers[$name]['error'] = "";
315 }else{
316 $this->servers[$name]['status'] = "query_failed";
317 $this->servers[$name]['error'] = @mysql_error();
318 }
319 }
320 }
321 $result['status'] =$this->servers[$name]['status'];
322 $result['error'] =$this->servers[$name]['error'];
323 return($result);
324 }
327 function save_object()
328 {
329 // Get timestamps
330 foreach(array("stopTime","startTime") as $attr){
331 if(isset($_POST[$attr])) $this->$attr = get_post($attr);
332 }
334 // Get server from post
335 if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){
336 $this->selected_server = get_post('selected_server');
337 }
339 // Get Host
340 if( isset($this->servers[$this->selected_server]['Hosts']) && isset($_POST['selected_host']) &&
341 ($_POST['selected_host'] == "all" ||
342 in_array_strict($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
343 $this->selected_host = get_post('selected_host');
344 }
346 // Get priority from post
347 if( isset($this->servers[$this->selected_server]['Priorities']) && isset($_POST['selected_priority']) &&
348 ($_POST['selected_priority'] == "all" ||
349 in_array_strict($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
350 $this->selected_priority = get_post('selected_priority');
351 }
353 // Get serach string
354 if(isset($_POST['search_for'])){
355 $this->search_for = trim(get_post('search_for'));
356 }
358 // Get page navigation attributes
359 if(isset($_GET['start'])) $this->page = $_GET['start'];
360 if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
361 $this->limit = get_post('limit');
362 }
364 // Get sorting attributes
365 if(isset($_GET['sort_value']) && in_array_strict($_GET['sort_value'],
366 array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
367 $sort_value = $_GET['sort_value'];
368 if($this->sort_value == $sort_value){
369 if($this->sort_type == "ASC"){
370 $this->sort_type="DESC";
371 }else{
372 $this->sort_type="ASC";
373 }
374 }
375 $this->sort_value=$sort_value;
376 }
377 }
380 function severity2string($severity)
381 {
382 $map= array( 0 => _("Emergency"),
383 1 => _("Alert"),
384 2 => _("Critical"),
385 3 => _("Error"),
386 4 => _("Warning"),
387 5 => _("Notice"),
388 6 => _("Informational"),
389 7 => _("Debug") );
391 if (isset($map[$severity])) {
392 return $map[$severity];
393 }
395 return sprintf(_("Unknown (%s)"), $severity);
396 }
399 function facility2string($facility)
400 {
401 $map= array( 0 => _("Kernel"),
402 1 => _("User level"),
403 2 => _("Mail system"),
404 3 => _("System daemon"),
405 4 => _("Security"),
406 5 => _("Internal"),
407 6 => _("Printing"),
408 7 => _("News"),
409 8 => _("UUCP"),
410 9 => _("Cron"),
411 10 => _("Security"),
412 11 => _("FTP"),
413 12 => _("NTP"),
414 13 => _("Log audit"),
415 14 => _("Log alert"),
416 15 => _("Clock"),
417 16 => "Local0",
418 17 => "Local1",
419 18 => "Local2",
420 19 => "Local3",
421 20 => "Local4",
422 21 => "Local5",
423 22 => "Local6",
424 23 => "Local7" );
426 if (isset($map[$facility])) {
427 return $map[$facility];
428 }
430 return sprintf(_("Unknown (%s)"), $facility);
431 }
434 function time2local($str)
435 {
436 list($date, $time)= explode(" ", $str);
437 list($y, $m, $d)= explode("-", $date);
438 return "$d.$m.$y $time";
439 }
441 }
442 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
443 ?>