From 5c98cb26156176ffbb826cf1975ad05603cc8fd9 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <awaksberg@ingenicoprepaid.com>
Date: Thu, 27 Jun 2019 10:46:33 +0200
Subject: [PATCH 1/3] feat: add check_mysql_query_count

---
 .../check_mysql_query_count.go                | 90 +++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 check_mysql_query_count/check_mysql_query_count.go

diff --git a/check_mysql_query_count/check_mysql_query_count.go b/check_mysql_query_count/check_mysql_query_count.go
new file mode 100644
index 0000000..c3b9d83
--- /dev/null
+++ b/check_mysql_query_count/check_mysql_query_count.go
@@ -0,0 +1,90 @@
+package main
+
+import (
+	"fmt"
+	"flag"
+	"os"
+
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+)
+
+// Option to parse
+var (
+  HOSTNAME = flag.String("hostname", "localhost", "mysql hostname")
+  PORT     = flag.Int("port", 3306, "mysql port")
+  USERNAME = flag.String("username", "", "mysql username")
+  PASSWORD = flag.String("password", "", "mysql password")
+  DATABASE = flag.String("database", "", "mysql database")
+  QUERY    = flag.String("query", "", "mysql query")
+  WARNING  = flag.Int("warning", 0, "limit to return a warning alert")
+  CRITICAL = flag.Int("critical", 0, "limit to return a critical alert")
+)
+
+func init() {
+  flag.Parse()
+
+  if *QUERY == "" {
+    Critical("Please set query option")
+  }
+
+  if *WARNING == 0 || *CRITICAL == 0 {
+    Critical("Please set warning and critical options")
+  }
+}
+
+// Critical print a message and exit with code 2
+func Critical(msg string) {
+  fmt.Println(fmt.Sprintf("CRITICAL - %s", msg))
+  os.Exit(2)
+}
+
+// Warning print a message and exit with code 1
+func Warning(msg string) {
+  fmt.Println(fmt.Sprintf("WARNING - %s", msg))
+  os.Exit(1)
+}
+
+// Ok print a message and exit with code 0
+func Ok(msg string) {
+  fmt.Println(fmt.Sprintf("OK - %s", msg))
+  os.Exit(0)
+}
+
+// Count execute query and return the result
+func Count() (int, error) {
+  var count int
+
+  conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", *USERNAME, *PASSWORD, *HOSTNAME, *PORT, *DATABASE)
+  db, err := sql.Open("mysql", conn)
+  if err != nil {
+    return 0, err
+  }
+  defer db.Close()
+
+  err = db.QueryRow(*QUERY).Scan(&count)
+  if err != nil {
+    return 0, err
+  }
+
+  return count, nil
+}
+
+func main() {
+  count, err := Count()
+  if err != nil {
+    Critical(fmt.Sprintf("%v", err))
+  }
+
+  msg := fmt.Sprintf("there are %d rows", count)
+  if count >= *CRITICAL {
+    Critical(msg)
+  }
+
+  if count >= *WARNING {
+    Warning(msg)
+  }
+
+  Ok(msg)
+}
+

From ab6f301467db237d76f3976fb10d61b93c04ea73 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <awaksberg@ingenicoprepaid.com>
Date: Thu, 27 Jun 2019 10:58:47 +0200
Subject: [PATCH 2/3] feat: add help option

---
 check_mysql_query_count/check_mysql_query_count.go | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/check_mysql_query_count/check_mysql_query_count.go b/check_mysql_query_count/check_mysql_query_count.go
index c3b9d83..02c5921 100644
--- a/check_mysql_query_count/check_mysql_query_count.go
+++ b/check_mysql_query_count/check_mysql_query_count.go
@@ -11,6 +11,7 @@ import (
 
 // Option to parse
 var (
+  HELP     = flag.Bool("help", false, "print the help message")
   HOSTNAME = flag.String("hostname", "localhost", "mysql hostname")
   PORT     = flag.Int("port", 3306, "mysql port")
   USERNAME = flag.String("username", "", "mysql username")
@@ -24,6 +25,11 @@ var (
 func init() {
   flag.Parse()
 
+  if *HELP {
+    flag.PrintDefaults()
+    os.Exit(3)
+  }
+
   if *QUERY == "" {
     Critical("Please set query option")
   }
@@ -87,4 +93,3 @@ func main() {
 
   Ok(msg)
 }
-

From 0cee7e7d374f37130cb6cba8b10ef2e0911966a3 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <awaksberg@ingenicoprepaid.com>
Date: Thu, 27 Jun 2019 11:34:02 +0200
Subject: [PATCH 3/3] fix: default value is -1 for critical and warning options

---
 check_mysql_query_count/check_mysql_query_count.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/check_mysql_query_count/check_mysql_query_count.go b/check_mysql_query_count/check_mysql_query_count.go
index 02c5921..e67f065 100644
--- a/check_mysql_query_count/check_mysql_query_count.go
+++ b/check_mysql_query_count/check_mysql_query_count.go
@@ -18,8 +18,8 @@ var (
   PASSWORD = flag.String("password", "", "mysql password")
   DATABASE = flag.String("database", "", "mysql database")
   QUERY    = flag.String("query", "", "mysql query")
-  WARNING  = flag.Int("warning", 0, "limit to return a warning alert")
-  CRITICAL = flag.Int("critical", 0, "limit to return a critical alert")
+  WARNING  = flag.Int("warning", -1, "limit to return a warning alert")
+  CRITICAL = flag.Int("critical", -1, "limit to return a critical alert")
 )
 
 func init() {
@@ -34,7 +34,7 @@ func init() {
     Critical("Please set query option")
   }
 
-  if *WARNING == 0 || *CRITICAL == 0 {
+  if *WARNING == -1 || *CRITICAL == -1 {
     Critical("Please set warning and critical options")
   }
 }