Skip to main content

Hi there!


My name is Martin Stone, and I’m a PagerDuty Solution Architect. I want to share a little side project that I’ve been working on for while, because it’s starting to get pretty useful. It’s a new command line interface (CLI) for PagerDuty.


There are lots of ways to interact with PagerDuty programmatically, but I wanted to make something that was user-focused, easy to install, and easy to use for common PagerDuty tasks. Among other things, it’s good at user and admin tasks like “acknowledge and snooze all my incidents,” “put a bunch of services into maintenance mode,” “output a CSV of some or all users,” and so on. It’s super easy to install and get started.


My PagerDuty CLI is open source and it’s implemented in TypeScript using the oclif framework.


I wrote a user guide that covers installation and gives some examples; you can find that here. The source (and a complete listing of available commands) is published here.


I’m sure I don’t need to tell you that this is just third party contributed software, it’s not endorsed or supported by PagerDuty, and you’re using it at your own risk. But I’m telling you anyway because those are the facts 🙂


I hope you check it out and let me know what you think - together we can make something really great!

This is great work, thanks for sharing!


Is this implemented in PagerDuty’s Terraform provider as the underlying client?


Great.


Hope it will be added to this page to help PagerDuty users to find it more easily: https://developer.pagerduty.com/docs/tools-libraries/client-libraries/


Hi @chiedu,


I missed your answer. But as I’m looking again at the PagerDuty CLI, I hope you (or any other Dutonian) will be able to share info for it on a visible place.

You mentioned it’s not supported by the company. Yes, as PHP API library but it’s available (and support clearly mentioned) there: https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTg2-api-client-libraries


Regards,

Sébastien


Hi @martin -


Can you please help me to get the Pagerduty CLI .PY file for below output -


List of Incident



  • Incident Number

  • Incident created date and time

  • Incident Ack by

  • Incident Ack Date and Time

  • Incident Resolve by

  • Incident Resolve Date and time

  • Incident Notes


For Specific Escalation Policy

For Specific Date or duration


Hi Vishal,


It’s a little bit more work because the incident record itself does not have information about the acknowledgers and resolvers, but here’s a Python script that does the work of stitching that information together from the incident log entries:




You would just need to make a couple of trivial changes to add incident number and filter on a specific escalation policy. Let me know if you need any additional help to do that.


Thanks!

Martin


Thanks @martin.


Currently I am using below format to run the script to get the PD report output.


user-tags.txt (1.0 KB)


py path\\File_name.py --output_file path\\File_name.csv


Can you please share something similar, if possible.


Hi @vishal garg,


I am not entirely sure what you are asking, but to get script I provided, you can:



In unix/linux/macos, you would then open a terminal in the directory where you saved the file, make the file executable with chmod +x basic_incident_details.py and then run it with some example arguments with ./basic_incident_details.py -o incidents.csv --start "30 days ago" --end now


It looks like you’re using Microsoft Windows; I am not able to advise you on how to run Python scripts on Windows in general, but I do know that it is possible. You would use the same kinds of arguments as I provided above.


Please let me know if I can help any further…


Thanks,

Martin


Thanks @martin, Appreciate your help as always. I will share the results.


Hi @martin - I have tried this but seems like results are not executing.


I am running on windows CMD -


pd auth:get

pd login


After executing below command -


C:\\WINDOWS\\system32>py C:\\Users\\gargv\\Downloads\\pd-cli-scripts\\PD_Incidents_Notes.py --output_file C:\\Users\\gargv\\Downloads\\pd-cli-scripts\\Incident_notes.csv

Getting incidents… didn’t get any


Just FYI - I can execute the below attached User Tags file.


user-tags.txt (1.0 KB)


Hi Vishal, I tried this on my mac and it works fine. I bet it’s something to do with the python libraries for windows and was going to try on my windows VM but want to ensure I’m using the same python you are. Let me know which python version you’re using and I’ll test it out on my VM.

Also, I assume you’re using Martins latest version of pd?


Thanks @jcalcada. @vishal garg, you can check the version of pd that you have by typing:


pd version


But also note that you did not specify a time range in your arguments to the Python script, and the script is saying that it didn’t get any incidents. The default time range for the script is the past one day. Do you have incidents in the past one day? If not, try making a longer time range by supplying the --start argument, for example --start "14 days ago"


If that doesn’t work, please let me know the version of pd you are using, and whether the following command gives you any incidents:


pd incident:list -s open -s closed --since "one day ago" --until "now"


this is the same pd command that is being used inside the Python script to get the list of incidents.


Hi @martin@jcalcada.


Thanks for your response.


I have enclosed the output of the commands that has been asked. I am able to successfully run this command.


pd incident:list -s open -s closed --since “one day ago” --until "now"PD incident Notes.txt (37.2 KB)


Hi Martin -

Hope you are doing good.


I have successfully run this command but not the others previously shared and output with successful output have shared in my previous notes.


pd incident:list -s open -s closed --since “one day ago” --until "now


Now Can you please share how to add below -



  • Incident Number

  • Incident created date and time

  • Incident Ack by

  • Incident Ack Date and Time

  • Incident Resolve by

  • Incident Resolve Date and time

  • Incident Notes


Hi @vishal garg,


Apologies for the delay in responding. I believe the problem is the way that Microsoft Windows handles quotation marks at the command prompt. Could you please try this modified version of the example script:




Just save that file and run it the same way you ran basic_incident_details.py, and let me know what you observe.


Thanks!


Hi @martin - Awesome and you rocks. This time it works. attached is the output file.IncidentNOTES.xlsx (27.3 KB)


C:\\WINDOWS\\system32>py C:\\Users\\gargv\\Downloads\\pd-cli-scripts\\Incident-Notes-New.py --output_file C:\\Users\\gargv\\Downloads\\pd-cli-scripts\\IncidentNOTES.csv --start “1 day ago”


Getting incidents… got 134

Getting incident logs… got 1374 log entries

Wrote 134 incidents to C:\\Users\\gargv\\Downloads\\pd-cli-scripts\\IncidentNOTES.csv.


Now Can you please do let me know if I need to export the Incidents with specific Service and customized dates, what to add in the command.


Hi @martin,


The modified version of the file has worked last time.


Now can you please share if I need to run this file for specific Escalation Policy and Customized time frame, how can I do that.


For example -

Service Name - Maintenance Alerts

Duration 1 Jan 2023 - 31 Jan 2023


Hi @martin, Hope you are dong well. Just checking if you can consider my small request regarding reporting


Hi @martin @jcalcada - Just checking if you can help me to get the report with * Specific to Escalation Policy and Customized Date/time.


The basic_incident_details routine looks at incident details but unfortunately the Escalation Policy isn’t part of the payload. To get that information you’ll need to look at the service associated with the incident and then lookup the EP from there.


The routine allows you to use a customized date/time instead of the pd-cli tools easier to use syntax such as “yesterday”. You can get more details on how to customize the date here https://developer.pagerduty.com/api-reference for more details.


Thanks @jcalcada - Apologies for the wrong question asked.


Can you please add specific service ( for example - Test), what should be the code looks like.


CC - @martin


Hi @martin @jcalcada - Let me know if I need to add service into the script -


Let me what I am missing here to get the service name in the Report.


image

image


C:\\Users\\gargv>py C:\\PD_Scripts\\Incident-Notes-New3.py --services=MiCloudFlex_MiNOC --output_file C:\\PD_Scripts\\TestIncidentnotes4.csv --start “1 day ago”

Getting incidents… got 116

Getting incident logs… got 1163 log entries

Traceback (most recent call last):

File “C:\\PD_Scripts\\Incident-Notes-New3.py”, line 81, in

incident ‘services’],

KeyError: ‘services’


Hi @jcalcada @martin - Appreciate your help here as looking for the solution on this Pagerduty reporting


If you run a GET API call on an incident (you can use the snippet below for example output), you’ll see it contains the service name and id. You’ll need to factor that into your code and you can certainly get that information from incident details. If you need help with developing a more robust solution one option you have is to leverage our professional services organization who would be happy to create something for you for a nominal cost.


echo “enter incident id:”

read A

curl --request GET

–url “https://api.pagerduty.com/incidents/{$A}?include{]=external_references

–header ‘Accept: application/vnd.pagerduty+json;version=2’

–header ‘Authorization: Token token=yourkey’

–header ‘Content-Type: application/json’


Hi Thanks @jcalcada - Just checking the cost for PD Professional services to create a one time solution in PD CLI for Reporting purpose.


Great. Your PagerDuty account executive can provide information on the professional services options available to you.


Reply