// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace Spludlow.Io
{
///
/// Save disk usage stats and report
///
public class DiskUsage
{
private static string _DataFilename = Spludlow.Config.ProgramData + @"\Data\DiskUsage.txt";
///
/// Call periodically using the sceduler to query all hosts disk usgae statistics and save to Text Table for later reporting
///
public static void Sample()
{
DataTable table = Spludlow.Data.TextTable.ReadText(new string[]
{
"Host Time Name UsedPercent VolumeLabel RootDirectoryFullName DriveType DriveFormat AvailableFreeSpace TotalFreeSpace TotalSize",
"String DateTime String Decimal String String String String Int64 Int64 Int64",
});
string[] hosts = Spludlow.Config.Hosts();
Dictionary results = new Dictionary();
Task[] tasks = new Task[hosts.Length];
for (int index = 0; index < hosts.Length; ++index)
{
string host = hosts[index];
results.Add(host, null);
tasks[index] = new Task(() => SampleWorker(host, results));
}
foreach (Task task in tasks)
task.Start();
Task.WaitAll(tasks);
DateTime sampleTime = DateTime.Now;
foreach (string host in results.Keys)
{
Spludlow.SysTables.DriveInfosDataTable drivesTable = new SysTables.DriveInfosDataTable();
drivesTable.Columns.Add("Host", typeof(string));
foreach (DataRow row in results[host].Tables["DriveInfos"].Rows)
drivesTable.ImportRow(row);
foreach (Spludlow.SysTables.DriveInfosRow driveRow in drivesTable.Rows)
{
if (driveRow.DriveType != "Fixed")
continue;
table.ImportRow(driveRow);
DataRow row = table.Rows[table.Rows.Count - 1];
row["Host"] = host;
row["Time"] = sampleTime;
}
}
// Write data to text table in append mode using a locked table
using (Spludlow.Data.LockedTextTable lockedTable = new Data.LockedTextTable(_DataFilename, true, table))
{
foreach (DataRow row in table.Rows)
lockedTable.Table.ImportRow(row);
}
Spludlow.Log.Info("DiskUsage Samples Saved", table);
}
private static void SampleWorker(string host, Dictionary results)
{
results[host] = Spludlow.Io.DirectoryList.ListDrives(host);
}
public static void Report()
{
DataTable table = Spludlow.Data.TextTable.ReadFile(_DataFilename);
List hosts = new List();
foreach (DataRow row in table.Rows)
{
}
}
}
}