Code

Added logging to addons
[gosa.git] / plugins / addons / logview / class_logview.inc
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       @log::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         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
95       }
97       /* check of log database is available */
98       if (! @mysql_select_db("gomon")){
99         print_red(_("Can't select log database for log generation!"));
100         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
101       }
103       /* Get Host list, if still empty */
104       if(count($this->hostlist) == 0){
106         /* Query database and check results */
107         $query= "SELECT DISTINCT host FROM golog LIMIT 200;";
108         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
109         $result = @mysql_query($query);
110         if ($result === false){
111           print_red(_("Query for log database failed!. Requesting host names failed."));
112           return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
113         }
115         /* Add hostnames to list */     
116         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
117           $this->hostlist[$line['host']]= $line['host'];
118         }
119         $this->hostlist['!All']= _("All");
120         ksort($this->hostlist);
121         $smarty->assign("hostlist", $this->hostlist);
122       }
124       /* Get log level list */
125       if(count($this->loglevellist) == 0){
127         /* Try to get all used log level types */
128         $query= "SELECT DISTINCT log_level FROM golog LIMIT 200;";
129         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
130         $result = @mysql_query($query);
131         if ($result === false){
132           print_red(_("Query for log database failed!"));
133           return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
134         }
136         /* Add each etry to log level list */
137         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
138           $this->loglevellist[$line['log_level']]= $line['log_level'];
139         }
140         $this->loglevellist['!All']= _("All");
141         ksort($this->loglevellist);
142         $smarty->assign("loglevellist", $this->loglevellist);
143       }
144     }
146     /* Set list of available time sequences */
147     if(count($this->tilist) == 0){
149       /* Time interval */
150       $this->tilist= array("0" => _("one hour"), "1" => _("6 hours"),
151           "2" => _("12 hours"), "3" => _("24 hours"),
152           "4" => _("2 days"), "5" => _("one week"),
153           "6" => _("2 weeks"), "7" => _("one month"));
154       $smarty->assign("tilist", $this->tilist);
155     }
157     $smarty->assign("regex", $logfilter['regex']);
160     /* Get acls */
161     
162     $tmp_cat_bases = $this->ui->get_module_departments("logview");
163     $all_acls = "";
164     foreach($tmp_cat_bases as $acl_base){
165       $all_acls .= $this->ui->get_permissions($acl_base,"logview/logview");
166     }
167     if(count($tmp_cat_bases) == 0 || !preg_match("/r/",$all_acls)){
168       $res = "<tr>
169         <td colspan=4>
170         "._("You have insufficient permissions to view syslog entries.")."
171         </td>
172         </tr>";
174       /* Show main page */
175       $smarty->assign("range_selector", "");
176       $smarty->assign("search_result", $res);
177       return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
178     }
180     /* Query stuff */
181     $res= "";
182     $cfg    = $this->config->data['SERVERS']['LOG'];
183     $tmp    = set_error_handler('dummy_error_handler');
184     $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
185     set_error_handler($tmp);
187     /* Test connection object && create up query string */
188     if ($link === FALSE){
189       print_red(_("Can't connect to log database, no logs can be shown!"));
190     } else {
191       if (! @mysql_select_db("gomon")){
192         print_red(_("Can't select log database for log generation!"));
193       } else {
195         /* Assemble time query */
196         switch ($logfilter['time']){
197           case '0':
198             $start= date ("YmdHis", time() - 3600);
199             break;
200             ;;
201           case '1':
202             $start= date ("YmdHis", time() - 21600);
203             break;
204             ;;
205           case '2':
206             $start= date ("YmdHis", time() - 43200);
207             break;
208             ;;
209           case '3':
210             $start= date ("YmdHis", time() - 86400);
211             break;
212             ;;
213           case '4':
214             $start= date ("YmdHis", time() - 172800);
215             break;
216             ;;
217           case '5':
218             $start= date ("YmdHis", time() - 604800);
219             break;
220             ;;
221           case '6':
222             $start= date ("YmdHis", time() - 1209600);
223             break;
224             ;;
225           case '7':
226             $start= date ("YmdHis", time() - 2419200);
227             break;
228             ;;
229         }
231         /* Assemble log level query */
232         if ($logfilter['log_level'] == '!All'){
233           $ll= "";
234         } else {
235           $ll= "AND log_level='".$logfilter['log_level']."'";
236         }
237         if ($logfilter['host'] == '!All'){
238           $hf= "";
239         } else {
240           $hf= "AND host='".$logfilter['host']."'";
241         }
243         /* Order setting */
244         if ($this->sort_direction == "down"){
245           $desc= "DESC";
246           $sort_sign = "\\/";
247         } else {
248           $desc= "";
249           $sort_sign="/\\";
250         }
251         $end= date ("YmdHis");
252         $query_base= "  FROM 
253           golog 
254           WHERE 
255           message like '".preg_replace("/\*/","%",$logfilter['regex'])."' 
256           $ll 
257           $hf 
258           AND 
259           time_stamp <= $end AND time_stamp >= $start";
261         /* Get number of entries */
262         $query= "SELECT COUNT(*)".$query_base.";";
263         $result = @mysql_query($query);
264         $line= mysql_fetch_array($result, MYSQL_ASSOC);
265         $count= $line['COUNT(*)'];
266         if ($count > 25){
267           $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
268         } else {
269           $smarty->assign("range_selector", "");
270         }
272         /* Query results */
273         $query= "SELECT *".$query_base." ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",".$this->range.";";
274         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
275         $result = @mysql_query($query);
277         /* Display results */
278         $mod= 0;
280         /* Add entries to result str */
281         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
283           /* Toggle background color */
284           if ( ($mod++) & 1){
285             $col= "background-color: #ECECEC;";
286           } else {
287             $col= "background-color: #F5F5F5;";
288           }
290           $res.=" <tr style=\"$col\">\n";
291           $res.="   <td style=\"text-align:center\">
292             <img alt=\"".$line['log_level']."\" 
293             src=\"".get_template_path('images/log_'.strtolower($line['log_level'])).".png\" 
294             title=\"Log level is '".$line['log_level']."'\">
295             </td>";
296           $res.="   <td>".
297             $line['host']."
298             </td>"; 
299             $res.=" <td>".
300             $line['time_stamp']."
301             </td>";
302           $res .= "<td width=\"100%\">".
303             $line['message']."
304             </td>";
305           $res.=" </tr>\n";
306         }
307         mysql_close($link);
308         $smarty->assign("mode".$this->sort, $sort_sign);
309         $smarty->assign("host", $logfilter['host']);
310         $smarty->assign("log_level", $logfilter['log_level']);
312         $smarty->assign("search_result", $res);
313       }
314     }
316     /* Show main page */
317     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
318   }
321   function save_object()
322   {
323     /* Get submitted range */   
324     if(isset($_POST['EntriesPerPage'])){
325       if(is_numeric($_POST['EntriesPerPage'])){
326         $this->range = $_POST['EntriesPerPage'];
327       }
328     }
330     /* Save data */
331     $logfilter= get_global("logfilter");
333     /* Get actual select boxe values */
334     $logfilter_changed = 0;
335     foreach( array("host", "time", "log_level", "regex") as $type){
336     
337       /* Set new value and test if value has changed */
338       $last[$type] = $logfilter[$type];
339       if (isset($_POST[$type])){
340         $logfilter[$type]= $_POST[$type];
341       }
342   
343       if ($last[$type] != $logfilter[$type]){
344         $logfilter_changed = 1;
345       }
346     }
348     /* Filter regex values */
349     if ($logfilter['regex'] == ""){
350       $logfilter['regex']= '%';
351     } else {
352       $new = preg_replace('/\*\**/', '*', $logfilter['regex']);
353       $logfilter['regex']= $new;
354     }
356     /* Store filter values */
357     register_global("logfilter", $logfilter);
359     /* Set start value */
360     if (isset($_GET['start'])){
361       $this->start= (int)$_GET['start'];
362     }
363   
364     /* Reset page number if filter has changed */
365     if ($logfilter_changed > 0){
366       $this->start= 0;
367     }
369     /* Adapt sorting */
370     if (isset($_GET['sort'])){
371       if ($this->sort == (int)$_GET['sort']){
372         if ($this->sort_direction == "down"){
373           $this->sort_direction= "up";
374         } else {
375           $this->sort_direction= "down";
376         }
377       }
378       $this->sort= (int)$_GET['sort'];
379       if ($this->sort < 0 || $this->sort > 3){
380         $this->sort= 0;
381       }
382     }
383   }
386   /* Return plugin informations for acl handling
387      #FIXME You can only read attributes within this report plugin */
388   function plInfo()
389   {
390     return (array(
391         "plShortName"   => _("Log view"),
392         "plDescription" => _("Log view addon"),
393         "plSelfModify"  => FALSE,
394         "plDepends"     => array(),
395         "plPriority"    => 0,
396         "plSection"     => array("addon"),
397         "plCategory"    => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
399         "plProvidedAcls" => array()
400         ));
401   }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>