Skip to content

Commit 02cfeae

Browse files
Zabbix插件
1 parent 7a383c9 commit 02cfeae

File tree

9 files changed

+214
-0
lines changed

9 files changed

+214
-0
lines changed

Zabbix/Readme.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## GVA Zabbix插件
2+
3+
本插件用于集成Zabbix
4+
5+
### 1. 使用场景
6+
7+
- 当需要集成Zabbix监控时使用
8+
9+
### 2. 配置说明
10+
11+
`plugin/notify/global/global.go` 文件中配置启用Zabbix插件
12+
13+
```go
14+
// 在gin-vue-admin 主程序的initialize中的plugin的InstallPlugin 函数中写入如下代码
15+
PluginInit(PublicGroup, Zabbix.CreateZabbixPlugin(
16+
global.GVA_CONFIG.Zabbix.Url,
17+
global.GVA_CONFIG.Zabbix.Username,
18+
global.GVA_CONFIG.Zabbix.Password,
19+
))
20+
}
21+
```
22+
23+
在config.yaml文件中配置Zabbix插件登录信息
24+
25+
zabbix:
26+
url: 'http://localhost/zabbix/api_jsonrpc.php'
27+
username: Admin
28+
password: Zabbix
29+
### 3 参数说明
30+
31+
#### 3-1 全局参数说明
32+
33+
```go
34+
Url string `mapstructure:"url" json:"url" yaml:"url"` //ZabbixAPI接口地址
35+
Username string `mapstructure:"username" json:"username" yaml:"username"` //Zabbix用户名
36+
Password string `mapstructure:"password" json:"password" yaml:"password"` //Zabbix密码
37+
```
38+
#### 3-2 请求入参说明
39+
```go
40+
41+
```
42+
43+
### 3方法API(可调用方法)
44+
```go
45+
46+
//获取所有主机列表
47+
GetHostAll()
48+
49+
50+
```
51+
52+
### 4. 可直接调用接口
53+
54+
获取主机列表接口: /zabbix/getHostAll [get] 已配置swagger注释
55+
56+
### 添加api SQL语句
57+
58+
```sql
59+
INSERT INTO `你的数据库名字`.`sys_apis`(`id`, `created_at`, `updated_at`, `deleted_at`, `path`, `description`, `api_group`, `method`) VALUES (100, '2022-03-28 14:04:19.194', '2022-03-28 14:04:19.194', NULL, '/zabbix/getHostAll', 'Zabbix主机列表', 'Zabbix', 'GET');
60+
61+
```

Zabbix/api/sys_zabbix.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package api
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
type zabbixAPI struct {
8+
}
9+
10+
func (s *zabbixAPI) HostItems(c *gin.Context) {
11+
}
12+
13+
func (s *zabbixAPI) Tiggers(c *gin.Context) {
14+
}
15+
16+
func (s *zabbixAPI) Graph(c *gin.Context) {
17+
}

Zabbix/config/zabbix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package config
2+
3+
type Zabbix struct {
4+
url string `mapstructure:"url" json:"url" yaml:"url"`
5+
username string `mapstructure:"username" json:"username" yaml:"username"`
6+
password string `mapstructure:"password" json:"password" yaml:"password"`
7+
}

Zabbix/global/global.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package global
2+
3+
import "github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/config"
4+
5+
var GlobalConfig = new(config.Zabbix)

Zabbix/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package Zabbix

Zabbix/model/response/get_host.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package response
2+
3+
type GetHostModel struct {
4+
Jsonrpc string `json:"jsonrpc"`
5+
Method string `json:"method"`
6+
Params struct {
7+
Output []string `json:"output"`
8+
SelectInterfaces []string `json:"selectInterfaces"`
9+
} `json:"params"`
10+
ID int `json:"id"`
11+
Auth string `json:"auth"`
12+
}

Zabbix/model/response/login.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package response
2+
3+
type ZabbixLoginModel struct {
4+
Jsonrpc string `json:"jsonrpc"`
5+
Method string `json:"method"`
6+
Params struct {
7+
User string `json:"user"`
8+
Password string `json:"password"`
9+
} `json:"params"`
10+
ID int `json:"id"`
11+
Auth interface{} `json:"auth"`
12+
}

Zabbix/service/sys_zabbix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package service
2+
3+
ZabbixLogin("Admin", "zabbix@ngcc", "http://192.168.128.181/zabbix/api_jsonrpc.php")

Zabbix/utils/zabbix.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/model/response"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
10+
)
11+
12+
//type getHostModel struct {
13+
// Jsonrpc string `json:"jsonrpc"`
14+
// Method string `json:"method"`
15+
// Params struct {
16+
// Output []string `json:"output"`
17+
// SelectInterfaces []string `json:"selectInterfaces"`
18+
// } `json:"params"`
19+
// ID int `json:"id"`
20+
// Auth string `json:"auth"`
21+
//}
22+
23+
type ResultModel struct {
24+
Jsonrpc string `json:"jsonrpc"`
25+
Result string `json:"result"`
26+
ID int `json:"id"`
27+
}
28+
29+
func PostProcessingJSON(ProcessStruct interface{}, url string) (Jsonstr string) {
30+
jsonBytes, _ := json.Marshal(ProcessStruct)
31+
body := string(jsonBytes)
32+
response, err := http.Post(url, "application/json-rpc; charset=utf-8", strings.NewReader(body))
33+
if err != nil {
34+
fmt.Println(err)
35+
}
36+
37+
content, err := ioutil.ReadAll(response.Body)
38+
if err != nil {
39+
fmt.Println(err)
40+
}
41+
JsonStr := string(content)
42+
return JsonStr
43+
}
44+
45+
func ZabbixLogin(username string, password string, url string) (resultToken string) {
46+
var resultModel ResultModel
47+
var loginstrcut = response.ZabbixLoginModel{
48+
Jsonrpc: "2.0",
49+
Method: "user.login",
50+
Params: struct {
51+
User string `json:"user"`
52+
Password string `json:"password"`
53+
}(struct {
54+
User string
55+
Password string
56+
}{User: username, Password: password}),
57+
}
58+
json.Unmarshal([]byte(PostProcessingJSON(loginstrcut, url)), &resultModel)
59+
resultToken = resultModel.Result
60+
return resultToken
61+
}
62+
63+
func GetHostList() (r1 string) {
64+
//var resultModel ResultModel
65+
var outPutList []string
66+
var interfaceList []string
67+
outPutList = append(outPutList, "hostid")
68+
outPutList = append(outPutList, "host")
69+
interfaceList = append(interfaceList, "interfaceid")
70+
interfaceList = append(interfaceList, "ip")
71+
72+
result := ZabbixLogin("Admin", "zabbix@ngcc", "http://192.168.128.181/zabbix/api_jsonrpc.php")
73+
fmt.Println(result)
74+
var getHostStruct = response.GetHostModel{
75+
Jsonrpc: "2.0",
76+
Method: "host.get",
77+
Params: struct {
78+
Output []string `json:"output"`
79+
SelectInterfaces []string `json:"selectInterfaces"`
80+
}(struct {
81+
Output []string
82+
SelectInterfaces []string
83+
}{Output: outPutList, SelectInterfaces: interfaceList}),
84+
Auth: result,
85+
}
86+
fmt.Println(getHostStruct)
87+
r1 = PostProcessingJSON(getHostStruct, "http://192.168.128.181/zabbix/api_jsonrpc.php")
88+
return r1
89+
}
90+
91+
func main() {
92+
//ZabbixLogin("Admin", "zabbix@ngcc", "http://192.168.128.181/zabbix/api_jsonrpc.php")
93+
r1 := GetHostList()
94+
fmt.Println(r1)
95+
10099
96+
}

0 commit comments

Comments
 (0)