Create directories automatically

This commit is contained in:
2016-03-24 20:09:51 +01:00
parent 145def940a
commit 05bfb02c77
10 changed files with 181 additions and 84 deletions
-1
View File
@@ -46,7 +46,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntp.System", "Ntp.System\Nt
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{6D7079EB-9FEE-4EC7-AA28-3671C607FDC6}"
ProjectSection(SolutionItems) = preProject
example\ntp.conf = example\ntp.conf
example\ntpa.graph.conf = example\ntpa.graph.conf
example\ntpa.stat.conf = example\ntpa.stat.conf
example\ntpa.web.conf = example\ntpa.web.conf
@@ -4,7 +4,7 @@
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -27,14 +27,12 @@
using System;
using System.IO;
using Ntp.Analyzer.Log;
using Ntp.System;
namespace Ntp.Analyzer.Export
{
public class DirectoryStreamDestination : StreamDestination
{
private readonly string path;
private readonly string prefix;
/// <summary>
/// Initializes a new instance of the <see cref="Ntp.Analyzer.Export.DirectoryStreamDestination" /> class.
/// </summary>
@@ -43,10 +41,22 @@ namespace Ntp.Analyzer.Export
/// <param name="prefix">Prefix.</param>
public DirectoryStreamDestination(string basePath, string path, string prefix)
{
path = (path != null ? path.Trim () : String.Empty);
basePath = (basePath != null ? basePath.Trim () : String.Empty);
if (basePath.Length > 1 && path [path.Length - 1] != '/')
basePath = basePath + "/";
if (path.Length > 1 && path [path.Length - 1] != '/')
path = path + "/";
this.path = basePath + path;
this.prefix = prefix;
}
private readonly string path;
private readonly string prefix;
public override string Location
{
get { return path; }
@@ -59,16 +69,13 @@ namespace Ntp.Analyzer.Export
public override void Test()
{
const string testContent = "FileStreamDestinationTest";
string testFile = AbsoluteLocation + "configtest";
string testContent = "FileStreamDestinationTest";
if (File.Exists(testFile))
{
File.Delete(testFile);
}
File.WriteAllText(testFile, testContent);
File.Delete(testFile);
}
@@ -76,29 +83,38 @@ namespace Ntp.Analyzer.Export
{
string file = path + prefix + Param;
if (File.Exists(file))
{
File.Delete(file);
string created = null;
try {
created = DirectoryCommand.CreateRecursiveFromFile (file);
} catch (Exception e) {
log.WriteLine (String.Format (
"Could create path for file {0}", file),
Severity.Error);
log.WriteLine (e, Severity.Trace);
}
if (created != null) {
log.WriteLine (String.Format (
"Created new directory {0}", created),
Severity.Notice);
}
if (File.Exists (file))
File.Delete (file);
FileStream fileStream = null;
try
{
fileStream = new FileStream(file, FileMode.CreateNew);
stream.CopyTo(fileStream);
}
catch (Exception e)
{
log.WriteLine(e);
}
finally
{
try {
fileStream = new FileStream (file, FileMode.CreateNew);
stream.CopyTo (fileStream);
} catch (Exception e) {
log.WriteLine (String.Format (
"Could not write file {0}", file),
Severity.Error);
log.WriteLine (e, Severity.Trace);
} finally {
// Release memory
if (fileStream != null)
{
fileStream.Close();
if (fileStream != null) {
fileStream.Close ();
fileStream = null;
}
}
+40 -30
View File
@@ -4,7 +4,7 @@
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -27,20 +27,26 @@
using System;
using System.IO;
using Ntp.Analyzer.Log;
using Ntp.System;
namespace Ntp.Analyzer.Export
{
public class FileStreamDestination : StreamDestination
{
private readonly string file;
private readonly string path;
public FileStreamDestination(string path, string file)
{
path = (path != null ? path.Trim () : String.Empty);
if (path.Length > 1 && path [path.Length - 1] != '/')
path = path + "/";
this.path = path;
this.file = file;
}
private readonly string file;
private readonly string path;
public override string Location
{
get { return file; }
@@ -53,46 +59,50 @@ namespace Ntp.Analyzer.Export
public override void Test()
{
string testFile = AbsoluteLocation;
string testContent = "FileStreamDestinationTest";
const string testContent = "FileStreamDestinationTest";
string testFile = AbsoluteLocation + "-configtest";
if (File.Exists(testFile))
{
File.Delete(testFile);
}
File.WriteAllText(testFile, testContent);
File.Delete(testFile);
}
public override void Write(Stream stream, LogBase log)
{
string absFile = AbsoluteLocation;
if (File.Exists(absFile))
{
File.Delete(absFile);
string created = null;
try {
DirectoryCommand.CreateRecursiveFromFile (AbsoluteLocation);
} catch (Exception e) {
log.WriteLine (String.Format (
"Could create path for file {0}", AbsoluteLocation),
Severity.Error);
log.WriteLine (e, Severity.Trace);
}
if (created != null) {
log.WriteLine (String.Format (
"Created new directory {0}", created),
Severity.Notice);
}
if (File.Exists (AbsoluteLocation))
File.Delete (AbsoluteLocation);
FileStream fileStream = null;
try
{
fileStream = new FileStream(absFile, FileMode.CreateNew);
stream.CopyTo(fileStream);
}
catch (Exception e)
{
log.WriteLine(e);
}
finally
{
try {
fileStream = new FileStream (AbsoluteLocation, FileMode.CreateNew);
stream.CopyTo (fileStream);
} catch (Exception e) {
log.WriteLine (String.Format (
"Could not write file {0}", AbsoluteLocation),
Severity.Error);
log.WriteLine (e, Severity.Trace);
} finally {
// Release memory
if (fileStream != null)
{
fileStream.Close();
if (fileStream != null) {
fileStream.Close ();
fileStream = null;
}
}
+8 -8
View File
@@ -105,10 +105,10 @@ namespace Ntp.Analyzer.Graph
// Calculate mean value
for (int i = 0; i < Time.Count; i++) {
totalOffset += System.Math.Abs (Offset [i]);
totalJitter += System.Math.Abs (Jitter [i]);
totalFrequency += System.Math.Abs (frequency [i]);
totalStability += System.Math.Abs (stability [i]);
totalOffset += Math.Abs (Offset [i]);
totalJitter += Math.Abs (Jitter [i]);
totalFrequency += Math.Abs (frequency [i]);
totalStability += Math.Abs (stability [i]);
}
double avgOffset = totalOffset / Time.Count;
@@ -120,10 +120,10 @@ namespace Ntp.Analyzer.Graph
// Find invalid values
for (int i = 0; i < Time.Count; i++) {
if (System.Math.Abs (Offset [i]) > avgOffset * config.FilterFactor ||
System.Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor ||
System.Math.Abs (frequency [i]) > avgFrequency * config.FilterFactor ||
System.Math.Abs (stability [i]) > avgStability * config.FilterFactor) {
if (Math.Abs (Offset [i]) > avgOffset * config.FilterFactor ||
Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor ||
Math.Abs (frequency [i]) > avgFrequency * config.FilterFactor ||
Math.Abs (stability [i]) > avgStability * config.FilterFactor) {
indexes.Add (i);
}
}
+8 -8
View File
@@ -138,10 +138,10 @@ namespace Ntp.Analyzer.Graph
// Calculate mean value
for (int i = 0; i < Time.Count; i++) {
totalOffset += System.Math.Abs (Offset [i]);
totalJitter += System.Math.Abs (Jitter [i]);
totalDelay += System.Math.Abs (delay [i]);
totalBalance += System.Math.Abs (balance [i]);
totalOffset += Math.Abs (Offset [i]);
totalJitter += Math.Abs (Jitter [i]);
totalDelay += Math.Abs (delay [i]);
totalBalance += Math.Abs (balance [i]);
}
double avgOffset = totalOffset / Time.Count;
@@ -153,10 +153,10 @@ namespace Ntp.Analyzer.Graph
// Find invalid values
for (int i = 0; i < Time.Count; i++) {
if (System.Math.Abs (Offset [i]) > avgOffset * config.FilterFactor ||
System.Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor ||
System.Math.Abs (delay [i]) > avgDelay * config.FilterFactor ||
System.Math.Abs (balance [i]) > avgBalance * config.FilterFactor) {
if (Math.Abs (Offset [i]) > avgOffset * config.FilterFactor ||
Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor ||
Math.Abs (delay [i]) > avgDelay * config.FilterFactor ||
Math.Abs (balance [i]) > avgBalance * config.FilterFactor) {
indexes.Add (i);
}
}
+4
View File
@@ -190,5 +190,9 @@
<Project>{C2DEBED1-FE91-4929-B686-8E920731E88C}</Project>
<Name>NPlot</Name>
</ProjectReference>
<ProjectReference Include="..\Ntp.System\Ntp.System.csproj">
<Project>{A98DB601-2993-4181-A573-5FCCC5E954C1}</Project>
<Name>Ntp.System</Name>
</ProjectReference>
</ItemGroup>
</Project>
+76
View File
@@ -0,0 +1,76 @@
//
// DirectoryCommand.cs
//
// Author:
// carsten <${AuthorEmail}>
//
// Copyright (c) 2016 carsten
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace Ntp.System
{
public static class DirectoryCommand
{
public static string CreateRecursiveFromFile(string file)
{
int pos = file.LastIndexOf ('/');
if (pos == -1)
return null;
string path = file.Substring (0, pos);
return CreateRecursive (path);
}
#if __MonoCS__
public static string CreateRecursive(string path)
{
string fullPath = path.Trim ();
if (fullPath.Length <= 1)
return null;
string created = null;
char[] s = new char[] { '/' };
List<string> elements = fullPath.Split (s, StringSplitOptions.RemoveEmptyEntries).ToList ();
string currentPath = String.Empty;
foreach (string element in elements) {
currentPath = currentPath + "/" + element;
if (!Directory.Exists (currentPath)) {
Directory.CreateDirectory (currentPath);
created = currentPath;
}
}
return created;
}
#else
public static string CreateRecursive(string path)
{
Directory.CreateDirectory(path);
return null;
}
#endif
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
//
// InterLock.cs
// InterProcess.cs
//
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
+1
View File
@@ -45,6 +45,7 @@
<Compile Include="Options.cs" />
<Compile Include="VersionInfo.cs" />
<Compile Include="InterProcess.cs" />
<Compile Include="DirectoryCommand.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
-9
View File
@@ -1,9 +0,0 @@
################################################ support.ntp.ord ID ##############################
peer ntp2.innolan.net iburst maxpoll 10 # 0901 Denmark Copenhagen
server 192.36.144.22 iburst minpoll 10 # 0250 Sweden Stockholm, Netnod IX
...
driftfile /etc/ntp/drift
statsdir /etc/ntp/
statistics clockstats