Pingdom Maintenance Window using C#

In this post we will create a simple console app to pause and unpause all the Pingdom checks, this can then be used to create a maintenance window for other tasks to happen without effecting your up time reports.

What is Pingdom?

Pingdom is a uptime and performance monitoring service that has monitoring probe servers all over the world.

How to create a Maintenance Window?

Using the Pingdom REST API we can modify all the checks in one call

´╗┐namespace PingdomMaintenanceWindowApp
{
    using System;
    using System.Configuration;
    using System.Text;

    using RestSharp;

    public class PingdomMaintenanceWindow
    {
        private string AppKey
        {
            get
            {
                return ConfigurationManager.AppSettings["app-key"];
            }
        }

        private string Username
        {
            get
            {
                return ConfigurationManager.AppSettings["authorization.username"];
            }
        }

        private string Password
        {
            get
            {
                return ConfigurationManager.AppSettings["authorization.password"];
            }
        }

        public string Message
        {
            get
            {
                return message;
            }
        }

        private const string Host = "https://api.pingdom.com:443";

        private const string Resource = "/api/2.0/checks";

        private readonly RestClient client;

        private readonly RestRequest request;

        private string message;

        public PingdomMaintenanceWindow()
        {
            client = new RestClient(Host);
            client.AddDefaultHeader("Accept","*/*");
            client.AddDefaultHeader("App-Key", AppKey);
            client.Authenticator = new HttpBasicAuthenticator(Username, Password);

            request = new RestRequest(Resource, Method.PUT);
        }

        public void Start()
        {
            PauseChecks(true);
        }

        public void Stop()
        {
            PauseChecks(false);
        }

        private void PauseChecks(bool is_paused)
        {
            request.AddParameter("paused", is_paused ? "true" : "false");

            var response = client.Execute(request);

            if (response.ResponseStatus == ResponseStatus.Completed)
            {
                message = response.Content;
            }
            else
            {
                message = response.ErrorMessage;

                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }
            }
        }
    }
}

The console app provides a little bit of simple UX

´╗┐using System;

namespace PingdomMaintenanceWindowApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var command = "";

            if (args.Length == 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Pingdom Maintenance Window options:");
                Console.WriteLine("");
                Console.WriteLine("start");
                Console.WriteLine("stop");
                Console.WriteLine("");
                Console.WriteLine("e.g. PingdomMaintenanceWindowApp.exe start");
                Console.WriteLine("");
                Console.WriteLine("Or type command now and press enter to proceed...");
                command = Console.ReadLine();
            } 
            else
            {
                command = args[0];
            }

            var maintenanceWindow = new PingdomMaintenanceWindow();

            if(command == "start")
            {
                Console.WriteLine("Starting maintenance window...");
                maintenanceWindow.Start();
            } 
            else if (command == "stop")
            {
                Console.WriteLine("Stopping maintenance window...");
                maintenanceWindow.Stop();
            }
            else
            {
                Console.WriteLine("Unknown command: {0}", command);
                return;
            }

            Console.WriteLine("");
            Console.WriteLine(maintenanceWindow.Message);
            
        }
    }
}

Or to use the menu system just run the app.

You can download the code from GitHub



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.