Skip to content

Commit c7a8975

Browse files
Add files via upload
1 parent 02cfeae commit c7a8975

File tree

10 files changed

+156
-53
lines changed

10 files changed

+156
-53
lines changed

Zabbix/api/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package api
2+
3+
type ApiGroup struct {
4+
zabbixAPI
5+
}
6+
7+
var ApiGroupApp = new(ApiGroup)

Zabbix/api/sys_zabbix.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package api
22

33
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/global"
5+
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
6+
Zabbixesult "github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/model/response"
7+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/service"
48
"github.com/gin-gonic/gin"
9+
"go.uber.org/zap"
510
)
611

712
type zabbixAPI struct {
813
}
914

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) {
15+
//@Tags Plugins
16+
//@Summary 获取主机列表
17+
//@Security ZabbixHostListGet
18+
//@Produce application/json
19+
//@Success 200 {string} string "{"code":0,"data":{},"msg":"操作成功"}"
20+
//@Router /zabbix/getHostList
21+
func (s *zabbixAPI) GetHostAll(c *gin.Context) {
22+
var resultMail Zabbixesult.ResultModel
23+
_ = c.ShouldBindJSON(&resultMail)
24+
err, gethostALl := service.GetHost()
25+
if err != nil {
26+
global.GVA_LOG.Error("调用失败!", zap.Error(err))
27+
response.FailWithMessage("调用失败", c)
28+
} else {
29+
response.OkWithData(gethostALl, c)
30+
}
1731
}

Zabbix/config/zabbix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package config
22

33
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"`
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"`
77
}

Zabbix/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package Zabbix
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/global"
5+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/router"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type zabbixPlugin struct{}
10+
11+
func CreateZabbixPlugin(url string, username string, password string) *zabbixPlugin {
12+
global.GlobalConfig.Url = url
13+
global.GlobalConfig.Username = username
14+
global.GlobalConfig.Password = password
15+
16+
return &zabbixPlugin{}
17+
}
18+
19+
func (*zabbixPlugin) Register(group *gin.RouterGroup) {
20+
router.RouterGroupApp.InitZabbixRouter(group)
21+
}
22+
23+
func (*zabbixPlugin) RouterPath() string {
24+
return "zabbix"
25+
}

Zabbix/model/response/get_host.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
package response
22

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+
}
13+
314
type GetHostModel struct {
415
Jsonrpc string `json:"jsonrpc"`
516
Method string `json:"method"`
617
Params struct {
7-
Output []string `json:"output"`
8-
SelectInterfaces []string `json:"selectInterfaces"`
18+
Output []string `json:"output"`
19+
SelectInterfaces []string `json:"selectInterfaces"`
20+
SelectGroups []string `json:"selectGroups"`
21+
SelectParentTemplates []string `json:"selectParentTemplates"`
922
} `json:"params"`
1023
ID int `json:"id"`
1124
Auth string `json:"auth"`
1225
}
26+
27+
type ResultModel struct {
28+
Jsonrpc string `json:"jsonrpc"`
29+
Result string `json:"result"`
30+
ID int `json:"id"`
31+
}

Zabbix/router/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package router
2+
3+
type RouterGroup struct {
4+
ZabbixRouter
5+
}
6+
7+
var RouterGroupApp = new(RouterGroup)

Zabbix/router/sys_zabbix.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package router
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
5+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/api"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type ZabbixRouter struct{}
10+
11+
func (s *ZabbixRouter) InitZabbixRouter(Router *gin.RouterGroup) {
12+
zabbixRouter := Router.Use(middleware.OperationRecord())
13+
GetHostAll := api.ApiGroupApp.GetHostAll
14+
{
15+
zabbixRouter.GET("getHostAll", GetHostAll) // 获取主机列表
16+
}
17+
}

Zabbix/service/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package service
2+
3+
type ServiceGroup struct {
4+
ZabbixService
5+
}
6+
7+
var ServiceGroupApp = new(ServiceGroup)

Zabbix/service/sys_zabbix.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
package service
22

3-
ZabbixLogin("Admin", "zabbix@ngcc", "http://192.168.128.181/zabbix/api_jsonrpc.php")
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/utils"
5+
)
6+
7+
type ZabbixService struct{}
8+
9+
func GetHost() (err error, HostAll string) {
10+
r1, r2 := utils.ZabbixLogin()
11+
HostAll = utils.GetHostList(r1, r2)
12+
return
13+
}

Zabbix/utils/zabbix.go

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1-
package main
1+
package utils
22

33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/global"
67
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/model/response"
78
"io/ioutil"
89
"net/http"
910
"strings"
1011
)
1112

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-
13+
//请求操作+byte转Json
2914
func PostProcessingJSON(ProcessStruct interface{}, url string) (Jsonstr string) {
3015
jsonBytes, _ := json.Marshal(ProcessStruct)
3116
body := string(jsonBytes)
@@ -42,8 +27,13 @@ func PostProcessingJSON(ProcessStruct interface{}, url string) (Jsonstr string)
4227
return JsonStr
4328
}
4429

45-
func ZabbixLogin(username string, password string, url string) (resultToken string) {
46-
var resultModel ResultModel
30+
//Zabbix API登录
31+
func ZabbixLogin() (resultToken string, resulturl string) {
32+
//获取本地Config.yaml配置的Zabbix参数
33+
username := global.GlobalConfig.Username
34+
password := global.GlobalConfig.Password
35+
url := global.GlobalConfig.Url
36+
var resultModel response.ResultModel
4737
var loginstrcut = response.ZabbixLoginModel{
4838
Jsonrpc: "2.0",
4939
Method: "user.login",
@@ -55,42 +45,50 @@ func ZabbixLogin(username string, password string, url string) (resultToken stri
5545
Password string
5646
}{User: username, Password: password}),
5747
}
48+
//进行POST请求及Json转换
5849
json.Unmarshal([]byte(PostProcessingJSON(loginstrcut, url)), &resultModel)
5950
resultToken = resultModel.Result
60-
return resultToken
51+
resulturl = url
52+
fmt.Println(resulturl, resultToken)
53+
return resultToken, resulturl
6154
}
6255

63-
func GetHostList() (r1 string) {
64-
//var resultModel ResultModel
56+
//获取所有主机列表
57+
func GetHostList(resultToken string, zabbixurl string) (r1 string) {
58+
//指定Zabbix返回值参数
6559
var outPutList []string
6660
var interfaceList []string
67-
outPutList = append(outPutList, "hostid")
61+
var groupsList []string
62+
var templatsList []string
63+
outPutList = append(outPutList, "name")
6864
outPutList = append(outPutList, "host")
65+
outPutList = append(outPutList, "proxy_hostid")
6966
interfaceList = append(interfaceList, "interfaceid")
7067
interfaceList = append(interfaceList, "ip")
68+
groupsList = append(groupsList, "name")
69+
templatsList = append(templatsList, "name")
7170

72-
result := ZabbixLogin("Admin", "zabbix@ngcc", "http://192.168.128.181/zabbix/api_jsonrpc.php")
73-
fmt.Println(result)
7471
var getHostStruct = response.GetHostModel{
7572
Jsonrpc: "2.0",
7673
Method: "host.get",
7774
Params: struct {
78-
Output []string `json:"output"`
79-
SelectInterfaces []string `json:"selectInterfaces"`
75+
Output []string `json:"output"`
76+
SelectInterfaces []string `json:"selectInterfaces"`
77+
SelectGroups []string `json:"selectGroups"`
78+
SelectParentTemplates []string `json:"selectParentTemplates"`
8079
}(struct {
81-
Output []string
82-
SelectInterfaces []string
83-
}{Output: outPutList, SelectInterfaces: interfaceList}),
84-
Auth: result,
80+
Output []string
81+
SelectInterfaces []string
82+
SelectGroups []string
83+
SelectParentTemplates []string
84+
}{Output: outPutList, SelectInterfaces: interfaceList, SelectGroups: groupsList, SelectParentTemplates: templatsList}),
85+
Auth: resultToken,
8586
}
86-
fmt.Println(getHostStruct)
87-
r1 = PostProcessingJSON(getHostStruct, "http://192.168.128.181/zabbix/api_jsonrpc.php")
87+
88+
r1 = PostProcessingJSON(getHostStruct, zabbixurl)
8889
return r1
8990
}
9091

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-
}
92+
func GetItemsAll(resultToken string, zabbixurl string) {
93+
94+
}

0 commit comments

Comments
 (0)