forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package funcs | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gaochao1/swcollector/g" | ||
) | ||
|
||
type EcmcRes struct { | ||
Dat json.RawMessage `json:"dat"` | ||
Err string `json:"err"` | ||
} | ||
|
||
type Host struct { | ||
ID int64 `json:"id"` | ||
SN string `json:"sn"` | ||
IP string `json:"ip"` | ||
Name string `json:"name"` | ||
Note string `json:"note"` | ||
Cpu string `json:"cpu"` | ||
Mem string `json:"mem"` | ||
Disk string `json:"disk"` | ||
Cate string `json:"cate"` | ||
Tenant string `json:"tenant"` | ||
} | ||
|
||
type n9eHost struct { | ||
ID int64 `json:"id"` | ||
Ident string `json:"ident"` | ||
Alias string `json:"alias"` | ||
} | ||
|
||
type NodeHosts struct { | ||
List []Host `json:"list"` | ||
Total int64 `json:"total"` | ||
} | ||
|
||
// HTTPGet 发起一个 http get 请求 | ||
func HTTPGet(url string, headers map[string]string) (body []byte, err error) { | ||
body = []byte{} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return | ||
} | ||
for k, v := range headers { | ||
req.Header.Set(k, v) | ||
} | ||
|
||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{ | ||
Transport: tr, | ||
Timeout: 15 * time.Second, | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
body, _ = ioutil.ReadAll(resp.Body) | ||
erroMsg := fmt.Sprintf("HTTP Connect Failed, Code is %d, body is %s", resp.StatusCode, string(body)) | ||
err = errors.New(erroMsg) | ||
return | ||
} | ||
body, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func GetNodeHosts(nodeID int64) (hosts []Host, err error) { | ||
p := 1 | ||
for { | ||
apiAddr := fmt.Sprintf("%s/api/hsp/node/obj/%d/host?p=%d", g.Config().Ecmc.Addr, nodeID, p) | ||
headers := map[string]string{} | ||
headers["x-user-token"] = g.Config().Ecmc.Token | ||
var res []byte | ||
res, err = HTTPGet(apiAddr, headers) | ||
if err != nil { | ||
return | ||
} | ||
var ecmcRes EcmcRes | ||
if err = json.Unmarshal(res, &ecmcRes); err != nil { | ||
return | ||
} | ||
if ecmcRes.Err != "" { | ||
err = errors.New(ecmcRes.Err) | ||
return | ||
} | ||
var nodeHosts NodeHosts | ||
if err = json.Unmarshal(ecmcRes.Dat, &nodeHosts); err != nil { | ||
return | ||
} | ||
if len(nodeHosts.List) == 0 { | ||
break | ||
} | ||
hosts = append(hosts, nodeHosts.List...) | ||
p = p + 1 | ||
} | ||
return | ||
} | ||
|
||
func GetAllByIpByEcmc() (ips []string) { | ||
ipMap := map[string]bool{} | ||
for _, id := range g.Config().Ecmc.Nodes { | ||
hosts, err := GetNodeHosts(id) | ||
if err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
for _, host := range hosts { | ||
if _, ok := ipMap[host.IP]; ok { | ||
continue | ||
} | ||
ipMap[host.IP] = true | ||
ips = append(ips, host.IP) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package funcs | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"log" | ||
|
||
"github.com/gaochao1/swcollector/g" | ||
) | ||
|
||
type N9eNodeHosts struct { | ||
List []N9eHost `json:"list"` | ||
Total int64 `json:"total"` | ||
} | ||
|
||
type N9eHost struct { | ||
ID int64 `json:"id"` | ||
IP string `json:"ident"` | ||
Alias string `json:"alias"` | ||
} | ||
|
||
func basicAuth(username, password string) string { | ||
auth := username + ":" + password | ||
return base64.StdEncoding.EncodeToString([]byte(auth)) | ||
} | ||
|
||
func GetN9eHosts(nodeID int64) (hosts []N9eHost, err error) { | ||
p := 1 | ||
for { | ||
apiAddr := fmt.Sprintf("%s/api/portal/node/%d/endpoint?p=%d", g.Config().N9e.Addr, nodeID, p) | ||
headers := map[string]string{} | ||
headers["Authorization"] = "Basic " + basicAuth(g.Config().N9e.User, g.Config().N9e.Pass) | ||
var res []byte | ||
res, err = HTTPGet(apiAddr, headers) | ||
if err != nil { | ||
return | ||
} | ||
var ecmcRes EcmcRes | ||
if err = json.Unmarshal(res, &ecmcRes); err != nil { | ||
return | ||
} | ||
if ecmcRes.Err != "" { | ||
err = errors.New(ecmcRes.Err) | ||
return | ||
} | ||
var nodeHosts N9eNodeHosts | ||
if err = json.Unmarshal(ecmcRes.Dat, &nodeHosts); err != nil { | ||
return | ||
} | ||
if len(nodeHosts.List) == 0 { | ||
break | ||
} | ||
hosts = append(hosts, nodeHosts.List...) | ||
p = p + 1 | ||
} | ||
return | ||
} | ||
|
||
func GetAllByIpByN9e() (ips []string) { | ||
ipMap := map[string]bool{} | ||
for _, id := range g.Config().N9e.Nodes { | ||
hosts, err := GetN9eHosts(id) | ||
if err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
for _, host := range hosts { | ||
if _, ok := ipMap[host.IP]; ok { | ||
continue | ||
} | ||
ipMap[host.IP] = true | ||
ips = append(ips, host.IP) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters