Skip to content

Commit

Permalink
configurable title using templates
Browse files Browse the repository at this point in the history
  • Loading branch information
frese committed Nov 2, 2020
1 parent f4c6dbd commit 4839876
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ token = your-secret-token-here
# default interval between poll's is 30 sec.
interval = 30
# set title of incident notification, use golang text/template references to
# "github.com/PagerDuty/go-pagerduty/incident.go" Incident struct
title = "Incident {{.Status}} at {{.CreatedAt | format}} ({{.Service.Summary}})"
# Optional query parameters for the Pagerduty API
# Use comma separated lists of names (for users also email). You will usually only use one of them.
# If left blank, you will receive all incidents from the account.
Expand Down Expand Up @@ -76,6 +80,6 @@ with the time of last seen alert. This is to make sure that you will be notified

## Future features

- Pause the alert notifications.
- Chose between multiple pause intervals from menu
- Acknowledge an alert directly from the notification.
- Resend last alert
- Overview of alerts from today in menu
44 changes: 35 additions & 9 deletions src/pd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"bytes"
"fmt"
"log"
"os"
"regexp"
"text/template"
"time"

"github.com/PagerDuty/go-pagerduty"
Expand All @@ -19,14 +21,20 @@ var teamIDs = []string{}
var userIDs = []string{}
var serviceIDs = []string{}

var includes = []Filter{}
var excludes = []Filter{}
var includeFilters = []Filter{}
var excludeFilters = []Filter{}
var location = time.Local
var titleTemplate *template.Template = nil

func format(str string) (string, error) {
date, _ := time.Parse(time.RFC3339, str)
return date.In(location).Format("15:04"), nil
}

func pdInit(cfg *ini.File) {

includes = filterInit("include", cfg)
excludes = filterInit("exclude", cfg)
includeFilters = filterInit("include", cfg)
excludeFilters = filterInit("exclude", cfg)
pd = pagerduty.NewClient(cfg.Section("pagerduty").Key("token").String())
timezone := cfg.Section("main").Key("timezone").String()
if timezone != "" {
Expand Down Expand Up @@ -87,6 +95,18 @@ func pdInit(cfg *ini.File) {
serviceIDs = append(serviceIDs, s.ID)
}
}

// setup and parse title template if any
var fm = make(template.FuncMap)
fm["format"] = format
title := cfg.Section("pagerduty").Key("title").String()
if title != "" {
titleTemplate, err = template.New("title").Funcs(fm).Parse(title)
if err != nil {
log.Printf("Error parsing title template: %s", err)
os.Exit(1)
}
}
}

func pdGetIncidents(cfg *ini.File) []pagerduty.Incident {
Expand All @@ -104,10 +124,10 @@ INCIDENTS:
log.Printf("Service: %s", i.Service.Summary)

// check include filter
if len(includes) == 0 {
if len(includeFilters) == 0 {
goto EXCLUDES
}
for _, filter := range includes {
for _, filter := range includeFilters {
switch filter.property {
case "service":
if (filter.notmatch && (i.Service.Summary != filter.match)) || (!filter.notmatch && (i.Service.Summary == filter.match)) {
Expand All @@ -133,7 +153,7 @@ INCIDENTS:

// check exclude filter
EXCLUDES:
for _, filter := range excludes {
for _, filter := range excludeFilters {
switch filter.property {
case "service":
if (filter.notmatch && i.Service.Summary != filter.match) || (!filter.notmatch && (i.Service.Summary == filter.match)) {
Expand Down Expand Up @@ -205,12 +225,18 @@ func pdNotify(i pagerduty.Incident) {

date, _ := time.Parse(time.RFC3339, i.CreatedAt)
reg := regexp.MustCompile(`\[#(\d+)\] (.+)`)

if titleTemplate != nil {
var tpl bytes.Buffer
titleTemplate.Execute(&tpl, i)
title = tpl.String()
} else {
title = fmt.Sprintf("Incident at %s (%s)", date.In(location).Format("15:04"), i.Status)
}
match := reg.FindStringSubmatch(i.APIObject.Summary)
if match != nil {
title = fmt.Sprintf("Incident at %s (%s)", date.In(location).Format("15:04"), i.Status)
message = removeCharacters(match[2], "[]")
} else {
title = fmt.Sprintf("Incident %s", i.Status)
message = removeCharacters(i.APIObject.Summary, "[]")
}
image := trayhost.Image{}
Expand Down

0 comments on commit 4839876

Please sign in to comment.