logstreamlineparsersyslog.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. *********************************************************************
  4. * LogAnalyzer - http://loganalyzer.adiscon.com
  5. * ----------------------------------------------------------------- *
  6. * LogStream Parser is used to split syslog messages into fields *
  7. * *
  8. * All directives are explained within this file *
  9. *
  10. * Copyright (C) 2008-2010 Adiscon GmbH.
  11. *
  12. * This file is part of LogAnalyzer.
  13. *
  14. * LogAnalyzer is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * LogAnalyzer is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with LogAnalyzer. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * A copy of the GPL can be found in the file "COPYING" in this
  28. * distribution.
  29. *
  30. * Adiscon LogAnalyzer is also available under a commercial license.
  31. * For details, contact info@adiscon.com or visit
  32. * http://loganalyzer.adiscon.com/commercial
  33. *********************************************************************
  34. */
  35. // --- Avoid directly accessing this file!
  36. if ( !defined('IN_PHPLOGCON') )
  37. {
  38. die('Hacking attempt');
  39. exit;
  40. }
  41. // ---
  42. // --- Basic Includes
  43. require_once($gl_root_path . 'classes/enums.class.php');
  44. require_once($gl_root_path . 'include/constants_errors.php');
  45. require_once($gl_root_path . 'include/constants_logstream.php');
  46. // ---
  47. class LogStreamLineParsersyslog extends LogStreamLineParser {
  48. // protected $_arrProperties = null;
  49. // Constructor
  50. public function LogStreamLineParsersyslog() {
  51. return; // Nothing
  52. }
  53. /**
  54. * ParseLine
  55. *
  56. * @param arrArguments array in&out: properties of interest. There can be no guarantee the logstream can actually deliver them.
  57. * @return integer Error stat
  58. */
  59. public function ParseLine($szLine, &$arrArguments)
  60. {
  61. // Set IUT Property first!
  62. $arrArguments[SYSLOG_MESSAGETYPE] = IUT_Syslog;
  63. // Sample (Syslog): Mar 10 14:45:44 debandre anacron[3226]: Job `cron.daily' terminated (mailing output)
  64. if ( preg_match("/^(\w+) ?(\d{1,2})\s+([\d:]+)\s+([\w\d\.-]+) %(\w+-((\d*[a-zA-Z]+\d*)?))-?(\d)-([^:]+): (.*?)$/", $szLine, $out) ) {
  65. $arrArguments[SYSLOG_FACILITY] = rtrim($out[5], "-");
  66. $arrArguments[SYSLOG_SEVERITY] = $out[8];
  67. $sTS = $out[1] . " " . $out[2] . " " . $out[3];
  68. $sDate = strtotime($sTS);
  69. $sFT = strftime("%b %d %H:%M:%S %Z", $sDate);
  70. $arrArguments[SYSLOG_DATE] = GetEventTime($sFT);
  71. $ $arrArguments[SYSLOG_DATE] = GetEventTime($sTS);
  72. $sIP = gethostbyname($out[4]);
  73. $arrArguments[SYSLOG_HOST] = $out[4] . " / " . $sIP;
  74. $arrArguments[SYSLOG_SYSLOGTAG] = $out[9];
  75. $arrArguments[SYSLOG_MESSAGE] = $out[10];
  76. }
  77. else if ( preg_match("/^(\w+) ?(\d{1,2})\s+([\d:]+)\s+([\w\d\.-]+) ?(.*?) ([A-Za-z]{3,4})?:\s*%(\w+-((\d*[a-zA-Z]+\d*)?))-?(\d)-([^:]+): (.*?)$/", $szLine, $out ) )
  78. {
  79. $arrArguments[SYSLOG_FACILITY] = rtrim($out[7], "-");
  80. $arrArguments[SYSLOG_SEVERITY] = $out[10];
  81. $sTS = $out[1] . " " . $out[2] . " " . $out[3] . " " . $out[6];
  82. $sDate = strtotime($sTS);
  83. $sFT = strftime("%b %d %H:%M:%S %Z", $sDate);
  84. $arrArguments[SYSLOG_DATE] = GetEventTime($sFT);
  85. $ $arrArguments[SYSLOG_DATE] = GetEventTime($sTS);
  86. $sIP = gethostbyname($out[4]);
  87. $arrArguments[SYSLOG_HOST] = $out[4] . " / " . $sIP;
  88. $arrArguments[SYSLOG_SYSLOGTAG] = $out[11];
  89. $arrArguments[SYSLOG_MESSAGE] = $out[12];
  90. }
  91. // Sample (Syslog): Mar 10 14:45:39 debandre syslogd 1.4.1#18: restart
  92. else if ( preg_match("/(...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32}):(.*?)$/", $szLine, $out ) )
  93. {
  94. // Copy parsed properties!
  95. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1] . " " . $out[2]);
  96. $arrArguments[SYSLOG_HOST] = $out[3];
  97. $arrArguments[SYSLOG_SYSLOGTAG] = $out[4];
  98. $arrArguments[SYSLOG_MESSAGE] = $out[5];
  99. }
  100. // Sample (Syslog): Mar 10 14:45:39 debandre syslogd restart
  101. else if ( preg_match("/(...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32}) (.*?)$/", $szLine, $out ) )
  102. {
  103. // Copy parsed properties!
  104. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1] . " " . $out[2]);
  105. $arrArguments[SYSLOG_HOST] = $out[3];
  106. $arrArguments[SYSLOG_SYSLOGTAG] = $out[4];
  107. $arrArguments[SYSLOG_MESSAGE] = $out[5];
  108. }
  109. // Sample (Syslog): Mar 7 17:18:35 debandre exiting on signal 15
  110. else if ( preg_match("/(...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) (.*?) (.*?)$/", $szLine, $out ) )
  111. {
  112. // Copy parsed properties!
  113. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1] . " " . $out[2]);
  114. $arrArguments[SYSLOG_HOST] = $out[3];
  115. $arrArguments[SYSLOG_MESSAGE] = $out[4];
  116. }
  117. // Sample (RSyslog): 2008-03-28T11:07:40+01:00 localhost rger: test 1
  118. else if ( preg_match("/([0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.[0-9]{1,2}:[0-9]{1,2}) (.*?) (.*?):(.*?)$/", $szLine, $out ) )
  119. {
  120. // Copy parsed properties!
  121. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1]);
  122. $arrArguments[SYSLOG_HOST] = $out[2];
  123. $arrArguments[SYSLOG_SYSLOGTAG] = $out[3];
  124. $arrArguments[SYSLOG_MESSAGE] = $out[4];
  125. }
  126. // Sample (RSyslog): 2008-03-28T11:07:40.591633+01:00 localhost rger: test 1
  127. else if ( preg_match("/([0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\.[0-9]{1,6}.[0-9]{1,2}:[0-9]{1,2}) (.*?) (.*?):(.*?)$/", $szLine, $out ) )
  128. {
  129. // Copy parsed properties!
  130. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1]);
  131. $arrArguments[SYSLOG_HOST] = $out[2];
  132. $arrArguments[SYSLOG_SYSLOGTAG] = $out[3];
  133. $arrArguments[SYSLOG_MESSAGE] = $out[4];
  134. }
  135. // Sample: 2008-03-28T15:17:05.480876+01:00,**NO MATCH**
  136. else if ( preg_match("/([0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\.[0-9]{1,6}.[0-9]{1,2}:[0-9]{1,2}),(.*?)$/", $szLine, $out ) )
  137. {
  138. // Some kind of debug message or something ...
  139. $arrArguments[SYSLOG_DATE] = GetEventTime($out[1]);
  140. $arrArguments[SYSLOG_MESSAGE] = $out[2];
  141. }
  142. else
  143. {
  144. if ( isset($arrArguments[SYSLOG_MESSAGE]) && strlen($arrArguments[SYSLOG_MESSAGE]) > 0 )
  145. OutputDebugMessage("Unparseable syslog msg - '" . $arrArguments[SYSLOG_MESSAGE] . "'", DEBUG_ERROR);
  146. }
  147. // If SyslogTag is set, we check for MessageType!
  148. if ( isset($arrArguments[SYSLOG_SYSLOGTAG]) )
  149. {
  150. if ( strpos($arrArguments[SYSLOG_SYSLOGTAG], "EvntSLog" ) !== false )
  151. $arrArguments[SYSLOG_MESSAGETYPE] = IUT_NT_EventReport;
  152. }
  153. // Return success!
  154. return SUCCESS;
  155. }
  156. }
  157. ?>