IN THIS ARTICLE

    Time is essential in a server environment. And when it comes to a possible failure we need to act fast!

    In this tutorial, we will build a Windows service application that monitors server processing load and memory consumption, sending alerts to a Windows Phone 8.1, with all communication through PubNub's Data Stream Network.

    The service starts a thread that will get CPU consumption and available memory and compares to parameters. If the CPU usage is greater than a threshold (MaxCPUUsage) for a certain length of time (Period) then send a message with the type AlertType.PROCESS_ALERT to the channel.

    Likewise, if available memory is less than a threshold (MinRAMAvailable) then it will send a message with the type AlertType.RAM_ALERT to the channel.

    Real Time Process Monitoring Service

    First is a Windows service project in the created using Visual Studio 2015 and .Net Framework 4. This service will send alert messages to the **PNRTPM** channel.

    Configuring parameters

    Next use RegEdit to configure the service parameters.

    You can use RegEdit to configure tree parameters at
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PubnubRTPM
    

    MaxCPUUsage

    The max CPU usage to alert in % (default=50)

    MinRAMAvailable

    The min RAM available to alert in % (default=10)

    Period

    The period of time to watch continue CPU usage in seconds (default=60)

    Edit imagePath and configure your parameters
    Example: "C:\Directory_OF_Application\PubnubRTPM.exe" 60 20 60
    

     

    OnStart Service Method

    The OnStart method in the service.cs file is triggered when Windows starts the service.

    It subscribes to the PNRTPM channel and creates a thread to monitor the server while the service is running.

    Process Thread

    The Process thread is in the service.cs file and is also started from the OnStart service method.

    This thread uses a PerformanceCounter class to get the CPU usage and available memory and compares with parameters. If the CPU usage is greater than a threshold (MaxCPUUsage) for a certain length of time (Period) then it sends a message with the type AlertType.PROCESS_ALERT to the channel.

    Likewise, if available memory is less than a threshold (MinRAMAvailable) then it sends a message with the type AlertType.RAM_ALERT to the channel.

    Sending Alert Message

    The SendAlertMessage method is in the service.cs file and is started by the process thread.

    To compose an alert message, a list of RTPMProcess objects is created getting CPU usage and memory of each process.

    After that, a RTPMServer class is created with the alert type, date, server name, CPU usage, RAM available, RTPMProcessList and then sent to the channel.

    OnStop Service Method

    The OnStop method is in the service.cs file and is triggered when Windows stops the service.

    This method unsubscribes from the PNRTPM channel and finalizes the thread that was created to monitor the server.

    Real Time Process Monitoring Windows Phone 8.1

    The second part of our project is a Windows Phone 8.1 app created using Visual Studio 2015 and Windows Phone 8.1 SDK

    This app will receive alert messages from the Real Time Process Monitoring service. It can handle messages from more than one server, each over the PNRTPM channel.

    Real Time Process Monitoring

    The mainpage.xaml file below creates the app seen above using XAML notation. It creates a grid for messages and a button to start and stop monitoring.

    Start Monitoring

    When you click on the Start Monitoring button, it will subscribe to the PNRTPM channel and wait for an alert message.

    Stop Monitoring

    If you click on the Stop Monitoring button it will unsubscribe from the PNRTPM channel.

    Alert Message Received

    The ReceivedMessageCallbackWhenSubscribed method is in the mainpage.xaml.cs file and is triggered when there is a message on PNRTPM channel.

    Conclusion

    This tutorial showed you how to create a Windows service, collecting CPU and memory information, Windows Phone 8.1 SDK and send/receive messages using PubNub C# SDK. We collected CPU and memory information using the PerformanceCounter class and send messages using publish function.

    In the future you could add an e-mail notification and SMS message on the service project to notify the administrator about CPU and memory consumption. You could also improve the Windows Phone 8.1 project by adding a window where the user could see all server processes and find out which is consuming the CPU or memory.

    You can download the Real Time Process Monitoring service project [here]

    You can download the Real Time Process Monitoring Windows Phone 8.1 project [here]

    Try PubNub today!

    Build realtime applications that perform reliably and securely, at global scale.
    Try Our APIs
    Try PubNub today!
    More From PubNub