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