mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-20 08:03:53 +08:00
telegraf raid plugin
This commit is contained in:
3
build/docker/Dockerfile.telegraf-raid
Normal file
3
build/docker/Dockerfile.telegraf-raid
Normal file
@@ -0,0 +1,3 @@
|
||||
FROM registry.cn-beijing.aliyuncs.com/yunionio/yunion-rootfs:v1.0
|
||||
|
||||
ADD ./_output/bin/telegraf-raid-plugin /opt/yunion/bin/telegraf-raid-plugin
|
||||
@@ -33,7 +33,8 @@ type BaseOptions struct {
|
||||
Host string `help:"SSH Host IP" default:"$RAID_HOST" metavar:"RAID_HOST"`
|
||||
Username string `help:"Username, usually root" default:"$RAID_USERNAME" metavar:"RAID_USERNAME"`
|
||||
Password string `help:"Password" default:"$RAID_PASSWORD" metavar:"RAID_PASSWORD"`
|
||||
Driver string `help:"Password" default:"$RAID_DRIVER" metavar:"RAID_DRIVER" choices:"MegaRaid|HPSARaid|Mpt2SAS|MarvelRaid"`
|
||||
Driver string `help:"Raid dirver" default:"$RAID_DRIVER" metavar:"RAID_DRIVER" choices:"MegaRaid|HPSARaid|Mpt2SAS|MarvelRaid"`
|
||||
LocalHost bool `help:"Run raidcli in localhost"`
|
||||
SUBCOMMAND string `help:"s3cli subcommand" subcommand:"true"`
|
||||
}
|
||||
|
||||
@@ -83,43 +84,48 @@ func showErrorAndExit(e error) {
|
||||
}
|
||||
|
||||
func newClient() (raid.IRaidDriver, error) {
|
||||
if len(options.Host) == 0 {
|
||||
return nil, fmt.Errorf("Missing host")
|
||||
}
|
||||
|
||||
if len(options.Username) == 0 {
|
||||
return nil, fmt.Errorf("Missing username")
|
||||
}
|
||||
|
||||
if len(options.Password) == 0 {
|
||||
return nil, fmt.Errorf("Missing password")
|
||||
if options.Debug {
|
||||
raid.Debug = true
|
||||
}
|
||||
|
||||
if len(options.Driver) == 0 {
|
||||
return nil, fmt.Errorf("Missing driver")
|
||||
}
|
||||
|
||||
if options.Debug {
|
||||
raid.Debug = true
|
||||
var drv raid.IRaidDriver
|
||||
if !options.LocalHost {
|
||||
if len(options.Host) == 0 {
|
||||
return nil, fmt.Errorf("Missing host")
|
||||
}
|
||||
|
||||
if len(options.Username) == 0 {
|
||||
return nil, fmt.Errorf("Missing username")
|
||||
}
|
||||
|
||||
if len(options.Password) == 0 {
|
||||
return nil, fmt.Errorf("Missing password")
|
||||
}
|
||||
|
||||
sshClient, err := ssh.NewClient(
|
||||
options.Host,
|
||||
22,
|
||||
options.Username,
|
||||
options.Password,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ssh client init fail: %s", err)
|
||||
}
|
||||
|
||||
drv = drivers.GetDriver(options.Driver, sshClient)
|
||||
if drv == nil {
|
||||
return nil, fmt.Errorf("not supported driver %s", options.Driver)
|
||||
}
|
||||
} else {
|
||||
drv = drivers.GetLocalDriver(options.Driver)
|
||||
}
|
||||
|
||||
sshClient, err := ssh.NewClient(
|
||||
options.Host,
|
||||
22,
|
||||
options.Username,
|
||||
options.Password,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ssh client init fail: %s", err)
|
||||
}
|
||||
|
||||
drv := drivers.GetDriver(options.Driver, sshClient)
|
||||
if drv == nil {
|
||||
return nil, fmt.Errorf("not supported driver %s", options.Driver)
|
||||
}
|
||||
|
||||
err = drv.ParsePhyDevs()
|
||||
err := drv.ParsePhyDevs()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse phyical devices error %s", err)
|
||||
}
|
||||
|
||||
192
cmd/telegraf-raid-plugin/main.go
Normal file
192
cmd/telegraf-raid-plugin/main.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/detect_storages"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid/drivers"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
const (
|
||||
INTERVAL_SECOND = 300
|
||||
TelegrafServer = "http://localhost:8087/write"
|
||||
)
|
||||
|
||||
// Failed, Offline, Degraded, Rebuilding, Out of Sync (OSY)
|
||||
|
||||
func main() {
|
||||
// creates the in-cluster config
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
// creates the clientset
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
nodeName := os.Getenv("NODENAME")
|
||||
if len(nodeName) == 0 {
|
||||
log.Fatalf("Missing env nodename")
|
||||
}
|
||||
|
||||
node, err := clientset.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
var masterAddress string
|
||||
if length := len(node.Status.Conditions); length > 0 {
|
||||
if node.Status.Conditions[length-1].Type == v1.NodeReady &&
|
||||
node.Status.Conditions[length-1].Status == v1.ConditionTrue {
|
||||
for _, addr := range node.Status.Addresses {
|
||||
if addr.Type == v1.NodeInternalIP {
|
||||
masterAddress = addr.Address
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("Start Colloct Raid Info And Send To Telegraf ...")
|
||||
c := NewRaidInfoCollector(nodeName, masterAddress, INTERVAL_SECOND)
|
||||
c.Start()
|
||||
}
|
||||
|
||||
type RaidInfoCollector struct {
|
||||
waitingReportData []string
|
||||
LastCollectTime time.Time
|
||||
ReportInterval int // seconds
|
||||
Hostname string
|
||||
HostIp string
|
||||
}
|
||||
|
||||
func NewRaidInfoCollector(hostname, hostIp string, interval int) *RaidInfoCollector {
|
||||
return &RaidInfoCollector{
|
||||
waitingReportData: make([]string, 0),
|
||||
ReportInterval: interval,
|
||||
Hostname: hostname,
|
||||
HostIp: hostIp,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) runMain() {
|
||||
timeBegin := time.Now()
|
||||
elapse := timeBegin.Sub(c.LastCollectTime)
|
||||
if elapse < time.Second*time.Duration(c.ReportInterval) {
|
||||
return
|
||||
} else {
|
||||
c.LastCollectTime = timeBegin
|
||||
}
|
||||
c.runMonitor()
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) runMonitor() {
|
||||
reportData := c.collectReportData()
|
||||
if len(reportData) > 0 {
|
||||
c.reportRaidInfoToTelegraf(reportData)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) collectReportData() string {
|
||||
if len(c.waitingReportData) > 60 {
|
||||
c.waitingReportData = c.waitingReportData[1:]
|
||||
}
|
||||
return c.CollectReportData()
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) CollectReportData() string {
|
||||
raidDiskInfo := make([]*baremetal.BaremetalStorage, 0)
|
||||
// raidDrivers := []string{}
|
||||
for _, drv := range drivers.GetDrivers(drivers.NewExecutor()) {
|
||||
if err := drv.ParsePhyDevs(); err != nil {
|
||||
log.Warningf("Raid driver %s ParsePhyDevs failed: %s", drv.GetName(), err)
|
||||
continue
|
||||
}
|
||||
raidDiskInfo = append(raidDiskInfo, detect_storages.GetRaidDevices(drv)...)
|
||||
// raidDrivers = append(raidDrivers, drv.GetName())
|
||||
}
|
||||
if len(raidDiskInfo) > 0 {
|
||||
ret := c.toTelegrafReportData(raidDiskInfo)
|
||||
return ret
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) Start() {
|
||||
for {
|
||||
c.runMain()
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
}
|
||||
|
||||
const MEASUREMENT = "host_raid"
|
||||
|
||||
func (c *RaidInfoCollector) toTelegrafReportData(raidDiskInfo []*baremetal.BaremetalStorage) string {
|
||||
tag := fmt.Sprintf("%s=%s,%s=%s", "hostname", c.Hostname, "host_ip", c.HostIp)
|
||||
ret := []string{}
|
||||
for i := 0; i < len(raidDiskInfo); i++ {
|
||||
statArr := []string{}
|
||||
jStat := jsonutils.Marshal(raidDiskInfo[i])
|
||||
jMap, _ := jStat.GetMap()
|
||||
for k, v := range jMap {
|
||||
statArr = append(statArr, fmt.Sprintf("%s=%s", k, v.String()))
|
||||
}
|
||||
stat := strings.Join(statArr, ",")
|
||||
diskTag := fmt.Sprintf(
|
||||
"%s,%s=%s,%s=%d,%s=%d", tag, "driver", raidDiskInfo[i].Driver,
|
||||
"adapter", raidDiskInfo[i].Adapter, "slot", raidDiskInfo[i].Slot,
|
||||
)
|
||||
line := fmt.Sprintf("%s,%s %s", MEASUREMENT, diskTag, stat)
|
||||
ret = append(ret, line)
|
||||
}
|
||||
return strings.Join(ret, "\n")
|
||||
}
|
||||
|
||||
func (c *RaidInfoCollector) reportRaidInfoToTelegraf(data string) {
|
||||
body := strings.NewReader(data)
|
||||
res, err := httputils.Request(
|
||||
httputils.GetDefaultClient(), context.Background(), "POST", TelegrafServer, nil, body, false)
|
||||
if err != nil {
|
||||
log.Errorf("Upload guest metric failed: %s", err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 204 {
|
||||
log.Errorf("upload guest metric failed %d", res.StatusCode)
|
||||
timestamp := time.Now().UnixNano()
|
||||
for _, line := range strings.Split(data, "\n") {
|
||||
c.waitingReportData = append(c.waitingReportData,
|
||||
fmt.Sprintf("%s %d", line, timestamp))
|
||||
}
|
||||
} else {
|
||||
if len(c.waitingReportData) > 0 {
|
||||
oldDatas := strings.Join(c.waitingReportData, "\n")
|
||||
body = strings.NewReader(oldDatas)
|
||||
res, err = httputils.Request(
|
||||
httputils.GetDefaultClient(), context.Background(), "POST", TelegrafServer, nil, body, false)
|
||||
if err == nil {
|
||||
defer res.Body.Close()
|
||||
}
|
||||
if res.StatusCode == 204 {
|
||||
c.waitingReportData = c.waitingReportData[len(c.waitingReportData):]
|
||||
} else {
|
||||
log.Errorf("upload guest metric failed code: %d", res.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid/drivers"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/types"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
"yunion.io/x/onecloud/pkg/util/sysutils"
|
||||
)
|
||||
|
||||
@@ -48,7 +47,7 @@ func GetRaidLogicVolumes(drv raid.IRaidDriver) ([]*raid.RaidLogicalVolume, error
|
||||
return lvs, nil
|
||||
}
|
||||
|
||||
func DetectStorageInfo(term *ssh.Client, wait bool) ([]*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, error) {
|
||||
func DetectStorageInfo(term raid.IExecTerm, wait bool) ([]*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, error) {
|
||||
raidDiskInfo := make([]*baremetal.BaremetalStorage, 0)
|
||||
lvDiskInfo := make([]*raid.RaidLogicalVolume, 0)
|
||||
|
||||
|
||||
@@ -29,10 +29,9 @@ import (
|
||||
_ "yunion.io/x/onecloud/pkg/baremetal/utils/raid/mvcli"
|
||||
_ "yunion.io/x/onecloud/pkg/baremetal/utils/raid/sas2iru"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
func GetDriver(name string, term *ssh.Client) raid.IRaidDriver {
|
||||
func GetDriver(name string, term raid.IExecTerm) raid.IRaidDriver {
|
||||
factory := raid.RaidDrivers[name]
|
||||
if factory == nil {
|
||||
return nil
|
||||
@@ -40,7 +39,15 @@ func GetDriver(name string, term *ssh.Client) raid.IRaidDriver {
|
||||
return factory(term)
|
||||
}
|
||||
|
||||
func GetDriverWithInit(name string, term *ssh.Client) (raid.IRaidDriver, error) {
|
||||
func GetLocalDriver(name string) raid.IRaidDriver {
|
||||
factory := raid.RaidDrivers[name]
|
||||
if factory == nil {
|
||||
return nil
|
||||
}
|
||||
return factory(NewExecutor())
|
||||
}
|
||||
|
||||
func GetDriverWithInit(name string, term raid.IExecTerm) (raid.IRaidDriver, error) {
|
||||
drv := GetDriver(name, term)
|
||||
if drv == nil {
|
||||
return nil, errors.Errorf("Not found raid driver %q", name)
|
||||
@@ -48,7 +55,7 @@ func GetDriverWithInit(name string, term *ssh.Client) (raid.IRaidDriver, error)
|
||||
return drv, drv.ParsePhyDevs()
|
||||
}
|
||||
|
||||
func GetDriverByKernelModule(module string, term *ssh.Client) (raid.IRaidDriver, error) {
|
||||
func GetDriverByKernelModule(module string, term raid.IExecTerm) (raid.IRaidDriver, error) {
|
||||
name := ""
|
||||
switch module {
|
||||
case raid.MODULE_MEGARAID:
|
||||
@@ -64,7 +71,7 @@ func GetDriverByKernelModule(module string, term *ssh.Client) (raid.IRaidDriver,
|
||||
return GetDriverWithInit(name, term)
|
||||
}
|
||||
|
||||
func GetDrivers(term *ssh.Client) []raid.IRaidDriver {
|
||||
func GetDrivers(term raid.IExecTerm) []raid.IRaidDriver {
|
||||
ret := []raid.IRaidDriver{}
|
||||
for _, factory := range raid.RaidDrivers {
|
||||
ret = append(ret, factory(term))
|
||||
|
||||
56
pkg/baremetal/utils/raid/drivers/executor.go
Normal file
56
pkg/baremetal/utils/raid/drivers/executor.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
type Executor struct{}
|
||||
|
||||
func (e *Executor) Run(cmds ...string) ([]string, error) {
|
||||
return e.run(true, cmds, nil)
|
||||
}
|
||||
|
||||
func (e *Executor) RunWithInput(input io.Reader, cmds ...string) ([]string, error) {
|
||||
return e.run(true, cmds, input)
|
||||
}
|
||||
|
||||
func (e *Executor) run(parseOutput bool, cmds []string, input io.Reader) ([]string, error) {
|
||||
ret := []string{}
|
||||
for _, cmd := range cmds {
|
||||
log.Debugf("Run command: %s", cmd)
|
||||
proc := exec.Command("sh", "-c", cmd)
|
||||
var stdOut bytes.Buffer
|
||||
var stdErr bytes.Buffer
|
||||
proc.Stdout = &stdOut
|
||||
proc.Stderr = &stdErr
|
||||
proc.Stdin = input
|
||||
if err := proc.Run(); err != nil {
|
||||
var outputErr error
|
||||
errMsg := stdErr.String()
|
||||
if len(stdOut.String()) != 0 {
|
||||
errMsg = fmt.Sprintf("%s %s", errMsg, stdOut.String())
|
||||
}
|
||||
outputErr = errors.Error(errMsg)
|
||||
err = errors.Wrapf(outputErr, "%q error: %v, cmd error", cmd, err)
|
||||
return nil, err
|
||||
}
|
||||
if parseOutput {
|
||||
ret = append(ret, ssh.ParseOutput(stdOut.Bytes())...)
|
||||
} else {
|
||||
ret = append(ret, stdOut.String())
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func NewExecutor() *Executor {
|
||||
return new(Executor)
|
||||
}
|
||||
@@ -31,7 +31,6 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/regutils2"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
type HPSARaidPhyDev struct {
|
||||
@@ -347,11 +346,11 @@ func (adapter *HPSARaidAdaptor) RemoveLogicVolumes() error {
|
||||
}
|
||||
|
||||
type HPSARaid struct {
|
||||
term *ssh.Client
|
||||
term raid.IExecTerm
|
||||
adapters []*HPSARaidAdaptor
|
||||
}
|
||||
|
||||
func NewHPSARaid(term *ssh.Client) raid.IRaidDriver {
|
||||
func NewHPSARaid(term raid.IExecTerm) raid.IRaidDriver {
|
||||
return &HPSARaid{
|
||||
term: term,
|
||||
adapters: make([]*HPSARaidAdaptor, 0),
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
package raid
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
)
|
||||
@@ -41,3 +43,8 @@ type IRaidAdapter interface {
|
||||
BuildRaid10(devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error
|
||||
BuildNoneRaid(devs []*baremetal.BaremetalStorage) error
|
||||
}
|
||||
|
||||
type IExecTerm interface {
|
||||
Run(cmds ...string) ([]string, error)
|
||||
RunWithInput(input io.Reader, cmds ...string) ([]string, error)
|
||||
}
|
||||
|
||||
@@ -27,10 +27,10 @@ import (
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid"
|
||||
raiddrivers "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/regutils2"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -320,7 +320,7 @@ func (adapter *MegaRaidAdaptor) GetIndex() int {
|
||||
return adapter.index
|
||||
}
|
||||
|
||||
func (adapter *MegaRaidAdaptor) getTerm() *ssh.Client {
|
||||
func (adapter *MegaRaidAdaptor) getTerm() raid.IExecTerm {
|
||||
return adapter.raid.term
|
||||
}
|
||||
|
||||
@@ -376,7 +376,7 @@ func (adapter *MegaRaidAdaptor) parseLogicVolumes(lines []string) ([]*raiddriver
|
||||
return lvs, nil
|
||||
}
|
||||
|
||||
func getLogicVolumeDeviceById(hostNum, scsiId int, term *ssh.Client) (string, error) {
|
||||
func getLogicVolumeDeviceById(hostNum, scsiId int, term raid.IExecTerm) (string, error) {
|
||||
items, err := raiddrivers.SGMap(term)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -892,13 +892,13 @@ func (adapter *MegaRaidAdaptor) clearJBODDisks() {
|
||||
}
|
||||
|
||||
type MegaRaid struct {
|
||||
term *ssh.Client
|
||||
term raid.IExecTerm
|
||||
adapters []*MegaRaidAdaptor
|
||||
PhyDevsCnt int
|
||||
Capacity int64
|
||||
}
|
||||
|
||||
func NewMegaRaid(term *ssh.Client) raiddrivers.IRaidDriver {
|
||||
func NewMegaRaid(term raid.IExecTerm) raiddrivers.IRaidDriver {
|
||||
return &MegaRaid{
|
||||
term: term,
|
||||
adapters: make([]*MegaRaidAdaptor, 0),
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
type MarvelRaidPhyDev struct {
|
||||
@@ -243,11 +242,11 @@ func (adapter *MarvelRaidAdaptor) BuildNoneRaid(devs []*baremetal.BaremetalStora
|
||||
}
|
||||
|
||||
type MarvelRaid struct {
|
||||
term *ssh.Client
|
||||
term raid.IExecTerm
|
||||
adapters []*MarvelRaidAdaptor
|
||||
}
|
||||
|
||||
func NewMarvelRaid(term *ssh.Client) raid.IRaidDriver {
|
||||
func NewMarvelRaid(term raid.IExecTerm) raid.IRaidDriver {
|
||||
return &MarvelRaid{
|
||||
term: term,
|
||||
adapters: make([]*MarvelRaidAdaptor, 0),
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
"yunion.io/x/onecloud/pkg/util/sysutils"
|
||||
)
|
||||
|
||||
@@ -45,7 +44,7 @@ const (
|
||||
UnknownLogicalVolumeIndex = MaxInt
|
||||
)
|
||||
|
||||
type RaidDriverFactory func(term *ssh.Client) IRaidDriver
|
||||
type RaidDriverFactory func(term IExecTerm) IRaidDriver
|
||||
|
||||
type sRaidDrivers map[string]RaidDriverFactory
|
||||
|
||||
@@ -106,7 +105,7 @@ func (dev *RaidBasePhyDev) ToBaremetalStorage(index int) *baremetal.BaremetalSto
|
||||
}
|
||||
}
|
||||
|
||||
func GetModules(term *ssh.Client) []string {
|
||||
func GetModules(term IExecTerm) []string {
|
||||
ret := []string{}
|
||||
lines, err := term.Run("/sbin/lsmod")
|
||||
if err != nil {
|
||||
@@ -142,7 +141,7 @@ type RaidLogicalVolume struct {
|
||||
BlockDev string
|
||||
}
|
||||
|
||||
func SGMap(term *ssh.Client) ([]compute.SGMapItem, error) {
|
||||
func SGMap(term IExecTerm) ([]compute.SGMapItem, error) {
|
||||
lines, err := term.Run("/usr/bin/sg_map -x")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "run sg_map")
|
||||
|
||||
@@ -26,7 +26,6 @@ import (
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/utils/raid"
|
||||
"yunion.io/x/onecloud/pkg/compute/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/util/ssh"
|
||||
)
|
||||
|
||||
type Mpt2SASRaidPhyDev struct {
|
||||
@@ -307,12 +306,12 @@ func (adapter *Mpt2SASRaidAdaptor) RemoveLogicVolumes() error {
|
||||
}
|
||||
|
||||
type Mpt2SASRaid struct {
|
||||
term *ssh.Client
|
||||
term raid.IExecTerm
|
||||
utility string
|
||||
adapters []*Mpt2SASRaidAdaptor
|
||||
}
|
||||
|
||||
func NewMpt2SASRaid(term *ssh.Client) raid.IRaidDriver {
|
||||
func NewMpt2SASRaid(term raid.IExecTerm) raid.IRaidDriver {
|
||||
return &Mpt2SASRaid{
|
||||
term: term,
|
||||
adapters: make([]*Mpt2SASRaidAdaptor, 0),
|
||||
|
||||
1
pkg/hostman/hostinfo/hostconsts/doc.go
Normal file
1
pkg/hostman/hostinfo/hostconsts/doc.go
Normal file
@@ -0,0 +1 @@
|
||||
package hostconsts // import "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts"
|
||||
12
pkg/hostman/hostinfo/hostconsts/hostconsts.go
Normal file
12
pkg/hostman/hostinfo/hostconsts/hostconsts.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package hostconsts
|
||||
|
||||
const (
|
||||
TELEGRAF_TAG_KEY_BRAND = "brand"
|
||||
TELEGRAF_TAG_KEY_RES_TYPE = "res_type"
|
||||
TELEGRAF_TAG_KEY_HOST_TYPE = "host_type"
|
||||
|
||||
TELEGRAF_TAG_ONECLOUD_BRAND = "OneCloud"
|
||||
TELEGRAF_TAG_ONECLOUD_RES_TYPE = "host"
|
||||
TELEGRAF_TAG_ONECLOUD_HOST_TYPE_HOST = "host"
|
||||
TELEGRAF_TAG_ONECLOUD_HOST_TYPE_CONTROLLER = "controller"
|
||||
)
|
||||
@@ -40,6 +40,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/hostman/host_health"
|
||||
deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
|
||||
"yunion.io/x/onecloud/pkg/hostman/hostinfo/hostbridge"
|
||||
"yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts"
|
||||
"yunion.io/x/onecloud/pkg/hostman/hostutils"
|
||||
"yunion.io/x/onecloud/pkg/hostman/isolated_device"
|
||||
"yunion.io/x/onecloud/pkg/hostman/options"
|
||||
@@ -1610,18 +1611,18 @@ func (h *SHostInfo) OnCatalogChanged(catalog mcclient.KeystoneServiceCatalogV3)
|
||||
conf := map[string]interface{}{}
|
||||
conf["hostname"] = h.getHostname()
|
||||
conf["tags"] = map[string]string{
|
||||
"host_id": h.HostId,
|
||||
"zone_id": h.ZoneId,
|
||||
"zone": h.Zone,
|
||||
"cloudregion_id": h.CloudregionId,
|
||||
"cloudregion": h.Cloudregion,
|
||||
"domain_id": h.Domain_id,
|
||||
"project_domain": h.Project_domain,
|
||||
"region": options.HostOptions.Region,
|
||||
"host_ip": h.GetMasterIp(),
|
||||
//"platform": "kvm",
|
||||
"brand": "OneCloud",
|
||||
"res_type": "host",
|
||||
"host_id": h.HostId,
|
||||
"zone_id": h.ZoneId,
|
||||
"zone": h.Zone,
|
||||
"cloudregion_id": h.CloudregionId,
|
||||
"cloudregion": h.Cloudregion,
|
||||
"domain_id": h.Domain_id,
|
||||
"project_domain": h.Project_domain,
|
||||
"region": options.HostOptions.Region,
|
||||
"host_ip": h.GetMasterIp(),
|
||||
hostconsts.TELEGRAF_TAG_KEY_BRAND: hostconsts.TELEGRAF_TAG_ONECLOUD_BRAND,
|
||||
hostconsts.TELEGRAF_TAG_KEY_RES_TYPE: hostconsts.TELEGRAF_TAG_ONECLOUD_RES_TYPE,
|
||||
hostconsts.TELEGRAF_TAG_KEY_HOST_TYPE: hostconsts.TELEGRAF_TAG_ONECLOUD_HOST_TYPE_HOST,
|
||||
}
|
||||
conf["nics"] = h.getNicsTelegrafConf()
|
||||
urls, _ := catalog.GetServiceURLs("kafka", options.HostOptions.Region, "", defaultEndpointType)
|
||||
|
||||
@@ -16,6 +16,7 @@ package system_service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
@@ -97,7 +98,7 @@ func (s *SBaseSystemService) reload(conf, conFile string) error {
|
||||
func (s *SBaseSystemService) reloadConf(conf, conFile string) (bool, error) {
|
||||
output, _ := procutils.NewRemoteCommandAsFarAsPossible("cat", conFile).Output()
|
||||
oldConf := string(output)
|
||||
if conf != oldConf {
|
||||
if strings.TrimSpace(conf) != strings.TrimSpace(oldConf) {
|
||||
log.Debugf("Reload service %s ...", s.name)
|
||||
err := procutils.NewRemoteCommandAsFarAsPossible("rm", "-f", conFile).Run()
|
||||
if err != nil {
|
||||
|
||||
@@ -15,8 +15,14 @@
|
||||
package system_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
type STelegraf struct {
|
||||
@@ -32,8 +38,13 @@ func (s *STelegraf) GetConfig(kwargs map[string]interface{}) string {
|
||||
conf += "[global_tags]\n"
|
||||
if tags, ok := kwargs["tags"]; ok {
|
||||
tgs, _ := tags.(map[string]string)
|
||||
for k, v := range tgs {
|
||||
conf += fmt.Sprintf(" %s = \"%s\"\n", k, v)
|
||||
keys := []string{}
|
||||
for k := range tgs {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
conf += fmt.Sprintf(" %s = \"%s\"\n", k, tgs[k])
|
||||
}
|
||||
}
|
||||
conf += "\n"
|
||||
@@ -119,6 +130,9 @@ func (s *STelegraf) GetConfig(kwargs map[string]interface{}) string {
|
||||
conf += "\n"
|
||||
conf += "[[inputs.system]]\n"
|
||||
conf += "\n"
|
||||
conf += "[[inputs.smart]]\n"
|
||||
conf += " path=\"/usr/sbin/smartctl\"\n"
|
||||
conf += "\n"
|
||||
conf += "[[inputs.net]]\n"
|
||||
if nics, ok := kwargs["nics"]; ok {
|
||||
ns, _ := nics.([]map[string]interface{})
|
||||
@@ -180,5 +194,26 @@ func (s *STelegraf) BgReload(kwargs map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (s *STelegraf) BgReloadConf(kwargs map[string]interface{}) {
|
||||
go s.reloadConf(s.GetConfig(kwargs), s.GetConfigFile())
|
||||
go func() {
|
||||
reload, err := s.reloadConf(s.GetConfig(kwargs), s.GetConfigFile())
|
||||
if err != nil {
|
||||
log.Errorf("Failed reload conf: %s", err)
|
||||
}
|
||||
if reload {
|
||||
err := s.ReloadTelegraf()
|
||||
if err != nil {
|
||||
log.Errorf("failed reload telegraf: %s", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *STelegraf) ReloadTelegraf() error {
|
||||
log.Infof("Start reolad telegraf...")
|
||||
telegrafReoladUrl := "http://localhost:8087/reload"
|
||||
_, _, err := httputils.JSONRequest(
|
||||
httputils.GetDefaultClient(), context.Background(),
|
||||
"POST", telegrafReoladUrl, nil, nil, false,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user