1 <?php
3 class rsyslog extends plugin
4 {
5 var $plHeadline= "System logs";
6 var $plDescription= "View system logs";
8 var $servers = array();
9 var $selected_server = "";
10 var $selected_host = "all";
11 var $selected_priority = "all";
13 var $startTime = "";
14 var $stopTime = "";
16 var $search_for ="";
18 var $sort_value = "DeviceReportedTime";
19 var $sort_type = "DESC";
20 var $limit = "0";
21 var $limits = array(20,50,100,200,500,1000,'-');
22 var $page = 0;
24 function rsyslog (&$config, $dn= NULL)
25 {
26 $this->config= &$config;
27 $this->ui = get_userinfo();
29 $this->startTime = date("d.m.Y", (time() - 7*24*60*60));
30 $this->stopTime = date("d.m.Y", time());
32 // Get list of rsyslog servers
33 $ldap = $this->config->get_ldap_link();
34 $ldap->cd($this->config->current['BASE']);
35 $ldap->search("objectClass=goLogDBServer",array("cn","goLogAdmin","gosaLogDB","goLogPassword"));
36 while($attrs = $ldap->fetch()){
37 if(empty($this->selected_server)) $this->selected_server = $attrs['cn'][0];
38 $s = array('cn' => $attrs['cn'][0], 'dn' => $attrs['dn'], 'Password' => '', 'status' => 'ok');
39 $s['User'] = $attrs['goLogAdmin'][0];
40 $s['Database'] = $attrs['gosaLogDB'][0];
41 $s['Hosts'] = array();
42 $s['Priorities'] = array();
43 $s['ACL'] = "";
44 if(isset($attrs['goLogPassword'])) $s['Password'] = $attrs['goLogPassword'][0];
45 $this->servers[$attrs['cn'][0]] = $s;
46 }
48 // Check for installed mysql extension, if missing abort
49 $this->mysql_extension_installed = is_callable("mysql_connect");
50 if(!$this->mysql_extension_installed) return;
52 // Test server connetivity
53 $ui = get_userinfo();
54 foreach($this->servers as $name => $server)
55 {
56 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
57 if ($link === FALSE){
58 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
59 $this->servers[$name]['status'] = "connectfailed";
60 $this->servers[$name]['error'] = @mysql_error();
61 continue;
62 }elseif (! @mysql_select_db($server['Database'])){
63 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
64 $this->servers[$name]['status'] = "dbmissing";
65 $this->servers[$name]['error'] = @mysql_error();
66 continue;
67 }else{
68 $this->servers[$name]['status'] = "ok";
69 $this->servers[$name]['error'] = "";
71 // Detect ACLs
72 $this->servers[$name]['ACL'] = $ui->get_permissions($server['dn'], 'server/rsyslog','viewEntries');
74 // Get list of Hosts using this log server..
75 if(preg_match('/r/', $this->servers[$name]['ACL'])){
76 $query = "SELECT distinct(FromHost) FROM SystemEvents;";
77 $res = @mysql_query($query, $link);
78 if($res){
79 while($attrs = @mysql_fetch_assoc($res)){
80 $this->servers[$name]['Hosts'][$attrs['FromHost']] = $attrs['FromHost'];
81 }
82 }else{
83 $this->servers[$name]['status'] = "query_failed";
84 $this->servers[$name]['error'] = @mysql_error();
85 }
86 $query = "SELECT distinct(Priority) FROM SystemEvents ORDER BY Priority;";
87 $res = @mysql_query($query, $link);
88 if($res){
89 while($attrs = @mysql_fetch_assoc($res)){
90 $this->servers[$name]['Priorities'][$attrs['Priority']] = $attrs['Priority'];
91 }
92 }else{
93 $this->servers[$name]['status'] = "query_failed";
94 $this->servers[$name]['error'] = @mysql_error();
95 }
96 }
97 }
98 }
99 }
101 function execute()
102 {
103 $smarty = get_smarty();
104 $smarty->assign("usePrototype", "true");
105 $ui = get_userinfo();
107 if(!isset($this->servers[$this->selected_server]['Hosts']) || !count($this->servers[$this->selected_server]['Hosts'])){
108 $hosts = array('-' => _("-"));
109 }else{
110 $hosts = array('all' => _("All"));
111 $hosts = array_merge($hosts,$this->servers[$this->selected_server]['Hosts']);
112 }
113 if(!isset($this->servers[$this->selected_server]['Priorities']) || !count($this->servers[$this->selected_server]['Priorities'])){
114 $priorities = array('-' => _("-"));
115 }else{
116 $priorities = array('all' => _("All"));
117 foreach($this->servers[$this->selected_server]['Priorities'] as $id => $name){
118 $priorities[$id] = $this->severity2string($name);
119 }
120 }
122 $result =$this->get_result();
124 $smarty->assign("servers",$this->servers);
125 $smarty->assign("hosts",$hosts);
126 $smarty->assign("priorities",$priorities);
127 $smarty->assign("selected_server",$this->selected_server);
128 $smarty->assign("selected_host",$this->selected_host);
129 $smarty->assign("selected_priority",$this->selected_priority);
130 $smarty->assign("search_for",$this->search_for);
131 $smarty->assign("startTime", $this->startTime);
132 $smarty->assign("stopTime", $this->stopTime);
133 $smarty->assign("sort_type", $this->sort_type);
134 $smarty->assign("sort_value", $this->sort_value);
135 $smarty->assign("limits", $this->limits);
136 $smarty->assign("limit", $this->limit);
137 $smarty->assign("page", $this->page);
138 $smarty->assign("plug_id", $_GET['plug']);
139 $smarty->assign("downimg", "<img src='images/lists/sort-down.png' class='center' alt='\\/' border='0'>");
140 $smarty->assign("upimg", "<img src='images/lists/sort-up.png' class='center' alt='\\/' border='0'>");
141 $smarty->assign("result", $result);
142 $smarty->assign("matches", sprintf(_("%s entries match the filter"), $result["count"]));
143 if($this->limits[$this->limit] != '-'){
144 $smarty->assign("page_sel", range_selector($result['count'],$this->page,$this->limits[$this->limit]));
145 }else{
146 $smarty->assign("page_sel", "");
147 }
149 return($smarty->fetch(get_template_path("rSyslog.tpl", TRUE)));
150 }
152 function get_result()
153 {
154 $result = array();
155 $result['entries'] = array();
156 $result['status'] = "ok";
157 $result['count'] = 0;
158 $result['error'] = "";
160 // Check whether the selected server exists.
161 if($this->selected_server == "unknown" || !isset($this->servers[$this->selected_server])){
162 $this->servers["unknown"]['status'] = sprintf(_("The selected server '%s' does not exists!"),$this->selected_server);
163 $this->servers["unknown"]["cn"] = "-";
164 return($result);
165 }
167 // Check database connectivity
168 $name = $this->selected_server;
172 $server = $this->servers[$name];
173 $link = @mysql_pconnect($server['cn'], $server['User'], $server['Password']);
174 if ($link === FALSE){
175 new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
176 $this->servers[$name]['status'] = "connectfailed";
177 $this->servers[$name]['error'] = @mysql_error();
178 }elseif (! @mysql_select_db($server['Database'])){
179 new log("debug","gosa_logging","selectdb",array(),@mysql_error());
180 $this->servers[$name]['status'] = "dbmissing";
181 $this->servers[$name]['error'] = @mysql_error();
182 }else{
183 $this->servers[$name]['status'] = "ok";
184 $this->servers[$name]['error'] = "";
185 }
187 // Get log entries matching the selected filter
188 if(isset($this->servers[$name]['ACL']) && preg_match('/r/', $this->servers[$name]['ACL'])){
190 $host = "%%";
191 if($this->selected_host != "all") $host = $this->selected_host;
192 $prio = "%%";
193 if($this->selected_priority != "all") $prio = $this->selected_priority;
195 $filter = "FROM SystemEvents WHERE FromHost like '{$host}' ";
196 $filter.= "AND Priority like '{$prio}' ";
197 $filter.= "AND DeviceReportedTime >= '".(date("Y.m.d 00:00:00", strtotime($this->startTime)))."' ";
198 $filter.= "AND DeviceReportedTime <= '".(date("Y.m.d 23:59:59", strtotime($this->stopTime)))."' ";
200 if(!empty($this->search_for)){
201 $filter.= "AND ( ( SysLogTag like '%".$this->search_for."%' ) OR ( Message like '%".$this->search_for."%' ) ) ";
202 }
204 // Detect amount of matching entries
205 $query = "SELECT count(ID) as `amount` ".$filter;
206 $res = @mysql_query($query, $link);
207 if($res && $attrs = @mysql_fetch_assoc($res)){
208 $result['count'] = $attrs['amount'];
209 }else{
210 $this->servers[$name]['status'] = "query_failed";
211 $this->servers[$name]['error'] = @mysql_error();
212 }
214 if($result['count']){
216 if($result['count'] < $this->page) $this->page = 0;
218 $filter.= "ORDER BY ".$this->sort_value." ".$this->sort_type.", DeviceReportedTime ".$this->sort_type." ";
219 if($this->limits[$this->limit]!= "-"){
220 $filter.= "LIMIT ".$this->page.", ".$this->limits[$this->limit];
221 }else{
222 $this->page = 0;
223 }
225 $query = "SELECT * ".$filter;
226 $res = mysql_query($query, $link);
227 if($res){
228 while($attrs = @mysql_fetch_assoc($res)){
229 $attrs['DeviceReportedTime']= $this->time2local($attrs['DeviceReportedTime']);
230 $attrs['Facility']= $this->facility2string($attrs['Facility']);
231 $attrs['Priority']= $this->severity2string($attrs['Priority']);
232 $result['entries'][] = $attrs;
233 }
234 $this->servers[$name]['status'] = "ok";
235 $this->servers[$name]['error'] = "";
236 }else{
237 $this->servers[$name]['status'] = "query_failed";
238 $this->servers[$name]['error'] = @mysql_error();
239 }
240 }
241 }
242 $result['status'] =$this->servers[$name]['status'];
243 $result['error'] =$this->servers[$name]['error'];
244 return($result);
245 }
248 function save_object()
249 {
250 // Get timestamps
251 foreach(array("stopTime","startTime") as $attr){
252 if(isset($_POST[$attr])) $this->$attr = get_post($attr);
253 }
255 // Get server from post
256 if( isset($_POST['selected_server']) && isset($this->servers[$_POST['selected_server']])){
257 $this->selected_server = $_POST['selected_server'];
258 }
260 // Get Host
261 if( isset($this->servers[$this->selected_server]['Hosts']) && isset($_POST['selected_host']) &&
262 ($_POST['selected_host'] == "all" ||
263 in_array($_POST['selected_host'],$this->servers[$this->selected_server]['Hosts']))){
264 $this->selected_host = $_POST['selected_host'];
265 }
267 // Get priority from post
268 if( isset($this->servers[$this->selected_server]['Priorities']) && isset($_POST['selected_priority']) &&
269 ($_POST['selected_priority'] == "all" ||
270 in_array($_POST['selected_priority'],$this->servers[$this->selected_server]['Priorities']))){
271 $this->selected_priority = $_POST['selected_priority'];
272 }
274 // Get serach string
275 if(isset($_POST['search_for'])){
276 $this->search_for = trim(get_post('search_for'));
277 }
279 // Get page navigation attributes
280 if(isset($_GET['start'])) $this->page = $_GET['start'];
281 if(isset($_POST['limit']) && isset($this->limits[$_POST['limit']])){
282 $this->limit = $_POST['limit'];
283 }
285 // Get sorting attributes
286 if(isset($_GET['sort_value']) && in_array($_GET['sort_value'],
287 array("DeviceReportedTime","FromHost","Facility","Priority","Message","SysLogTag"))){
288 $sort_value = $_GET['sort_value'];
289 if($this->sort_value == $sort_value){
290 if($this->sort_type == "ASC"){
291 $this->sort_type="DESC";
292 }else{
293 $this->sort_type="ASC";
294 }
295 }
296 $this->sort_value=$sort_value;
297 }
298 }
301 function severity2string($severity)
302 {
303 $map= array( 0 => _("Emergency"),
304 1 => _("Alert"),
305 2 => _("Critical"),
306 3 => _("Error"),
307 4 => _("Warning"),
308 5 => _("Notice"),
309 6 => _("Informational"),
310 7 => _("Debug") );
312 if (isset($map[$severity])) {
313 return $map[$severity];
314 }
316 return sprintf(_("Unknown (%s)"), $severity);
317 }
320 function facility2string($facility)
321 {
322 $map= array( 0 => _("Kernel"),
323 1 => _("User level"),
324 2 => _("Mail system"),
325 3 => _("System daemon"),
326 4 => _("Security"),
327 5 => _("Internal"),
328 6 => _("Printing"),
329 7 => _("News"),
330 8 => _("UUCP"),
331 9 => _("Cron"),
332 10 => _("Security"),
333 11 => _("FTP"),
334 12 => _("NTP"),
335 13 => _("Log audit"),
336 14 => _("Log alert"),
337 15 => _("Clock"),
338 16 => "Local0",
339 17 => "Local1",
340 18 => "Local2",
341 19 => "Local3",
342 20 => "Local4",
343 21 => "Local5",
344 22 => "Local6",
345 23 => "Local7" );
347 if (isset($map[$facility])) {
348 return $map[$facility];
349 }
351 return sprintf(_("Unknown (%s)"), $facility);
352 }
355 function time2local($str)
356 {
357 list($date, $time)= split(" ", $str);
358 list($y, $m, $d)= split("-", $date);
359 return "$d.$m.$y $time";
360 }
362 }
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>