Code

Added support for master-key encrypted passwords in gosa.conf
[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;
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("time" => "1",
30                                 "log_level" => "!All",
31                                 "host" => "!All",
32                                 "regex" => "*");
33                 register_global("logfilter", $logfilter);
34         }
37   }
39   function execute()
40   {
41         /* Call parent execute */
42         plugin::execute();
44         $logfilter= get_global("logfilter");
45         $smarty= get_smarty();
46         $smarty->assign("search_result", "");
47         $smarty->assign("plug", "?plug=".validate($_GET['plug']));
48         $smarty->assign("search_image", get_template_path('images/search.png'));
49         $smarty->assign("time_image", get_template_path('images/time.png'));
50         $smarty->assign("server_image", get_template_path('images/server.png'));
51         $smarty->assign("log_image", get_template_path('images/log_warning.png'));
52         $smarty->assign("ruleset_image", get_template_path('images/edit.png'));
53         $smarty->assign("launchimage", get_template_path('images/launch.png'));
54         $smarty->assign("hostlist", $this->hostlist);
55         $smarty->assign("loglevellist", $this->loglevellist);
56         $smarty->assign("tilist", $this->tilist);
57         $smarty->assign("mode0", "");
58         $smarty->assign("mode1", "");
59         $smarty->assign("mode2", "");
60         $smarty->assign("mode3", "");
62         $logfilter['regex']= preg_replace('/\%/', '*', $logfilter['regex']);
63         foreach( array("host", "log_level", "time", "regex") as $type){
64                 $smarty->assign("$type", $logfilter[$type]);
65         }
67         if (!isset($this->config->data['SERVERS']['LOG'])){
68                 print_red (_("No LOG servers defined!"));
69                 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
70         }elseif(!is_callable("mysql_connect")){
71                 print_red(_("There is no mysql extension available, please check your php setup."));
72                 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
73         }else{
74                 $cfg= $this->config->data['SERVERS']['LOG'];
75                 
76                 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
77                 if ($link === FALSE){
78                         print_red(_("Can't connect to log database, no logs can be shown!"));
79                         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
80                 }
81                 if (! @mysql_select_db("gomon")){
82                         print_red(_("Can't select log database for log generation!"));
83                         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
84                 }
86                 /* Host list */
87                 $query= "SELECT DISTINCT host FROM golog LIMIT 200;";
88                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
89                 $result = @mysql_query($query);
90                 if ($result === false){
91                         print_red(_("Query for log database failed!"));
92                         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
93                 }
94                 
95                 if(count($this->hostlist) == 0){
96                         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
97                                 $this->hostlist[$line['host']]= $line['host'];
98                         }
99                         $this->hostlist['!All']= _("All");
100                         ksort($this->hostlist);
101                         $smarty->assign("hostlist", $this->hostlist);
102                 }
104                 /* Log level list */
105                 $query= "SELECT DISTINCT log_level FROM golog LIMIT 200;";
106                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
107                 $result = @mysql_query($query);
108                 if ($result === false){
109                         print_red(_("Query for log database failed!"));
110                         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
111                 }
112                 
113                 if(count($this->loglevellist) == 0){
114                         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
115                                 $this->loglevellist[$line['log_level']]= $line['log_level'];
116                         }
117                         $this->loglevellist['!All']= _("All");
118                         ksort($this->loglevellist);
119                         $smarty->assign("loglevellist", $this->loglevellist);
120                 }
121         }
123         if(count($this->tilist) == 0){
124                 /* Time interval */
125                 $this->tilist= array("0" => _("one hour"), "1" => _("6 hours"),
126                                 "2" => _("12 hours"), "3" => _("24 hours"),
127                                 "4" => _("2 days"), "5" => _("one week"),
128                                 "6" => _("2 weeks"), "7" => _("one month"));
129                 $smarty->assign("tilist", $this->tilist);
130         }
131         
132         if(isset($_POST['EntriesPerPage'])){
133                 $this->range = $_POST['EntriesPerPage'];
134         }
136         /* Save data */
137         $logfilter= get_global("logfilter");
138         $logfilter_changed = 0;
139         foreach( array("host", "time", "log_level", "regex") as $type){
140                 $last[$type] = $logfilter[$type];
141                 if (isset($_POST[$type])){
142                         $logfilter[$type]= $_POST[$type];
143                 }
144                 if ($last[$type] != $logfilter[$type]){
145                         $logfilter_changed = 1;
146                 }
147         }
148         $smarty->assign("regex", $logfilter['regex']);
149         if ($logfilter['regex'] == ""){
150                 $logfilter['regex']= '%';
151         } else {
152                 $logfilter['regex']= preg_replace('/\*/', '%', $logfilter['regex']);
153         }
154         register_global("logfilter", $logfilter);
156         if (isset($_GET['start'])){
157                 $this->start= (int)$_GET['start'];
158         }
159         if ($logfilter_changed > 0){
160                 $this->start= 0;
161         }
163         /* Adapt sorting */
164         if (isset($_GET['sort'])){
165                 if ($this->sort == (int)$_GET['sort']){
166                         if ($this->sort_direction == "down"){
167                                 $this->sort_direction= "up";
168                         } else {
169                                 $this->sort_direction= "down";
170                         }
171                 }
172                 $this->sort= (int)$_GET['sort'];
173                 if ($this->sort < 0 || $this->sort > 3){
174                         $this->sort= 0;
175                 }
176         }
178         /* Query stuff */
179         $res= "";
180         $cfg= $this->config->data['SERVERS']['LOG'];
181         $tmp= set_error_handler('dummy_error_handler');
182         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
183         set_error_handler($tmp);
184         if ($link === FALSE){
185                 print_red(_("Can't connect to log database, no logs can be shown!"));
186         } else {
187                 if (! @mysql_select_db("gomon")){
188                         print_red(_("Can't select log database for log generation!"));
189                 } else {
191                         /* Assemble time query */
192                         switch ($logfilter['time']){
193                                 case '0':
194                                         $start= date ("YmdHis", time() - 3600);
195                                         break;
196                                         ;;
197                                 case '1':
198                                         $start= date ("YmdHis", time() - 21600);
199                                         break;
200                                         ;;
201                                 case '2':
202                                         $start= date ("YmdHis", time() - 43200);
203                                         break;
204                                         ;;
205                                 case '3':
206                                         $start= date ("YmdHis", time() - 86400);
207                                         break;
208                                         ;;
209                                 case '4':
210                                         $start= date ("YmdHis", time() - 172800);
211                                         break;
212                                         ;;
213                                 case '5':
214                                         $start= date ("YmdHis", time() - 604800);
215                                         break;
216                                         ;;
217                                 case '6':
218                                         $start= date ("YmdHis", time() - 1209600);
219                                         break;
220                                         ;;
221                                 case '7':
222                                         $start= date ("YmdHis", time() - 2419200);
223                                         break;
224                                         ;;
225                         }
227                         /* Assemble log level query */
228                         if ($logfilter['log_level'] == '!All'){
229                                 $ll= "";
230                         } else {
231                                 $ll= "AND log_level='".$logfilter['log_level']."'";
232                         }
233                         $smarty->assign("log_level", $logfilter['log_level']);
234                         if ($logfilter['host'] == '!All'){
235                                 $hf= "";
236                         } else {
237                                 $hf= "AND host='".$logfilter['host']."'";
238                         }
239                         $smarty->assign("host", $logfilter['host']);
241                         /* Order setting */
242                         if ($this->sort_direction == "down"){
243                                 $desc= "DESC";
244                                 $sort_sign = "\\/";
245                         } else {
246                                 $desc= "";
247                                 $sort_sign="/\\";
248                         }
249                         $smarty->assign("mode".$this->sort, $sort_sign);
251                         $query_base= " FROM golog WHERE message like '".$logfilter['regex']."' $ll $hf AND time_stamp >= $start";
253                         /* Get number of entries */
254                         $query= "SELECT COUNT(*)".$query_base.";";
255                         $result = @mysql_query($query);
256                         $line= mysql_fetch_array($result, MYSQL_ASSOC);
257                         $count= $line['COUNT(*)'];
258                         if ($count > 25){
259                                 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
260                         } else {
261                                 $smarty->assign("range_selector", "");
262                         }
264                         /* Query results */
265                         $query= "SELECT *".$query_base." ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",".$this->range.";";
266                         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
267                         $result = @mysql_query($query);
269                         /* Display results */
270                         $mod= 0;
271                         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
272                                 if ( ($mod++) & 1){
273                                         $col= "background-color: #ECECEC;";
274                                 } else {
275                                         $col= "background-color: #F5F5F5;";
276                                 }
278                                 $res.="<tr style=\"$col\">\n";
279                                 $res.="<td style=\"text-align:center\">
280                                         <img alt=\"".$line['log_level']."\" src=\"".get_template_path('images/log_'.strtolower($line['log_level'])).".png\" title=\"Log level is '".$line['log_level']."'\"></td><td>".$line['host']."</td>";
281                                 $res.="<td>".$line['time_stamp']."</td><td width=\"100%\">".$line['message']."</td>";
282                                 $res.="</tr>\n";
283                         }
284                         mysql_close($link);
285                         $smarty->assign("search_result", $res);
286                 }
287         }
288         $logfilter['regex']= preg_replace('/\%/', '*', $logfilter['regex']);
289         foreach( array("host", "log_level", "time", "regex") as $type){
290                 $smarty->assign("$type", $logfilter[$type]);
291         }
293         /* Show main page */
294         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
295   }
299 ?>