Code

Added empty lines
[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");
20   function logview ($config, $dn= NULL)
21   {
22         /* Include config object */
23         $this->config= $config;
25         /* Get global filter config */
26         if (!is_global("logfilter")){
27                 $logfilter= array("time" => "1",
28                                 "log_level" => "!ALL",
29                                 "host" => "!All",
30                                 "regex" => "*");
31                 register_global("logfilter", $logfilter);
32         }
34         /* Get list of hosts and log levels */
35         if (!isset($this->config->data['SERVERS']['LOG'])){
36                 print_red (_("No LOG servers defined!"));
37                 return;
38         }
39         
40         $cfg= $this->config->data['SERVERS']['LOG'];
41         $tmp= set_error_handler('dummy_error_handler');
42         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
43         set_error_handler($tmp);
44         if ($link === FALSE){
45                 return;
46         }
47         if (! @mysql_select_db("gomon")){
48                 print_red(_("Can't select log database for log generation!"));
49                 return;
50         }
52         /* Host list */
53         $query= "SELECT DISTINCT host FROM golog LIMIT 200;";
54         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
55         $result = @mysql_query($query);
56         if ($result === false){
57                 print_red(_("Query for log database failed!"));
58                 return;
59         }
60         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
61                 $this->hostlist[$line['host']]= $line['host'];
62         }
63         $this->hostlist['!All']= _("All");
64         ksort($this->hostlist);
66         /* Log level list */
67         $query= "SELECT DISTINCT log_level FROM golog LIMIT 200;";
68         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
69         $result = @mysql_query($query);
70         if ($result === false){
71                 print_red(_("Query for log database failed!"));
72                 return;
73         }
74         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
75                 $this->loglevellist[$line['log_level']]= $line['log_level'];
76         }
77         $this->loglevellist['!All']= _("All");
78         ksort($this->loglevellist);
80         /* Time interval */
81         $this->tilist= array("0" => _("one hour"), "1" => _("6 hours"),
82                         "2" => _("12 hours"), "3" => _("24 hours"),
83                         "4" => _("2 days"), "5" => _("one week"),
84                         "6" => _("2 weeks"), "7" => _("one month"));
85   }
87   function execute()
88   {
89         /* Call parent execute */
90         plugin::execute();
92         /* Ignore if not defined */
93         if (!isset($this->config->data['SERVERS']['LOG'])){
94                 return;
95         }
97         /* Save data */
98         $logfilter= get_global("logfilter");
99         foreach( array("host", "time", "log_level", "regex") as $type){
100                 if (isset($_POST[$type])){
101                         $logfilter[$type]= $_POST[$type];
102                 }
103         }
104         if ($logfilter['regex'] == ""){
105                 $logfilter['regex']= '%';
106         } else {
107                 $logfilter['regex']= preg_replace('/\*/', '%', $logfilter['regex']);
108         }
109         register_global("logfilter", $logfilter);
111         $smarty= get_smarty();
112         if (isset($_GET['start'])){
113                 $this->start= (int)$_GET['start'];
114         }
116         /* Adapt sorting */
117         if (isset($_GET['sort'])){
118                 if ($this->sort == (int)$_GET['sort']){
119                         if ($this->sort_direction == "down"){
120                                 $this->sort_direction= "up";
121                         } else {
122                                 $this->sort_direction= "down";
123                         }
124                 }
125                 $this->sort= (int)$_GET['sort'];
126                 if ($this->sort < 0 || $this->sort > 3){
127                         $this->sort= 0;
128                 }
129         }
131         /* Query stuff */
132         $res= "";
133         $cfg= $this->config->data['SERVERS']['LOG'];
134         $tmp= set_error_handler('dummy_error_handler');
135         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
136         set_error_handler($tmp);
137         if ($link === FALSE){
138                 print_red(_("Can't connect to log database, no logs can be shown!"));
139         } else {
140                 if (! @mysql_select_db("gomon")){
141                         print_red(_("Can't select log database for log generation!"));
142                 } else {
144                         /* Get number of entries */
145                         $query= "SELECT COUNT(*) FROM golog;";
146                         $result = @mysql_query($query);
147                         $line= mysql_fetch_array($result, MYSQL_ASSOC);
148                         $count= $line['COUNT(*)'];
149                         if ($count > 25){
150                                 $smarty->assign("range_selector", range_selector($count, $this->start, 25));
151                         }
153                         /* Assemble time query */
154                         switch ($logfilter['time']){
155                                 case '0':
156                                         $start= date ("YmdHis", time() - 3600);
157                                         break;
158                                         ;;
159                                 case '1':
160                                         $start= date ("YmdHis", time() - 21600);
161                                         break;
162                                         ;;
163                                 case '2':
164                                         $start= date ("YmdHis", time() - 43200);
165                                         break;
166                                         ;;
167                                 case '3':
168                                         $start= date ("YmdHis", time() - 86400);
169                                         break;
170                                         ;;
171                                 case '4':
172                                         $start= date ("YmdHis", time() - 172800);
173                                         break;
174                                         ;;
175                                 case '5':
176                                         $start= date ("YmdHis", time() - 604800);
177                                         break;
178                                         ;;
179                                 case '6':
180                                         $start= date ("YmdHis", time() - 1209600);
181                                         break;
182                                         ;;
183                                 case '7':
184                                         $start= date ("YmdHis", time() - 2419200);
185                                         break;
186                                         ;;
187                         }
189                         /* Assemble log level query */
190                         if ($logfilter['log_level'] == '!All'){
191                                 $ll= "";
192                         } else {
193                                 $ll= "AND log_level='".$logfilter['log_level']."'";
194                         }
195                         if ($logfilter['host'] == '!All'){
196                                 $hf= "";
197                         } else {
198                                 $hf= "AND host='".$logfilter['host']."'";
199                         }
201                         /* Order setting */
202                         if ($this->sort_direction == "down"){
203                                 $desc= "DESC";
204                         } else {
205                                 $desc= "";
206                         }
208                         $end= date ("YmdHis");
209                         $query= "SELECT * FROM golog WHERE message like '".$logfilter['regex']."' $ll $hf AND time_stamp <= $end AND time_stamp >= $start ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",25;";
210                         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
211                         $result = @mysql_query($query);
212                         if ($result === false){
213                                 print_red(_("Query for log database failed!"));
214                                 return;
215                         }
217                         /* Display results */
218                         $mod= 0;
219                         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
220                                 if ( ($mod++) & 1){
221                                         $col= "background-color: #ECECEC;";
222                                 } else {
223                                         $col= "background-color: #F5F5F5;";
224                                 }
226                                 $res.="<tr style=\"$col\">\n";
227                                 $res.="<td style=\"text-align:center\">
228                                                 <img alt=\"\" src=\"".get_template_path('images/log_'.strtolower($line['log_level'])).".png\" title=\"Log level is '".$line['log_level']."'\"></td><td>".$line['host']."</td>";
229                                 $res.="<td>".$line['time_stamp']."</td><td width=\"100%\">".$line['message']."</td>";
230                                 $res.="</tr>\n";
231                         }
232                         mysql_close($link);
233                 }
234         }
236         /* Show main page */
237         $smarty->assign("search_result", $res);
238         $smarty->assign("plug", "?plug=".validate($_GET['plug']));
239         $smarty->assign("search_image", get_template_path('images/search.png'));
240         $smarty->assign("time_image", get_template_path('images/time.png'));
241         $smarty->assign("server_image", get_template_path('images/server.png'));
242         $smarty->assign("log_image", get_template_path('images/log_warning.png'));
243         $smarty->assign("ruleset_image", get_template_path('images/edit.png'));
244         $smarty->assign("launchimage", get_template_path('images/launch.png'));
247         $smarty->assign("hostlist", $this->hostlist);
249         $smarty->assign("loglevellist", $this->loglevellist);
251         $smarty->assign("tilist", $this->tilist);
252         
253         $logfilter['regex']= preg_replace('/\%/', '*', $logfilter['regex']);
254         foreach( array("host", "log_level", "time", "regex") as $type){
255                 $smarty->assign("$type", $logfilter[$type]);
256         }
257         $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
258                         ".png\" border=0 align=middle>");
259         return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
260   }
264 ?>