Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Jan 29, 2021
1 parent d1851f6 commit 6a60050
Show file tree
Hide file tree
Showing 30 changed files with 3,196 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.exe
vsphere-mon
etc/vsphere.local.yml
etc/address.local.yml
logs/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# vsphere-mon
# vsphere-mon

142 changes: 142 additions & 0 deletions config/address/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package address

import (
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"

"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/runner"
)

type Module struct {
HTTP string `yaml:"http"`
RPC string `yaml:"rpc"`
Addresses []string `yaml:"addresses"`
}

var (
lock sync.Once
mods map[string]Module
)

func GetHTTPListen(mod string) string {
return getMod(mod).HTTP
}

func GetHTTPPort(mod string) int {
return convPort(mod, getMod(mod).HTTP, "http")
}

func GetRPCListen(mod string) string {
return getMod(mod).RPC
}

func GetRPCPort(mod string) int {
return convPort(mod, getMod(mod).RPC, "rpc")
}

func convPort(module, listen, portType string) int {
splitChar := ":"
if IsIPv6(listen) {
splitChar = "]:"
}
port, err := strconv.Atoi(strings.Split(listen, splitChar)[1])
if err != nil {
fmt.Printf("%s.%s invalid", module, portType)
os.Exit(1)
}

return port
}

func GetHTTPAddresses(mod string) []string {
modConf := getMod(mod)

count := len(modConf.Addresses)
if count == 0 {
return []string{}
}

port := convPort(mod, modConf.HTTP, "http")

addresses := make([]string, count)
for i := 0; i < count; i++ {
addresses[i] = fmt.Sprintf("%s:%d", modConf.Addresses[i], port)
}

return addresses
}

func GetAddresses(mod string) []string {
modConf := getMod(mod)
return modConf.Addresses
}

func GetRPCAddresses(mod string) []string {
modConf := getMod(mod)

count := len(modConf.Addresses)
if count == 0 {
return []string{}
}

port := convPort(mod, modConf.RPC, "rpc")

addresses := make([]string, count)
for i := 0; i < count; i++ {
addresses[i] = fmt.Sprintf("%s:%d", modConf.Addresses[i], port)
}

return addresses
}

func getMod(modKey string) Module {
lock.Do(func() {
parseConf()
})

mod, has := mods[modKey]
if !has {
fmt.Printf("module(%s) configuration section not found", modKey)
os.Exit(1)
}

return mod
}

func parseConf() {
conf := getConf()

var c map[string]Module
err := file.ReadYaml(conf, &c)
if err != nil {
fmt.Println("cannot parse file:", conf)
os.Exit(1)
}

mods = c
}

func getConf() string {
conf := path.Join(runner.Cwd, "etc", "address.local.yml")
if file.IsExist(conf) {
return conf
}

conf = path.Join(runner.Cwd, "etc", "address.yml")
if file.IsExist(conf) {
return conf
}

fmt.Println("configuration file address.[local.]yml not found")
os.Exit(1)
return ""
}

func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
79 changes: 79 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package config

import (
"bytes"
"fmt"
"sync"

"github.com/spf13/viper"
"github.com/toolkits/pkg/file"
)

type ConfYaml struct {
Logger LoggerSection `yaml:"logger"`
Interval int64 `yaml:"interval"`
Report ReportSection `yaml:"report"`
Vspheres []VsphereSection `yaml:"vspheres"`
Performance PerfSection `yaml:"performance"`
}

type ReportSection struct {
Token string `yaml:"token"`
Cate string `yaml:"cate"`
UniqKey string `yaml:"uniqkey"`
Tenant string `yaml:"tenant"`
}

type VsphereSection struct {
Addr string `yaml:"addr"`
User string `yaml:"user"`
Pwd string `yaml:"pwd"`
EsxiPerf bool `yaml:"esxiperf"`
VM bool `yaml:"vm"`
Nid string `yaml:"nid"`
VmList []string `yaml:"vmlist"`
VmPerf bool `yaml:"vmperf"`
VmPerfList []string `yaml:"vmperflist"`
}

type PerfSection struct {
Esxi []string `yaml:"esxi"`
VM []string `yaml:"vm"`
}

var (
Config *ConfYaml
lock = new(sync.RWMutex)
Endpoint string
Cwd string
)

// Get configuration file
func Get() *ConfYaml {
lock.RLock()
defer lock.RUnlock()
return Config
}

func Parse(conf string) error {
bs, err := file.ReadBytes(conf)
if err != nil {
return fmt.Errorf("cannot read yml[%s]: %v", conf, err)
}

lock.Lock()
defer lock.Unlock()

viper.SetConfigType("yaml")
err = viper.ReadConfig(bytes.NewBuffer(bs))
if err != nil {
return fmt.Errorf("cannot read yml[%s]: %v", conf, err)
}

err = viper.Unmarshal(&Config)
if err != nil {
return fmt.Errorf("Unmarshal %v", err)
}

return nil
}
3 changes: 3 additions & 0 deletions config/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

const Version = "0.1.0"
27 changes: 27 additions & 0 deletions config/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"fmt"
"os"

"github.com/toolkits/pkg/logger"
)

type LoggerSection struct {
Dir string `json:"dir"`
Level string `json:"level"`
KeepHours uint `json:"keepHours"`
}

func InitLog(l LoggerSection) {

lb, err := logger.NewFileBackend(l.Dir)
if err != nil {
fmt.Println("cannot init logger:", err)
os.Exit(1)
}
lb.SetRotateByHour(true)
lb.SetKeepHours(l.KeepHours)

logger.SetLogging(l.Level, lb)
}
122 changes: 122 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash

# release version

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

start()
{
mod="vsphere"

binfile=${mod}-mon

if [ ! -f $binfile ]; then
echo "file[$binfile] not found"
exit 1
fi

if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -gt 0 ]; then
echo "${mod} already started"
return
fi

mkdir -p logs/
nohup $CWD/$binfile &> logs/stdout.log &

for((i=1;i<=15;i++)); do
if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -gt 0 ]; then
echo "${mod} started"
return
fi
sleep 0.2
done

echo "cannot start ${mod}"
exit 1
}

stop()
{
mod="vsphere"

binfile=${mod}-mon

if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
echo "${mod} already stopped"
return
fi

ps aux|grep -v grep|grep -v control|grep "$binfile"|awk '{print $2}'|xargs kill
for((i=1;i<=15;i++)); do
if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
echo "${mod} stopped"
return
fi
sleep 0.2
done

echo "cannot stop $mod"
exit 1
}

restart()
{
mod="vsphere"
stop $mod
start $mod

status
}

status()
{
ps aux|grep -v grep|grep "vsphere-mon"
}

build()
{
go build
if [ $? -ne 0 ]; then
exit $?
fi
mod="vsphere"

binfile=${mod}-mon
./$binfile -v
}

pack()
{
build
git log -1 --pretty=%h > gitversion
mod="vsphere"
binfile=${mod}-mon
version=`./$binfile -v | cut -d " " -f2`
file_list="control install.sh etc service $binfile"
echo "...tar $binfile-$version.tar.gz <= $file_list"
tar zcf $binfile-$version.tar.gz gitversion $file_list
}

case "$1" in
start)
start $2
;;
stop)
stop $2
;;
restart)
restart $2
;;
status)
status
;;
build)
build
;;
pack)
pack
;;
*)
usage
esac
Loading

0 comments on commit 6a60050

Please sign in to comment.