Skip to content

Commit

Permalink
Merge pull request #31 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 2.2.0
  • Loading branch information
andyone authored Sep 23, 2020
2 parents 9ecc72f + 8fa33db commit 58b300f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: go

go:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- tip

os:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################

# This Makefile generated by GoMakeGen 1.3.0 using next command:
# This Makefile generated by GoMakeGen 1.3.1 using next command:
# gomakegen .
#
# More info: https://kaos.sh/gomakegen
Expand All @@ -27,7 +27,7 @@ git-config: ## Configure git redirects for stable import path services
git config --global http.https://pkg.re.followRedirects true

deps: git-config ## Download dependencies
go get -d -v pkg.re/essentialkaos/ek.v11
go get -d -v pkg.re/essentialkaos/ek.v12

fmt: ## Format source code with gofmt
find . -name "*.go" -exec gofmt -s -w {} \;
Expand All @@ -43,6 +43,6 @@ help: ## Show this info
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-19s\033[0m %s\n", $$1, $$2}'
@echo -e ''
@echo -e '\033[90mGenerated by GoMakeGen 1.3.0\033[0m\n'
@echo -e '\033[90mGenerated by GoMakeGen 1.3.1\033[0m\n'

################################################################################
10 changes: 7 additions & 3 deletions common/redis-cli-monitor.spec
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

Summary: Tiny Redis client for renamed MONITOR commands
Name: redis-cli-monitor
Version: 2.1.1
Version: 2.2.0
Release: 0%{?dist}
Group: Applications/System
License: Apache License, Version 2.0
URL: https://github.com/essentialkaos/redis-cli-monitor
URL: https://kaos.sh/redis-cli-monitor

Source0: https://source.kaos.st/%{name}/%{name}-%{version}.tar.bz2

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

BuildRequires: golang >= 1.13
BuildRequires: golang >= 1.14

Provides: %{name} = %{version}-%{release}

Expand Down Expand Up @@ -85,6 +85,10 @@ fi
################################################################################

%changelog
* Tue Sep 22 2020 Anton Novojilov <[email protected]> - 2.2.0-0
- Added option for filtering data by DB number
- ek package updated to the latest stable version

* Thu Oct 17 2019 Anton Novojilov <[email protected]> - 2.1.1-0
- ek package updated to the latest stable version

Expand Down
58 changes: 47 additions & 11 deletions redis-cli-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@ import (
"strings"
"time"

"pkg.re/essentialkaos/ek.v11/env"
"pkg.re/essentialkaos/ek.v11/fmtc"
"pkg.re/essentialkaos/ek.v11/fsutil"
"pkg.re/essentialkaos/ek.v11/options"
"pkg.re/essentialkaos/ek.v11/timeutil"
"pkg.re/essentialkaos/ek.v11/usage"
"pkg.re/essentialkaos/ek.v11/usage/completion/bash"
"pkg.re/essentialkaos/ek.v11/usage/completion/fish"
"pkg.re/essentialkaos/ek.v11/usage/completion/zsh"
"pkg.re/essentialkaos/ek.v12/env"
"pkg.re/essentialkaos/ek.v12/fmtc"
"pkg.re/essentialkaos/ek.v12/fsutil"
"pkg.re/essentialkaos/ek.v12/options"
"pkg.re/essentialkaos/ek.v12/timeutil"
"pkg.re/essentialkaos/ek.v12/usage"
"pkg.re/essentialkaos/ek.v12/usage/completion/bash"
"pkg.re/essentialkaos/ek.v12/usage/completion/fish"
"pkg.re/essentialkaos/ek.v12/usage/completion/zsh"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Application info
const (
APP = "Redis CLI Monitor"
VER = "2.1.1"
VER = "2.2.0"
DESC = "Tiny Redis client for renamed MONITOR commands"
)

// Supported command line options
const (
OPT_HOST = "h:host"
OPT_PORT = "p:port"
OPT_DB = "n:db"
OPT_RAW = "r:raw"
OPT_AUTH = "a:password"
OPT_TIMEOUT = "t:timeout"
Expand All @@ -55,6 +56,7 @@ const (
var optMap = options.Map{
OPT_HOST: {Type: options.MIXED, Value: "127.0.0.1"},
OPT_PORT: {Value: "6379"},
OPT_DB: {Type: options.INT, Min: 0, Max: 999},
OPT_TIMEOUT: {Type: options.INT, Value: 3, Min: 1, Max: 300},
OPT_RAW: {Type: options.BOOL},
OPT_AUTH: {},
Expand All @@ -67,6 +69,7 @@ var optMap = options.Map{

var conn net.Conn
var useRawOutput bool
var dbNum string

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down Expand Up @@ -98,6 +101,10 @@ func main() {
return
}

if options.Has(OPT_DB) {
dbNum = options.GetS(OPT_DB)
}

connectToRedis()
monitor(args[0])
}
Expand Down Expand Up @@ -159,6 +166,7 @@ func connectToRedis() {
// monitor starts outout commands in monitor
func monitor(cmd string) {
buf := bufio.NewReader(conn)

conn.Write([]byte(cmd + "\r\n"))

for {
Expand All @@ -173,6 +181,10 @@ func monitor(cmd string) {
printErrorAndExit("Redis return error message: " + strings.TrimRight(str[1:], "\r\n"))
}

if dbNum != "" && !isMatchDB(str[1:]) {
continue
}

if useRawOutput {
fmt.Printf("%s", str[1:])
} else {
Expand All @@ -193,14 +205,37 @@ func formatCommand(cmd string) {
infoStart := strings.IndexRune(cmd, '[')
infoEnd := strings.IndexRune(cmd, ']')

if infoStart == -1 || infoEnd == -1 {
return
}

fmtc.Printf(
"{s}%s.%s{!} {s-}%s{!} %s",
"{s-}%s.%s{!} {s}%-26s{!} %s",
timeutil.Format(time.Unix(sec, 0), "%Y/%m/%d %H:%M:%S"), cmd[11:17],
cmd[infoStart:infoEnd+1],
cmd[infoEnd+2:],
)
}

// isMatchDB returns true if given command executed over DB with given number
func isMatchDB(cmd string) bool {
start := strings.IndexRune(cmd, '[')

if start == -1 {
return false
}

end := strings.IndexRune(cmd[start:], ' ')

if end == -1 {
return false
}

end += start

return dbNum == cmd[start+1:end]
}

// printError prints error message to console
func printError(f string, a ...interface{}) {
fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...)
Expand All @@ -225,6 +260,7 @@ func genUsage() *usage.Info {

info.AddOption(OPT_HOST, "Server hostname {s-}(127.0.0.1 by default){!}", "ip/host")
info.AddOption(OPT_PORT, "Server port {s-}(6379 by default){!}", "port")
info.AddOption(OPT_DB, "Database number", "db")
info.AddOption(OPT_RAW, "Print raw data")
info.AddOption(OPT_AUTH, "Password to use when connecting to the server", "password")
info.AddOption(OPT_TIMEOUT, "Connection timeout in seconds {s-}(3 by default){!}", "1-300")
Expand Down

0 comments on commit 58b300f

Please sign in to comment.