Skip to content

Commit

Permalink
allow do define a 'responsible' string per bp that can be used in the… (
Browse files Browse the repository at this point in the history
#5)

* allow do define a 'responsible' string per bp that can be used in the trigger subcommand

* allow to define a responsible string on each level (BP, KPI, SVC) and inherit from above if not defined

* allow to print responsible on run via -r/--resp flag

* updated doc to reflect 'responsible' string
  • Loading branch information
sontags authored Oct 20, 2017
1 parent 499e62e commit cf2bdb0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ id: app_x
# the availabilities section from the global configuration...? This links
# there.
availability: 9to5
# You can also specify a 'responsible' string. This string can then be used in
# the trigger template. This could be for example trigger a specific http
# end point, pass some uri parameters, send an email to a specific address etc.
# The 'responsible' string is inherited by its KPIs if not overwritten...
responsible: [email protected]
# Now the KPIs...
kpis:
-
Expand All @@ -164,6 +169,9 @@ kpis:
# need to be 'OK'
# * MINPERCENT x: As 'MIN', but in percent.
operation: OR
# Again, a 'responsible' string can be specified in order not to inherit
# from the parent BP.
responsible: [email protected]
# And now the processes. Host and service relate to how you named those
# things in your Icinga2 setup.
services:
Expand All @@ -173,7 +181,7 @@ kpis:
id: app_availability
operation: MINPERCENT 50
services:
- { host: app1.example.com, service: api_health }
- { host: app1.example.com, service: api_health, responsible: [email protected] }
- { host: app2.example.com, service: api_health }
- { host: app3.example.com, service: api_health }
- { host: app4.example.com, service: api_health }
Expand Down
50 changes: 31 additions & 19 deletions bp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ type BP struct {
Kpis []KPI `yaml:"kpis"`
AvailabilityName string `yaml:"availability"`
Availability Availability `yaml:"-"`
Responsible string `yaml:"responsible"`
}

func (bp BP) Status(ssp ServiceStatusProvider, pp PersistenceProvider, r rules.Rules) ResultSet {
rs := ResultSet{
Kind: "BP",
Name: bp.Name,
Id: bp.Id,
Children: []*ResultSet{},
Vals: make(map[string]bool),
Kind: "BP",
Responsible: bp.Responsible,
Name: bp.Name,
Id: bp.Id,
Children: []*ResultSet{},
Vals: make(map[string]bool),
}

ch := make(chan *ResultSet)
var calcValues []bool
for _, k := range bp.Kpis {
if k.Responsible == "" {
k.Responsible = bp.Responsible
}
go func(k KPI, ssp ServiceStatusProvider, pp PersistenceProvider, r rules.Rules) {
childRs := k.Status(ssp, pp, r)
ch <- &childRs
Expand Down Expand Up @@ -60,24 +65,29 @@ func (bp BP) Status(ssp ServiceStatusProvider, pp PersistenceProvider, r rules.R
}

type KPI struct {
Name string
Id string
Operation string
Services []Service
Name string
Id string
Operation string
Services []Service
Responsible string
}

func (k KPI) Status(ssp ServiceStatusProvider, pp PersistenceProvider, r rules.Rules) ResultSet {
rs := ResultSet{
Kind: "KPI",
Name: k.Name,
Id: k.Id,
Children: []*ResultSet{},
Vals: make(map[string]bool),
Kind: "KPI",
Responsible: k.Responsible,
Name: k.Name,
Id: k.Id,
Children: []*ResultSet{},
Vals: make(map[string]bool),
}

ch := make(chan *ResultSet)
var calcValues []bool
for _, s := range k.Services {
if s.Responsible == "" {
s.Responsible = k.Responsible
}
go func(s Service, ssp ServiceStatusProvider, pp PersistenceProvider, r rules.Rules) {
childRs := s.Status(ssp, pp, r)
ch <- &childRs
Expand Down Expand Up @@ -117,16 +127,18 @@ type SvcResult struct {
}

type Service struct {
Host string
Service string
Host string
Service string
Responsible string
}

func (s Service) Status(ssp ServiceStatusProvider, pp PersistenceProvider, r rules.Rules) ResultSet {
name := fmt.Sprintf("%s!%s", s.Host, s.Service)
rs := ResultSet{
Name: name,
Id: name,
Kind: "SVC",
Name: name,
Responsible: s.Responsible,
Id: name,
Kind: "SVC",
}
at, msg, vals, err := ssp.Status(s.Host, s.Service)
rs.Err = err
Expand Down
10 changes: 6 additions & 4 deletions cmd/bpmon/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

var (
printTimestamps bool
printValues bool
printTimestamps bool
printValues bool
printResponsible bool
)

var runCmd = &cobra.Command{
Expand Down Expand Up @@ -41,13 +42,14 @@ var runCmd = &cobra.Command{
if c.Influx.GetLastStatus {
rs.AddPreviousStatus(infl, c.Influx.SaveOK)
}
fmt.Println(rs.PrettyPrint(0, printTimestamps, printValues))
fmt.Println(rs.PrettyPrint(0, printTimestamps, printValues, printResponsible))
}
},
}

func init() {
RootCmd.AddCommand(runCmd)
runCmd.PersistentFlags().BoolVarP(&printTimestamps, "ts", "t", false, "print timestamps of measurement")
runCmd.PersistentFlags().BoolVarP(&printValues, "vals", "v", false, "print raw measurement results if available")
runCmd.PersistentFlags().BoolVarP(&printValues, "vals", "v", false, "print raw measurement results if available")
runCmd.PersistentFlags().BoolVarP(&printResponsible, "resp", "r", false, "print responsible of measurement")
}
8 changes: 6 additions & 2 deletions resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ type ResultSet struct {
StatusChanged bool
Err error
Output string
Responsible string
Children []*ResultSet
}

func (rs ResultSet) PrettyPrint(level int, ts bool, vals bool) string {
func (rs ResultSet) PrettyPrint(level int, ts bool, vals bool, resp bool) string {
ident := strings.Repeat(" ", level)
out := rs.Status.Colorize(fmt.Sprintf("%s%s %s is %v", ident, rs.Kind, rs.Name, rs.Status))
if rs.WasChecked {
Expand All @@ -40,6 +41,9 @@ func (rs ResultSet) PrettyPrint(level int, ts bool, vals bool) string {
timestamp := rs.At.Format("2006-01-02 15:04:05")
out += fmt.Sprintf(" (%s)", timestamp)
}
if resp {
out += fmt.Sprintf(" (Responsible: %s)", rs.Responsible)
}
if rs.Err != nil {
out += fmt.Sprintf("\n%sError occured: %s", ident, rs.Err.Error())
}
Expand All @@ -65,7 +69,7 @@ func (rs ResultSet) PrettyPrint(level int, ts bool, vals bool) string {
}
out += "\n"
for _, childRs := range rs.Children {
out += childRs.PrettyPrint(level+1, ts, vals)
out += childRs.PrettyPrint(level+1, ts, vals, resp)
}
return out
}
Expand Down

0 comments on commit cf2bdb0

Please sign in to comment.