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 $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 /* Ignore if not defined */
90 if (!isset($this->config->data['SERVERS']['LOG'])){
91 return;
92 }
94 /* Save data */
95 $logfilter= get_global("logfilter");
96 foreach( array("host", "time", "log_level", "regex") as $type){
97 if (isset($_POST[$type])){
98 $logfilter[$type]= $_POST[$type];
99 }
100 }
101 if ($logfilter['regex'] == ""){
102 $logfilter['regex']= '%';
103 } else {
104 $logfilter['regex']= preg_replace('/\*/', '%', $logfilter['regex']);
105 }
106 register_global("logfilter", $logfilter);
108 $smarty= get_smarty();
109 if (isset($_GET['start'])){
110 $this->start= (int)$_GET['start'];
111 }
113 /* Adapt sorting */
114 if (isset($_GET['sort'])){
115 if ($this->sort == (int)$_GET['sort']){
116 if ($this->sort_direction == "down"){
117 $this->sort_direction= "up";
118 } else {
119 $this->sort_direction= "down";
120 }
121 }
122 $this->sort= (int)$_GET['sort'];
123 if ($this->sort < 0 || $this->sort > 3){
124 $this->sort= 0;
125 }
126 }
128 /* Query stuff */
129 $res= "";
130 $cfg= $this->config->data['SERVERS']['LOG'];
131 $tmp= set_error_handler('dummy_error_handler');
132 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
133 set_error_handler($tmp);
134 if ($link === FALSE){
135 print_red(_("Can't connect to log database, no logs can be shown!"));
136 } else {
137 if (! @mysql_select_db("gomon")){
138 print_red(_("Can't select log database for log generation!"));
139 } else {
141 /* Get number of entries */
142 $query= "SELECT COUNT(*) FROM golog;";
143 $result = @mysql_query($query);
144 $line= mysql_fetch_array($result, MYSQL_ASSOC);
145 $count= $line['COUNT(*)'];
146 if ($count > 25){
147 $smarty->assign("range_selector", range_selector($count, $this->start, 25));
148 }
150 /* Assemble time query */
151 switch ($logfilter['time']){
152 case '0':
153 $start= date ("YmdHis", time() - 3600);
154 break;
155 ;;
156 case '1':
157 $start= date ("YmdHis", time() - 21600);
158 break;
159 ;;
160 case '2':
161 $start= date ("YmdHis", time() - 43200);
162 break;
163 ;;
164 case '3':
165 $start= date ("YmdHis", time() - 86400);
166 break;
167 ;;
168 case '4':
169 $start= date ("YmdHis", time() - 172800);
170 break;
171 ;;
172 case '5':
173 $start= date ("YmdHis", time() - 604800);
174 break;
175 ;;
176 case '6':
177 $start= date ("YmdHis", time() - 1209600);
178 break;
179 ;;
180 case '7':
181 $start= date ("YmdHis", time() - 2419200);
182 break;
183 ;;
184 }
186 /* Assemble log level query */
187 if ($logfilter['log_level'] == '!All'){
188 $ll= "";
189 } else {
190 $ll= "AND log_level='".$logfilter['log_level']."'";
191 }
192 if ($logfilter['host'] == '!All'){
193 $hf= "";
194 } else {
195 $hf= "AND host='".$logfilter['host']."'";
196 }
198 /* Order setting */
199 if ($this->sort_direction == "down"){
200 $desc= "DESC";
201 } else {
202 $desc= "";
203 }
205 $end= date ("YmdHis");
206 $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;";
207 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
208 $result = @mysql_query($query);
209 if ($result === false){
210 print_red(_("Query for log database failed!"));
211 return;
212 }
214 /* Display results */
215 $mod= 0;
216 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
217 if ( ($mod++) & 1){
218 $col= "background-color: #ECECEC;";
219 } else {
220 $col= "background-color: #F5F5F5;";
221 }
223 $res.="<tr style=\"$col\">\n";
224 $res.="<td style=\"text-align:center\">
225 <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>";
226 $res.="<td>".$line['time_stamp']."</td><td width=\"100%\">".$line['message']."</td>";
227 $res.="</tr>\n";
228 }
229 mysql_close($link);
230 }
231 }
233 /* Show main page */
234 $smarty->assign("search_result", $res);
235 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
236 $smarty->assign("search_image", get_template_path('images/search.png'));
237 $smarty->assign("time_image", get_template_path('images/time.png'));
238 $smarty->assign("server_image", get_template_path('images/server.png'));
239 $smarty->assign("log_image", get_template_path('images/log_warning.png'));
240 $smarty->assign("ruleset_image", get_template_path('images/edit.png'));
241 $smarty->assign("launchimage", get_template_path('images/launch.png'));
244 $smarty->assign("hostlist", $this->hostlist);
246 $smarty->assign("loglevellist", $this->loglevellist);
248 $smarty->assign("tilist", $this->tilist);
250 $logfilter['regex']= preg_replace('/\%/', '*', $logfilter['regex']);
251 foreach( array("host", "log_level", "time", "regex") as $type){
252 $smarty->assign("$type", $logfilter[$type]);
253 }
254 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
255 ".png\" border=0 align=middle>");
256 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
257 }
259 }
261 ?>