ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How do I use PagerDuty with C#?

Updated on June 7, 2015

What is PagerDuty?

PagerDuty is an alerting system that allows you to set up groups of people for alerts, escalations, and allows for multiple alerting methods. The purpose here is not to go into detail about PagerDuty itself, but give you a way to add a PagerDuty alert to an existing application using C#. This will allow you to take advantage of your company’s PagerDuty implementation from a wide variety of other applications.

Why alert?

The purpose of alerting us to notify someone that they need to do something. Usually this is a critical operation for the company. There are many ways to do alerting, probably the most common method is by email. With PagerDuty, there is an application, SMS text messaging option, or an email option. In addition, you can personally set multiple notification schemes. These can all go off at the same time, or you can set in interval between which each will go to you. This system also allows you to create escalations. For critical items. This means that someone else will be notified if you do not respond in a set period of time. That period of time is to find with in the web-based application.

What is required?

For the implementation I am going to discuss, I used the NuGet package RestSharp. I will discuss NuGet later on. RestSharp is a package that allows you to use rest API calls necessary in this case for dealing with PagerDuty.

From the PagerDuty side, you will need to create a service and have it give you an API key. If you’re familiar with PagerDuty, you probably know that you have a specific URL to use in order to look at your incidents. When you use the API call, however, you call a generic URL and it is this key that associates the incident with your system.

The Program

Before we get into the actual code, let’s take a look at what we are going to do.

  1. We need to set up the rest call by setting the client, the base URL, the response, and the data format, in this case, JSON.

  2. We then set the resource, which is the remainder of the URL.

  3. We then add the body of the request. In this case we will use the following pieces:

    1. The service key, which is the API key that you created at PagerDuty.

    2. The event type, which is always trigger.

    3. The description, what text you want to show.

    4. The incident key, which is the subject.

With these four pieces of information, we now have enough to create a request.

  1. We now set the method to post.

  2. And create our call.

  3. I also split off the incident key from the response. This allows me to return that value.

Now for the code, I will note that this is generic code available online as a demonstration on how to use RestSharp. I included here only to illustrate how to use it for PagerDuty.

This is also been put as a subroutine, to allow you to use it in multiple places.

public static string PagerDutySend(string APIKey, string Description, string Incident)
{
var client = new RestClient(); //("URL/create_event.json");

client.BaseUrl = "URL";

var request = new RestRequest();

string returnVal = "";

request.RequestFormat = DataFormat.Json;


request.Resource = "generic/2010-04-15/create_event.json";


request.AddBody(

new

{

service_key = APIKey,

event_type = "trigger",

description = Description,

incident_key = Incident

});

request.Method = Method.POST;


var response = client.Execute(request) as RestResponse;

if (response != null && ((response.StatusCode == HttpStatusCode.OK) &&

(response.ResponseStatus == ResponseStatus.Completed)))

// It's probably not necessary to test both

{

var content = response.Content;

string[] values = Regex.Split(content, "incident_key\":\"");

returnVal = values[1].Replace("\"}", "");


}

else if (response != null)

{

returnVal = string.Format

("Status code is {0} ({1}); response status is {2}",

response.StatusCode, response.StatusDescription, response.ResponseStatus);

}

return returnVal;

}


}

NuGet and RestSharp

NuGet is a repository of packages that you can use within Visual Studio. Within Visual Studio itself, is a manage NuGet packages. This makes it very easy to search for and add the proper resources to your project. If you do a search for RestSharp at that site, on the left is an ability to look at the project site. When you go to the project site, you will see sample code. You will notice that this sample code differs from the code that I used. I did start off attempting to use that code, I found it only by putting it together as a body would it reliably go to PagerDuty. The code I shared above is the code that I’m using in actual practice.

Summary

As you can see, you can easily add a PagerDuty module to your application. This will allow you to quickly and easily wrap alerting into any application. This will allow you to notify someone when they need to take action.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)