[feat]新增MySql助手插件,自动清理binlog日志
This commit is contained in:
parent
7e9a9078b7
commit
bc02b408e1
|
@ -0,0 +1,71 @@
|
|||
using NewLife;
|
||||
using NewLife.Log;
|
||||
using NewLife.Threading;
|
||||
using Stardust.Services;
|
||||
|
||||
namespace MySqlAgent;
|
||||
|
||||
internal class BinlogClear
|
||||
{
|
||||
public IEventProvider Event { get; set; }
|
||||
|
||||
private TimerX _timer;
|
||||
public void Start()
|
||||
{
|
||||
Event?.WriteInfoEvent("Binlog", "启动二进制日志清理");
|
||||
|
||||
_timer = new TimerX(DoClear, null, 1000, 3600_000);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_timer.TryDispose();
|
||||
}
|
||||
|
||||
private void DoClear(Object? state)
|
||||
{
|
||||
try
|
||||
{
|
||||
//var dir = "C:/ProgramData/MySQL/MySQL Server 8.0/Data";
|
||||
//if (!Directory.Exists(dir)) dir = "/var/lib/mysql";
|
||||
var dir = "/var/lib/mysql";
|
||||
if (!Directory.Exists(dir)) return;
|
||||
|
||||
var logs = Directory.GetFiles(dir, "binlog.*");
|
||||
if (logs == null || logs.Length == 0) return;
|
||||
|
||||
// 删除最近1小时之前的日志文件
|
||||
var exp = DateTime.Now.AddHours(-1);
|
||||
foreach (var item in logs)
|
||||
{
|
||||
var fi = new FileInfo(item);
|
||||
if (fi.Extension.TrimStart('.').ToInt() > 0 && fi.LastWriteTime < exp)
|
||||
{
|
||||
Event?.WriteInfoEvent("Binlog", $"删除二进制日志 {fi.Name}");
|
||||
XTrace.WriteLine("删除二进制日志 {0}", fi.Name);
|
||||
try
|
||||
{
|
||||
fi.Delete();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Event?.WriteErrorEvent("Binlog", ex.Message);
|
||||
XTrace.WriteException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//var sql = new StringBuilder();
|
||||
//sql.AppendFormat("PURGE BINARY LOGS TO '{0}'", log);
|
||||
//sql.AppendLine(";");
|
||||
|
||||
//var rs = MySqlHelper.Execute(sql.ToString());
|
||||
//if (rs > 0) XTrace.WriteLine("清理二进制日志 {0} 成功", log);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Event?.WriteErrorEvent("DeleteBinlog", ex.Message);
|
||||
XTrace.WriteException(ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
|
||||
<AssemblyTitle>MySql助手</AssemblyTitle>
|
||||
<Description>监控MySql性能,上报星尘平台</Description>
|
||||
<Company>新生命开发团队</Company>
|
||||
<Copyright>©2002-2024 NewLife</Copyright>
|
||||
<VersionPrefix>1.0</VersionPrefix>
|
||||
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
|
||||
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
|
||||
<FileVersion>$(Version)</FileVersion>
|
||||
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
|
||||
<Deterministic>false</Deterministic>
|
||||
<OutputPath>..\..\Bin\Agent</OutputPath>
|
||||
<!--<GenerateDocumentationFile>True</GenerateDocumentationFile>-->
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsPackable>False</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NewLife.XCode" Version="11.12.2024.507" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Stardust\Stardust.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,42 @@
|
|||
using System.ComponentModel;
|
||||
using NewLife.Log;
|
||||
using NewLife.Model;
|
||||
using NewLife.Threading;
|
||||
using Stardust.Plugins;
|
||||
using Stardust.Services;
|
||||
|
||||
namespace MySqlAgent;
|
||||
|
||||
[DisplayName("MySql助手")]
|
||||
public class MySqlPlugin : AgentPlugin
|
||||
{
|
||||
private TimerX _timer;
|
||||
private ITracer _tracer;
|
||||
private BinlogClear _clear;
|
||||
|
||||
/// <summary>开始工作</summary>
|
||||
public override void Start()
|
||||
{
|
||||
_tracer = Provider.GetService<ITracer>();
|
||||
|
||||
_clear = new BinlogClear
|
||||
{
|
||||
Event = Provider.GetService<IEventProvider>(),
|
||||
};
|
||||
_clear.Start();
|
||||
}
|
||||
|
||||
/// <summary>停止工作</summary>
|
||||
/// <param name="reason"></param>
|
||||
public override void Stop(String reason)
|
||||
{
|
||||
_clear.Stop();
|
||||
}
|
||||
|
||||
protected override void Dispose(Boolean disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
Stop(disposing ? "Dispose" : "GC");
|
||||
}
|
||||
}
|
|
@ -27,6 +27,22 @@ public class NetDetect : AgentPlugin
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>停止工作</summary>
|
||||
/// <param name="reason"></param>
|
||||
public override void Stop(String reason)
|
||||
{
|
||||
_timer.TryDispose();
|
||||
|
||||
NetworkDetectSetting.Provider.Changed -= Provider_Changed;
|
||||
}
|
||||
|
||||
protected override void Dispose(Boolean disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
Stop(disposing ? "Dispose" : "GC");
|
||||
}
|
||||
|
||||
private void Provider_Changed(Object sender, EventArgs e)
|
||||
{
|
||||
// 配置改变,重新加载
|
||||
|
@ -44,22 +60,6 @@ public class NetDetect : AgentPlugin
|
|||
_timer?.SetNext(-1);
|
||||
}
|
||||
|
||||
/// <summary>停止工作</summary>
|
||||
/// <param name="reason"></param>
|
||||
public override void Stop(String reason)
|
||||
{
|
||||
_timer.TryDispose();
|
||||
|
||||
NetworkDetectSetting.Provider.Changed -= Provider_Changed;
|
||||
}
|
||||
|
||||
protected override void Dispose(Boolean disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
Stop(disposing ? "Dispose" : "GC");
|
||||
}
|
||||
|
||||
private void CheckWorker(Object state)
|
||||
{
|
||||
//var flag = NetworkInterface.GetIsNetworkAvailable();
|
||||
|
@ -71,7 +71,7 @@ public class NetDetect : AgentPlugin
|
|||
var set = NetworkDetectSetting.Current;
|
||||
if (set.Services != null && set.Services.Length > 0)
|
||||
{
|
||||
ws = new List<Worker>();
|
||||
ws = [];
|
||||
foreach (var item in set.Services)
|
||||
{
|
||||
if (item.Enable)
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
|
||||
<AssemblyTitle>网络监测</AssemblyTitle>
|
||||
<Description>检测网络,如果长期不可用,则重启系统,主要应用于嵌入式工控机</Description>
|
||||
<Company>新生命开发团队</Company>
|
||||
<Copyright>©2002-2022 NewLife</Copyright>
|
||||
<Copyright>©2002-2024 NewLife</Copyright>
|
||||
<VersionPrefix>1.0</VersionPrefix>
|
||||
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
|
||||
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
|
||||
<FileVersion>$(Version)</FileVersion>
|
||||
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
|
||||
<Deterministic>false</Deterministic>
|
||||
<OutputPath>..\..\Bin\NetworkDetect</OutputPath>
|
||||
<OutputPath>..\..\Bin\Agent</OutputPath>
|
||||
<!--<GenerateDocumentationFile>True</GenerateDocumentationFile>-->
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
|
|
@ -90,6 +90,8 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
/// <summary>宿主服务提供者</summary>
|
||||
public IServiceProvider Provider { get; set; }
|
||||
|
||||
private IObjectContainer _container;
|
||||
|
||||
public MyService()
|
||||
{
|
||||
ServiceName = "StarAgent";
|
||||
|
@ -112,6 +114,9 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
// set2.AutoRestart = 24 * 60;
|
||||
// set2.Save();
|
||||
//}
|
||||
|
||||
_container = ObjectContainer.Current;
|
||||
Provider = ObjectContainer.Provider;
|
||||
}
|
||||
|
||||
#region 菜单控制
|
||||
|
@ -169,6 +174,7 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
manager.ServiceChanged += OnServiceChanged;
|
||||
|
||||
_Manager = manager;
|
||||
_container.AddSingleton(manager);
|
||||
|
||||
// 插件管理器
|
||||
var pm = _PluginManager = new PluginManager
|
||||
|
@ -178,6 +184,7 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
|
||||
Log = XTrace.Log,
|
||||
};
|
||||
_container.AddSingleton(pm);
|
||||
|
||||
// 监听端口,用于本地通信
|
||||
if (set.LocalPort > 0) StartLocalServer(set.LocalPort);
|
||||
|
@ -207,6 +214,8 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
}
|
||||
}
|
||||
|
||||
if (_Client != null) _Client.Plugins = pm.Plugins.Select(e => e.GetType().Name.TrimEnd("Plugin")).ToArray();
|
||||
|
||||
base.StartWork(reason);
|
||||
}
|
||||
|
||||
|
@ -338,6 +347,9 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
client.UseTrace();
|
||||
|
||||
_Client = client;
|
||||
_container.AddSingleton(client);
|
||||
_container.AddSingleton<ICommandClient>(client);
|
||||
_container.AddSingleton<IEventProvider>(client);
|
||||
|
||||
// 可能需要多次尝试
|
||||
_timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
|
||||
|
@ -351,6 +363,7 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
if (!server.IsNullOrEmpty())
|
||||
{
|
||||
_factory = new StarFactory(server, "StarAgent", null);
|
||||
_container.AddSingleton(_factory);
|
||||
|
||||
// 激活配置中心,获取PluginServer
|
||||
var config = _factory.GetConfig();
|
||||
|
@ -383,6 +396,8 @@ internal class MyService : ServiceBase, IServiceProvider
|
|||
}, null);
|
||||
|
||||
_server = svr;
|
||||
_container.AddSingleton(svr);
|
||||
|
||||
svr.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -108,5 +108,8 @@ public class NodeInfo
|
|||
/// 跨系统传递UTC时间是严谨的,但是UTC时间序列化比较头疼,目前能够做到自己序列化后,自己能够解析出来,暂时用着,将来向netcore的system.text.json序列化迁移
|
||||
/// </remarks>
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
/// <summary>插件列表</summary>
|
||||
public String[]? Plugins { get; set; }
|
||||
#endregion
|
||||
}
|
|
@ -56,6 +56,9 @@ public class StarClient : ApiHttpClient, ICommandClient, IEventProvider
|
|||
///// <summary>本地应用服务管理</summary>
|
||||
//public ServiceManager Manager { get; set; }
|
||||
|
||||
/// <summary>插件列表</summary>
|
||||
public String[] Plugins { get; set; }
|
||||
|
||||
/// <summary>最大失败数。超过该数时,新的数据将被抛弃,默认10 * 24 * 60</summary>
|
||||
public Int32 MaxFails { get; set; } = 10 * 24 * 60;
|
||||
|
||||
|
@ -243,6 +246,7 @@ public class StarClient : ApiHttpClient, ICommandClient, IEventProvider
|
|||
Runtime = Environment.Version + "",
|
||||
|
||||
Time = DateTime.UtcNow,
|
||||
Plugins = Plugins,
|
||||
};
|
||||
|
||||
// 获取最新机器名
|
||||
|
|
9
星尘.sln
9
星尘.sln
|
@ -38,7 +38,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins", "Plugins", "{11AD
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkDetect", "Plugins\NetworkDetect\NetworkDetect.csproj", "{7178875E-F5DD-4136-A5E6-EEBF151396CB}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeployAgent", "DeployAgent\DeployAgent.csproj", "{A9D12BD2-5BB2-498C-9E7C-70073FC9B3E4}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeployAgent", "DeployAgent\DeployAgent.csproj", "{A9D12BD2-5BB2-498C-9E7C-70073FC9B3E4}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlAgent", "Plugins\MySqlAgent\MySqlAgent.csproj", "{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -94,6 +96,10 @@ Global
|
|||
{A9D12BD2-5BB2-498C-9E7C-70073FC9B3E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A9D12BD2-5BB2-498C-9E7C-70073FC9B3E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A9D12BD2-5BB2-498C-9E7C-70073FC9B3E4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -102,6 +108,7 @@ Global
|
|||
{7DE10A4D-1749-4474-A6B2-F52CA8462813} = {48B9E6E7-289F-42F2-8ACA-E7DF8E7C9059}
|
||||
{677C6502-2C9C-4907-AD82-670BE77B0AE6} = {48B9E6E7-289F-42F2-8ACA-E7DF8E7C9059}
|
||||
{7178875E-F5DD-4136-A5E6-EEBF151396CB} = {11AD7F18-1320-41C5-B4A9-F208DBCAA5A9}
|
||||
{3E3CB5E2-529C-4BCE-A4B5-B1108359B2D7} = {11AD7F18-1320-41C5-B4A9-F208DBCAA5A9}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {9337283C-C795-479F-A2F1-C892EBE2490C}
|
||||
|
|
Loading…
Reference in New Issue