-
Notifications
You must be signed in to change notification settings - Fork 29
/
trigger.go
104 lines (86 loc) · 2.18 KB
/
trigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"strconv"
"strings"
"time"
)
type Trigger struct {
ID string `json:"triggerid"`
Description string `json:"description"`
Hostname string `json:"host"`
Value string `json:"value"`
Comments string `json:"comments"`
Functions []Function `json:"functions"`
LastChange string `json:"lastchange"`
LastEvent struct {
ID string `json:"eventid"`
Acknowledged string `json:"acknowledged"`
} `json:"lastEvent"`
Hosts []struct {
Hostid string `json:"hostid"`
Name string `json:"name"`
} `json:"hosts"`
Priority string `json:"priority"`
}
func (trigger *Trigger) String() string {
return trigger.LastEvent.ID + " " +
trigger.Hostname + " " + trigger.Description
}
func (trigger *Trigger) GetHostName() string {
if len(trigger.Hosts) > 0 {
return trigger.Hosts[0].Name
}
return "<missing>"
}
func (trigger *Trigger) StatusAcknowledge() string {
if trigger.LastEvent.Acknowledged == "1" {
return "ACK"
}
return "NACK"
}
func (trigger *Trigger) StatusProblem() string {
if trigger.Value == "1" {
return "PROBLEM"
}
return "OK"
}
func (trigger *Trigger) Severity() Severity {
value, err := strconv.Atoi(trigger.Priority)
if err != nil {
debugf("Error: %+v", err)
}
return Severity(value)
}
func (trigger *Trigger) DateTime() string {
return trigger.date().Format("2006-01-02 15:04:05")
}
func (trigger *Trigger) Age() string {
date := time.Since(trigger.date())
var (
seconds = int(date.Seconds()) % 60
minutes = int(date.Minutes()) % 60
hours = int(date.Hours())
days = hours / 24
months = days / 30.
)
var units []string
units = addUnit(units, months, "mon")
units = addUnit(units, days%7, "d")
units = addUnit(units, hours%24, "h")
units = addUnit(units, minutes, "m")
units = addUnit(units, seconds, "s")
return strings.Join(units, " ")
}
func (trigger *Trigger) date() time.Time {
date, err := strconv.ParseInt(trigger.LastChange, 10, 64)
if err != nil {
debugf("Error: %+v", err)
}
return time.Unix(date, 0)
}
func addUnit(units []string, value int, unit string) []string {
if value > 1 {
units = append(units, strconv.Itoa(value)+unit)
}
return units
}