From e942003563095da59cc14a93aa57b18395451aa4 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 23 Sep 2020 00:24:47 +0300 Subject: [PATCH 1/2] Add option for filtering data by DB number + Update ek to the latest version --- common/redis-cli-monitor.spec | 10 ++++-- redis-cli-monitor.go | 58 ++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/common/redis-cli-monitor.spec b/common/redis-cli-monitor.spec index f1dbf07..57be1d4 100644 --- a/common/redis-cli-monitor.spec +++ b/common/redis-cli-monitor.spec @@ -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} @@ -85,6 +85,10 @@ fi ################################################################################ %changelog +* Tue Sep 22 2020 Anton Novojilov - 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 - 2.1.1-0 - ek package updated to the latest stable version diff --git a/redis-cli-monitor.go b/redis-cli-monitor.go index 54af703..e33cc30 100644 --- a/redis-cli-monitor.go +++ b/redis-cli-monitor.go @@ -16,15 +16,15 @@ 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" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -32,7 +32,7 @@ import ( // Application info const ( APP = "Redis CLI Monitor" - VER = "2.1.1" + VER = "2.2.0" DESC = "Tiny Redis client for renamed MONITOR commands" ) @@ -40,6 +40,7 @@ const ( 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" @@ -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: {}, @@ -67,6 +69,7 @@ var optMap = options.Map{ var conn net.Conn var useRawOutput bool +var dbNum string // ////////////////////////////////////////////////////////////////////////////////// // @@ -98,6 +101,10 @@ func main() { return } + if options.Has(OPT_DB) { + dbNum = options.GetS(OPT_DB) + } + connectToRedis() monitor(args[0]) } @@ -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 { @@ -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 { @@ -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...) @@ -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") From 8fa33db101fbdb97864661392aa867db0ccad362 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 23 Sep 2020 00:37:34 +0300 Subject: [PATCH 2/2] Update Makefile and Travis config --- .travis.yml | 3 +-- Makefile | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 805acf6..5c5ee8d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: go go: - - 1.12.x - - 1.13.x - 1.14.x + - 1.15.x - tip os: diff --git a/Makefile b/Makefile index ac5ff6a..33c789a 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 {} \; @@ -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' ################################################################################