75f6ed3d394a1914562a6538b8cc054f38fa1648
1 <?php
3 class logview extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "System logs";
7 var $plDescription= "This does something";
8 var $plIcon = "plugins/log/images/plugin.png";
10 /* attribute list for save action */
11 var $attributes= array();
12 var $objectclasses= array();
13 var $start= 0;
14 var $sort= 2;
15 var $sort_direction= "down";
16 var $hostlist= array();
17 var $loglevellist= array();
18 var $tilist= array();
19 var $fields= array("log_level", "host", "time_stamp", "message");
20 var $last= array("log_level", "host", "time", "regex");
21 var $range = 25;
22 var $view_logged = FALSE;
24 function logview (&$config, $dn= NULL)
25 {
26 /* Include config object */
27 $this->config= &$config;
29 /* Get global filter config */
30 if (!session::is_set("logfilter")){
31 $logfilter= array(
32 "time" => "1",
33 "log_level" => "!All",
34 "host" => "!All",
35 "regex" => "*");
37 session::set("logfilter", $logfilter);
38 }
40 $this->ui = get_userinfo();
41 }
43 function execute()
44 {
45 /* Call parent execute */
46 plugin::execute();
48 /* Log view */
49 if(!$this->view_logged){
50 $this->view_logged = TRUE;
51 new log("view","logview/".get_class($this),$this->dn);
52 }
54 $logfilter= session::get("logfilter");
55 $smarty= get_smarty();
56 $smarty->assign("search_result", "");
57 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
58 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
59 $smarty->assign("time_image", get_template_path('plugins/log/images/time.png'));
60 $smarty->assign("server_image", get_template_path('plugins/systems/images/server.png'));
61 $smarty->assign("log_image", get_template_path('plugins/logview/images/log_warning.png'));
62 $smarty->assign("ruleset_image", get_template_path('images/lists/edit.png'));
63 $smarty->assign("launchimage", get_template_path('images/launch.png'));
64 $smarty->assign("hostlist", $this->hostlist);
65 $smarty->assign("loglevellist", $this->loglevellist);
66 $smarty->assign("tilist", $this->tilist);
67 $smarty->assign("mode0", "");
68 $smarty->assign("mode1", "");
69 $smarty->assign("mode2", "");
70 $smarty->assign("mode3", "");
72 /* Assign select option content */
73 foreach( array("host", "log_level", "time", "regex") as $type){
74 $smarty->assign("$type", $logfilter[$type]);
75 }
77 /* Test connection to log database */
78 if (!isset($this->config->data['SERVERS']['LOG'])){
79 msg_dialog::display(_("Warning"), msgPool::noserver("syslog"), WARNING_DIALOG);
80 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
82 }elseif(!is_callable("mysql_connect")){
83 msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
84 new log("debug","logview","Missing MYSQL extension.");
85 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
87 }else{
89 /* Cehck connection informations */
90 $cfg= $this->config->data['SERVERS']['LOG'];
92 /* Open link to database and check if it is valid */
93 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
94 if ($link === FALSE){
95 msg_dialog::display(_("Error"), msgPool::dbconnect($cfg['SERVER'],@mysql_error(),"Log view"), ERROR_DIALOG);
96 new log("debug","log view","dbconnect",array(),@mysql_error());
97 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
98 }
100 /* check of log database is available */
101 if (! @mysql_select_db($cfg["DB"])){
102 msg_dialog::display(_("Error"), msgPool::dbselect($cfg["DB"],@mysql_error(),"Log view"), ERROR_DIALOG);
103 new log("debug","log view","dbselect",array(),@mysql_error());
104 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
105 }
107 /* Get Host list, if still empty */
108 if(count($this->hostlist) == 0){
110 /* Query database and check results */
111 $query= "SELECT DISTINCT host FROM golog LIMIT 200;";
112 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
113 $result = @mysql_query($query);
114 if ($result === false){
115 msg_dialog::display(_("Error"), msgPool::dbquery("golog",@mysql_error(),"Log view"), ERROR_DIALOG);
116 new log("debug","log view","dbquery",array(),@mysql_error());
117 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
118 }
120 /* Add hostnames to list */
121 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
122 $this->hostlist[$line['host']]= $line['host'];
123 }
124 $this->hostlist['!All']= _("All");
125 ksort($this->hostlist);
126 $smarty->assign("hostlist", $this->hostlist);
127 }
129 /* Get log level list */
130 if(count($this->loglevellist) == 0){
132 /* Try to get all used log level types */
133 $query= "SELECT DISTINCT log_level FROM golog LIMIT 200;";
134 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
135 $result = @mysql_query($query);
136 if ($result === false){
137 msg_dialog::display(_("Error"), msgPool::dbquery("golog",@mysql_error(),"Log view"), ERROR_DIALOG);
138 new log("debug","log view","dbquery",array(),@mysql_error());
139 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
140 }
142 /* Add each etry to log level list */
143 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
144 $this->loglevellist[$line['log_level']]= $line['log_level'];
145 }
146 $this->loglevellist['!All']= _("All");
147 ksort($this->loglevellist);
148 $smarty->assign("loglevellist", $this->loglevellist);
149 }
150 }
152 /* Set list of available time sequences */
153 if(count($this->tilist) == 0){
155 /* Time interval */
156 $this->tilist= array("0" => _("one hour"), "1" => _("6 hours"),
157 "2" => _("12 hours"), "3" => _("24 hours"),
158 "4" => _("2 days"), "5" => _("one week"),
159 "6" => _("2 weeks"), "7" => _("one month"));
160 $smarty->assign("tilist", $this->tilist);
161 }
163 $smarty->assign("regex", $logfilter['regex']);
166 /* Get acls */
168 $tmp_cat_bases = $this->ui->get_module_departments("logview");
169 $all_acls = "";
170 foreach($tmp_cat_bases as $acl_base){
171 $all_acls .= $this->ui->get_permissions($acl_base,"logview/logview");
172 }
173 if(count($tmp_cat_bases) == 0 || !preg_match("/r/",$all_acls)){
174 $res = "<tr>
175 <td colspan=4>
176 ".msgPool::permView()."
177 </td>
178 </tr>";
180 /* Show main page */
181 $smarty->assign("range_selector", "");
182 $smarty->assign("search_result", $res);
183 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
184 }
186 /* Query stuff */
187 $res= "";
188 $cfg = $this->config->data['SERVERS']['LOG'];
189 $tmp = set_error_handler('dummy_error_handler');
190 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
191 set_error_handler($tmp);
193 /* Test connection object && create up query string */
194 if ($link === FALSE){
195 msg_dialog::display(_("Error"), msgPool::dbconnect($cfg['SERVER'],@mysql_error(),"Log view"), ERROR_DIALOG);
196 new log("debug","log view","dbconnect",array(),@mysql_error());
197 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
198 } else {
199 if (! @mysql_select_db($cfg["DB"])){
200 msg_dialog::display(_("Error"), msgPool::dbselect($cfg["DB"],@mysql_error(),"Log view"), ERROR_DIALOG);
201 new log("debug","log view","dbselect",array(),@mysql_error());
202 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
203 } else {
205 @timezone::get_default_timezone();
207 /* Assemble time query */
208 switch ($logfilter['time']){
209 case '0':
210 $start= date ("YmdHis", time() - 3600);
211 break;
212 ;;
213 case '1':
214 $start= date ("YmdHis", time() - 21600);
215 break;
216 ;;
217 case '2':
218 $start= date ("YmdHis", time() - 43200);
219 break;
220 ;;
221 case '3':
222 $start= date ("YmdHis", time() - 86400);
223 break;
224 ;;
225 case '4':
226 $start= date ("YmdHis", time() - 172800);
227 break;
228 ;;
229 case '5':
230 $start= date ("YmdHis", time() - 604800);
231 break;
232 ;;
233 case '6':
234 $start= date ("YmdHis", time() - 1209600);
235 break;
236 ;;
237 case '7':
238 $start= date ("YmdHis", time() - 2419200);
239 break;
240 ;;
241 }
243 /* Assemble log level query */
244 if ($logfilter['log_level'] == '!All'){
245 $ll= "";
246 } else {
247 $ll= "AND log_level='".$logfilter['log_level']."'";
248 }
249 if ($logfilter['host'] == '!All'){
250 $hf= "";
251 } else {
252 $hf= "AND host='".$logfilter['host']."'";
253 }
255 /* Order setting */
256 if ($this->sort_direction == "down"){
257 $desc= "DESC";
258 $sort_sign = "<img src='images/lists/sort-down.png' class='center' alt='\\/' border='0'>";
259 } else {
260 $desc= "";
261 $sort_sign = "<img src='images/lists/sort-up.png' class='center' alt='/\\' border='0'>";
262 }
263 $end= date ("YmdHis");
264 $query_base= " FROM
265 golog
266 WHERE
267 message like '".preg_replace("/\*/","%",$logfilter['regex'])."'
268 $ll
269 $hf
270 AND
271 time_stamp <= $end AND time_stamp >= $start";
273 /* Get number of entries */
274 $query= "SELECT COUNT(*)".$query_base.";";
275 $result = @mysql_query($query);
276 if(!$result){
277 msg_dialog::display(_("Error"), msgPool::dbquery("golog",@mysql_error(),"Log view"), ERROR_DIALOG);
278 new log("debug","log view","dbquery",array(),@mysql_error());
279 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
280 }
281 $line= mysql_fetch_array($result, MYSQL_ASSOC);
282 $count= $line['COUNT(*)'];
283 if ($count > 25){
284 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
285 } else {
286 $smarty->assign("range_selector", "");
287 }
289 /* Query results */
290 $query= "SELECT *".$query_base." ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",".$this->range.";";
291 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
292 $result = @mysql_query($query);
293 if(!$result){
294 msg_dialog::display(_("Error"), msgPool::dbquery("golog",@mysql_error(),"Log view"), ERROR_DIALOG);
295 new log("debug","log view","dbquery",array(),@mysql_error());
296 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
297 }
298 /* Display results */
299 $mod= 0;
301 /* Add entries to result str */
302 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
304 /* Toggle background color */
305 if ( ($mod++) & 1){
306 $col= "background-color: #ECECEC;";
307 } else {
308 $col= "background-color: #F5F5F5;";
309 }
311 $res.=" <tr style=\"$col\">\n";
312 $res.=" <td style=\"text-align:center\">
313 <img alt=\"".$line['log_level']."\"
314 src=\"".get_template_path('images/log_'.strtolower($line['log_level'])).".png\"
315 title=\"Log level is '".$line['log_level']."'\">
316 </td>";
317 $res.=" <td>".
318 $line['host']."
319 </td>";
320 $res.=" <td>".
321 $line['time_stamp']."
322 </td>";
323 $res .= "<td width=\"100%\">".
324 $line['message']."
325 </td>";
326 $res.=" </tr>\n";
327 }
328 mysql_close($link);
329 $smarty->assign("mode".$this->sort, $sort_sign);
330 $smarty->assign("host", $logfilter['host']);
331 $smarty->assign("log_level", $logfilter['log_level']);
333 $smarty->assign("search_result", $res);
334 }
335 }
337 /* Show main page */
338 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
339 }
342 function save_object()
343 {
344 /* Get submitted range */
345 if(isset($_POST['EntriesPerPage'])){
346 if(is_numeric($_POST['EntriesPerPage'])){
347 $this->range = $_POST['EntriesPerPage'];
348 }
349 }
351 /* Save data */
352 $logfilter= session::get("logfilter");
354 /* Get actual select boxe values */
355 $logfilter_changed = 0;
356 foreach( array("host", "time", "log_level", "regex") as $type){
358 /* Set new value and test if value has changed */
359 $last[$type] = $logfilter[$type];
360 if (isset($_POST[$type])){
361 $logfilter[$type]= $_POST[$type];
362 }
364 if ($last[$type] != $logfilter[$type]){
365 $logfilter_changed = 1;
366 }
367 }
369 /* Filter regex values */
370 if ($logfilter['regex'] == ""){
371 $logfilter['regex']= '%';
372 } else {
373 $new = preg_replace('/\*\**/', '*', $logfilter['regex']);
374 $logfilter['regex']= $new;
375 }
377 /* Store filter values */
378 session::set("logfilter", $logfilter);
380 /* Set start value */
381 if (isset($_GET['start'])){
382 $this->start= (int)$_GET['start'];
383 }
385 /* Reset page number if filter has changed */
386 if ($logfilter_changed > 0){
387 $this->start= 0;
388 }
390 /* Adapt sorting */
391 if (isset($_GET['sort'])){
392 if ($this->sort == (int)$_GET['sort']){
393 if ($this->sort_direction == "down"){
394 $this->sort_direction= "up";
395 } else {
396 $this->sort_direction= "down";
397 }
398 }
399 $this->sort= (int)$_GET['sort'];
400 if ($this->sort < 0 || $this->sort > 3){
401 $this->sort= 0;
402 }
403 }
404 }
407 /* Return plugin informations for acl handling
408 #FIXME You can only read attributes within this report plugin */
409 static function plInfo()
410 {
411 return (array(
412 "plShortName" => _("Log view"),
413 "plDescription" => _("Log view addon"),
414 "plSelfModify" => FALSE,
415 "plDepends" => array(),
416 "plPriority" => 0,
417 "plSection" => array("addon"),
418 "plCategory" => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
420 "plProvidedAcls" => array()
421 ));
422 }
423 }
424 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
425 ?>