forked from kovetskiy/zabbixctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.go
98 lines (80 loc) · 2.09 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
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, _ := strconv.Atoi(trigger.Priority)
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.Now().Sub(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, _ := strconv.ParseInt(trigger.LastChange, 10, 64)
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
}