This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package zabbix | ||
|
||
import ( | ||
"fmt" | ||
"github.com/AlekSi/reflector" | ||
) | ||
|
||
type ( | ||
ItemType int | ||
ValueType int | ||
DataType int | ||
DeltaType int | ||
) | ||
|
||
const ( | ||
ZabbixAgent ItemType = 0 | ||
SNMPv1Agent ItemType = 1 | ||
ZabbixTrapper ItemType = 2 | ||
SimpleCheck ItemType = 3 | ||
SNMPv2Agent ItemType = 4 | ||
ZabbixInternal ItemType = 5 | ||
SNMPv3Agent ItemType = 6 | ||
ZabbixAgentActive ItemType = 7 | ||
ZabbixAggregate ItemType = 8 | ||
WebItem ItemType = 9 | ||
ExternalCheck ItemType = 10 | ||
DatabaseMonitor ItemType = 11 | ||
IPMIAgent ItemType = 12 | ||
SSHAgent ItemType = 13 | ||
TELNETAgent ItemType = 14 | ||
Calculated ItemType = 15 | ||
JMXAgent ItemType = 16 | ||
|
||
Float ValueType = 0 | ||
Character ValueType = 1 | ||
Log ValueType = 2 | ||
Unsigned ValueType = 3 | ||
Text ValueType = 4 | ||
|
||
Decimal DataType = 0 | ||
Octal DataType = 1 | ||
Hexadecimal DataType = 2 | ||
Boolean DataType = 3 | ||
|
||
AsIs DeltaType = 0 | ||
Speed DeltaType = 1 | ||
Delta DeltaType = 2 | ||
) | ||
|
||
// https://www.zabbix.com/documentation/2.0/manual/appendix/api/item/definitions | ||
type Item struct { | ||
ItemId string `json:"itemid,omitempty"` | ||
Delay int `json:"delay"` | ||
HostId string `json:"hostid"` | ||
InterfaceId string `json:"interfaceid,omitempty"` | ||
Key string `json:"key_"` | ||
Name string `json:"name"` | ||
Type ItemType `json:"type"` | ||
ValueType ValueType `json:"value_type"` | ||
DataType DataType `json:"data_type"` | ||
Delta DeltaType `json:"delta"` | ||
Description string `json:"description"` | ||
Error string `json:"error"` | ||
|
||
// Fields below used only when creating applications | ||
ApplicationIds []string `json:"applications,omitempty"` | ||
} | ||
|
||
type Items []Item | ||
|
||
func (items Items) ByKey() (res map[string]Item) { | ||
res = make(map[string]Item, len(items)) | ||
for _, i := range items { | ||
_, present := res[i.Key] | ||
if present { | ||
panic(fmt.Errorf("Duplicate key %s", i.Key)) | ||
} | ||
res[i.Key] = i | ||
} | ||
return | ||
} | ||
|
||
// Wrapper for item.get https://www.zabbix.com/documentation/2.0/manual/appendix/api/item/get | ||
func (api *API) ItemsGet(params Params) (res Items, err error) { | ||
if _, present := params["output"]; !present { | ||
params["output"] = "extend" | ||
} | ||
response, err := api.CallWithError("item.get", params) | ||
if err != nil { | ||
return | ||
} | ||
|
||
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json") | ||
return | ||
} | ||
|
||
func (api *API) ItemsGetByApplicationId(id string) (res Items, err error) { | ||
return api.ItemsGet(Params{"applicationids": id}) | ||
} | ||
|
||
// Wrapper for item.create: https://www.zabbix.com/documentation/2.0/manual/appendix/api/item/create | ||
func (api *API) ItemsCreate(items Items, applicationIds []string) (err error) { | ||
if len(applicationIds) > 0 { | ||
for i := range items { | ||
items[i].ApplicationIds = applicationIds | ||
} | ||
} | ||
|
||
response, err := api.CallWithError("item.create", items) | ||
if err != nil { | ||
return | ||
} | ||
|
||
result := response.Result.(map[string]interface{}) | ||
itemids := result["itemids"].([]interface{}) | ||
for i, id := range itemids { | ||
items[i].ItemId = id.(string) | ||
} | ||
return | ||
} | ||
|
||
// Wrapper for item.delete: https://www.zabbix.com/documentation/2.0/manual/appendix/api/item/delete | ||
func (api *API) ItemsDelete(items Items) (err error) { | ||
ids := make([]string, len(items)) | ||
for i, item := range items { | ||
ids[i] = item.ItemId | ||
} | ||
|
||
err = api.ItemsDeleteByIds(ids) | ||
if err == nil { | ||
for i := range items { | ||
items[i].ItemId = "" | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Wrapper for item.delete: https://www.zabbix.com/documentation/2.0/manual/appendix/api/item/delete | ||
func (api *API) ItemsDeleteByIds(ids []string) (err error) { | ||
response, err := api.CallWithError("item.delete", ids) | ||
if err != nil { | ||
return | ||
} | ||
|
||
result := response.Result.(map[string]interface{}) | ||
itemids := result["itemids"].(map[string]interface{}) | ||
if len(ids) != len(itemids) { | ||
err = &ExpectedMore{len(ids), len(itemids)} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package zabbix_test | ||
|
||
import ( | ||
. "." | ||
"testing" | ||
) | ||
|
||
func CreateItem(app *Application, t *testing.T) *Item { | ||
items := Items{{ | ||
HostId: app.HostId, | ||
Key: "key.lala.laa", | ||
Name: "name for key", | ||
Type: ZabbixTrapper, | ||
}} | ||
err := getAPI(t).ItemsCreate(items, []string{app.ApplicationId}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return &items[0] | ||
} | ||
|
||
func DeleteItem(item *Item, t *testing.T) { | ||
err := getAPI(t).ItemsDelete(Items{*item}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestItems(t *testing.T) { | ||
api := getAPI(t) | ||
|
||
group := CreateHostGroup(t) | ||
defer DeleteHostGroup(group, t) | ||
|
||
host := CreateHost(group, t) | ||
defer DeleteHost(host, t) | ||
|
||
app := CreateApplication(host, t) | ||
defer DeleteApplication(app, t) | ||
|
||
items, err := api.ItemsGetByApplicationId(app.ApplicationId) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(items) != 0 { | ||
t.Fatal("Found items") | ||
} | ||
|
||
item := CreateItem(app, t) | ||
DeleteItem(item, t) | ||
} |