Skip to content

Commit

Permalink
[SmarterMail100] Added support for calculating disk space and bandwidth
Browse files Browse the repository at this point in the history
Tested with SmarterMail Free 100.0.9088.25586 (Nov 18, 2024)
  • Loading branch information
FuseCP-TRobinson committed Nov 22, 2024
1 parent 0f7a39e commit 81cdd82
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Version 1.5.0
- [SmarterMail100] Fix for Deleting Space and/or User Account in SCP does not remove the domain off the SM server
- [SmarterMail100] Fixed creating groups
- [SmarterMail100] Added tinymce to the Signature and Autoresponder messages so they can be html when using latest SmarterMail
- [SmarterMail100] Added support for calculating diskspace and bandwidth


Version 1.4.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,129 @@ public override void DeleteServiceItems(ServiceProviderItem[] items)
}
}

public override ServiceProviderItemDiskSpace[] GetServiceItemsDiskSpace(ServiceProviderItem[] items)
{
List<ServiceProviderItemDiskSpace> itemsDiskspace = new List<ServiceProviderItemDiskSpace>();

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.DateSeparator = "-";

DateTime date = DateTime.Now;

// update items with diskspace
foreach (ServiceProviderItem item in items)
{
if (item is MailAccount)
{
try
{
var userstatsPram = new
{
email = item.Name
};

dynamic result = ExecDomainPostCommand("report/user-stats/" + date.ToString("d", dtfi) + "/" + date.ToString("d", dtfi), GetDomainName(item.Name), userstatsPram).Result;

bool success = Convert.ToBoolean(result["success"]);
if (!success)
throw new Exception(result["message"]);

Log.WriteStart(String.Format("Calculating mail account '{0}' size", item.Name));
// calculate disk space
ServiceProviderItemDiskSpace diskspace = new ServiceProviderItemDiskSpace();
diskspace.ItemId = item.Id;
//diskspace.DiskSpace = 0;
diskspace.DiskSpace = result.bytesSize;
itemsDiskspace.Add(diskspace);
Log.WriteEnd(String.Format("Calculating mail account '{0}' size", item.Name));
}
catch (Exception ex)
{
Log.WriteError(ex);
}
}
}
return itemsDiskspace.ToArray();
}

public override ServiceProviderItemBandwidth[] GetServiceItemsBandwidth(ServiceProviderItem[] items, DateTime since)
{
ServiceProviderItemBandwidth[] itemsBandwidth = new ServiceProviderItemBandwidth[items.Length];

// update items with diskspace
for (int i = 0; i < items.Length; i++)
{
ServiceProviderItem item = items[i];

// create new bandwidth object
itemsBandwidth[i] = new ServiceProviderItemBandwidth();
itemsBandwidth[i].ItemId = item.Id;
itemsBandwidth[i].Days = new DailyStatistics[0];

if (item is MailDomain)
{
try
{
// get daily statistics
Log.WriteInfo("[Smartermail100] Calculating Bandwidth for domain {0}", item.Name);
itemsBandwidth[i].Days = GetDomainStatistics(since, item.Name);
}
catch (Exception ex)
{
Log.WriteError(ex);
System.Diagnostics.Debug.WriteLine(ex);
}
}
}

return itemsBandwidth;
}

public DailyStatistics[] GetDomainStatistics(DateTime since, string maildomainName)
{

ArrayList days = new ArrayList();
// read statistics
DateTime now = DateTime.Now;
DateTime date = since;

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.DateSeparator = "-";

try
{
while (date < now)
{
dynamic result = ExecGetCommand("report/domain-stats/" + date.ToString("d", dtfi) + "/" + date.ToString("d", dtfi) + "/" + maildomainName).Result;

bool success = Convert.ToBoolean(result["success"]);
if (!success)
throw new Exception(result["message"]);

if (result.bytesReceived != 0 | result.bytesSent != 0)
{
DailyStatistics dailyStats = new DailyStatistics();
dailyStats.Year = date.Year;
dailyStats.Month = date.Month;
dailyStats.Day = date.Day;
dailyStats.BytesSent = result.bytesSent;
dailyStats.BytesReceived = result.bytesReceived;
days.Add(dailyStats);
}

// advance day
date = date.AddDays(1);
}
}
catch (Exception ex)
{
Log.WriteError("Could not get SmarterMail domain statistics", ex);
}
return (DailyStatistics[])days.ToArray(typeof(DailyStatistics));
}

#endregion

#region Mail Accounts
Expand Down

0 comments on commit 81cdd82

Please sign in to comment.