I'm currently working on a 'monitoring' solution. The idea is to set eyes on a remote ressource and alert user when avaiable disk space goes below a defined threshold.
The app is a Windows service. A timer is launched and make a WQL request every x seconds. The request contains metrics which I then use further in my code (e.g. total disk size, space avaiable).
I've been testing the whole thing in a console application and it works just fine. But when I implement it in my Win service the WQL request goes wild and return a null element.
How come ? Am I missing something ?
Here is my code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace DatafactMonitoring
{
public partial class DatafactMonitoring : ServiceBase
{
public DatafactMonitoring()
{
InitializeComponent();
//Creating the event log entry
eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("ServiceMonitoringDatafact"))
{
System.Diagnostics.EventLog.CreateEventSource("ServiceMonitoringDatafact", "Événements Monitoring Datafact");
}
eventLog1.Source = "ServiceMonitoringDatafact";
eventLog1.Log = "Événements Monitoring Datafact";
}
//Parameters
protected static int threshold = 10;
//Declarations
private static int indexLog = 0;
protected static float diskUsage;
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Starting the Datafact monitoring service...");
// Timer that triggers OnTimer function every 60 seconds
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 10000; // 10 seconds, to be changed
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
public ManagementObject GetMetrics()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\ad", options);
scope.Connect();
SelectQuery query1 = new SelectQuery("Select Name, Size, FreeSpace from Win32_LogicalDisk Where DeviceID = 'P:'");
eventLog1.WriteEntry("Requete...");
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
ManagementObject mo = queryCollection1.OfType<ManagementObject>().First();
return mo;
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
ManagementObject datafactMetrics = GetMetrics();
//Metrics parsing
string diskName = datafactMetrics["Name"].ToString();
eventLog1.WriteEntry(diskName);
float diskSize = float.Parse(datafactMetrics["Size"].ToString());
float freeSpace = float.Parse(datafactMetrics["FreeSpace"].ToString());
diskUsage = (freeSpace / diskSize) * 100;
indexLog += 1;
eventLog1.WriteEntry("Log n°" + indexLog + " - Monitoring Datafact server - Space avaiable : " + freeSpace + "Go", EventLogEntryType.Information);
if (diskUsage >= threshold)
{
try
{
//TODO: Change url to SMS sender
System.Diagnostics.Process.Start("http://www.google.com");
eventLog1.WriteEntry("Space avaiable below " + threshold + "% (Disk usage : " + diskUsage + "%) - Mail & SMS sent to the team", EventLogEntryType.Warning);
}
catch (Exception ex)
{
eventLog1.WriteEntry("Error : " + ex.ToString(), EventLogEntryType.Error);
}
}
else
{
eventLog1.WriteEntry("Disk usage : " + diskUsage + "%", EventLogEntryType.Information);
}
}
protected override void OnStop()
{
eventLog1.WriteEntry("Datafact monitoring service stopped.");
}
}
}