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