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