Skip to content

Commit 0a49a2b

Browse files
Add files via upload
1 parent af69802 commit 0a49a2b

File tree

10 files changed

+202
-28
lines changed

10 files changed

+202
-28
lines changed

Zabbix/Readme.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
`plugin/notify/global/global.go` 文件中配置启用Zabbix插件
1212

1313
```go
14-
// 在gin-vue-admin 主程序的initialize中的plugin的InstallPlugin 函数中写入如下代码
14+
// 在gin-vue-admin 主程序的initialize中的plugin的InstallPlugin 函数中写入如下代码
1515
PluginInit(PublicGroup, Zabbix.CreateZabbixPlugin(
1616
global.GVA_CONFIG.Zabbix.Url,
1717
global.GVA_CONFIG.Zabbix.Username,
@@ -26,6 +26,17 @@
2626
url: 'http://localhost/zabbix/api_jsonrpc.php'
2727
username: Admin
2828
password: Zabbix
29+
在全局config目录下增加Zabbix.go及修改config.go文件
30+
31+
Zabbix.go
32+
type Zabbix struct {
33+
Url string `mapstructure:"url" json:"url" yaml:"url"`
34+
Username string `mapstructure:"username" json:"username" yaml:"username"`
35+
Password string `mapstructure:"password" json:"password" yaml:"password"`
36+
}
37+
config.go
38+
Zabbix Zabbix `mapstructure:"zabbix" json:"zabbix" yaml:"zabbix"` //增加在结构体后面
39+
2940
### 3 参数说明
3041

3142
#### 3-1 全局参数说明
@@ -37,25 +48,35 @@
3748
```
3849
#### 3-2 请求入参说明
3950
```go
40-
51+
/zabbix/getHostAll //请求为GET无需设置参数
52+
/zabbix/getTiggerList //请求为GET无需设置参数
53+
/zabbix/getwebcode //请求为POST入参如下
54+
{
55+
"url":"http://www.baidu.com,http://google.com" //支持批量查询,以英文逗号分割
56+
}
4157
```
4258

4359
### 3方法API(可调用方法)
4460
```go
45-
46-
//获取所有主机列表
47-
GetHostAll()
48-
49-
61+
GetHost() //获取所有主机列表
62+
GetAlertTiggerList() //获取所有正在告警状态的触发器
63+
WebMonitor(urls string) //获取url参数中提供的网址StatusCode状态是否为200 OK
5064
```
5165

5266
### 4. 可直接调用接口
5367

54-
获取主机列表接口: /zabbix/getHostAll [get] 已配置swagger注释
68+
//获取所有主机列表
69+
/zabbix/getHostAll [get] 已配置swagger注释
70+
//获取所有正在告警状态的触发器 status:0为触发器启用状态,status:1为触发器禁用状态(可忽略的触发器告警)
71+
/zabbix/getTiggerList [get] 已配置swagger注释
72+
//获取url参数中提供的网址StatusCode状态是否为200 OK
73+
/zabbix/getwebcode [post] 已配置swagger注释
74+
5575

5676
### 添加api SQL语句
5777

5878
```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-
79+
INSERT INTO `你的数据库名字`.`sys_apis`(`id`, `created_at`, `updated_at`, `deleted_at`, `path`, `description`, `api_group`, `method`) VALUES (104, '2022-04-12 10:33:05.000', '2022-04-12 10:33:05.000', NULL, '/zabbix/getHostAll', 'Zabbix主机列表', 'Zabbix', 'GET');
80+
INSERT INTO `你的数据库名字`.`sys_apis`(`id`, `created_at`, `updated_at`, `deleted_at`, `path`, `description`, `api_group`, `method`) VALUES (106, '2022-04-12 15:03:31.302', '2022-04-12 15:03:31.302', NULL, '/zabbix/getwebcode', '监控网页状态组件', 'Zabbix', 'POST');
81+
INSERT INTO `你的数据库名字`.`sys_apis`(`id`, `created_at`, `updated_at`, `deleted_at`, `path`, `description`, `api_group`, `method`) VALUES (108, '2022-04-13 16:23:07.714', '2022-04-13 16:23:07.714', NULL, '/zabbix/getTiggerList', '获取正在触发的告警列表', 'Zabbix', 'GET');
6182
```

Zabbix/api/sys_zabbix.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,39 @@ func (s *zabbixAPI) GetHostAll(c *gin.Context) {
2929
response.OkWithData(gethostALl, c)
3030
}
3131
}
32+
33+
//@Tags Plugins
34+
//@Summary 进行Web状态码监控
35+
//@Security ZabbixWebCodeMonitor
36+
//@Produce application/json
37+
//@Success 200 {string} string "{"code":0,"data":{},"msg":"操作成功"}"
38+
//@Router /zabbix/getwebcode
39+
func (s *zabbixAPI) GetWebMonitorStatus(c *gin.Context) {
40+
var webStatusUrl Zabbixesult.GetWebCodeStatusModel
41+
_ = c.ShouldBindJSON(&webStatusUrl)
42+
webStatusUrlCode, err := service.WebMonitor(webStatusUrl.Url)
43+
if err != nil {
44+
global.GVA_LOG.Error("调用失败!", zap.Error(err))
45+
response.FailWithMessage("调用失败", c)
46+
} else {
47+
response.OkWithData(&webStatusUrlCode, c)
48+
}
49+
}
50+
51+
//@Tags Plugins
52+
//@Summary 获取主机列表
53+
//@Security GetTiggerList
54+
//@Produce application/json
55+
//@Success 200 {string} string "{"code":0,"data":{},"msg":"操作成功"}"
56+
//@Router /zabbix/getHostList
57+
func (s *zabbixAPI) GetTiggerList(c *gin.Context) {
58+
var resultMail Zabbixesult.ResultModel
59+
_ = c.ShouldBindJSON(&resultMail)
60+
getTiggerList, err := service.GetAlertTiggerList()
61+
if err != nil {
62+
global.GVA_LOG.Error("调用失败!", zap.Error(err))
63+
response.FailWithMessage("调用失败", c)
64+
} else {
65+
response.OkWithData(getTiggerList, c)
66+
}
67+
}

Zabbix/config/config.go

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

Zabbix/model/response/get_host.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ type ZabbixLoginModel struct {
1414
type GetHostModel struct {
1515
Jsonrpc string `json:"jsonrpc"`
1616
Method string `json:"method"`
17-
Params struct {
17+
18+
Params struct {
1819
Output []string `json:"output"`
1920
SelectInterfaces []string `json:"selectInterfaces"`
2021
SelectGroups []string `json:"selectGroups"`
2122
SelectParentTemplates []string `json:"selectParentTemplates"`
2223
} `json:"params"`
23-
ID int `json:"id"`
24-
Auth string `json:"auth"`
24+
ID int `json:"id"`
25+
Auth interface{} `json:"auth"`
2526
}
2627

2728
type ResultModel struct {
2829
Jsonrpc string `json:"jsonrpc"`
2930
Result string `json:"result"`
3031
ID int `json:"id"`
3132
}
33+
34+
type WebMonitorModel struct {
35+
Url string `json:"url"`
36+
Code int `json:"status_code"`
37+
}

Zabbix/model/response/get_tigger.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package response
2+
3+
type AlertTiggerModel struct {
4+
Jsonrpc string `json:"jsonrpc"`
5+
Method string `json:"method"`
6+
Params Params `json:"params"`
7+
Auth interface{} `json:"auth"`
8+
ID int `json:"id"`
9+
}
10+
type Filter struct {
11+
Value int `json:"value"`
12+
}
13+
type Params struct {
14+
Output []string `json:"output"`
15+
Filter Filter `json:"filter"`
16+
Sortfield string `json:"sortfield"`
17+
Sortorder string `json:"sortorder"`
18+
SelectHosts string `json:"selectHosts"`
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package response
2+
3+
type GetWebCodeStatusModel struct {
4+
Url string `json:"url"`
5+
Status string `json:"status"`
6+
}

Zabbix/router/sys_zabbix.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ type ZabbixRouter struct{}
1111
func (s *ZabbixRouter) InitZabbixRouter(Router *gin.RouterGroup) {
1212
zabbixRouter := Router.Use(middleware.OperationRecord())
1313
GetHostAll := api.ApiGroupApp.GetHostAll
14+
GetWebStatusCode := api.ApiGroupApp.GetWebMonitorStatus
15+
GetTiggerList := api.ApiGroupApp.GetTiggerList
1416
{
15-
zabbixRouter.GET("getHostAll", GetHostAll) // 获取主机列表
17+
zabbixRouter.GET("getHostAll", GetHostAll) // 获取主机列表
18+
zabbixRouter.GET("getTiggerList", GetTiggerList) //获取正在告警列表
19+
zabbixRouter.POST("getwebcode", GetWebStatusCode) // 监控Web网页状态
1620
}
1721
}

Zabbix/service/sys_zabbix.go

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

33
import (
4+
"fmt"
45
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/utils"
6+
"strings"
57
)
68

79
type ZabbixService struct{}
810

9-
func GetHost() (err error, HostAll string) {
11+
func GetHost() (err error, HostAll interface{}) {
1012
r1, r2 := utils.ZabbixLogin()
1113
HostAll = utils.GetHostList(r1, r2)
1214
return
1315
}
16+
17+
func GetAlertTiggerList() (TiggersAll interface{}, err error) {
18+
r1, r2 := utils.ZabbixLogin()
19+
TiggersAll, err = utils.GetAlertTiggerList(r1, r2)
20+
return TiggersAll, err
21+
}
22+
23+
func WebMonitor(urls string) (Code interface{}, err error) {
24+
urlsList := strings.Split(urls, ",")
25+
Code123, err := utils.WebMonitor(urlsList)
26+
if err != nil {
27+
fmt.Println(err)
28+
}
29+
return Code123, err
30+
}

Zabbix/utils/get_AlertTiggers.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/Zabbix/model/response"
6+
)
7+
8+
func GetAlertTiggerList(resultToken interface{}, zabbixurl string) (r1 interface{}, err error) {
9+
//指定Zabbix返回值参数
10+
paramsOutput := make([]string, 0)
11+
paramsOutput = append(paramsOutput, "triggerid")
12+
paramsOutput = append(paramsOutput, "description")
13+
paramsOutput = append(paramsOutput, "priority")
14+
paramsOutput = append(paramsOutput, "status")
15+
paramsOutput = append(paramsOutput, "selectHosts")
16+
var AlertParams = response.Params{
17+
Output: paramsOutput,
18+
Filter: response.Filter(struct{ Value int }{Value: 1}),
19+
Sortfield: "priority",
20+
Sortorder: "DESC",
21+
SelectHosts: "extend",
22+
}
23+
24+
var getTiggerStruct = response.AlertTiggerModel{
25+
Jsonrpc: "2.0",
26+
Method: "trigger.get",
27+
Params: AlertParams,
28+
Auth: resultToken,
29+
}
30+
r1, err = PostProcessingJSON(getTiggerStruct, zabbixurl)
31+
if err != nil {
32+
fmt.Println(err)
33+
}
34+
return r1, err
35+
}

Zabbix/utils/zabbix.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strings"
1111
)
1212

13-
//请求操作+byte转Json
14-
func PostProcessingJSON(ProcessStruct interface{}, url string) (Jsonstr string) {
13+
//请求操作+byte转map
14+
func PostProcessingJSON(ProcessStruct interface{}, url string) (ResultMap interface{}, err error) {
1515
jsonBytes, _ := json.Marshal(ProcessStruct)
1616
body := string(jsonBytes)
1717
response, err := http.Post(url, "application/json-rpc; charset=utf-8", strings.NewReader(body))
@@ -23,17 +23,22 @@ func PostProcessingJSON(ProcessStruct interface{}, url string) (Jsonstr string)
2323
if err != nil {
2424
fmt.Println(err)
2525
}
26-
JsonStr := string(content)
27-
return JsonStr
26+
result := make(map[string]interface{})
27+
err = json.Unmarshal(content, &result)
28+
if err != nil {
29+
fmt.Println(err)
30+
}
31+
//JsonStr := string(content)
32+
return result["result"], err
2833
}
2934

3035
//Zabbix API登录
31-
func ZabbixLogin() (resultToken string, resulturl string) {
36+
func ZabbixLogin() (resultToken interface{}, resulturl string) {
3237
//获取本地Config.yaml配置的Zabbix参数
3338
username := global.GlobalConfig.Username
3439
password := global.GlobalConfig.Password
3540
url := global.GlobalConfig.Url
36-
var resultModel response.ResultModel
41+
//var resultModel response.ResultModel
3742
var loginstrcut = response.ZabbixLoginModel{
3843
Jsonrpc: "2.0",
3944
Method: "user.login",
@@ -46,15 +51,16 @@ func ZabbixLogin() (resultToken string, resulturl string) {
4651
}{User: username, Password: password}),
4752
}
4853
//进行POST请求及Json转换
49-
json.Unmarshal([]byte(PostProcessingJSON(loginstrcut, url)), &resultModel)
50-
resultToken = resultModel.Result
54+
resultToken, err := PostProcessingJSON(loginstrcut, url)
55+
if err != nil {
56+
return err, resulturl
57+
}
5158
resulturl = url
52-
fmt.Println(resulturl, resultToken)
5359
return resultToken, resulturl
5460
}
5561

5662
//获取所有主机列表
57-
func GetHostList(resultToken string, zabbixurl string) (r1 string) {
63+
func GetHostList(resultToken interface{}, zabbixurl string) (r1 interface{}) {
5864
//指定Zabbix返回值参数
5965
var outPutList []string
6066
var interfaceList []string
@@ -63,6 +69,7 @@ func GetHostList(resultToken string, zabbixurl string) (r1 string) {
6369
outPutList = append(outPutList, "name")
6470
outPutList = append(outPutList, "host")
6571
outPutList = append(outPutList, "proxy_hostid")
72+
outPutList = append(outPutList, "status")
6673
interfaceList = append(interfaceList, "interfaceid")
6774
interfaceList = append(interfaceList, "ip")
6875
groupsList = append(groupsList, "name")
@@ -85,10 +92,28 @@ func GetHostList(resultToken string, zabbixurl string) (r1 string) {
8592
Auth: resultToken,
8693
}
8794

88-
r1 = PostProcessingJSON(getHostStruct, zabbixurl)
95+
r1, err := PostProcessingJSON(getHostStruct, zabbixurl)
96+
if err != nil {
97+
fmt.Println(err)
98+
}
8999
return r1
90100
}
91101

92-
func GetItemsAll(resultToken string, zabbixurl string) {
102+
//根据web状态码进行监控网页状态
103+
func WebMonitor(urls []string) (webCodeStatusList []response.GetWebCodeStatusModel, err error) {
104+
var getWebCodeStatus response.GetWebCodeStatusModel
105+
getWebCodeStatusList := make([]response.GetWebCodeStatusModel, 0)
106+
for _, v := range urls {
107+
responseGet, _ := http.Get(v)
108+
getWebCodeStatus.Url = v
109+
if responseGet.StatusCode != 200 {
110+
getWebCodeStatus.Status = "该web网址有异常"
111+
getWebCodeStatusList = append(getWebCodeStatusList, getWebCodeStatus)
112+
} else {
113+
getWebCodeStatus.Status = "该web网址运行正常"
114+
getWebCodeStatusList = append(getWebCodeStatusList, getWebCodeStatus)
115+
}
116+
}
93117

94-
}
118+
return getWebCodeStatusList, nil
119+
}

0 commit comments

Comments
 (0)