Skip to content

Commit

Permalink
Merge pull request #19 from mriedmann/probe-mysql
Browse files Browse the repository at this point in the history
feat: add mysql probe
  • Loading branch information
mriedmann authored May 2, 2023
2 parents 7572bdc + 809d6fb commit e6b0ddb
Show file tree
Hide file tree
Showing 17 changed files with 1,013 additions and 699 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/python-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand All @@ -30,11 +30,9 @@ jobs:
make init
- name: Lint
run: |
source $HOME/.poetry/env
make lint
- name: Test
run: |
source $HOME/.poetry/env
make test
- name: Process coverage
uses: AndreMiras/coveralls-python-action@develop
Expand All @@ -58,7 +56,6 @@ jobs:
make init
- name: Test image
run: |
source $HOME/.poetry/env
make test_image
coveralls_finish:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
make init
source $HOME/.poetry/env
make update
make bump
- name: Run tests
run: |
source $HOME/.poetry/env
Expand Down
33 changes: 8 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,16 @@ helm_version = $(shell cat helm/pipecheck/Chart.yaml | yq '.version')
helm_short_version = $(shell cat helm/pipecheck/Chart.yaml | yq '.version' | cut -d'+' -d'-' -f1)

init:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
pip install poetry
poetry self add "poetry-dynamic-versioning[plugin]"

check-bump:
check-bump: init
which yq || { echo "yq not installed!"; exit 1; }
which gh || { echo "gh (github cli) not installed!"; exit 1; }
[ "$(shell git rev-parse --abbrev-ref HEAD)" == "main" ] || { echo "bumping only possible on main branch"; exit 1; }

bump-major: check-bump
$(eval new_version = $(shell IFS=. read -r a b c<<<"$(short_version)";echo "$$((a+1)).0.0"))
poetry version $(new_version)
sed -i 's/appVersion: .*/appVersion: $(new_version)/' helm/pipecheck/Chart.yaml
echo "__version__ = \"$(new_version)\"" > pipecheck/__init__.py
git commit -a -m "bump major-version from $(version) to $(new_version)"
which git || { echo "git not installed!"; exit 1; }

bump: check-bump
$(eval new_version = $(shell IFS=. read -r a b c<<<"$(short_version)";echo "$$a.$$((b+1)).0"))
poetry version $(new_version)
sed -i 's/appVersion: .*/appVersion: $(new_version)/' helm/pipecheck/Chart.yaml
echo "__version__ = \"$(new_version)\"" > pipecheck/__init__.py
git commit -a -m "bump minor-version from $(version) to $(new_version)"

bump-patch: check-bump
$(eval new_version = $(shell IFS=. read -r a b c<<<"$(short_version)";echo "$$a.$$b.$$((c+1))"))
poetry version $(new_version)
sed -i 's/appVersion: .*/appVersion: $(new_version)/' helm/pipecheck/Chart.yaml
echo "__version__ = \"$(new_version)\"" > pipecheck/__init__.py
git commit -a -m "bump patch-version from $(version) to $(new_version)"
poetry dynamic-versioning
sed -i 's/appVersion: .*/appVersion: $(short_version)/' helm/pipecheck/Chart.yaml

helm-bump-major: check-bump
$(eval helm_new_version = $(shell IFS=. read -r a b c<<<"$(helm_short_version)";echo "$$((a+1)).0.0"))
Expand All @@ -56,9 +39,9 @@ release:
update:
poetry update
poetry export --output requirements.txt
poetry export --dev --output requirements.dev.txt
poetry export --with dev --output requirements.dev.txt

install: init
install: bump
poetry install

lint: install
Expand Down
2 changes: 1 addition & 1 deletion helm/pipecheck/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ version: 0.2.4
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
appVersion: 0.4.1
appVersion: 0.4.2
2 changes: 1 addition & 1 deletion pipecheck/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.1"
__version__ = "0.4.2-2+2db1f87"
6 changes: 5 additions & 1 deletion pipecheck/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ def signal_handler(signal, frame):
if not supports_color() or ("no_color" in args and args["no_color"]):
no_color = True

sys.tracebacklimit = 0
if "verbose" in args and args["verbose"]:
sys.tracebacklimit = None

calls = list(gen_calls(args))
if len(calls) <= 0:
print_error("No probes specified")
sys.exit(0)

last_status = 0
if "interval" in args and args["interval"]:
start_http_server(args["port"])
start_http_server(args["prom_port"])

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
Expand Down
3 changes: 2 additions & 1 deletion pipecheck/checks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pipecheck.checks.dns import DnsProbe
from pipecheck.checks.http import HttpProbe
from pipecheck.checks.icmp import PingProbe
from pipecheck.checks.mysql import MysqlProbe
from pipecheck.checks.tcp import TcpProbe

probes = {}

for cls in [HttpProbe, DnsProbe, PingProbe, TcpProbe]:
for cls in [HttpProbe, DnsProbe, PingProbe, TcpProbe, MysqlProbe]:
probes[cls.get_type()] = cls
34 changes: 34 additions & 0 deletions pipecheck/checks/mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Optional

import pymysql

from pipecheck.api import CheckResult, Err, Ok, Probe


class MysqlProbe(Probe):
"""Try MySQL Connection to given host and port using given user and password"""

host: str = ""
port: int = 3306
database: Optional[str] = None
user: str = ""
password: str = ""
timeout: int = 5

def __call__(self) -> CheckResult:
connection = pymysql.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=self.database,
connect_timeout=self.timeout,
defer_connect=True,
)
try:
connection.connect()
return Ok(f"MySQL connection successfully established to port {self.port} on {self.host}")
except Exception as e:
return Err(f"MySQL connection failed on port {self.port} for {self.host} ({e})")
finally:
connection.close()
22 changes: 21 additions & 1 deletion pipecheck/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from urllib.parse import urlparse

from pipecheck import __version__
from pipecheck.checks import probes
Expand Down Expand Up @@ -45,7 +46,7 @@ def parse_args(args=None):
help="don't exit but repeat checks in given interval. Also activates prometheus exporter",
)

parser.add_argument("-p", "--port", nargs="?", default=9000, type=int, help="promtheus exporter port")
parser.add_argument("-p", "--prom-port", nargs="?", default=9000, type=int, help="promtheus exporter port")

for probe in probes:
parser.add_argument("--%s" % probes[probe].get_type(), nargs="*", help=probes[probe].get_help())
Expand All @@ -71,6 +72,25 @@ def parse_tcp(x):
return {"type": "tcp", "host": host, "port": int(port)}


def parse_mysql(x):
if not x.startswith("mysql://"):
x = "mysql://" + x
try:
u = urlparse(x)
o = {
"type": "mysql",
"host": u.hostname,
"user": u.username,
"password": u.password,
"database": str(u.path).removeprefix("/"),
}
if u.port:
o["port"] = int(u.port)
return o
except Exception as e:
raise Exception(f"Unable to parse mysql-probe target-url ({e})") from None


def parse_http(x):
return {"type": "http", "url": x}

Expand Down
1 change: 0 additions & 1 deletion pipecheck/cli_backport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

class BooleanOptionalAction(Action):
def __init__(self, option_strings, dest, default=None, type=None, choices=None, required=False, help=None, metavar=None):

_option_strings = []
for option_string in option_strings:
_option_strings.append(option_string)
Expand Down
Loading

0 comments on commit e6b0ddb

Please sign in to comment.