Code

22bd6ad1d2f326fd81e32d82ea5638b22d1952ab
[gosa.git] / plugins / addons / logview / class_logview.inc
1 <?php
3 require_once("tabs_log.inc");
4 class logview extends plugin
5 {
6   /* Definitions */
7   var $plHeadline= "System logs";
8   var $plDescription= "This does something";
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 (!is_global("logfilter")){
31       $logfilter= array(
32           "time" => "1",
33           "log_level" => "!All",
34           "host" => "!All",
35           "regex" => "*");
37       register_global("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= get_global("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/search.png'));
59     $smarty->assign("time_image", get_template_path('images/time.png'));
60     $smarty->assign("server_image", get_template_path('images/server.png'));
61     $smarty->assign("log_image", get_template_path('images/log_warning.png'));
62     $smarty->assign("ruleset_image", get_template_path('images/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       print_red (_("No LOG servers defined!"));
80       return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
82     }elseif(!is_callable("mysql_connect")){
83       print_red(_("There is no mysql extension available, please check your php setup."));
84       return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
86     }else{
88       /* Cehck connection informations */       
89       $cfg= $this->config->data['SERVERS']['LOG'];
91       /* Open link to database  and check if it is valid */     
92       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
93       if ($link === FALSE){
94         print_red(_("Can't connect to log database, no logs can be shown!"));
95         new log("debug","logview","",array(),@mysql_error());
96         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
97       }
99       /* check of log database is available */
100       if (! @mysql_select_db("gomon")){
101         print_red(_("Can't select log database for log generation!"));
102         new log("debug","logview","",array(),@mysql_error());
103         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
104       }
106       /* Get Host list, if still empty */
107       if(count($this->hostlist) == 0){
109         /* Query database and check results */
110         $query= "SELECT DISTINCT host FROM golog LIMIT 200;";
111         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
112         $result = @mysql_query($query);
113         if ($result === false){
114           print_red(_("Query for log database failed!. Requesting host names failed."));
115           new log("debug","logview","",array(),@mysql_error());
116           return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
117         }
119         /* Add hostnames to list */     
120         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
121           $this->hostlist[$line['host']]= $line['host'];
122         }
123         $this->hostlist['!All']= _("All");
124         ksort($this->hostlist);
125         $smarty->assign("hostlist", $this->hostlist);
126       }
128       /* Get log level list */
129       if(count($this->loglevellist) == 0){
131         /* Try to get all used log level types */
132         $query= "SELECT DISTINCT log_level FROM golog LIMIT 200;";
133         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
134         $result = @mysql_query($query);
135         if ($result === false){
136           new log("debug","logview","",array(),@mysql_error());
137           print_red(_("Query for log database failed!"));
138           return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
139         }
141         /* Add each etry to log level list */
142         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
143           $this->loglevellist[$line['log_level']]= $line['log_level'];
144         }
145         $this->loglevellist['!All']= _("All");
146         ksort($this->loglevellist);
147         $smarty->assign("loglevellist", $this->loglevellist);
148       }
149     }
151     /* Set list of available time sequences */
152     if(count($this->tilist) == 0){
154       /* Time interval */
155       $this->tilist= array("0" => _("one hour"), "1" => _("6 hours"),
156           "2" => _("12 hours"), "3" => _("24 hours"),
157           "4" => _("2 days"), "5" => _("one week"),
158           "6" => _("2 weeks"), "7" => _("one month"));
159       $smarty->assign("tilist", $this->tilist);
160     }
162     $smarty->assign("regex", $logfilter['regex']);
165     /* Get acls */
166     
167     $tmp_cat_bases = $this->ui->get_module_departments("logview");
168     $all_acls = "";
169     foreach($tmp_cat_bases as $acl_base){
170       $all_acls .= $this->ui->get_permissions($acl_base,"logview/logview");
171     }
172     if(count($tmp_cat_bases) == 0 || !preg_match("/r/",$all_acls)){
173       $res = "<tr>
174         <td colspan=4>
175         "._("You have insufficient permissions to view syslog entries.")."
176         </td>
177         </tr>";
179       /* Show main page */
180       $smarty->assign("range_selector", "");
181       $smarty->assign("search_result", $res);
182       return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
183     }
185     /* Query stuff */
186     $res= "";
187     $cfg    = $this->config->data['SERVERS']['LOG'];
188     $tmp    = set_error_handler('dummy_error_handler');
189     $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
190     set_error_handler($tmp);
192     /* Test connection object && create up query string */
193     if ($link === FALSE){
194       print_red(_("Can't connect to log database, no logs can be shown!"));
195       new log("debug","logview","",array(),@mysql_error());
196     } else {
197       if (! @mysql_select_db("gomon")){
198         print_red(_("Can't select log database for log generation!"));
199         new log("debug","logview","",array(),@mysql_error());
200       } else {
202         /* Assemble time query */
203         switch ($logfilter['time']){
204           case '0':
205             $start= date ("YmdHis", time() - 3600);
206             break;
207             ;;
208           case '1':
209             $start= date ("YmdHis", time() - 21600);
210             break;
211             ;;
212           case '2':
213             $start= date ("YmdHis", time() - 43200);
214             break;
215             ;;
216           case '3':
217             $start= date ("YmdHis", time() - 86400);
218             break;
219             ;;
220           case '4':
221             $start= date ("YmdHis", time() - 172800);
222             break;
223             ;;
224           case '5':
225             $start= date ("YmdHis", time() - 604800);
226             break;
227             ;;
228           case '6':
229             $start= date ("YmdHis", time() - 1209600);
230             break;
231             ;;
232           case '7':
233             $start= date ("YmdHis", time() - 2419200);
234             break;
235             ;;
236         }
238         /* Assemble log level query */
239         if ($logfilter['log_level'] == '!All'){
240           $ll= "";
241         } else {
242           $ll= "AND log_level='".$logfilter['log_level']."'";
243         }
244         if ($logfilter['host'] == '!All'){
245           $hf= "";
246         } else {
247           $hf= "AND host='".$logfilter['host']."'";
248         }
250         /* Order setting */
251         if ($this->sort_direction == "down"){
252           $desc= "DESC";
253           $sort_sign = "\\/";
254         } else {
255           $desc= "";
256           $sort_sign="/\\";
257         }
258         $end= date ("YmdHis");
259         $query_base= "  FROM 
260           golog 
261           WHERE 
262           message like '".preg_replace("/\*/","%",$logfilter['regex'])."' 
263           $ll 
264           $hf 
265           AND 
266           time_stamp <= $end AND time_stamp >= $start";
268         /* Get number of entries */
269         $query= "SELECT COUNT(*)".$query_base.";";
270         $result = @mysql_query($query);
271         if(!$result){
272           new log("debug","logview","",array(),@mysql_error());
273         }
274         $line= mysql_fetch_array($result, MYSQL_ASSOC);
275         $count= $line['COUNT(*)'];
276         if ($count > 25){
277           $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
278         } else {
279           $smarty->assign("range_selector", "");
280         }
282         /* Query results */
283         $query= "SELECT *".$query_base." ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",".$this->range.";";
284         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
285         $result = @mysql_query($query);
286         if(!$result){
287           new log("debug","logview","",array(),@mysql_error());
288         }
289         /* Display results */
290         $mod= 0;
292         /* Add entries to result str */
293         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
295           /* Toggle background color */
296           if ( ($mod++) & 1){
297             $col= "background-color: #ECECEC;";
298           } else {
299             $col= "background-color: #F5F5F5;";
300           }
302           $res.=" <tr style=\"$col\">\n";
303           $res.="   <td style=\"text-align:center\">
304             <img alt=\"".$line['log_level']."\" 
305             src=\"".get_template_path('images/log_'.strtolower($line['log_level'])).".png\" 
306             title=\"Log level is '".$line['log_level']."'\">
307             </td>";
308           $res.="   <td>".
309             $line['host']."
310             </td>"; 
311             $res.=" <td>".
312             $line['time_stamp']."
313             </td>";
314           $res .= "<td width=\"100%\">".
315             $line['message']."
316             </td>";
317           $res.=" </tr>\n";
318         }
319         mysql_close($link);
320         $smarty->assign("mode".$this->sort, $sort_sign);
321         $smarty->assign("host", $logfilter['host']);
322         $smarty->assign("log_level", $logfilter['log_level']);
324         $smarty->assign("search_result", $res);
325       }
326     }
328     /* Show main page */
329     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
330   }
333   function save_object()
334   {
335     /* Get submitted range */   
336     if(isset($_POST['EntriesPerPage'])){
337       if(is_numeric($_POST['EntriesPerPage'])){
338         $this->range = $_POST['EntriesPerPage'];
339       }
340     }
342     /* Save data */
343     $logfilter= get_global("logfilter");
345     /* Get actual select boxe values */
346     $logfilter_changed = 0;
347     foreach( array("host", "time", "log_level", "regex") as $type){
348     
349       /* Set new value and test if value has changed */
350       $last[$type] = $logfilter[$type];
351       if (isset($_POST[$type])){
352         $logfilter[$type]= $_POST[$type];
353       }
354   
355       if ($last[$type] != $logfilter[$type]){
356         $logfilter_changed = 1;
357       }
358     }
360     /* Filter regex values */
361     if ($logfilter['regex'] == ""){
362       $logfilter['regex']= '%';
363     } else {
364       $new = preg_replace('/\*\**/', '*', $logfilter['regex']);
365       $logfilter['regex']= $new;
366     }
368     /* Store filter values */
369     register_global("logfilter", $logfilter);
371     /* Set start value */
372     if (isset($_GET['start'])){
373       $this->start= (int)$_GET['start'];
374     }
375   
376     /* Reset page number if filter has changed */
377     if ($logfilter_changed > 0){
378       $this->start= 0;
379     }
381     /* Adapt sorting */
382     if (isset($_GET['sort'])){
383       if ($this->sort == (int)$_GET['sort']){
384         if ($this->sort_direction == "down"){
385           $this->sort_direction= "up";
386         } else {
387           $this->sort_direction= "down";
388         }
389       }
390       $this->sort= (int)$_GET['sort'];
391       if ($this->sort < 0 || $this->sort > 3){
392         $this->sort= 0;
393       }
394     }
395   }
398   /* Return plugin informations for acl handling
399      #FIXME You can only read attributes within this report plugin */
400   function plInfo()
401   {
402     return (array(
403         "plShortName"   => _("Log view"),
404         "plDescription" => _("Log view addon"),
405         "plSelfModify"  => FALSE,
406         "plDepends"     => array(),
407         "plPriority"    => 0,
408         "plSection"     => array("addon"),
409         "plCategory"    => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
411         "plProvidedAcls" => array()
412         ));
413   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>