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