forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lld_rule_test.go
94 lines (82 loc) · 2.22 KB
/
lld_rule_test.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
package zabbix_test
import (
"testing"
dd "github.com/claranet/go-zabbix-api"
zapi "github.com/claranet/go-zabbix-api"
)
func testCreateLLDRule(template *dd.Template, t *testing.T) *dd.LLDRule {
lldrulefiltercondition := dd.LLDRulesFilterCondition{
LLDMacro: "{#MY_MACRO_NAME_FD}",
Value: "^lo$",
Operator: 8,
}
lldrulefilter := dd.LLDRuleFilter{
Conditions: dd.LLDRulesFilterConditions{lldrulefiltercondition},
EvalType: 0,
}
lldrules := dd.LLDRules{{
Delay: "34",
HostID: template.TemplateID,
InterfaceID: "0",
Key: "lld_rule_key",
Name: "My low level discovery rule",
Type: dd.ZabbixAgent,
Filter: lldrulefilter,
}}
err := testGetAPI(t).DiscoveryRulesCreate(lldrules)
if err != nil {
t.Fatal(err)
}
return &lldrules[0]
}
func testDeleteLLDRule(rule *dd.LLDRule, t *testing.T) {
err := testGetAPI(t).DiscoveryRulesDelete(dd.LLDRules{*rule})
if err != nil {
t.Fatal(err)
}
}
func TestLLDRule(t *testing.T) {
api := testGetAPI(t)
// Zabbix v6.2 introduced Template Groups and requires them for Templates
var groupIds zapi.HostGroupIDs
if compLessThan, _ := isVersionLessThan(t, "6.2"); compLessThan {
hostGroup := testCreateHostGroup(t)
defer testDeleteHostGroup(hostGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: hostGroup.GroupID,
},
}
} else {
templateGroup := testCreateTemplateGroup(t)
defer testDeleteTemplateGroup(templateGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: templateGroup.GroupID,
},
}
}
template := testCreateTemplate(&groupIds, t)
defer testDeleteTemplate(template, t)
lldRule := testCreateLLDRule(template, t)
getlldRule, err := api.DiscoveryRulesGet(dd.Params{"itemids": lldRule.ItemID})
if err != nil {
t.Error(err)
}
if len(getlldRule) != 1 {
t.Errorf("Expecting one result and got : %d", len(getlldRule))
}
lldRule.Name = "update_lld_name"
err = api.DiscoveryRulesUpdate(dd.LLDRules{*lldRule})
if err != nil {
t.Error(err)
}
updateRule, err := api.DiscoveryRulesGetByID(lldRule.ItemID)
if err != nil {
t.Error(err)
}
if updateRule.Name != lldRule.Name {
t.Errorf("LLD rule name is %q and should be %q", updateRule.Name, lldRule.Name)
}
testDeleteLLDRule(lldRule, t)
}