```
func UpdateAgent(agentInfo *model.AgentUpdateInfo) {
sql := ""
if g.Config().Hosts == "" {
sql = fmt.Sprintf(
"insert into host(hostname, ip, agent_version, plugin_version) values ('%s', '%s', '%s', '%s') on duplicate key update ip='%s', agent_version='%s', plugin_version='%s'",
agentInfo.ReportRequest.Hostname,
agentInfo.ReportRequest.IP,
agentInfo.ReportRequest.AgentVersion,
agentInfo.ReportRequest.PluginVersion,
agentInfo.ReportRequest.IP,
agentInfo.ReportRequest.AgentVersion,
agentInfo.ReportRequest.PluginVersion,
)
} else {
// sync, just update
sql = fmt.Sprintf(
"update host set ip='%s', agent_version='%s', plugin_version='%s' where hostname='%s'",
agentInfo.ReportRequest.IP,
agentInfo.ReportRequest.AgentVersion,
agentInfo.ReportRequest.PluginVersion,
agentInfo.ReportRequest.Hostname,
)
}
defer func() {
if err := recover(); err != nil {
log.Panicln("出错了:", err)
}
}()
_, err := DB.Exec(sql)
if err != nil {
log.Println("exec", sql, "fail", err)
} else {
url := g.Config().GroupHostAddUrl
fmt.Println(url)
var hosts, group_name string
hosts = agentInfo.ReportRequest.Hostname
group_name = g.Config().DefaultHostGroup
var jsonStr = []byte(`{"hosts":"` + hosts + `","group_name":"` + group_name + `"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
log.Println("Post To Portal API Fail:", sql, err)
}
}
}
```
报指针为空错误错:
```
panic: 出错了: runtime error: invalid memory address or nil pointer dereference
```
新手求解
有疑问加站长微信联系(非本文作者)