Skip to content

Commit

Permalink
Merge pull request #23 from MatinRh/master
Browse files Browse the repository at this point in the history
ElasticSearch support
  • Loading branch information
ph4r5h4d committed Oct 26, 2020
2 parents 9656dc5 + f9cf8d8 commit aa70a62
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ It also supports **timeout** so it can wait for a particular time and then fail.
* Redis
* RabbitMQ
* Memcached
* ElasticSearch

## Install
You can download the latest [release](https://github.com/ph4r5h4d/wait4it/releases), or you can build it yourself.
Expand Down Expand Up @@ -98,6 +99,11 @@ Check a Memcached instance
./wait4it -type=memcached -h=127.0.0.1 -p=11211 -t=60
```

Check ElasticSearch instance
```bash
./wait4it -type=elasticsearch -h=http://127.0.0.1 -p=9200 -t=60
```

### Docker
You can run this `wait4it` inside a docker container, and it's possible to run this container as init container inside
K8s and Openshift.
Expand Down Expand Up @@ -153,6 +159,11 @@ Check a Memcached instance
docker run ph4r5h4d/wait4it -type=memcached -h=127.0.0.1 -p=11211
```

Check a ElasticSearch instance
```bash
docker run ph4r5h4d/wait4it -type=elasticsearch -h=http://127.0.0.1 -p=9200
```

## Notes
#### Configuration
* note that environment variables have higher priority than command-line arguments.
Expand All @@ -178,4 +189,8 @@ This means if you define both `W4IT_TYPE` and `-type`, the application takes the
* this version can only check one host within Redis cluster, using multiple hosts to check cluster status for Redis will be added in the next version.

#### Memcached check
* for the moment multiple hosts and cluster check is not supported.
* for the moment multiple hosts and cluster checks are not supported.

#### ElasticSearch
* for the moment multiple hosts and cluster checks are not supported.
* as you know, username/password authentication mechanism is only supported along with the X-Pack extension and if the X-Pack extension wasn't activated, filling username/password won't have any effect. According to these reasons, username/password isn't required.
2 changes: 1 addition & 1 deletion cmd/check-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func RunCheck(c model.CheckContext) {
time.Sleep(time.Duration(c.Config.Timeout) * time.Second)
done <- true

fmt.Print("failed")
fmt.Println("failed")
os.Exit(1)
}

Expand Down
18 changes: 10 additions & 8 deletions cmd/check-module-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"wait4it/PostgreSQLChecker"
"wait4it/RedisChecker"
"wait4it/TcpChecker"
"wait4it/elasticsearch"
"wait4it/httpChecker"
"wait4it/rabbitmq"
)

var cm = map[string]interface{}{
"tcp": &TcpChecker.Tcp{},
"mysql": &MySQLChecker.MySQLConnection{},
"postgres": &PostgreSQLChecker.PostgresSQLConnection{},
"http": &httpChecker.HttpCheck{},
"mongo": &MongoDbChecker.MongoDbConnection{},
"redis": &RedisChecker.RedisConnection{},
"rabbitmq": &rabbitmq.RabbitChecker{},
"memcached": &MemcachedChecker.MemcachedConnection{},
"tcp": &TcpChecker.Tcp{},
"mysql": &MySQLChecker.MySQLConnection{},
"postgres": &PostgreSQLChecker.PostgresSQLConnection{},
"http": &httpChecker.HttpCheck{},
"mongo": &MongoDbChecker.MongoDbConnection{},
"redis": &RedisChecker.RedisConnection{},
"rabbitmq": &rabbitmq.RabbitChecker{},
"memcached": &MemcachedChecker.MemcachedConnection{},
"elasticsearch": &elasticsearch.ElasticSearchChecker{},
}
4 changes: 2 additions & 2 deletions cmd/output.std.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

func wStdOut(r bool) {
if r {
_, _ = fmt.Fprint(os.Stdout, "succeed")
_, _ = fmt.Fprintln(os.Stdout, "succeed")
os.Exit(0)
} else {
_, _ = fmt.Fprint(os.Stdout, ".")
}
}

func wStdErr(a ...interface{}) {
_, _ = fmt.Fprint(os.Stderr, a...)
_, _ = fmt.Fprintln(os.Stderr, a...)
}
60 changes: 60 additions & 0 deletions elasticsearch/elasticsearchchecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package elasticsearch

import (
"errors"
"strconv"
"wait4it/model"

elasticsearch "github.com/elastic/go-elasticsearch/v8"
)

type ElasticSearchChecker struct {
Host string
Port int
Username string
Password string
}

func (esc *ElasticSearchChecker) BuildContext(cx model.CheckContext) {
esc.Host = cx.Host
esc.Port = cx.Port
esc.Username = cx.Username
esc.Password = cx.Password
}

func (esc *ElasticSearchChecker) Validate() error {
if len(esc.Host) == 0 {
return errors.New("Host can't be empty")
}

if esc.Port < 1 || esc.Port > 65535 {
return errors.New("Invalid port range for ElasticSearch")
}

return nil
}

func (esc *ElasticSearchChecker) Check() (bool, bool, error) {
cfg := elasticsearch.Config{
Addresses: []string{
esc.BuildConnectionString(),
},
Username: esc.Username,
Password: esc.Password,
}

es, err := elasticsearch.NewClient(cfg)
if err != nil {
return false, true, err
}

if _, err := es.Ping(); err != nil {
return false, false, err
}

return true, true, nil
}

func (esc *ElasticSearchChecker) BuildConnectionString() string {
return esc.Host + ":" + strconv.Itoa(esc.Port)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201007143536-4b4020669208
github.com/go-redis/redis/v8 v8.0.0-beta.8
github.com/go-sql-driver/mysql v1.5.0
github.com/lib/pq v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/elastic/go-elasticsearch v0.0.0 h1:Pd5fqOuBxKxv83b0+xOAJDAkziWYwFinWnBO0y+TZaA=
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201007143536-4b4020669208 h1:e7eoyubUAsH05vn5eMkgMPQO6E+B+CT7yxWbLSQe2xU=
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201007143536-4b4020669208/go.mod h1:xe9a/L2aeOgFKKgrO3ibQTnMdpAeL0GC+5/HpGScSa4=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
Expand Down

0 comments on commit aa70a62

Please sign in to comment.