BUGFIX: undefined variables and indices when processing sendmail logs.
[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          else
50          {
51             if ($developrelease == 'TRUE')
52             {
53
54                $local_failing_string = "Failing string: ".$dbms->db_result_row[5];
55                syslog (LOG_INFO, "Unrecognized kernelline:".$local_log_string);
56                syslog (LOG_INFO, $local_failing_string);
57             }
58             return "FALSE";
59          }
60       }
61       break;
62
63    case "anacron":
64       $local_result = linux_daemon();
65       break;
66
67    case "apmd":
68       $local_result = linux_daemon();
69       break;
70
71    case "atd":
72       $local_result = linux_daemon();
73       break;
74
75    case "crond":
76       $local_result = linux_daemon();
77       break;
78
79    case "httpd":
80       $local_result = linux_daemon();
81       break;
82
83    case "lpd":
84       $local_result = linux_daemon();
85       break;
86
87    case "mysqld":
88       $local_result = linux_daemon();
89       break;
90
91    case "postfix":
92       $local_result = linux_daemon();
93       break;
94
95    case "random":
96       $local_result = linux_daemon();
97       break;
98
99    case "rhnsd":
100       $local_result = linux_daemon();
101       break;
102
103    case "sendmail":
104       $local_result = linux_daemon_sendmail();
105       break;
106
107    case "syslog":
108       $local_result = linux_daemon();
109       break;
110
111    case "syslogd":
112       $local_result = linux_daemon();
113       break;
114
115    case "xinetd":
116       $local_result = linux_daemon();
117       break;
118
119    default:
120       break;
121    }
122 }
123
124 function linux_daemon_sendmail()
125 {
126
127    /* This function is able to deal with the logs delivered by MTAs
128     * the following are currently supported:
129     * - sendmail
130     * INPUT   : NONE
131     * GLOBALS : $dbms, $dbms_working
132     * OUTPUT  : "TRUE" for success and "FALSE" for failure.
133     */
134
135    global $dbms;
136    global $dbms_working;
137
138     //Basic processing.
139
140     //Determine the type of records
141     //When this is sendmail find the beginning by chopping everything into
142     //little pieces.
143     $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
144     $local_logline_array = explode (" ", $local_log_string);
145     $local_sql_1 = "INSERT INTO log_adv_daemon_email"; //BASIC STATEMENT
146     $local_sql_2 = "logid, detailed_table, service, internal_messageid "; //FIELDS
147     $local_sql_3 = "'".$dbms->db_result_row[0]."', 'log_adv_daemon_email', 'sendmail'"; //VALUES
148
149     $local_sql_3 .= ", '".substr (trim($local_logline_array[5]), 0
150                                   ,strlen(trim($local_logline_array[5])) -1)."'";
151     $local_len   = 0;
152     $local_id    = 0;
153
154     for ($i = 6; $i <= ( count($local_logline_array) - 1); $i++)
155     {
156
157         //Get rid of the nasty comma's at the end
158         if ( substr($local_logline_array[$i], strlen($local_logline_array[$i])-1, 1)  == "," )
159         {
160            $local_dummylength = strlen($local_logline_array[$i]) -1;
161            $local_dummy = substr ($local_logline_array[$i], 0,$local_dummylength );
162            $local_logline_array[$i] = trim($local_dummy);
163         }
164
165         if (substr($local_logline_array[$i],0,1) == '[')
166         {
167           $local_dummy = trim($local_logline_array[$i]);
168           $local_sql_2 .= ", source_ip";
169           $local_sql_3 .= ", '".substr($local_dummy, 1, strlen($local_dummy)-2)."'";
170         }
171         else
172         {
173
174            $local_element = explode("=", $local_logline_array[$i]);
175
176            switch (strtolower($local_element[0]))
177            {
178            case "from":
179               $local_sql_2 .= ", from_email";
180               $local_sql_3 .= ", '".$local_element[1]."'";
181               break;
182
183            case "size":
184               $local_sql_2 .= ", size";
185               $local_sql_3 .= ", '".$local_element[1]."'";
186               break;
187
188            case "delay":
189               $local_sql_2 .= ", delay";
190               $local_sql_3 .= ", '".$local_element[1]."'";
191               break;
192
193            case "xdelay":
194               $local_sql_2 .= ", xdelay";
195               $local_sql_3 .= ", '".$local_element[1]."'";
196               break;
197
198            case "mailer":
199               $local_sql_2 .= ", mailer";
200               $local_sql_3 .= ", '".$local_element[1]."'";
201               break;
202
203            case "dsn":
204               $local_sql_2 .= ", dsn";
205               $local_sql_3 .= ", '".$local_element[1]."'";
206               break;
207
208            case "msgid":
209               $local_sql_2 .= ", external_messageid";
210               if (substr($local_element[1],0,1) == '<')
211               {
212                  $local_sql_3 .= ", '";
213                  $local_sql_3 .= substr($local_element[1],1,(strlen($local_element[1])-2));
214                  $local_sql_3 .= "'";
215               }
216               else
217               {
218                  $local_sql_3 .= ", '".$local_element[1]."'";
219               }
220               break;
221
222            //As of this point we only deal with Status
223            case "stat":
224               $local_sql_2 .= ", status";
225               $local_sql_3 .= ", '".$local_element[1]."'";
226
227               $local_pos = strrpos (strtolower($local_logline_array[$i]), "stat=");
228               $local_len = strlen($local_logline_array[$i]) - $local_pos - 6;
229               $local_sql_2 .= ", status_details";
230               $local_sql_3 .= ", '".substr($local_logline_array[$i], $local_pos + 5, $local_len) . "'";
231               break;
232
233            case "status":
234               $local_sql_2 .= ", status";
235               $local_sql_3 .= ", '".$local_element[1]."'";
236
237               $local_pos = strrpos (strtolower($local_logline_array[$i]), "status=");
238               $local_len = strlen($local_logline_array[$i]) - $local_pos - 8;
239               $local_sql_2 .= ", status_details";
240               $local_sql_3 .= ", '".substr($local_logline_array[$i], $local_pos + 7, $local_len) . "'";
241               break;
242
243            default:
244               if (substr(strtolower($local_element[0]),0,1) == "[")
245               {
246                 $local_sql_2 .= ", destination_ip";
247                 $local_sql_3 .= ", '". substr($local_element[1], 1, strlen($local_element[1]) - 2)."'";
248               }
249               break;
250            }
251         }
252     }
253
254     //Now that the data is complete create the SQL-statement
255     $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";
256     $dbms_working->query($local_sql);
257
258     RETURN "TRUE";
259 }
260
261 function linux_kernel_network()
262 {
263
264    /* This function is able to deal with the output of kernel-network messages
265     * coming from iptables and other similar tools. When elements are found
266     * that cannot be identified a notification will be written to the logbook
267     * for easy expansion of this routine.
268     * INPUT    : NONE
269     * GLOBALS  : $dbms, $dbms_working;
270     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
271     */
272
273     global $dbms;
274     global $dbms_working;
275
276     $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
277     $local_logline_array = explode (" ", $local_log_string);
278     $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
279     $local_sql_2 = "logid, detailed_table"; //FIELDS
280     $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
281     $local_len   = 0;
282     $local_id    = 0;
283     $lcoal_tos   = "F";
284
285     for ($i = 4; $i <= ( count($local_logline_array) - 1); $i++)
286     {
287         $local_element = explode("=", $local_logline_array[$i]);
288         switch (strtolower($local_element[0]))
289         {
290           case "in":
291             $local_sql_2 .= ", device_in";
292             $local_sql_3 .= ", '".$local_element[1]."'";
293             break;
294
295           case "out":
296             $local_sql_2 .= ", device_out";
297             $local_sql_3 .= ", '".$local_element[1]."'";
298             break;
299
300           case "mac":
301             $local_sql_2 .= ", hw_address";
302             $local_sql_3 .= ", '".$local_element[1]."'";
303             break;
304
305           case "src":
306             $local_sql_2 .= ", source_ip";
307             $local_sql_3 .= ", '".$local_element[1]."'";
308             break;
309
310          case "dst":
311             $local_sql_2 .= ", destination_ip";
312             $local_sql_3 .= ", '".$local_element[1]."'";
313             break;
314
315          case "len":
316             if ($local_len == 0) {
317                $local_sql_2 .= ", packet_length";
318                $local_len++;
319             } else {
320                $local_sql_2 .= ", body_len";
321             }
322
323             $local_sql_3 .= ", '".$local_element[1]."'";
324             break;
325
326          case "tos":
327             if ($local_tos == "F") {
328                $local_sql_2 .= ", tos_bit";
329                $local_sql_3 .= ", '".$local_element[1]."'";
330             }
331             $local_tos = "T";
332             break;
333
334          case "prec":
335             $local_sql_2 .= ", prec_bit";
336             $local_sql_3 .= ", '".$local_element[1]."'";
337             break;
338
339          case "ttl":
340             $local_sql_2 .= ", ttl";
341             $local_sql_3 .= ", '".$local_element[1]."'";
342             break;
343
344         case "id":
345
346             if ($local_id == 0) {
347                $local_sql_2 .= ", header_id";
348                $local_sql_3 .= ", '".$local_element[1]."'";
349                $local_id = 1;
350             }
351             break;
352
353         case "proto":
354             $local_sql_2 .= ", protocol";
355             $local_sql_3 .= ", '".$local_element[1]."'";
356             if ($local_element[1] == 'ICMP') {
357                $local_icmp = true;
358             }
359             break;
360
361        case "spt":
362             $local_sql_2 .= ", destination_port";
363             $local_sql_3 .= ", '".$local_element[1]."'";
364             break;
365
366        case "dpt":
367             $local_sql_2 .= ", source_port";
368             $local_sql_3 .= ", '".$local_element[1]."'";
369             break;
370
371        case "window":
372             $local_sql_2 .= ", window";
373             $local_sql_3 .= ", '".$local_element[1]."'";
374             break;
375
376        case "urgp":
377            $local_sql_2 .= ", urgp";
378            $local_sql_3 .= ", '".$local_element[1]."'";
379            break;
380
381        case "rst":
382            $local_sql_2 .= ", rst";
383            $local_sql_3 .= ", true";
384            break;
385
386        case "syn":
387            $local_sql_2 .= ", syn";
388            $local_sql_3 .= ", true";
389            break;
390
391        case "df":
392           $local_sql_2 .= ", df";
393           $local_sql_3 .= ", true";
394           break;
395
396        case "type":
397            $local_sql_2 .= ", type";
398            $local_sql_3 .= ", '".$local_element[1]."'";
399            break;
400
401        case "code":
402           $local_sql_2 .= ", code";
403           $local_sql_3 .= ", '".$local_element[1]."'";
404           break;
405
406        case "seq":
407           $local_sql_2 .= ", sequence_number";
408           $local_sql_3 .= ", '".$local_element[1]."'";
409           break;
410
411        case "res":
412           $local_sql_2 .= ", res";
413           $local_sql_3 .= ", '".$local_element[1]."'";
414           break;
415
416        case "[src":
417           /*This record is different. In ICMP information is sometimes returned on an original packet.
418            * When the brackets are used a second line will be added to the
419            * log_adv_kernel_network-table. For that reason the processing into the database will be
420            * done here as well. After that a new insert-string will be created.
421            */
422
423           //Enter the data into the database
424           $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";
425           $dbms_working->query($local_sql);
426
427           $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
428           $local_sql_2 = "logid, detailed_table"; //FIELDS
429           $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
430           $local_len   = 0;
431           $local_id    = 0;
432           break;
433       default:
434 /*           $local_element[0];
435            syslog(LOG_INFO, "Unrecognized kernel/network entry: ".$local_element[0]);
436
437 */
438      }
439
440    }
441
442
443    //Now that the data is complete create the SQL-statement
444    $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";
445    $dbms_working->query($local_sql);
446    RETURN "TRUE";
447
448 }
449
450 function linux_kernel_device()
451 {
452    /* This function is able to deal with the output of kernel-network messages
453     * coming from device related processes. Typically networkcard and other
454     * hardware-related data will show-up here
455     * INPUT    : NONE
456     * GLOBALS  : $dbms, $dbms_working
457     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
458     */
459
460     global $dbms, $dbms_working;
461
462  }
463
464 function linux_daemon()
465 {
466    /* This function is able to deal with the output of kernel-network messages
467     * coming from device related processes. Typically networkcard and other
468     * hardware-related data will show-up here
469     * INPUT    : NONE
470     * GLOBALS  : $dbms, $dbms_working
471     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
472     */
473
474     global $dbms, $dbms_working;
475
476     $local_log_line = strtolower($dbms->db_result_row[6]);
477
478     //Find a sign of stop
479     //Using the word shutdown
480     $pos = strpos($local_log_line, "shutdown");
481     if ($pos > 0)
482     {
483        $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
484        $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
485                     .$dbms->db_result_row[3]."', 'stop')";
486
487        $dbms_working->query($local_sql);
488     }
489     else
490     {
491       //Using the word stop
492       $pos = strpos($local_log_line, "stop");
493       if ($pos > 0)
494       {
495          $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
496          $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
497                       .$dbms->db_result_row[3]."', 'stop')";
498          $dbms_working->query($local_sql);
499       }
500       else
501       {
502         //As the word restart
503         $pos = strpos($local_log_line, "restart");
504         if ($pos > 0)
505         {
506           $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
507           $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
508                        .$dbms->db_result_row[3]."', 'stop')";
509           $dbms_working->query($local_sql);
510
511           $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
512           $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
513                        .$dbms->db_result_row[3]."', 'start')";
514           $dbms_working->query($local_sql);
515         }
516         else
517         {
518           //As the word start this is an else for restart.
519           //If we wouldn't do so restart would also give a positive on start
520           $pos = strpos($local_log_line, "start");
521           if ($pos > 0)
522           {
523             $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
524             $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
525                          .$dbms->db_result_row[3]."', 'start')";
526             $dbms_working->query($local_sql);
527           }
528           else
529           {
530
531             //The word error indicates problems.
532             $pos = strpos($local_log_line, "error");
533             $pos2 = strpos($local_log_line, "crash"); //The word crash is also considered to be an error
534
535             if ($pos > 0 or $pos2 > 0)
536             {
537                $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
538                $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
539                             .$dbms->db_result_row[3]."', 'error detected')";
540                $dbms_working->query($local_sql);
541
542                //Quite often an error will be followed with information that the daemon or service ended.
543                $pos = strpos($local_log_line, "abort");
544
545                if ($pos > 0)
546                {
547                   $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
548                   $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
549                                .$dbms->db_result_row[3]."', 'abort')";
550                   $dbms_working->query($local_sql);
551                }
552                else
553                {
554                  $pos = strpos($local_log_line, "ended");
555                  if ($pos > 0)
556                  {
557                     $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
558                     $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
559                                  .$dbms->db_result_row[3]."', 'abort')";
560                     $dbms_working->query($local_sql);
561                  }
562                  else
563                  {
564                    $pos = strpos($local_log_line, "stop");
565                    if ($pos > 0)
566                    {
567                       $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
568                       $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
569                                    .$dbms->db_result_row[3]."', 'abort')";
570                       $dbms_working->query($local_sql);
571                    }
572                    else
573                    {
574
575                      //For power management there is a charge warning
576                      $pos = strpos($local_log_line, "charge");
577                      if ($pos > 0)
578                      {
579                         $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
580                         $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
581                                      .$dbms->db_result_row[3]."', 'Power warning')";
582                         $dbms_working->query($local_sql);
583                      }
584                      else
585                      {
586
587                        //As the word start this is an else for restart.
588                        //If we wouldn't do so restart would also give a positive on start
589                        //This can only be done if we ensured nothing else was the case
590                        //PLEASE USE THIS AS LATE AS POSSIBLE!!!
591                        $pos = strpos($local_log_line, "exiting");
592                        if ($pos > 0)
593                        {
594                           $local_sql = "INSERT INTO log_adv_daemon (logid, detailed_table, service, event) VALUES ";
595                           $local_sql .= "('".$dbms->db_result_row[0]."', 'log_adv_daemon', '"
596                                        .$dbms->db_result_row[3]."', 'start')";
597                           $dbms_working->query($local_sql);
598                        }
599                      }
600                    }
601                  }
602                }
603             }
604           }
605         }
606       }
607     }
608     return "ok";
609
610 }
611
612 ?>