protected void WorkerProc() { string[] allLines = File.ReadAllLines(@"C:\Temp\etc\storage.txt"); while (true) { Thread.Sleep(50); if (token.IsCancellationRequested) { break; } foreach (string line in allLines) { MessageDataItem message = new MessageDataItem(line); message.AddAttribute("ReciveTimestamp", DateTime.Now); while (!MessageSender.Invoke(message, true)) { Thread.Sleep(1); } if (token.IsCancellationRequested) { break; } } } }
protected MessageDataItem CreateMessageDataItem(string messageText, EndPoint endPoint) { MessageDataItem message = new MessageDataItem(messageText); if (AddSenderIPAttribute) { message.AddAttribute("SenderIP", (endPoint as IPEndPoint)?.Address.ToString()); } if (AddReciveTimestampAttribute && ReciveTimestampAttributeLocal) { message.AddAttribute("ReciveTimestamp", DateTime.Now); } if (AddReciveTimestampAttribute && !ReciveTimestampAttributeLocal) { message.AddAttribute("ReciveTimestamp", DateTime.UtcNow); } return(message); }
protected void AddSyslogPRI(MessageDataItem message, int facility, int severity) { if (OutCode) { message.AddAttribute(FacilityCodeAttribute, facility); message.AddAttribute(SeverityCodeAttribute, severity); } if (OutText) { if (facility >= 0 && facility < FacilityText.Length) { message.AddAttribute(FacilityTextAttribute, FacilityText[facility]); } else { message.AddAttribute(FacilityTextAttribute, $"Unknown facility code{facility}"); } if (severity >= 0 && severity < SeverityText.Length) { message.AddAttribute(SeverityTextAttribute, SeverityText[severity]); } else { message.AddAttribute(SeverityTextAttribute, $"Unknown severity code{severity}"); } } }
private bool MessageAcceptor(MessageDataItem message, bool canPause) { for (int i = 0; i < AttributeCount; i++) { message.AddAttribute(Attibutes[i].Item1, Attibutes[i].Item2); } bool posted = AttachedQueue.TryAdd(message); if (!posted && !canPause) { Server.Logger?.LogEvent(InputModule, Severity.Warning, ServerConstants.Reasons.QueueOverflow, $"Input message dropped from '{ModuleKey}'. The module doesn't support input throttling."); Server.HealthReporter?.SetModuleHealth(InputModule, ServerConstants.Components.AttachedQueue, HealthState.Warning, "Message dropped"); } return(posted); }
public void ExtractAttributes(MessageDataItem message) { if (message.AttributeExists(InputAttribute) && !message.AttributeExists(OutputAttribute)) { try { IPAddress hostIPAddress = IPAddress.Parse(message.GetAttributeAsString(InputAttribute)); IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress); message.AddAttribute(OutputAttribute, hostInfo.HostName); } catch { // none } } }
public MessageDataItem Parse(MessageDataItem message) { // result -- keep it as separate vars to allow modifications and overrides string outMessage = null; Dictionary <string, Variant> outAttributes = new Dictionary <string, Variant>(); bool AnyMatches = false; if (config.DefaultFieldSettings != null) { foreach (FieldSetting attrCfg in config.DefaultFieldSettings) { try { attrCfg.SetField(null, message, ref outMessage, outAttributes); } catch (Exception e) { ServerLogger?.LogEvent(this, Severity.Warning, "Parser", $"Failed to parse field {attrCfg.OutputAttribute ?? "<UNKNOWN>"}", e); } } } foreach (ParsingExpression expr in config.ParsingExpressions) { if (string.IsNullOrEmpty(expr.MatchingRegEx) || Regex.IsMatch(message.Message, expr.MatchingRegEx)) { MatchCollection matches = Regex.Matches(message.Message, expr.ParsingRegEx, expr.RegexOptions); if (matches.Count > 0) { AnyMatches = true; foreach (Match match in matches) { if (match.Success) { if (expr.FieldSettings != null) { foreach (FieldSetting attrCfg in expr.FieldSettings) { try { attrCfg.SetField(match.Groups, message, ref outMessage, outAttributes); } catch (Exception e) { ServerLogger?.LogEvent(this, Severity.Warning, "Parser", $"Failed to parse field {attrCfg.OutputAttribute ?? "<UNKNOWN>"}", e); } } } } } } else { if (expr.StopIfMatched) { ServerLogger?.LogEvent(this, Severity.Warning, "Parser", "Matching expression matched in a stopping expression, but the parsing expression didn't match."); } } // exit from loop of ParsingExpressions if (expr.StopIfMatched) { break; } } } if (AnyMatches) { MessageDataItem result = new MessageDataItem(outMessage ?? ""); foreach (KeyValuePair <string, Variant> newAttribute in outAttributes) { result.AddAttribute(newAttribute.Key, newAttribute.Value); } return(result); } else { if (config.PassthroughFieldSettings != null) { foreach (FieldSetting attrCfg in config.PassthroughFieldSettings) { try { attrCfg.SetField(null, message, ref outMessage, outAttributes); } catch (Exception e) { ServerLogger?.LogEvent(this, Severity.Warning, "Parser", $"Failed to parse field {attrCfg.OutputAttribute ?? "<UNKNOWN>"}", e); } } } MessageDataItem result = new MessageDataItem(outMessage ?? ""); foreach (KeyValuePair <string, Variant> newAttribute in outAttributes) { result.AddAttribute(newAttribute.Key, newAttribute.Value); } return(result); // return message.Clone(); } }
protected void WorkerProc() { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] buffer = new byte[BufferSize]; while (true) { if (socket.SocketType == SocketType.Dgram) { Task <SocketReceiveFromResult> udpAsyncResult = socket.ReceiveFromAsync(new ArraySegment <byte>(buffer), SocketFlags.None, remoteEndPoint); try { udpAsyncResult.Wait(token); } catch (OperationCanceledException) { break; } int bytesReceived = udpAsyncResult.Result.ReceivedBytes; if (bytesReceived > 0) { MessageDataItem message = new MessageDataItem { Message = Encoding.ASCII.GetString(buffer, 0, bytesReceived) }; message.AddAttribute("SenderIP", (udpAsyncResult.Result.RemoteEndPoint as IPEndPoint)?.Address.ToString()); message.AddAttribute("ReciveTimestamp", DateTime.Now); message.AddAttribute("Channel", "UDP"); foreach (IQueueModule queue in OutputQueues) { queue.Enqueue(message); } } } if (socket.SocketType == SocketType.Stream) { Task <Socket> tcpAsyncAcceptResult = socket.AcceptAsync(); try { tcpAsyncAcceptResult.Wait(token); } catch { break; } Socket innerSocket = tcpAsyncAcceptResult.Result; bool isCancelled = false; StringBuilder stringBuilder = new StringBuilder(); int bytesRead = 0; do { Task <int> tcpAsyncReceiveResult = innerSocket.ReceiveAsync(new ArraySegment <byte>(buffer), SocketFlags.None); try { tcpAsyncReceiveResult.Wait(token); } catch { isCancelled = true; break; } bytesRead = tcpAsyncReceiveResult.Result; if (bytesRead > 0) { stringBuilder.Append(Encoding.ASCII.GetString(buffer, 0, tcpAsyncReceiveResult.Result)); } } while (bytesRead > 0); // propagate cancellation if (isCancelled) { break; } MessageDataItem message = new MessageDataItem { Message = stringBuilder.ToString() }; message.AddAttribute("SenderIP", (tcpAsyncAcceptResult.Result.RemoteEndPoint as IPEndPoint)?.Address.ToString()); message.AddAttribute("ReciveTimestamp", DateTime.Now); message.AddAttribute("Channel", "TCP"); foreach (IQueueModule queue in OutputQueues) { queue.Enqueue(message); } } } }