Skip to content

Commit

Permalink
Extract Perfmon counter instance as a tag (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadu authored Apr 22, 2020
1 parent 8b3faeb commit 139e7de
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Influxer.Test/Influxer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AdysTech.InfluxDB.Client.Net, Version=0.7.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AdysTech.InfluxDB.Client.Net.0.7.0\lib\net451\AdysTech.InfluxDB.Client.Net.dll</HintPath>
<Reference Include="AdysTech.InfluxDB.Client.Net, Version=0.11.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AdysTech.InfluxDB.Client.Net.0.11.1\lib\net451\AdysTech.InfluxDB.Client.Net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand Down
23 changes: 16 additions & 7 deletions Influxer.Test/InfluxerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void TestExportCommand()
[TestMethod]
public async Task TestGenericHeaderless()
{
var settings = InfluxerConfigSection.Load(Path.Combine(TestFilesPath, "HeaderlessText.conf"),true);
var settings = InfluxerConfigSection.Load(Path.Combine(TestFilesPath, "HeaderlessText.conf"), true);
settings.InputFileName = Path.Combine(TestFilesPath, "HeaderlessText.txt");
var client = await GetClientAsync(settings);
var file = new GenericFile();
Expand Down Expand Up @@ -158,15 +158,24 @@ public async Task TestPerfmonFileMultiMeasurement()
{
var settings = InfluxerConfigSection.LoadDefault();
settings.FileFormat = FileFormats.Perfmon;
settings.InfluxDB.RetentionDuration = (int)TimeSpan.FromDays(365*2).TotalMinutes;
settings.InfluxDB.RetentionPolicy = "autogen1";
//settings.InfluxDB.RetentionDuration = (int)TimeSpan.FromDays(365 * 2).TotalMinutes;
//settings.InfluxDB.RetentionPolicy = "autogen1";
settings.PerfmonFile.MultiMeasurements = true;

settings.InfluxDB.PointsInSingleBatch = 100;
settings.InputFileName = Path.Combine(TestFilesPath, "Perfmon.csv");
var client = await GetClientAsync(settings);
var result = await new PerfmonFile().ProcessPerfMonLog(settings.InputFileName, client);
//Debug.WriteLine (result.ToString ());
Assert.IsTrue(result.ExitCode == ExitCode.ProcessedWithErrors && result.PointsFound == 5347 && result.PointsFailed == 0, "Processing Perfmon file failed");
try
{
var result = await new PerfmonFile().ProcessPerfMonLog(settings.InputFileName, client);
//Debug.WriteLine (result.ToString ());
Assert.IsTrue(result.ExitCode == ExitCode.ProcessedWithErrors && result.PointsFound == 191 && result.PointsFailed == 0, "Processing Perfmon file failed");
}
catch (Exception e)
{
Assert.Fail("Unexpected exception of type {0} caught: {1}",
e.GetType(), e.Message);
return;
}
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions Influxer.Test/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AdysTech.InfluxDB.Client.Net" version="0.7.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net452" />
<package id="AdysTech.InfluxDB.Client.Net" version="0.11.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net452" />
</packages>
2 changes: 1 addition & 1 deletion Influxer/Config/InfluxDBConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public string Password
}

[CommandLineArg("-batch", Usage = "-batch <number of points>", Description = "No of points to send to InfluxDB in one request")]
[DefaultValue(Value = "128", Converter = Converters.IntParser)]
[DefaultValue(Value = "10000", Converter = Converters.IntParser)]
public int PointsInSingleBatch
{
get; set;
Expand Down
6 changes: 3 additions & 3 deletions Influxer/Influxer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
</PropertyGroup>
<PropertyGroup />
<ItemGroup>
<Reference Include="AdysTech.InfluxDB.Client.Net, Version=0.7.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AdysTech.InfluxDB.Client.Net.0.7.0\lib\net451\AdysTech.InfluxDB.Client.Net.dll</HintPath>
<Reference Include="AdysTech.InfluxDB.Client.Net, Version=0.11.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AdysTech.InfluxDB.Client.Net.0.11.1\lib\net451\AdysTech.InfluxDB.Client.Net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
1 change: 1 addition & 0 deletions Influxer/PerfmonCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal class PerfmonCounter
public string CounterName { get; set; }
public string Host { get; set; }
public string PerformanceObject { get; set; }
public string CounterInstance { get; set; }
}
}
46 changes: 31 additions & 15 deletions Influxer/PerfmonFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PerfmonFile
private int minOffset;

private Regex pattern;

private Regex instanceCapture;
private IInfluxRetentionPolicy policy = null;

private InfluxerConfigSection settings;
Expand Down Expand Up @@ -47,15 +47,25 @@ private List<PerfmonCounter> ParsePerfMonFileHeader(string headerLine, bool quot
var columns = pattern.Split(headerLine);
var column = 1;

perfCounters.AddRange(columns.Skip(quoted ? 1 : 0).Where(s => quoted ? s.StartsWith("\"\\") : s.StartsWith("\\")).Select(p =>
p.Replace(settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray(), settings.InfluxDB.InfluxReserved.ReplaceReservedWith).Split('\\')).Select(p =>
new PerfmonCounter()
{
ColumnIndex = column++,
Host = p[2].Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith),
PerformanceObject = p[3].Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith),
CounterName = p[4].Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith)
}));
perfCounters.AddRange(columns.Skip(quoted ? 1 : 0).Where(s => quoted ? s.StartsWith("\"\\") : s.StartsWith("\\")).Select(c =>
{
var p = c.Split('\\');
var pc = new PerfmonCounter()
{
ColumnIndex = column++,
Host = p[2],
CounterName = p[4].Replace(settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray(), settings.InfluxDB.InfluxReserved.ReplaceReservedWith).Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith)
};
if (instanceCapture.IsMatch(p[3])) {
var match = instanceCapture.Match(p[3]);
pc.PerformanceObject = match.Groups[1].Value.Replace(settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray(), settings.InfluxDB.InfluxReserved.ReplaceReservedWith).Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith);
pc.CounterInstance = match.Groups[2].Value.Replace(settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray(), settings.InfluxDB.InfluxReserved.ReplaceReservedWith).Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith);
}
else {
pc.PerformanceObject = p[3].Replace(settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray(), settings.InfluxDB.InfluxReserved.ReplaceReservedWith).Trim(settings.InfluxDB.InfluxReserved.ReplaceReservedWith);
}
return pc;
}));
return perfCounters;
}

Expand All @@ -68,7 +78,7 @@ private List<IInfluxDatapoint> ProcessPerfmonLogLine(string line, IEnumerable<IG
throw new FormatException("Couldn't parse " + columns[0] + " using format " + settings.PerfmonFile.TimeFormat + ", check -timeformat argument");
var utcTime = timeStamp.AddMinutes(minOffset);

var points = new List<IInfluxDatapoint>();
var points = new List<IInfluxDatapoint>(perfGroup.Count());

foreach (var performanceObject in perfGroup)
{
Expand All @@ -83,14 +93,17 @@ private List<IInfluxDatapoint> ProcessPerfmonLogLine(string line, IEnumerable<IG
MeasurementName = performanceObject.Key,
UtcTimestamp = utcTime
};

if (defaultTags.Count > 0) point.InitializeTags(defaultTags);
point.Tags.Add("Host", hostGrp.Key);

double value = 0.0;

foreach (var counter in hostGrp)
{
if (!point.Tags.ContainsKey("CounterInstance") && !string.IsNullOrWhiteSpace(counter.CounterInstance))
point.Tags.Add("CounterInstance", counter.CounterInstance);

if (!String.IsNullOrWhiteSpace(columns[counter.ColumnIndex]) && Double.TryParse(columns[counter.ColumnIndex], out value))
{
//Perfmon file can have duplicate columns!!
Expand Down Expand Up @@ -119,9 +132,10 @@ private List<IInfluxDatapoint> ProcessPerfmonLogLine(string line, IEnumerable<IG

if (defaultTags.Count > 0) point.InitializeTags(defaultTags);
point.Tags.Add("Host", hostGrp.Key);

point.Tags.Add("PerformanceObject", counter.PerformanceObject);
point.Tags.Add("PerformanceCounter", counter.CounterName);
if(!string.IsNullOrWhiteSpace(counter.CounterInstance))
point.Tags.Add("CounterInstance", counter.CounterInstance);
point.Fields.Add("CounterValue", value);
points.Add(point);
}
Expand All @@ -136,6 +150,8 @@ public PerfmonFile()
{
settings = InfluxerConfigSection.GetCurrentOrDefault();
pattern = new Regex(settings.PerfmonFile.ColumnSplitter, RegexOptions.Compiled);
instanceCapture = new Regex("(?<object>.+?)\\((?<instance>.*)\\)", RegexOptions.Compiled);

defaultTags = new Dictionary<string, string>();
if (settings.PerfmonFile.DefaultTags != null && settings.PerfmonFile.DefaultTags.Count > 0)
{
Expand Down Expand Up @@ -249,7 +265,7 @@ public async Task<ProcessStatus> ProcessPerfMonLog(string InputFileName, InfluxD
bool postresult = false;
try
{
postresult = await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points);
postresult = await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points, settings.InfluxDB.PointsInSingleBatch);
}
catch (ServiceUnavailableException)
{
Expand Down Expand Up @@ -298,7 +314,7 @@ public async Task<ProcessStatus> ProcessPerfMonLog(string InputFileName, InfluxD
{
if (await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points))
{
result.PointsProcessed += points.Count;
result.PointsProcessed += points.Count(p => p.Saved);
points.Clear();
}
else
Expand Down
8 changes: 4 additions & 4 deletions Influxer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Adys Tech")]
[assembly: AssemblyProduct("Influxer")]
[assembly: AssemblyCopyright("AdysTech © 2017")]
[assembly: AssemblyCopyright("AdysTech © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.9.5.0")]
[assembly: AssemblyFileVersion("0.9.5.0")]
[assembly: AssemblyInformationalVersion("0.9, Released : 12/31/2018")]
[assembly: AssemblyVersion("0.9.9.0")]
[assembly: AssemblyFileVersion("0.9.9.0")]
[assembly: AssemblyInformationalVersion("0.9.9, Released : 04/21/2020")]
4 changes: 2 additions & 2 deletions Influxer/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AdysTech.InfluxDB.Client.Net" version="0.7.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net451" />
<package id="AdysTech.InfluxDB.Client.Net" version="0.11.1" targetFramework="net451" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net451" />
</packages>

0 comments on commit 139e7de

Please sign in to comment.