This commit is contained in:
2025-02-16 13:30:54 +01:00
parent 0991422672
commit 99b3417186
15 changed files with 541 additions and 99 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
namespace EonaCat.Logger.Servers.GrayLog;
@@ -21,10 +22,20 @@ public class Graylog : IDisposable
public Graylog() { }
public Graylog(string hostname = "127.0.0.1", int port = 12201, string nickName = null)
/// <summary>
/// Graylog server
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="nickName"></param>
/// <param name="logType"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public Graylog(string hostname = "127.0.0.1", int port = 12201, string nickName = null, List<ELogType> typesToLog = null)
{
_Hostname = hostname ?? throw new ArgumentNullException(nameof(hostname));
_Port = port >= 0 ? port : throw new ArgumentException("Port must be zero or greater.");
TypesToLog = typesToLog;
Nickname = nickName ?? IpPort;
SetUdp();
@@ -56,17 +67,38 @@ public class Graylog : IDisposable
}
}
private void SetUdp()
public List<ELogType> TypesToLog { get; set; }
public bool IsConnected { get; set; }
internal void SetUdp()
{
DisposeUdp();
Udp = new UdpClient(_Hostname, _Port);
try
{
DisposeUdp();
Udp = new UdpClient(_Hostname, _Port);
IsConnected = true;
}
catch
{
IsConnected = false;
}
}
private void DisposeUdp()
internal void DisposeUdp()
{
if (Udp != null)
{
Udp.Dispose();
IsConnected = false;
try
{
Udp?.Close();
Udp.Dispose();
}
catch
{
// Do nothing
}
Udp = null;
}
}

View File

@@ -1,8 +1,4 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using EonaCat.Json;
using EonaCat.Json;
namespace EonaCat.Logger.Servers.Splunk;
@@ -14,6 +10,7 @@ using EonaCat.Logger.Servers.Splunk.Models;
/// Splunk Server.
/// </summary>
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@@ -33,13 +30,23 @@ public class Splunk : IDisposable
public bool IsHttpsHecUrl => HasHecUrl && _splunkHecUrl.ToLower().StartsWith("https");
public bool IsLocalHost => HasHecUrl && (_splunkHecUrl.ToLower().Contains("127.0.0.1") || _splunkHecUrl.ToLower().Contains("localhost"));
public Splunk(string splunkHecUrl, string splunkHecToken, HttpClientHandler httpClientHandler = null, string nickName = null)
/// <summary>
/// Splunk Server
/// </summary>
/// <param name="splunkHecUrl"></param>
/// <param name="splunkHecToken"></param>
/// <param name="httpClientHandler"></param>
/// <param name="nickName"></param>
/// <param name="typesToLog"></param>
/// <exception cref="ArgumentNullException"></exception>
public Splunk(string splunkHecUrl, string splunkHecToken, HttpClientHandler httpClientHandler = null, string nickName = null, List<ELogType> typesToLog = null)
{
SplunkHecUrl = splunkHecUrl ?? throw new ArgumentNullException(nameof(splunkHecUrl));
SplunkHecToken = splunkHecToken ?? throw new ArgumentNullException(nameof(splunkHecToken));
Nickname = nickName ?? $"{splunkHecUrl}_{splunkHecToken}";
SplunkClientHandler = httpClientHandler ?? new HttpClientHandler();
TypesToLog = typesToLog;
CreateHttpClient();
}
@@ -59,6 +66,8 @@ public class Splunk : IDisposable
}
}
public List<ELogType> TypesToLog { get; set; }
private void CreateHttpClient()
{
DisposeHttpClient();
@@ -84,7 +93,7 @@ public class Splunk : IDisposable
CreateHttpClient();
}
public async Task<HttpResponseMessage> SendAsync(SplunkPayload splunkPayload)
public async Task<HttpResponseMessage> SendAsync(SplunkPayload splunkPayload, bool disableSplunkSSL = false)
{
if (splunkPayload == null) return null;
@@ -95,6 +104,16 @@ public class Splunk : IDisposable
host = splunkPayload.Host
};
if (disableSplunkSSL)
{
DisableSSLValidation();
}
if (HttpClient == null)
{
CreateHttpClient();
}
var eventJson = JsonHelper.ToJson(eventObject);
if (!HasHecToken) CreateHttpClient();
@@ -102,7 +121,7 @@ public class Splunk : IDisposable
return await HttpClient.PostAsync("/services/collector/event", content);
}
private void DisposeHttpClient()
internal void DisposeHttpClient()
{
if (HttpClient != null)
{

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
namespace EonaCat.Logger.Servers.Syslog;
@@ -12,17 +13,29 @@ public class Syslog : IDisposable
{
private string _Hostname = "127.0.0.1";
private int _Port = 514;
private bool IsConnected { get; set; }
public bool IsConnected { get; private set; }
internal UdpClient Udp;
public Syslog() { }
public Syslog(string hostname = "127.0.0.1", int port = 514, string nickName = null)
/// <summary>
/// Syslog server
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="nickName"></param>
/// <param name="typesToLog"></param>
/// <param name="convertToRfc5424"></param>
/// <param name="convertToRfc3164"></param>
public Syslog(string hostname = "127.0.0.1", int port = 514, string nickName = null, List<ELogType> typesToLog = null, bool convertToRfc5424 = false, bool convertToRfc3164 = false)
{
Hostname = hostname;
Port = port;
Nickname = nickName ?? IpPort;
TypesToLog = typesToLog;
ConvertToRfc5424 = convertToRfc5424;
ConvertToRfc3164 = convertToRfc3164;
}
public string Hostname
@@ -54,8 +67,11 @@ public class Syslog : IDisposable
public string IpPort => _Hostname + ":" + _Port;
public bool SupportsTcp { get; set; }
public string Nickname { get; set; }
public List<ELogType> TypesToLog { get; set; }
public bool ConvertToRfc5424 { get; set; }
public bool ConvertToRfc3164 { get; set; }
private void SetUdp()
internal void SetUdp()
{
try
{
@@ -69,12 +85,21 @@ public class Syslog : IDisposable
}
}
private void DisposeUdp()
internal void DisposeUdp()
{
if (Udp != null)
{
IsConnected = false;
Udp.Dispose();
try
{
Udp?.Close();
Udp.Dispose();
}
catch
{
// Do nothing
}
Udp = null;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
@@ -12,16 +13,27 @@ namespace EonaCat.Logger.Servers.Tcp
private int _Port = 514;
internal TcpClient _tcp;
private bool IsConnected { get; set; }
public bool IsConnected { get; private set; }
public string Nickname { get; set; }
public string IpPort => _Hostname + ":" + _Port;
public Tcp(string hostname, int port, string nickname = null)
/// <summary>
/// Tcp Server
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="nickname"></param>
/// <param name="typesToLog"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public Tcp(string hostname, int port, string nickname = null, List<ELogType> typesToLog = null)
{
_Hostname = hostname ?? throw new ArgumentNullException(nameof(hostname));
_Port = port >= 0 ? port : throw new ArgumentException("Port must be zero or greater.");
Nickname = nickname ?? IpPort;
TypesToLog = typesToLog;
SetTcp();
}
@@ -51,6 +63,8 @@ namespace EonaCat.Logger.Servers.Tcp
}
}
public List<ELogType> TypesToLog { get; set; }
internal void SetTcp()
{
try
@@ -59,7 +73,7 @@ namespace EonaCat.Logger.Servers.Tcp
_tcp = new TcpClient(_Hostname, _Port);
IsConnected = true;
}
catch
catch
{
IsConnected = false;
}
@@ -70,9 +84,17 @@ namespace EonaCat.Logger.Servers.Tcp
if (_tcp != null)
{
IsConnected = false;
_tcp.Close();
_tcp.Dispose();
_tcp = null;
try
{
_tcp.Close();
_tcp.Dispose();
_tcp = null;
}
catch
{
// Do nothing
}
}
}
@@ -125,7 +147,7 @@ namespace EonaCat.Logger.Servers.Tcp
}
if (IsConnected)
{
{
var sendData = Encoding.UTF8.GetBytes(data);
using var stream = _tcp.GetStream();
await stream.WriteAsync(sendData, 0, sendData.Length);

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
@@ -13,15 +14,24 @@ namespace EonaCat.Logger.Servers.Udp
private int _Port = 514;
internal UdpClient _udp;
private bool IsConnected { get; set; }
public bool IsConnected { get; private set; }
public string Nickname { get; set; }
public string IpPort => _Hostname + ":" + _Port;
public Udp(string hostname, int port, string nickname = null)
/// <summary>
/// Udp Server
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="nickname"></param>
/// <param name="typesToLog"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public Udp(string hostname, int port, string nickname = null, List<ELogType> typesToLog = null)
{
_Hostname = hostname ?? throw new ArgumentNullException(nameof(hostname));
_Port = port >= 0 ? port : throw new ArgumentException("Port must be zero or greater.");
TypesToLog = typesToLog;
Nickname = nickname ?? IpPort;
SetUdp();
}
@@ -52,6 +62,8 @@ namespace EonaCat.Logger.Servers.Udp
}
}
public List<ELogType> TypesToLog { get; set; }
internal void SetUdp()
{
try
@@ -71,7 +83,16 @@ namespace EonaCat.Logger.Servers.Udp
if (_udp != null)
{
IsConnected = false;
_udp.Dispose();
try
{
_udp?.Close();
_udp.Dispose();
}
catch
{
// Do nothing
}
_udp = null;
}
}

View File

@@ -11,6 +11,13 @@ namespace EonaCat.Logger.Servers.Zabbix.API;
public class ZabbixApi
{
/// <summary>
/// Zabbix Api
/// </summary>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="zabbixURL"></param>
/// <param name="basicAuth"></param>
public ZabbixApi(string user, string password, string zabbixURL, bool basicAuth)
{
_user = user;

View File

@@ -22,6 +22,12 @@ namespace EonaCat.Logger.Servers.Zabbix
/// </summary>
public ZabbixData[] Data { get; set; }
/// <summary>
/// Zabbix request
/// </summary>
/// <param name="host"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public ZabbixRequest(string host, string key, string value)
{
Request = "sender data";