Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
customs-il committed Oct 7, 2024
1 parent 77a4ad9 commit a39b7c8
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 23 deletions.
1 change: 0 additions & 1 deletion generic jobs/Common/BaseDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ protected BaseDefault(IConfigurationSection section, BaseDefault baseDefault) :

public bool Veto { get; set; }
public string? VetoReason { get; set; }

public CheckStatus CheckStatus { get; set; }
}
2 changes: 2 additions & 0 deletions generic jobs/FolderCheck/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public Folder(IConfigurationSection section, Defaults defaults) : base(section,
public string? CreatedAge { get; private set; }
public string? ModifiedAge { get; private set; }

public FolderResult Result { get; } = new();

//// --------------------------------------- ////

public long? TotalSizeNumber { get; }
Expand Down
10 changes: 10 additions & 0 deletions generic jobs/FolderCheck/FolderResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace FolderCheck;

internal class FolderResult
{
public long TotalSize { get; set; }
public long FileSize { get; set; }
public int FileCount { get; set; }
public DateTime? CreatedAge { get; set; }
public DateTime? ModifiedAge { get; set; }
}
8 changes: 6 additions & 2 deletions generic jobs/FolderCheck/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Planar.Job;
using System.Net;
using YamlDotNet.Core.Tokens;

namespace FolderCheck;

Expand Down Expand Up @@ -154,9 +152,12 @@ private void InvokeFolderInner(Folder folder)

var files = GetFiles(path, folder);
var filesCount = files.Count();
folder.Result.FileCount = filesCount;

if (folder.TotalSizeNumber != null && filesCount > 0)
{
var size = files.Sum(f => f.Length);
folder.Result.TotalSize = size;
Logger.LogInformation("folder '{Path}' size is {Size:N0} byte(s)", path, size);
if (size > folder.TotalSizeNumber)
{
Expand All @@ -167,6 +168,7 @@ private void InvokeFolderInner(Folder folder)
if (folder.FileSizeNumber != null && filesCount > 0)
{
var max = files.Max(f => f.Length);
folder.Result.FileSize = max;
Logger.LogInformation("folder '{Path}' max file size is {Size:N0} byte(s)", path, max);
if (max > folder.FileSizeNumber)
{
Expand All @@ -186,6 +188,7 @@ private void InvokeFolderInner(Folder folder)
if (folder.CreatedAgeDate != null && filesCount > 0)
{
var created = files.Min(f => f.CreationTime);
folder.Result.CreatedAge = created;
Logger.LogInformation("folder '{Path}' most old created file is {Created}", path, created);
if (created < folder.CreatedAgeDate)
{
Expand All @@ -196,6 +199,7 @@ private void InvokeFolderInner(Folder folder)
if (folder.ModifiedAgeDate != null && filesCount > 0)
{
var modified = files.Min(f => f.LastWriteTime);
folder.Result.ModifiedAge = modified;
Logger.LogInformation("folder '{Path}' most old modified file is {Created}", path, modified);
if (modified < folder.ModifiedAgeDate)
{
Expand Down
2 changes: 2 additions & 0 deletions generic jobs/HealthCheck/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public Endpoint(IConfigurationSection section, Defaults defaults) : base(section

//// -------------------------- ////

public EndpointResult Result { get; } = new();

private static Uri? SetAbsoluteUrl(string url)
{
if (Uri.TryCreate(url, UriKind.Absolute, out var result)) { return result; }
Expand Down
8 changes: 8 additions & 0 deletions generic jobs/HealthCheck/EndpointResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Net;

namespace HealthCheck;

internal class EndpointResult
{
public HttpStatusCode HttpStatusCode { get; set; }
}
1 change: 1 addition & 0 deletions generic jobs/HealthCheck/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private async Task InvokeEndpointInner(Endpoint endpoint)
throw new CheckException($"health check fail for endpoint name '{endpoint.Name}' with url '{uri}'. message: {ex.Message}");
}

endpoint.Result.HttpStatusCode = response.StatusCode;
if (endpoint.SuccessStatusCodes.Any(s => s == (int)response.StatusCode))
{
Logger.LogInformation("health check success for endpoint name '{EndpointName}' with url '{EndpointUrl}'",
Expand Down
2 changes: 2 additions & 0 deletions generic jobs/InfluxDBCheck/InfluxQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ internal class InfluxQuery(IConfigurationSection section, Defaults defaults) :
public Condition? InternalValueCondition { get; set; }

//// -------------------------- ////

public InfluxQueryResult Result { get; } = new();
}
7 changes: 7 additions & 0 deletions generic jobs/InfluxDBCheck/InfluxQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InfluxDBCheck;

internal class InfluxQueryResult
{
public double? QueryValue { get; set; }
public int? QueryRecordsCount { get; set; }
}
9 changes: 5 additions & 4 deletions generic jobs/InfluxDBCheck/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Newtonsoft.Json;
using Planar.Job;
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

Expand All @@ -22,7 +21,7 @@ internal partial class Job : BaseCheckJob

static partial void VetoQuery(InfluxQuery query);

static partial void Finalayze(IEnumerable<InfluxQuery> endpoints);
static partial void Finalayze(IEnumerable<InfluxQuery> queries);

public override void Configure(IConfigurationBuilder configurationBuilder, IJobExecutionContext context)
{
Expand Down Expand Up @@ -139,6 +138,7 @@ private async Task InvokeQueryCheckInner(InfluxQuery query, InfluxProxy proxy)
if (query.InternalValueCondition != null)
{
var value = GetValue(result, query.Name) ?? 0;
query.Result.QueryValue = value;
var ok = query.InternalValueCondition.Evaluate(value);
if (!ok)
{
Expand All @@ -150,7 +150,8 @@ private async Task InvokeQueryCheckInner(InfluxQuery query, InfluxProxy proxy)

if (query.InternalRecordsCondition != null)
{
var value = GetRecords(result);
var value = GetRecordsCount(result);
query.Result.QueryRecordsCount = value;
var ok = query.InternalRecordsCondition.Evaluate(value);
if (!ok)
{
Expand All @@ -164,7 +165,7 @@ private async Task InvokeQueryCheckInner(InfluxQuery query, InfluxProxy proxy)
IncreaseEffectedRows();
}

private static double GetRecords(List<FluxTable> tables)
private static int GetRecordsCount(List<FluxTable> tables)
{
if (tables.Count == 0) { return 0; }
var table = tables[0];
Expand Down
23 changes: 17 additions & 6 deletions generic jobs/RabbitMQCheck/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ internal partial class Job : BaseCheckJob

static partial void Finalayze(IEnumerable<Queue> queues);

static partial void Finalayze(Node node);

static partial void Finalayze(HealthCheck healthCheck);

public override void Configure(IConfigurationBuilder configurationBuilder, IJobExecutionContext context)
{
CustomConfigure(configurationBuilder, context);
Expand Down Expand Up @@ -69,6 +73,8 @@ public async override Task ExecuteJob(IJobExecutionContext context)

await Task.WhenAll(tasks);

Finalayze(healthCheck);
Finalayze(node);
Finalayze(queues);
Finalayze();
}
Expand Down Expand Up @@ -187,6 +193,9 @@ private async Task InvokeNodeCheckInner(Node node, Server server, string host)

var proxy = RabbitMqProxy.GetProxy(host, server, Logger);
var details = await proxy.GetNodeDetails();

node.Result = details;

foreach (var item in details)
{
if (node.DiskFreeAlarm.GetValueOrDefault())
Expand Down Expand Up @@ -225,12 +234,14 @@ private async Task InvokeHealthCheck(HealthCheck healthCheck, Server server)
}
}

private async Task InvokeQueueCheckInner(Queue queue, IEnumerable<QueueDetails> details)
private async Task InvokeQueueCheckInner(Queue queue, IEnumerable<QueueResult> details)
{
if (!queue.IsValid) { return; }
var detail = details.FirstOrDefault(x => string.Equals(x.Name, queue.Name, StringComparison.OrdinalIgnoreCase))
?? throw new CheckException($"queue '{queue.Name}' does not exists");

queue.Result = detail;

CheckState(queue, detail);
CheckConsumers(queue, detail);
CheckMessages(queue, detail);
Expand All @@ -240,7 +251,7 @@ private async Task InvokeQueueCheckInner(Queue queue, IEnumerable<QueueDetails>
await Task.CompletedTask;
}

private void CheckMemory(Queue queue, QueueDetails detail)
private void CheckMemory(Queue queue, QueueResult detail)
{
// Memory
if (queue.MemoryNumber.HasValue)
Expand All @@ -256,7 +267,7 @@ private void CheckMemory(Queue queue, QueueDetails detail)
}
}

private void CheckMessages(Queue queue, QueueDetails detail)
private void CheckMessages(Queue queue, QueueResult detail)
{
// Messages
if (queue.Messages.HasValue)
Expand All @@ -272,7 +283,7 @@ private void CheckMessages(Queue queue, QueueDetails detail)
}
}

private void CheckUnacked(Queue queue, QueueDetails detail)
private void CheckUnacked(Queue queue, QueueResult detail)
{
if (queue.Unacked.HasValue)
{
Expand All @@ -287,7 +298,7 @@ private void CheckUnacked(Queue queue, QueueDetails detail)
}
}

private void CheckConsumers(Queue queue, QueueDetails detail)
private void CheckConsumers(Queue queue, QueueResult detail)
{
// Consumers
if (queue.Consumers.HasValue)
Expand All @@ -303,7 +314,7 @@ private void CheckConsumers(Queue queue, QueueDetails detail)
}
}

private void CheckState(Queue queue, QueueDetails detail)
private void CheckState(Queue queue, QueueResult detail)
{
// Check State
if (queue.CheckState.GetValueOrDefault())
Expand Down
2 changes: 2 additions & 0 deletions generic jobs/RabbitMQCheck/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ internal class Node(IConfigurationSection section, Defaults defaults) : BaseDefa
public bool IsValid => MemoryAlarm.GetValueOrDefault() || DiskFreeAlarm.GetValueOrDefault();

public string Key => "[nodes]";

public IEnumerable<NodeResult> Result { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RabbitMQCheck;

internal class NodeDetails
internal class NodeResult
{
[JsonProperty("disk_free")]
public long DiskFree { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions generic jobs/RabbitMQCheck/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ internal class Queue(IConfigurationSection section, Defaults defaults) : BaseDef
public long? MemoryNumber { get; private set; } = CommonUtil.GetSize(section.GetValue<string>("memory"), "memory");
public string Key => Name;
public bool IsValid => Messages.HasValue || Consumers.HasValue || CheckState.HasValue || MemoryNumber.HasValue;

public QueueResult Result { get; set; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RabbitMQCheck;

public class QueueDetails
public class QueueResult
{
public string Name { get; set; } = null!;
public int Messages { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions generic jobs/RabbitMQCheck/RabbitMqProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public async Task VirtualHosts()
await Alarm("virtual hosts", "virtual-hosts");
}

public async Task<IEnumerable<NodeDetails>> GetNodeDetails()
public async Task<IEnumerable<NodeResult>> GetNodeDetails()
{
const string resource = "api/nodes";
var request = new RestRequest(resource, Method.Get);
var response = await _restClient.ExecuteAsync<IEnumerable<NodeDetails>>(request);
var response = await _restClient.ExecuteAsync<IEnumerable<NodeResult>>(request);
if (!response.IsSuccessful)
{
throw new CheckException($"node check on url {resource} failed. status code {response.StatusCode}", response.ErrorException);
Expand All @@ -72,11 +72,11 @@ public async Task<IEnumerable<NodeDetails>> GetNodeDetails()
return response.Data ?? [];
}

public async Task<IEnumerable<QueueDetails>> GetQueueDetails()
public async Task<IEnumerable<QueueResult>> GetQueueDetails()
{
const string resource = "api/queues";
var request = new RestRequest(resource, Method.Get);
var response = await _restClient.ExecuteAsync<IEnumerable<QueueDetails>>(request);
var response = await _restClient.ExecuteAsync<IEnumerable<QueueResult>>(request);
if (!response.IsSuccessful)
{
throw new CheckException($"queue check on url {resource} failed. status code {response.StatusCode}", response.ErrorException);
Expand Down
9 changes: 8 additions & 1 deletion generic jobs/RedisCheck/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal partial class Job : BaseCheckJob

static partial void Finalayze(IEnumerable<RedisKey> keys);

static partial void Finalayze(HealthCheck healthCheck);

public override void Configure(IConfigurationBuilder configurationBuilder, IJobExecutionContext context)
{
CustomConfigure(configurationBuilder, context);
Expand Down Expand Up @@ -58,6 +60,7 @@ public async override Task ExecuteJob(IJobExecutionContext context)
await SafeInvokeCheck(healthCheck, InvokeHealthCheckInner);
await SafeInvokeCheck(keys, InvokeKeyCheckInner);

Finalayze(healthCheck);
Finalayze(keys);
Finalayze();
}
Expand Down Expand Up @@ -215,7 +218,9 @@ private async Task InvokeHealthCheckInner(HealthCheck healthCheck)

private async Task InvokeKeyCheckInner(RedisKey key)
{
if (!await RedisFactory.Exists(key))
var exists = await RedisFactory.Exists(key);
key.Result.Exists = exists;
if (key.Exists.GetValueOrDefault() && !exists)
{
throw new CheckException($"key '{key.Key}' is not exists");
}
Expand All @@ -225,12 +230,14 @@ private async Task InvokeKeyCheckInner(RedisKey key)
if (key.Length > 0)
{
length = await RedisFactory.GetLength(key);
key.Result.Length = length;
Logger.LogInformation("key '{Key}' length is {Length:N0}", key.Key, length);
}

if (key.MemoryUsageNumber > 0)
{
size = await RedisFactory.GetMemoryUsage(key);
key.Result.MemoryUsage = size;
Logger.LogInformation("key '{Key}' size is {Size:N0} byte(s)", key.Key, size);
}

Expand Down
4 changes: 3 additions & 1 deletion generic jobs/RedisCheck/RedisKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class RedisKey(IConfigurationSection section, Defaults defaults) : Base
{
public string Key { get; } = section.GetValue<string>("key") ?? string.Empty;
public string? MemoryUsage { get; } = section.GetValue<string>("memory usage");
public int? Length { get; } = section.GetValue<int?>("length");
public long? Length { get; } = section.GetValue<int?>("length");
public int? Database { get; } = section.GetValue<int?>("database");
public bool? Exists { get; } = section.GetValue<bool?>("exists");

Expand All @@ -19,6 +19,8 @@ internal class RedisKey(IConfigurationSection section, Defaults defaults) : Base

//// --------------------------------------- ////

public RedisKeyResult Result { get; } = new();

private static int? GetSize(string? source, string fieldName)
{
var factor = 0;
Expand Down
8 changes: 8 additions & 0 deletions generic jobs/RedisCheck/RedisKeyResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RedisCheck;

internal class RedisKeyResult
{
public long? MemoryUsage { get; set; }
public long? Length { get; set; }
public bool? Exists { get; set; }
}
Loading

0 comments on commit a39b7c8

Please sign in to comment.