forked from kovetskiy/zabbixctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
53 lines (43 loc) · 1 KB
/
item.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
var (
reItemKeyParams = regexp.MustCompile(`\[([^\]]+)\]`)
)
type Item struct {
ID string `json:"itemid"`
HostID string `json:"hostid"`
Name string `json:"name"`
ValueType string `json:"value_type"`
LastValue string `json:"lastvalue"`
LastChange string `json:"lastclock"`
Key string `json:"key_"`
Type ItemType `json:"type"`
}
func (item *Item) DateTime() string {
if item.LastChange == "0" {
return "-"
}
return item.date().Format("2006-01-02 15:04:05")
}
func (item *Item) date() time.Time {
date, _ := strconv.ParseInt(item.LastChange, 10, 64)
return time.Unix(date, 0)
}
func (item *Item) Format() string {
name := item.Name
match := reItemKeyParams.FindStringSubmatch(item.Key)
if len(match) == 0 {
return name
}
args := strings.Split(match[1], ",")
for index, arg := range args {
name = strings.Replace(name, fmt.Sprintf(`$%d`, index+1), arg, -1)
}
return name
}