Detect more conditions for daemons.
[gnucomo.git] / src / gcm_daemon / classes / gnucomo.process_log.php
1 <?PHP
2
3 /**********************************************************************************
4 **  (c) Copyright 2002, Brenno J.S.A.A.F. de Winter, De Winter Information Soltions
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***********************************************************************************/
8
9 function linux_log ()
10 {
11   /* The function linux_log will seperate the logline in several elements. This will
12    * ease the work of recognizing the type of logline. Once this has been detected
13    * the correct module will start using the data for a log_adv-table.
14    * INPUT    : NONE
15    * GLOBALS  : $dbms (database class containing the logline)
16    * OUTPUT   : Status of success ('TRUE' for success and 'FALSE' for failure
17    */
18    
19    global $dbms;
20    global $developrelease;
21    
22    $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
23    $local_logline_array = explode (" ", $local_log_string);
24
25    $service_type = $dbms->db_result_row[3];
26    switch (strtolower($service_type))
27    {
28      case "kernel":
29        //This is a kernel logline now discover which type kernel-record we have
30        
31        //Detect if this is a network-line
32        if (strtolower(substr($local_logline_array[5],0,3)) == "in=")
33        {
34           //this is a networkline call the processing the routines
35           $local_result = linux_kernel_network();
36           return $local_result;
37        }
38        else
39        {
40
41           //This line is a kernel line writing about a device.
42           if (strtolower($local_logline_array[4]) == 'device')
43           {
44              echo $local_log_string;
45
46              $local_result = linux_kernel_device();
47              return $local_result;
48
49           }
50
51           else
52
53           {
54
55              if ($developrelease == 'TRUE')
56
57              {
58
59                 $local_failing_string = "Failing string: ".$dbms->db_result_row[5];
60                 syslog (LOG_INFO, "Unrecognized kernelline:".$local_log_string);
61                 syslog (LOG_INFO, $local_failing_string);
62
63              }
64
65              return "FALSE";
66      
67           }
68        }     
69        break;
70
71      case "anacron":
72         $local_result = linux_daemon();
73         break;
74
75      case "apmd":
76         $local_result = linux_daemon();
77         break;
78
79      case "atd":
80         $local_result = linux_daemon();
81         break;
82        
83      case "crond":
84         $local_result = linux_daemon();
85         break;
86
87      case "httpd":
88         $local_result = linux_daemon();
89         break;
90
91      case "lpd":
92         $local_result = linux_daemon();
93         break;
94
95      case "mysqld":
96         $local_result = linux_daemon();
97         break;
98
99      case "postfix":
100         $local_result = linux_daemon();
101         break;
102
103      case "random":
104         $local_result = linux_daemon();
105         break;
106
107      case "rhnsd":
108         $local_result = linux_daemon();
109         break;
110
111      case "syslog":
112         $local_result = linux_daemon();
113         break;
114
115      case "syslogd":
116         $local_result = linux_daemon();
117         break;
118
119      case "xinetd":
120         $local_result = linux_daemon();
121         break;
122
123      default:
124        break;
125    }
126 }
127
128 function linux_kernel_network() {
129
130    /* This function is able to deal with the output of kernel-network messages
131     * coming from iptables and other similar tools. When elements are found
132     * that cannot be identified a notification will be written to the logbook
133     * for easy expansion of this routine.
134     * INPUT    : NONE
135     * GLOBALS  : $dbms, $dbms_working;
136     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
137     */
138     global $dbms;
139     global $dbms_working;
140
141     $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
142     $local_logline_array = explode (" ", $local_log_string);
143     $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
144     $local_sql_2 = "logid, detailed_table"; //FIELDS
145     $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
146     $local_len   = 0;
147     $local_id    = 0;
148
149     for ($i = 4; $i <= ( count($local_logline_array) - 1); $i++) {
150         //Process each element by exploding this based on the sign: =
151         $local_element = explode("=", $local_logline_array[$i]);
152         switch (strtolower($local_element[0])) {
153
154           case "in":
155             $local_sql_2 .= ", device_in";
156             $local_sql_3 .= ", '".$local_element[1]."'";
157             break;
158
159           case "out":
160             $local_sql_2 .= ", device_out";
161             $local_sql_3 .= ", '".$local_element[1]."'";
162             break;
163
164           case "mac":
165             $local_sql_2 .= ", hw_address";
166             $local_sql_3 .= ", '".$local_element[1]."'";
167             break;
168    
169           case "src":
170             $local_sql_2 .= ", source_ip";
171             $local_sql_3 .= ", '".$local_element[1]."'";
172             break;
173
174          case "dst":
175             $local_sql_2 .= ", destination_ip";
176             $local_sql_3 .= ", '".$local_element[1]."'";
177             break;
178
179          case "len":
180             if ($local_len == 0) { 
181                $local_sql_2 .= ", packet_length";
182                $local_len++;
183             } else {
184                $local_sql_2 .= ", body_len";
185             }
186
187             $local_sql_3 .= ", '".$local_element[1]."'";
188             break;
189
190          case "tos":
191             $local_sql_2 .= ", tos_bit";
192             $local_sql_3 .= ", '".$local_element[1]."'";
193             break;
194
195          case "prec":
196             $local_sql_2 .= ", prec_bit";
197             $local_sql_3 .= ", '".$local_element[1]."'";
198             break;
199
200          case "ttl":
201             $local_sql_2 .= ", ttl";
202             $local_sql_3 .= ", '".$local_element[1]."'";
203             break;
204
205         case "id":
206             
207             if ($local_id == 0) {
208                $local_sql_2 .= ", header_id";
209                $local_sql_3 .= ", '".$local_element[1]."'";
210                $local_id = 1;
211             }
212             break;
213
214         case "proto":
215             $local_sql_2 .= ", protocol";
216             $local_sql_3 .= ", '".$local_element[1]."'";
217             if ($local_element[1] == 'ICMP') {
218                $local_icmp = true;
219             }
220             break;
221
222        case "spt":
223             $local_sql_2 .= ", destination_port";
224             $local_sql_3 .= ", '".$local_element[1]."'";
225             break;
226
227        case "dpt":
228             $local_sql_2 .= ", source_port";
229             $local_sql_3 .= ", '".$local_element[1]."'";
230             break;
231
232        case "window":
233             $local_sql_2 .= ", window";
234             $local_sql_3 .= ", '".$local_element[1]."'";
235             break;
236        
237        case "urgp":
238            $local_sql_2 .= ", urgp";
239            $local_sql_3 .= ", '".$local_element[1]."'";
240            break;
241
242        case "rst":
243            $local_sql_2 .= ", rst";
244            $local_sql_3 .= ", true";
245            break;
246
247        case "syn":
248            $local_sql_2 .= ", syn";
249            $local_sql_3 .= ", true";
250            break;
251
252        case "df":
253           $local_sql_2 .= ", df";
254           $local_sql_3 .= ", true";
255           break;
256
257        case "type":
258            $local_sql_2 .= ", type";
259            $local_sql_3 .= ", '".$local_element[1]."'";
260            break;
261
262        case "code":
263           $local_sql_2 .= ", code";
264           $local_sql_3 .= ", '".$local_element[1]."'";
265           break;
266
267        case "seq":
268           $local_sql_2 .= ", sequence_number";
269           $local_sql_3 .= ", '".$local_element[1]."'";
270           break;      
271
272        case "res":          
273           $local_sql_2 .= ", res";
274           $local_sql_3 .= ", '".$local_element[1]."'";
275           break;
276
277        case "[src":
278           /*This record is different. In ICMP information is sometimes returned on an original packet.
279            * When the brackets are used a second line will be added to the 
280            * log_adv_kernel_network-table. For that reason the processing into the database will be
281            * done here as well. After that a new insert-string will be created.
282            */
283
284           //Enter the data into the database
285           $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";  
286           $dbms_working->query($local_sql);
287
288           $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
289           $local_sql_2 = "logid, detailed_table"; //FIELDS
290           $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
291           $local_len   = 0;
292           $local_id    = 0;
293           break;   
294       default: 
295 /*           $local_element[0];
296            syslog(LOG_INFO, "Unrecognized kernel/network entry: ".$local_element[0]);
297
298 */
299      }
300
301  }
302
303
304  //Now that the data is complete create the SQL-statement
305  $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";
306  $dbms_working->query($local_sql);
307  RETURN "TRUE";
308
309  }
310
311  function linux_kernel_device() {
312    /* This function is able to deal with the output of kernel-network messages
313     * coming from device related processes. Typically networkcard and other
314     * hardware-related data will show-up here
315     * INPUT    : NONE
316     * GLOBALS  : $dbms, $dbms_working
317     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
318     */
319  
320     global $dbms, $dbms_working;
321
322  }
323
324 function linux_daemon() {
325    /* This function is able to deal with the output of kernel-network messages
326     * coming from device related processes. Typically networkcard and other
327     * hardware-related data will show-up here
328     * INPUT    : NONE
329     * GLOBALS  : $dbms, $dbms_working
330     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
331     */
332
333     global $dbms, $dbms_working;
334     
335     $local_log_line = strtolower($dbms->db_result_row[6]);
336
337     //Find a sign of stop
338     //Using the word shutdown
339     $pos = strpos($local_log_line, "shutdown");
340     if ($pos > 0) {
341      $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
342      $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'stop')";
343      $dbms_working->query($local_sql);
344     } else {
345       //Using the word stop 
346       $pos = strpos($local_log_line, "stop");
347       if ($pos > 0) {
348          $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
349          $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'stop')";
350          $dbms_working->query($local_sql);
351       } else {
352       //As the word restart
353       $pos = strpos($local_log_line, "restart");
354       if ($pos > 0) {
355          $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
356          $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'stop')";
357          $dbms_working->query($local_sql);
358
359          $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
360          $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'start')";
361          $dbms_working->query($local_sql);
362       } else {
363         //As the word start this is an else for restart.
364         //If we wouldn't do so restart would also give a positive on start
365         $pos = strpos($local_log_line, "start");
366         if ($pos > 0) {
367           $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
368           $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'start')";
369           $dbms_working->query($local_sql);
370         } else {
371
372           //The word error indicates problems.
373           $pos = strpos($local_log_line, "error");
374           $pos2 = strpost($local_log_line, "crash"); //The word crash is also considered to be an error
375           if ($pos > 0 or $pos2 > 0) {
376           
377              $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
378              $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'error detected')";
379              $dbms_working->query($local_sql);
380              
381              //Quite often an error will be followed with information that the daemon or service ended.
382              $pos = strpos($local_log_line, "abort");
383              if ($pos > 0) {
384                 $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
385                 $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'abort')";
386                 $dbms_working->query($local_sql);
387              } else {
388                $pos = strpos($local_log_line, "ended");
389                if ($pos > 0) {
390                   $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
391                   $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'abort')";
392                   $dbms_working->query($local_sql);
393                } else {
394                  $pos = strpos($local_log_line, "stop");
395                  if ($pos > 0) {
396                     $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
397                     $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'abort')";
398                     $dbms_working->query($local_sql);
399              } else {                                    
400           
401              //For power management there is a charge warning
402              $pos = strpos($local_log_line, "charge");
403              if ($pos > 0) {
404                 $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
405                 $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'Power warning')";
406                 $dbms_working->query($local_sql);
407              } else {                                                   
408           
409           //As the word start this is an else for restart.
410           //If we wouldn't do so restart would also give a positive on start
411           //This can only be done if we ensured nothing else was the case
412           //PLEASE USE THIS AS LATE AS POSSIBLE!!!
413           $pos = strpos($local_log_line, "exiting");
414           if ($pos > 0) {
415              $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
416              $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '".$dbms->db_result_row[3]."', 'start')";
417              $dbms_working->query($local_sql);
418           }
419         }
420       }
421     }
422    }
423   }
424  }
425 }
426 }
427 }
428    return "ok";
429
430  }
431  
432 ?>