Skip to content

Commit

Permalink
1.5.0网卡数据获取问题
Browse files Browse the repository at this point in the history
2.复杂密码导致PgSql连接失败
  • Loading branch information
canghai908 committed Feb 5, 2024
1 parent 1b59552 commit 8ec3f90
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 72 deletions.
17 changes: 11 additions & 6 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/urfave/cli/v2"
"gopkg.in/ini.v1"
"net/http"
"net/url"
"os"
"time"
"zbxtable/models"
Expand Down Expand Up @@ -96,9 +97,9 @@ func CheckDb(dbdriver, dbhost, dbuser, dbpass, dbname string, dbport string) err
//database type
switch dbdriver {
case "mysql":
dburl := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbURL := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbport + ")/" + dbname + "?parseTime=true&loc=Asia%2FShanghai&timeout=5s&charset=utf8&collation=utf8_general_ci"
db, err := sql.Open("mysql", dburl)
db, err := sql.Open("mysql", dbURL)
if err != nil {
return err
}
Expand All @@ -107,8 +108,12 @@ func CheckDb(dbdriver, dbhost, dbuser, dbpass, dbname string, dbport string) err
return err
}
case "postgresql":
dburl := "postgres://" + dbuser + ":" + dbpass + "@" + dbhost + ":" + dbport + "/" + dbname + "?sslmode=disable"
db, err := sql.Open("postgres", dburl)
dbURL := "postgres://" + dbuser + ":" + url.QueryEscape(dbpass) + "@" + dbhost + ":" + dbport + "/" + dbname + "?sslmode=disable"
connString, err := url.Parse(dbURL)
if err != nil {
return err
}
db, err := sql.Open("postgres", connString.String())
if err != nil {
return err
}
Expand All @@ -127,11 +132,11 @@ func CheckZabbixAPI(args ...string) (string, error) {
pass := args[2]
token := args[3]
//判断API地址是否正确,http get访问访问api地址判断状态码是不是412
url := address + "/api_jsonrpc.php"
addURL := address + "/api_jsonrpc.php"
dClient := http.Client{
Timeout: 3 * time.Second, // 设置超时时间为 3 秒
}
resp, err := dClient.Get(url)
resp, err := dClient.Get(addURL)
if err != nil {
logs.Error("Zabbix Web get request failed:", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion control
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# release version
version=2.1.4
version=2.1.5

CWD=$(cd $(dirname $0)/; pwd)
cd $CWD
Expand Down
22 changes: 14 additions & 8 deletions models/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
workwx "github.com/xen0n/go-workwx"
ini "gopkg.in/ini.v1"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -62,24 +63,29 @@ func ModelsInit(zabbix_web, zabbix_user, zabbix_pass, zabbix_token,
//database chechek
switch dbtype {
case "mysql":
dburl := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbURL := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbport + ")/" + dbname + "?parseTime=true&loc=Asia%2FShanghai&timeout=5s&charset=utf8&collation=utf8_general_ci"
err := orm.RegisterDataBase("default", "mysql", dburl)
err := orm.RegisterDataBase("default", "mysql", dbURL)
if err != nil {
logs.Error(err)
os.Exit(1)
}
case "postgresql":
dburl := "postgres://" + dbuser + ":" + dbpass + "@" + dbhost + ":" + dbport + "/" + dbname + "?sslmode=disable"
err := orm.RegisterDataBase("default", "postgres", dburl)
dbURl := "postgres://" + dbuser + ":" + url.QueryEscape(dbpass) + "@" + dbhost + ":" + dbport + "/" + dbname + "?sslmode=disable"
connString, err := url.Parse(dbURl)
if err != nil {
logs.Error(err)
os.Exit(1)
}
err = orm.RegisterDataBase("default", "postgres", connString.String())
if err != nil {
logs.Error(err)
os.Exit(1)
}
default:
dburl := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbURL := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" +
dbport + ")/" + dbname + "?parseTime=true&loc=Asia%2FShanghai&timeout=5s&charset=utf8&collation=utf8_general_ci"
err := orm.RegisterDataBase("default", "mysql", dburl)
err := orm.RegisterDataBase("default", "mysql", dbURL)
if err != nil {
logs.Error(err)
os.Exit(1)
Expand All @@ -102,11 +108,11 @@ func ModelsInit(zabbix_web, zabbix_user, zabbix_pass, zabbix_token,
// init admin
DatabaseInit()
//判断API地址是否正确,http get访问访问api地址判断状态码是不是412
url := zabbix_web + "/api_jsonrpc.php"
addURL := zabbix_web + "/api_jsonrpc.php"
dClient := http.Client{
Timeout: 3 * time.Second, // 设置超时时间为 3 秒
}
resp, err := dClient.Get(url)
resp, err := dClient.Get(addURL)
if err != nil {
logs.Error("Zabbix Web get request failed:", err)
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions models/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ func GetInterfaceGraphData(data InterfaceData) (series TrafficData, err error) {

// GetFloat64ArrayData 计算float64数组的平均值,最大,最小,95%平均值,95%值,并保留3位小数
func GetFloat64ArrayData(data []float64) (avg, max, min, perc95Avg, perc95Val string) {
//空数组判断
if len(data) == 0 {
return "0", "0", "0", "0", "0"
}
//data拷贝到data1
data1 := make([]float64, 0)
data1 = append(data1, data[:]...)
Expand Down
94 changes: 72 additions & 22 deletions models/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,27 +407,27 @@ func GetInterfaceData(hostid string) ([]InterfaceData, error) {
p := strings.Split(v.SNMPOid, ".")
existingRow.Index = p[len(p)-1]
existingRow.Lastclock = v.Lastclock
existingRow.BitsReceived = utils.InterfaceTrafficeStrTofloat64(v.Lastvalue)
existingRow.BitsReceived = v.Lastvalue
existingRow.BitsReceivedItemId = v.Itemid
existingRow.BitsReceivedValueType = v.ValueType
case strings.Contains(v.Name, "Bits sent"):
existingRow.BitsSent = utils.InterfaceTrafficeStrTofloat64(v.Lastvalue)
existingRow.BitsSent = v.Lastvalue
existingRow.BitsSentItemId = v.Itemid
existingRow.BitsSentValueType = v.ValueType
case strings.Contains(v.Name, "Inbound packets discarded"):
existingRow.InDiscarded = utils.InterfaceStrToInt64(v.Lastvalue)
existingRow.InDiscarded = v.Lastvalue
existingRow.InDiscardedItemId = v.Itemid
existingRow.InDiscardedValueType = v.ValueType
case strings.Contains(v.Name, "Inbound packets with errors"):
existingRow.InErrors = utils.InterfaceStrToInt64(v.Lastvalue)
existingRow.InErrors = v.Lastvalue
existingRow.InErrorsItemId = v.Itemid
existingRow.InErrorsValueType = v.ValueType
case strings.Contains(v.Name, "Outbound packets discarded"):
existingRow.OutDiscarded = utils.InterfaceStrToInt64(v.Lastvalue)
existingRow.OutDiscarded = v.Lastvalue
existingRow.OutDiscardedItemId = v.Itemid
existingRow.OutDiscardedValueType = v.ValueType
case strings.Contains(v.Name, "Outbound packets with errors"):
existingRow.OutErrors = utils.InterfaceStrToInt64(v.Lastvalue)
existingRow.OutErrors = v.Lastvalue
existingRow.OutErrorsItemId = v.Itemid
existingRow.OutErrorsValueType = v.ValueType
case strings.Contains(v.Name, "Speed"):
Expand All @@ -451,7 +451,9 @@ func GetInterfaceData(hostid string) ([]InterfaceData, error) {
}

//旧版本处理
selectItemsPar := []string{"itemid", "value_type", "name", "key_", "delay", "units", "lastvalue", "lastclock", "valuemapid"}
//获取网卡应用所有地址
selectItemsPar := []string{"itemid", "value_type", "name", "key_", "delay", "units",
"lastvalue", "lastclock", "snmp_oid", "valuemapid"}
Key2Par := []string{"Interface "}
Search2Par := make(map[string][]string)
Search2Par["name"] = Key2Par
Expand All @@ -474,27 +476,75 @@ func GetInterfaceData(hostid string) ([]InterfaceData, error) {
if err != nil {
return InterfaceDataList{}, err
}
//

var list InterfaceDataList
var data InterfaceData
//遍历应用内所有接口
for _, v := range ApplicationRes {
//过滤interface {#IFNAME}接口
if len(v.Items) < 7 {
continue
}
data.Index = v.Applicationid
data.Name = strings.Replace(v.Name, "Interface ", "", -1)
data.InDiscarded = utils.InterfaceStrToInt64(v.Items[0].Lastvalue)
data.InErrors = utils.InterfaceStrToInt64(v.Items[1].Lastvalue)
data.BitsReceived = utils.InterfaceTrafficeStrTofloat64(v.Items[2].Lastvalue)
data.OutDiscarded = utils.InterfaceStrToInt64(v.Items[3].Lastvalue)
data.OutErrors = utils.InterfaceStrToInt64(v.Items[4].Lastvalue)
data.BitsSent = utils.InterfaceTrafficeStrTofloat64(v.Items[5].Lastvalue)
data.Speed = v.Items[6].Lastvalue
//valuemap 映射
if v.Items[7].ValuemapID != "0" {
p, _ := GetValueMapByID(v.Items[7].ValuemapID, v.Items[7].Lastvalue)
data.OperationalStatus = p + "(" + v.Items[7].Lastvalue + ")"
} else {
data.OperationalStatus = v.Items[7].Lastvalue
for _, vv := range v.Items {
//接口对应
//收流量
if strings.Contains(vv.Name, "Bits received") {
data.BitsReceived = vv.Lastvalue
data.Lastclock = vv.Lastclock
index := strings.Split(vv.SNMPOid, ".")
data.Index = index[len(index)-1]
data.BitsReceivedItemId = vv.Itemid
data.BitsReceivedValueType = vv.ValueType
}
//发流量
if strings.Contains(vv.Name, "Bits sent") {
data.BitsSent = vv.Lastvalue
data.BitsSentItemId = vv.Itemid
data.BitsSentValueType = vv.ValueType

}
//Inbound packets with errors
if strings.Contains(vv.Name, "Inbound packets with errors") {
data.InErrors = vv.Lastvalue
data.InErrorsItemId = vv.Itemid
data.InErrorsValueType = vv.ValueType
}
//Outbound packets with errors
if strings.Contains(vv.Name, "Outbound packets with errors") {
data.OutErrors = vv.Lastvalue
data.OutErrorsItemId = vv.Itemid
data.OutErrorsValueType = vv.ValueType
}
//Outbound packets discarded
if strings.Contains(vv.Name, "Outbound packets discarded") {
data.OutDiscarded = vv.Lastvalue
data.OutDiscardedItemId = vv.Itemid
data.OutDiscardedValueType = vv.ValueType
}
//Inbound packets discarded
if strings.Contains(vv.Name, "Inbound packets discarded") {
data.InDiscarded = vv.Lastvalue
data.InDiscardedItemId = vv.Itemid
data.InDiscardedValueType = vv.ValueType
}
//Speed
if strings.Contains(vv.Name, "Speed") {
data.Speed = vv.Lastvalue
}
//Operational status
if strings.Contains(vv.Name, "Operational status") {
if vv.ValuemapID != "0" {
p, _ := GetValueMapByID(vv.ValuemapID, vv.Lastvalue)
data.OperationalStatus = p + "(" + vv.Lastvalue + ")"
} else {
data.OperationalStatus = vv.Lastvalue
}
data.OperationalStatusItemId = vv.Itemid
data.OperationalStatusValueType = vv.ValueType
}
}
data.Lastclock = v.Items[2].Lastclock
list = append(list, data)
}
return list, nil
Expand Down
55 changes: 28 additions & 27 deletions models/host_mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,39 +291,40 @@ type MonItem struct {
Units string `json:"units"`
Lastvalue string `json:"lastvalue"`
Lastclock string `json:"lastclock"`
SNMPOid string `json:"snmp_oid"`
ValuemapID string `json:"valuemapid"`
} `json:"items"`
}

type InterfaceDataList []InterfaceData
type InterfaceData struct {
Index string `json:"index"`
Name string `json:"name"`
InDiscarded int64 `json:"in_discarded"`
InDiscardedItemId string `json:"in_discarded_itemid"`
InDiscardedValueType string `json:"in_discarded_value_type"`
InErrors int64 `json:"in_errors"`
InErrorsItemId string `json:"in_errors_itemid"`
InErrorsValueType string `json:"in_errors_value_type"`
BitsReceived float64 `json:"bits_received"`
BitsReceivedItemId string `json:"bits_received_itemid"`
BitsReceivedValueType string `json:"bits_received_value_type"`
BitsSent float64 `json:"bits_sent"`
BitsSentItemId string `json:"bits_sent_itemid"`
BitsSentValueType string `json:"bits_sent_value_type"`
OutDiscarded int64 `json:"out_discarded"`
OutDiscardedItemId string `json:"out_discarded_itemid"`
OutDiscardedValueType string `json:"out_discarded_value_type"`
OutErrors int64 `json:"out_errors"`
OutErrorsItemId string `json:"out_errors_itemid"`
OutErrorsValueType string `json:"out_errors_value_type"`
Speed string `json:"speed"`
OperationalStatus string `json:"operational_status"`
OperationalStatusItemId string `json:"operational_status_itemid"`
OperationalStatusValueType string `json:"operational_status_value_type"`
Lastclock string `json:"lastclock"`
Begin string `json:"begin"`
End string `json:"end"`
Index string `json:"index"`
Name string `json:"name"`
InDiscarded string `json:"in_discarded"`
InDiscardedItemId string `json:"in_discarded_itemid"`
InDiscardedValueType string `json:"in_discarded_value_type"`
InErrors string `json:"in_errors"`
InErrorsItemId string `json:"in_errors_itemid"`
InErrorsValueType string `json:"in_errors_value_type"`
BitsReceived string `json:"bits_received"`
BitsReceivedItemId string `json:"bits_received_itemid"`
BitsReceivedValueType string `json:"bits_received_value_type"`
BitsSent string `json:"bits_sent"`
BitsSentItemId string `json:"bits_sent_itemid"`
BitsSentValueType string `json:"bits_sent_value_type"`
OutDiscarded string `json:"out_discarded"`
OutDiscardedItemId string `json:"out_discarded_itemid"`
OutDiscardedValueType string `json:"out_discarded_value_type"`
OutErrors string `json:"out_errors"`
OutErrorsItemId string `json:"out_errors_itemid"`
OutErrorsValueType string `json:"out_errors_value_type"`
Speed string `json:"speed"`
OperationalStatus string `json:"operational_status"`
OperationalStatusItemId string `json:"operational_status_itemid"`
OperationalStatusValueType string `json:"operational_status_value_type"`
Lastclock string `json:"lastclock"`
Begin string `json:"begin"`
End string `json:"end"`
}
type WinFilesSystemData struct {
ID int64 `json:"id"`
Expand Down
12 changes: 6 additions & 6 deletions models/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ func GetOverviewData() (OverviewList, error) {
}
listmap[v] = ArrayOne
}
var Newlist OverviewList
Newlist.Lin = listmap["VM_LIN"]
Newlist.Win = listmap["VM_WIN"]
Newlist.NET = listmap["HW_NET"]
Newlist.SRV = listmap["HW_SRV"]
return Newlist, nil
var newList OverviewList
newList.Lin = listmap["VM_LIN"]
newList.Win = listmap["VM_WIN"]
newList.NET = listmap["HW_NET"]
newList.SRV = listmap["HW_SRV"]
return newList, nil
}
func GetEgressData() (EgressList, error) {
var ctx = context.Background()
Expand Down
5 changes: 3 additions & 2 deletions models/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func HostTypeSet(s *System, groupId []string) error {
}
//inventory
InventoryPara := make(map[string]string)
//主机类型直接写入,不关联监控指标
InventoryPara["type"] = hType
//开启主机Inventory为自动,并归类
_, err = API.CallWithError("host.massupdate", Params{
"hosts": p,
Expand All @@ -157,8 +159,7 @@ func HostTypeSet(s *System, groupId []string) error {
if err != nil {
return err
}
//主机类型直接写入,不关联监控指标
InventoryPara["type"] = hType

//其他指标绑定
ItemToInventory(s.UptimeID, UptimeID)
ItemToInventory(s.CPUCore, CPUCore)
Expand Down

0 comments on commit 8ec3f90

Please sign in to comment.