Skip to content

Commit 1b59552

Browse files
authored
Merge pull request #97 from canghai908/dev
2.1.4 1.增加网卡数据详情获取 2.增加API地址检测 3.增加延迟指标
2 parents 2ad86f3 + 0f5ca2d commit 1b59552

File tree

17 files changed

+1542
-231
lines changed

17 files changed

+1542
-231
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tmp_dir = "tmp"
66
bin = "tmp\\main.exe web"
77
cmd = "go build -o ./tmp/main.exe ."
88
delay = 1000
9-
exclude_dir = ["assets", "tmp", "vendor", "testdata","download"]
9+
exclude_dir = ["assets", "tmp", "vendor", "testdata","download","web",".idea"]
1010
exclude_file = []
1111
exclude_regex = ["_test.go"]
1212
exclude_unchanged = false

cmd/web.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"github.com/canghai908/zabbix-go"
1212
"github.com/urfave/cli/v2"
1313
"gopkg.in/ini.v1"
14+
"net/http"
1415
"os"
16+
"time"
1517
"zbxtable/models"
1618
"zbxtable/packfile"
1719
"zbxtable/routers"
@@ -124,8 +126,23 @@ func CheckZabbixAPI(args ...string) (string, error) {
124126
user := args[1]
125127
pass := args[2]
126128
token := args[3]
129+
//判断API地址是否正确,http get访问访问api地址判断状态码是不是412
130+
url := address + "/api_jsonrpc.php"
131+
dClient := http.Client{
132+
Timeout: 3 * time.Second, // 设置超时时间为 3 秒
133+
}
134+
resp, err := dClient.Get(url)
135+
if err != nil {
136+
logs.Error("Zabbix Web get request failed:", err)
137+
os.Exit(1)
138+
}
139+
defer resp.Body.Close()
140+
if resp.StatusCode != http.StatusPreconditionFailed {
141+
logs.Error("Zabbix Web is incorrectly!")
142+
os.Exit(1)
143+
}
144+
// api定义
127145
API = zabbix.NewAPI(address + "/api_jsonrpc.php")
128-
address = args[0]
129146
if token != "" {
130147
API.Auth = token
131148
} else {
@@ -138,7 +155,7 @@ func CheckZabbixAPI(args ...string) (string, error) {
138155
//zabbix api data get test
139156
OutputPar := []string{"hostid", "host", "available", "status", "name", "error"}
140157
type params map[string]interface{}
141-
_, err := API.CallWithError("host.get", params{
158+
_, err = API.CallWithError("host.get", params{
142159
"output": OutputPar,
143160
"hostids": "10084",
144161
})

control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# release version
3-
version=2.1.3
3+
version=2.1.4
44

55
CWD=$(cd $(dirname $0)/; pwd)
66
cd $CWD

controllers/base.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,35 @@ import (
44
"github.com/astaxie/beego"
55
jwtbeego "github.com/canghai908/jwt-beego"
66
jsoniter "github.com/json-iterator/go"
7+
"net/http"
78
)
89

9-
var json = jsoniter.ConfigCompatibleWithStandardLibrary
10+
var json = jsoniter.Config{
11+
EscapeHTML: false,
12+
SortMapKeys: true,
13+
ValidateJsonRawMessage: true,
14+
}.Froze()
1015

11-
//BaseController use
16+
// BaseController use
1217
type BaseController struct {
1318
beego.Controller
1419
}
1520

16-
//Resp Resp all
21+
func (c *BaseController) ServeJSON() {
22+
c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
23+
var err error
24+
data := c.Data["json"]
25+
encoder := json.NewEncoder(c.Ctx.Output.Context.ResponseWriter)
26+
encoder.SetEscapeHTML(false)
27+
encoder.SetIndent("", " ")
28+
err = encoder.Encode(data)
29+
if err != nil {
30+
http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
31+
return
32+
}
33+
}
34+
35+
// Resp all
1736
type Resp struct {
1837
Code int `json:"code"`
1938
Message string `json:"message"`
@@ -23,16 +42,13 @@ type Resp struct {
2342
} `json:"data"`
2443
}
2544

26-
//Res var
45+
// Res var
2746
var Res = &Resp{}
2847

29-
//API for zabbix
30-
//var API = &zabbix.API{}
31-
32-
//Tuser is userinfo
48+
// Tuser is userinfo
3349
var Tuser string
3450

35-
//Prepare Prepare login
51+
// Prepare login
3652
func (c *BaseController) Prepare() {
3753
tokenString := c.Ctx.Request.Header.Get("X-Token")
3854
et := jwtbeego.EasyToken{}

controllers/host.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type HostController struct {
1010
BaseController
1111
}
1212

13-
//HostRes restp
13+
// HostRes restp
1414
var HostRes models.HostList
1515
var HostInfoRes models.HostInfo
1616
var HostInterfaceRes models.HostInterfaceInfo
@@ -21,6 +21,7 @@ func (c *HostController) URLMapping() {
2121
c.Mapping("GetOne", c.GetOne)
2222
c.Mapping("GetMonItem", c.GetMonItem)
2323
c.Mapping("GetMonInterface", c.GetMonInterface)
24+
c.Mapping("getOneInterface", c.GetOneInterface)
2425
c.Mapping("GetMonWinFileSystem", c.GetMonWinFileSystem)
2526
c.Mapping("GetMonLinFileSystem", c.GetMonLinFileSystem)
2627
}
@@ -55,9 +56,6 @@ func (c *HostController) Post() {
5556
c.ServeJSON()
5657
}
5758

58-
//ApplicationRes str
59-
var ApplicationRes models.ApplicationList
60-
6159
// GetOne ...
6260
// @Title 获取单个设备信息
6361
// @Description 获取单个设备的资产信息
@@ -184,15 +182,37 @@ func (c *HostController) GetMonInterface() {
184182
if err != nil {
185183
HostInterfaceRes.Code = 500
186184
HostInterfaceRes.Message = err.Error()
185+
} else {
186+
HostInterfaceRes.Code = 200
187+
HostInterfaceRes.Message = "获取数据成功"
188+
HostInterfaceRes.Data.Items = hs
189+
HostInterfaceRes.Data.Total = int64(len(hs))
187190
}
188-
HostInterfaceRes.Code = 200
189-
HostInterfaceRes.Message = "获取数据成功"
190-
HostInterfaceRes.Data.Items = hs
191-
HostInterfaceRes.Data.Total = int64(len(hs))
192191
c.Data["json"] = HostInterfaceRes
193192
c.ServeJSON()
194193
}
195194

195+
// GetOneInterface
196+
// @Title 获取网络设备接口流量详情图标数据
197+
// @Description 获取网络设备接口流量
198+
// @Param X-Token header string true "x-token in header"
199+
// @Param body body models.InterfaceData true "hosts info"
200+
// @Success 200 {object} models.MonItemList
201+
// @Failure 403
202+
// @router /interface/data [post]
203+
func (c *HostController) GetOneInterface() {
204+
var v models.InterfaceData
205+
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
206+
b, err := models.GetInterfaceGraphData(v)
207+
if err != nil {
208+
c.Data["json"] = b
209+
c.ServeJSON()
210+
}
211+
c.Data["json"] = b
212+
}
213+
c.ServeJSON()
214+
}
215+
196216
// GetMonWinFilesystem
197217
// @Title 获取windows系统监控指标
198218
// @Description 获取windows网卡、磁盘信息

controllers/system.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *SystemController) GetOne() {
4646
c.ServeJSON()
4747
}
4848

49-
// GetOne ...
49+
// PutOne 更新初始化数据 ...
5050
// @Title Get One
5151
// @Description get Alarm by id
5252
// @Param X-Token header string true "x-token in header"
@@ -58,18 +58,19 @@ func (c *SystemController) GetOne() {
5858
func (c *SystemController) PutOne() {
5959
idStr := c.Ctx.Input.Param(":id")
6060
id, _ := strconv.Atoi(idStr)
61-
cpu_core := gjson.Get(string(c.Ctx.Input.RequestBody), "cpu_core").String()
62-
uptime_id := gjson.Get(string(c.Ctx.Input.RequestBody), "uptime_id").String()
63-
cpu_utilization_id := gjson.Get(string(c.Ctx.Input.RequestBody), "cpu_utilization_id").String()
64-
group_id := gjson.Get(string(c.Ctx.Input.RequestBody), "group_id").String()
65-
memory_total_id := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_total_id").String()
66-
memory_used_id := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_used_id").String()
67-
memory_utilization_id := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_utilization_id").String()
61+
cpuCore := gjson.Get(string(c.Ctx.Input.RequestBody), "cpu_core").String()
62+
uptimeId := gjson.Get(string(c.Ctx.Input.RequestBody), "uptime_id").String()
63+
cpuUtilizationId := gjson.Get(string(c.Ctx.Input.RequestBody), "cpu_utilization_id").String()
64+
groupId := gjson.Get(string(c.Ctx.Input.RequestBody), "group_id").String()
65+
memoryTotalId := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_total_id").String()
66+
memoryUsedId := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_used_id").String()
67+
memoryUtilizationId := gjson.Get(string(c.Ctx.Input.RequestBody), "memory_utilization_id").String()
6868
model := gjson.Get(string(c.Ctx.Input.RequestBody), "model").String()
69-
v := models.System{ID: int64(id), CPUCore: cpu_core, CPUUtilizationID: cpu_utilization_id,
70-
GroupID: group_id, MemoryTotalID: memory_total_id, UptimeID: uptime_id, Model: model,
71-
MemoryUsedID: memory_used_id, MemoryUtilizationID: memory_utilization_id,
72-
}
69+
pingTemplateId := gjson.Get(string(c.Ctx.Input.RequestBody), "ping_template_id").String()
70+
v := models.System{ID: int64(id), CPUCore: cpuCore, CPUUtilizationID: cpuUtilizationId,
71+
GroupID: groupId, MemoryTotalID: memoryTotalId, UptimeID: uptimeId, Model: model,
72+
MemoryUsedID: memoryUsedId, MemoryUtilizationID: memoryUtilizationId,
73+
PingTemplateID: pingTemplateId}
7374
err := models.UpdateSystem(&v)
7475
if err != nil {
7576
SystemRes.Code = 500

models/application_mod.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
//ApplicationList struct
3+
// ApplicationList struct
44
type ApplicationList struct {
55
Code int `json:"code"`
66
Message string `json:"mess age"`
@@ -10,14 +10,14 @@ type ApplicationList struct {
1010
} `json:"data"`
1111
}
1212

13-
//Application struct
13+
// Application struct
1414
type Application struct {
1515
Applicationid string `json:"applicationid"`
1616
Name string `json:"name"`
1717
Items []ApplicationItem `json:"items"`
1818
}
1919

20-
//ApplicationItem struct
20+
// ApplicationItem struct
2121
type ApplicationItem struct {
2222
ItemID string `json:"itemid"`
2323
Name string `json:"name"`
@@ -35,15 +35,15 @@ type MonIts struct {
3535
Tag string `json:"tag"`
3636
Value string `json:"value"`
3737
} `json:"tags"`
38-
ValutType string `json:"value_type"`
38+
SNMPOid string `json:"snmp_oid"`
3939
Trends string `json:"trends"`
4040
Units string `json:"units"`
4141
ValueType string `json:"value_type"`
4242
Delay string `json:"delay"`
4343
ValuemapID string `json:"valuemapid"`
4444
}
4545

46-
//Application struct
46+
// Application struct
4747
type TagsItems struct {
4848
Name string `json:"name"`
4949
Items []MonIts `json:"items"`

models/base.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
_ "github.com/go-sql-driver/mysql"
1212
workwx "github.com/xen0n/go-workwx"
1313
ini "gopkg.in/ini.v1"
14+
"net/http"
1415
"strconv"
1516
"strings"
17+
"time"
1618
"zbxtable/utils"
1719

1820
jsoniter "github.com/json-iterator/go"
@@ -21,8 +23,12 @@ import (
2123
)
2224

2325
var (
24-
API = &zabbix.API{}
25-
json = jsoniter.ConfigCompatibleWithStandardLibrary
26+
API = &zabbix.API{}
27+
json = jsoniter.Config{
28+
EscapeHTML: false,
29+
SortMapKeys: true,
30+
ValidateJsonRawMessage: true,
31+
}.Froze()
2632
RDB = &redis.Client{}
2733
ZBX_VER string
2834
ZBX_V bool
@@ -95,7 +101,22 @@ func ModelsInit(zabbix_web, zabbix_user, zabbix_pass, zabbix_token,
95101
}
96102
// init admin
97103
DatabaseInit()
98-
104+
//判断API地址是否正确,http get访问访问api地址判断状态码是不是412
105+
url := zabbix_web + "/api_jsonrpc.php"
106+
dClient := http.Client{
107+
Timeout: 3 * time.Second, // 设置超时时间为 3 秒
108+
}
109+
resp, err := dClient.Get(url)
110+
if err != nil {
111+
logs.Error("Zabbix Web get request failed:", err)
112+
os.Exit(1)
113+
}
114+
defer resp.Body.Close()
115+
if resp.StatusCode != http.StatusPreconditionFailed {
116+
logs.Error("Zabbix Web is incorrectly!")
117+
os.Exit(1)
118+
}
119+
//api变量
99120
API = zabbix.NewAPI(zabbix_web + "/api_jsonrpc.php")
100121
if zabbix_token != "" {
101122
API.Auth = zabbix_token

0 commit comments

Comments
 (0)