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