Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
hanlei3 committed Oct 18, 2019
0 parents commit 518731c
Show file tree
Hide file tree
Showing 16 changed files with 1,380 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang AS build-env
COPY . /root

WORKDIR /root

RUN \
export GO111MODULE=on &&\
export GOPROXY=https://goproxy.io &&\
CGO_ENABLED=0 go build -v

FROM alpine

COPY --from=build-env /root/check /root/conf.toml /
CMD ["/check"]

24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# this is check service
## env
```
go version 1.12.5
linux
```
## api info
```$xslt
http://127.0.0.1:7789/checkport #check service port,tcp only
http://127.0.0.1:7789/checkpod #check k8s pods running status
http://127.0.0.1:7789/checkmgo #check mongo running status
http://127.0.0.1:7789/checkes #check es-cluster running status
http://127.0.0.1:7789/checknode #check k8scluster nodes running status,,,error
http://127.0.0.1:7789/checktotalag #check kafka consumer total lag
```
http://127.0.0.1:7789/postport #post port data to db
eg:
```cassandraql
{"topic":"test","data":"127.0.0.1:1234"}
```



11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e
# build bin file
export GO111MODULE=on
export GOPROXY=https://goproxy.io
go build -v
# make my dir
mkdir -p $BUILD_ROOT
mv check $BUILD_ROOT
mv conf.toml $BUILD_ROOT
28 changes: 28 additions & 0 deletions conf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[check]
hostPort = "0.0.0.0:7789"

ipPort = [""]

[SendMsg]
AppID =
AppKey =
MsgGw =
To =

[db] #pod db
addr =
dbuser =
dbpass =

[mongo]
conn = []
replset_authdb =
replset_user =
replset_passwd =

[es]
eslist = []
nodes =



20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module check

go 1.12

require (
github.com/PuerkitoBio/goquery v1.5.0
github.com/Shopify/sarama v1.23.1 // indirect
github.com/bsm/sarama-cluster v2.1.15+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.7
github.com/imdario/mergo v0.3.7 // indirect
github.com/julienschmidt/httprouter v1.2.0
github.com/spf13/viper v1.4.0
go.uber.org/zap v1.10.0
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
gopkg.in/natefinch/lumberjack.v2 v2.0.0
k8s.io/api v0.0.0-20190717022910-653c86b0609b
k8s.io/apimachinery v0.0.0-20190717022731-0bb8574e0887
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20190712204705-3dccf664f023 // indirect
)
312 changes: 312 additions & 0 deletions go.sum

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions httpd/escheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package httpd

import (
"encoding/json"
"io/ioutil"
"net/http"

"go.uber.org/zap"

"github.com/julienschmidt/httprouter"

"github.com/spf13/viper"
)

type EsClusterStat struct {
ClusterName string `json:"cluster_name"`
Status string `json:"status"`
TimeOut bool `json:"time_out"`
NumberOfNodes int `json:"number_of_nodes"`
NumberOfDataNodes int `json:"number_of_data_nodes"`
ActivePrimaryShards int `json:"active_primary_shards"`
ActiveShards int `json:"active_shards"`
RelocatingShards int `json:"relocating_shards"`
InitializingShards int `json:"initializing_shards"`
UnassignedShards int `json:"unassigned_shards"`
DelayedUnassignedShards int `json:"delayed_unassigned_shards"`
NumberOfPendingTasks int `json:"number_of_pending_tasks"`
NumberOfInFlightFetch int `json:"number_of_in_flight_fetch"`
TaskMaxWaitingInQueueMillis int `json:"task_max_waiting_in_queue_millis"`
ActiveShardsPercentAsNumber float32 `json:"active_shards_percent_as_number"`
}

type EsStatus struct {
Msg string `json:"msg"`
Data string `json:"data"`
}

type HttpEs struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []EsStatus `json:"data"`
}

func Escheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

httpStat := HttpEs{}
esList := viper.GetStringSlice("es.eslist")
for _, ip := range esList {
ch := make(chan EsClusterStat)
go func(ip string) {
url := "http://" + ip + "/_cluster/health?pretty"

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Loges.Error("new request is err:", zap.Error(err))
}

req.SetBasicAuth(viper.GetString("es.es-user"), viper.GetString("es.es-pwd"))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
Loges.Error("do request is err:", zap.Error(err))

ch <- EsClusterStat{Status: "red"}
return
}
defer res.Body.Close()

resData := EsClusterStat{}
body, _ := ioutil.ReadAll(res.Body)
err = json.Unmarshal(body, &resData)
if err != nil {
Loges.Error("json exchange is err:", zap.Error(err))
}

ch <- resData
}(ip)

v := <-ch

if v.Status == "red" {
httpStat.Msg = "yellow"
// SendWechat(ip)
httpStat.Data = append(httpStat.Data, EsStatus{Msg: "red", Data: ip})
} else if v.Status == "yellow" {
httpStat.Msg = "yellow"
httpStat.Data = append(httpStat.Data, EsStatus{Msg: "yellow", Data: ip})
} else if v.NumberOfNodes != viper.GetInt("es.nodes") {
httpStat.Msg = "yellow"
// SendWechat(ip)
httpStat.Data = append(httpStat.Data, EsStatus{Msg: "yellow", Data: ip})
} else {
httpStat.Code = 200
httpStat.Msg = "green"
httpStat.Data = append(httpStat.Data, EsStatus{Msg: "green", Data: ip})
}
}
w2, _ := json.Marshal(httpStat)
w.Write(w2)
}
55 changes: 55 additions & 0 deletions httpd/kfkcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package httpd

import (
"github.com/PuerkitoBio/goquery"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
"net/http"
"strconv"
"strings"
)

func KfkCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

//fmt.Println("kafka topic check ")
url := "http://127.0.0.1/clusters/kafkalog_tj/consumers/newsclientLogConsumerES72/topic/newsclientLog/type/KF"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Loges.Error("error : ", zap.Error(err))
}

req.SetBasicAuth("admin", "passwd")

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
Loges.Error("error : ", zap.Error(err))
}
defer res.Body.Close()
if res.StatusCode != 200 {
Loges.Info("status code error:", zap.Any("statusCode", res.StatusCode))
}

doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
Loges.Error("error : ", zap.Error(err))
}

doc.Find("tr:contains(Total)").Each(func(i int, selection *goquery.Selection) {
ss := selection.Text()

aa := strings.SplitAfter(ss, "Total Lag")

dd, err := strconv.Atoi(strings.ReplaceAll(strings.TrimSpace(aa[1]), ",", ""))
if err != nil {
Loges.Error("error : ", zap.Error(err))
}
if dd >= 10000000 {
Loges.Info("kafka total lag is error :", zap.Any("total lag", dd))
SendWechat("newsClientES total lag :", strings.ReplaceAll(strings.TrimSpace(aa[1]), ",", ""))
}

})
w.Write([]byte("check ok"))

}
59 changes: 59 additions & 0 deletions httpd/loginit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package httpd

import (
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

var Loges = LogInit()

func LogInit() *zap.Logger {
hook := lumberjack.Logger{
Filename: "./info.log",
MaxSize: 128,
MaxBackups: 30,
MaxAge: 30,
Compress: true,
}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "lenum",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(zap.InfoLevel)

core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)),
atomicLevel,
)

caller := zap.AddCaller()

development := zap.Development()

//filed := zap.Fields(zap.String("serviceName", "serviceName"))

logger := zap.New(core, caller, development)
return logger

//logger.Info("log 初始化成功")
//logger.Info("无法获取网址",
// zap.String("url", "http://www.baidu.com"),
// zap.Int("attempt", 3),
// zap.Duration("backoff", time.Second))

}
Loading

0 comments on commit 518731c

Please sign in to comment.