Skip to content

Commit

Permalink
Merge pull request #21 from jiuka/checkmk23
Browse files Browse the repository at this point in the history
Checkmk 2.3.0 Support
  • Loading branch information
jiuka authored Jun 11, 2024
2 parents c480384 + b81b007 commit 9530db8
Show file tree
Hide file tree
Showing 22 changed files with 635 additions and 133 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/ubuntu/.devcontainer/base.Dockerfile

# [Choice] CheckMK version: 2.2.0-latest
ARG VARIANT="2.2.0-latest"
# [Choice] CheckMK version: 2.3.0-latest
ARG VARIANT="2.3.0-latest"
FROM checkmk/check-mk-raw:${VARIANT}

RUN /docker-entrypoint.sh /bin/true
Expand Down
14 changes: 4 additions & 10 deletions .devcontainer/build.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#!/bin/bash
set -e

NAME=$(python3 -c 'print(eval(open("package").read())["name"])')
VERSION=$(python3 -c 'print(eval(open("package").read())["version"])')
rm -f $NAME-$VERSION.mkp \
/omd/sites/cmk/var/cat check_mk/packages/${NAME}-*.mkp \
rm /omd/sites/cmk/var/check_mk/packages/${NAME} \
/omd/sites/cmk/var/check_mk/packages_local/${NAME}-*.mkp ||:

mkp -v package package 2>&1 | sed '/Installing$/Q' ||:
mkp -v package package

rm $NAME-$VERSION.mkp ||:
cp /omd/sites/cmk/var/check_mk/packages_local/$NAME-$VERSION.mkp .

mkp inspect $NAME-$VERSION.mkp

# Set Outputs for GitHub Workflow steps
if [ -n "$GITHUB_WORKSPACE" ]; then
echo "pkgfile=${NAME}-${VERSION}.mkp" >> $GITHUB_OUTPUT
echo "pkgname=${NAME}" >> $GITHUB_OUTPUT
echo "pkgversion=$VERSION" >> $GITHUB_OUTPUT
fi
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick an Ubuntu version: focal, bionic
"args": { "VARIANT": "2.2.0-latest" }
"args": { "VARIANT": "2.3.0-latest" }
},

"customizations": {
Expand Down
2 changes: 2 additions & 0 deletions .devcontainer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
flake8
pytest
pytest-cov
pytest-md
pytest-emoji
requests-mock
2 changes: 2 additions & 0 deletions .devcontainer/symlink.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
PKGNAME=$(python3 -c 'print(eval(open("package").read())["name"])')
ln -sv $WORKSPACE $OMD_ROOT/local/lib/python3/cmk_addons/plugins/$PKGNAME

for DIR in 'agents' 'checkman' 'checks' 'doc' 'inventory' 'notifications' 'pnp-templates' 'web'; do
rm -rfv $OMD_ROOT/local/share/check_mk/$DIR
Expand Down
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buy_me_a_coffee: jiuka
21 changes: 14 additions & 7 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Build Release Package
runs-on: ubuntu-latest
container:
image: checkmk/check-mk-raw:2.2.0-latest
image: checkmk/check-mk-raw:2.3.0-latest
permissions:
contents: write

Expand All @@ -24,17 +24,24 @@ jobs:
steps:
- name: Initialize Checkmk Site
run: /docker-entrypoint.sh /bin/true
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Parse Package File
run: |
NAME=$(python3 -c 'print(eval(open("package").read())["name"])')
VERSION=$(python3 -c 'print(eval(open("package").read())["version"])')
echo "CMKPKG_NAME=$NAME" >> "$GITHUB_ENV"
echo "CMKPKG_VERSION=$VERSION" >> "$GITHUB_ENV"
- name: Setup links
run: .devcontainer/symlink.sh
- name: Update GITHUB_PATH
run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH
- name: Build Extension
run: .devcontainer/build.sh
run: |
chown -R cmk:cmk $GITHUB_WORKSPACE
su -l -c "mkp -v package $GITHUB_WORKSPACE/package" cmk
cp /omd/sites/cmk/var/check_mk/packages_local/${CMKPKG_NAME}-${CMKPKG_VERSION}.mkp .
echo "pkgfile=${CMKPKG_NAME}-${CMKPKG_VERSION}.mkp" >> $GITHUB_OUTPUT
id: cmkpkg

- name: Create Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
release_name: Release ${{ github.ref }}
draft: false
Expand Down
20 changes: 14 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Build Checkmk package
runs-on: ubuntu-latest
container:
image: checkmk/check-mk-raw:2.2.0-latest
image: checkmk/check-mk-raw:2.3.0-latest

env:
OMD_ROOT: /omd/sites/cmk
Expand All @@ -24,16 +24,24 @@ jobs:
steps:
- name: Initialize Checkmk Site
run: /docker-entrypoint.sh /bin/true
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Parse Package File
run: |
NAME=$(python3 -c 'print(eval(open("package").read())["name"])')
VERSION=$(python3 -c 'print(eval(open("package").read())["version"])')
echo "CMKPKG_NAME=$NAME" >> "$GITHUB_ENV"
echo "CMKPKG_VERSION=$VERSION" >> "$GITHUB_ENV"
- name: Setup links
run: .devcontainer/symlink.sh
- name: Update GITHUB_PATH
run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH
- name: Build Extension
run: .devcontainer/build.sh
run: |
chown -R cmk:cmk $GITHUB_WORKSPACE
su -l -c "mkp -v package $GITHUB_WORKSPACE/package" cmk
cp /omd/sites/cmk/var/check_mk/packages_local/${CMKPKG_NAME}-${CMKPKG_VERSION}.mkp .
echo "pkgfile=${CMKPKG_NAME}-${CMKPKG_VERSION}.mkp" >> $GITHUB_OUTPUT
id: cmkpkg
- name: Upload artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ steps.cmkpkg.outputs.pkgfile }}
path: ${{ steps.cmkpkg.outputs.pkgfile }}
9 changes: 5 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ on:
push:
paths:
- '**.py'
- .github/workflows/lint.yml

jobs:
flake8_py3:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: '3.12'
- name: Install flake8
run: pip install flake8
- name: Run flake8
Expand Down
31 changes: 18 additions & 13 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
name: pytest

on:
push: []
push:
paths:
- '**.py'
- .github/workflows/pytest.yml

jobs:
pytest:

runs-on: ubuntu-latest
container:
image: checkmk/check-mk-raw:2.2.0-latest
image: checkmk/check-mk-raw:2.3.0-latest

env:
OMD_ROOT: /omd/sites/cmk
Expand All @@ -17,14 +20,16 @@ jobs:
WORKSPACE: ${{ github.workspace }}

steps:
- name: Initialize Checkmk Site
run: /docker-entrypoint.sh /bin/true
- uses: actions/checkout@v3
- name: Setup links
run: ./.devcontainer/symlink.sh
- name: Install pytest
run: su -l -c "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt pip3 install -r $GITHUB_WORKSPACE/.devcontainer/requirements.txt" cmk
- name: Update GITHUB_PATH
run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH
- name: Run pytest
run: python3 -m pytest
- name: Initialize Checkmk Site
run: /docker-entrypoint.sh /bin/true
- uses: actions/checkout@v4
- name: Setup links
run: .devcontainer/symlink.sh
- name: Install pytest
run: su -l -c "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt pip3 install -r $GITHUB_WORKSPACE/.devcontainer/requirements.txt" cmk
- name: Run pytest
run: |
REPORT="$(mktemp)"
chown -R cmk:cmk $GITHUB_WORKSPACE $REPORT
su -l -c "cd $GITHUB_WORKSPACE; python3 -m pytest -v --emoji -cov . --md $REPORT " cmk
cat $REPORT > $GITHUB_STEP_SUMMARY
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

This Plugin and check can monitor the Veeam for Office 365 jobs and licenses.

| Checkmk Version | Extension Version |
|-----------------|-------------------|
| Checkmk 2.3.0 | [latest](https://github.com/jiuka/checkmk_veeam_o365/releases/latest) |
| Checkmk 2.2.0 | [2.6.1](https://github.com/jiuka/checkmk_veeam_o365/releases/tag/v2.6.1) |
| Checkmk 2.1.0 | [2.5.1](https://github.com/jiuka/checkmk_veeam_o365/releases/tag/v2.5.1) |
| Checkmk 2.0.0 | [2.5.1](https://github.com/jiuka/checkmk_veeam_o365/releases/tag/v2.5.1) |
| Checkmk 1. | [1.3](https://github.com/jiuka/checkmk_veeam_o365/releases/tag/v1.3) |

## Screenshots
### Services
![Services](examples/veeam_o365_services.png)
Expand Down
24 changes: 13 additions & 11 deletions agent_based/veeam_o365jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@

from dataclasses import dataclass
from typing import Optional
from .agent_based_api.v1 import (
from cmk.agent_based.v2 import (
Service,
Result,
State,
check_levels,
render,
register,
CheckPlugin,
RuleSetType,
StringTable,
AgentSection,
)
from .agent_based_api.v1.type_defs import StringTable

VEEAM_O365JOBS_CHECK_DEFAULT_PARAMETERS = {
'states': {
Expand Down Expand Up @@ -84,7 +86,7 @@ def parse_veeam_o365jobs(string_table: StringTable) -> dict[str, VeeamO365Job]:
return parsed


register.agent_section(
agent_section_veeam_o365jobs = AgentSection(
name='veeam_o365jobs',
parse_function=parse_veeam_o365jobs,
)
Expand Down Expand Up @@ -136,7 +138,7 @@ def check_veeam_o365jobs(item, params, section):
yield from check_levels(
float(job.duration),
metric_name='duration',
levels_upper=params.get('duration', None),
levels_upper=params.get('duration', ('no_levels', None)),
label='Backup duration',
render_func=render.timespan,
)
Expand All @@ -152,14 +154,14 @@ def check_veeam_o365jobs(item, params, section):
)


register.check_plugin(
name = 'veeam_o365jobs',
check_plugin_veeam_o365jobs = CheckPlugin(
name='veeam_o365jobs',
service_name = 'VEEAM O365 Job %s',
discovery_ruleset_name='inventory_veeam_o365jobs_rules',
discovery_ruleset_type=register.RuleSetType.MERGED,
discovery_function=discovery_veeam_o365jobs,
discovery_ruleset_name='veeam_o365jobs',
discovery_ruleset_type=RuleSetType.MERGED,
discovery_default_parameters={},
discovery_function = discovery_veeam_o365jobs,
check_function = check_veeam_o365jobs,
check_function=check_veeam_o365jobs,
check_ruleset_name='veeam_o365jobs',
check_default_parameters=VEEAM_O365JOBS_CHECK_DEFAULT_PARAMETERS,
)
74 changes: 28 additions & 46 deletions agent_based/veeam_o365licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
# <<<veeam_o365licenses:sep(9) >>>
# Valid 28.06.2020 02:00:00 2592000 42 50000

from .agent_based_api.v1 import (
from cmk.agent_based.v2 import (
Service,
Result,
State,
check_levels,
render,
Metric,
register,
CheckPlugin,
)


Expand All @@ -50,54 +49,37 @@ def check_veeam_o365licenses(item, params, section):

yield from check_levels(
float(license_validity),
levels_lower=params.get('validity', (0, 0)),
levels_lower=params.get('validity', ('fixed', (0, 0))),
label='Period of validity' if float(license_validity) > 0 else 'Expired since',
render_func=lambda f: render.timespan(f if f > 0 else -f),
)

license_params = params.get('licenses', None)

if license_params is False:
license_warn = None
license_crit = None
elif not license_params:
license_warn = int(license_total)
license_crit = int(license_total)
elif isinstance(license_params[0], int):
license_warn = max(0, int(license_total) - license_params[0])
license_crit = max(0, int(license_total) - license_params[1])
else:
license_warn = int(license_total) * (1 - license_params[0] / 100.0)
license_crit = int(license_total) * (1 - license_params[1] / 100.0)

yield Metric('licenses',
int(license_used),
levels=(license_warn, license_crit),
boundaries=(0, int(license_total)))

if int(license_used) <= int(license_total):
infotext = 'used %d out of %d licenses' % (int(license_used), int(license_total))
else:
infotext = 'used %d licenses, but you have only %d' % (int(license_used), int(license_total))

if license_crit is not None and int(license_used) >= license_crit:
status = State.CRIT
elif license_warn is not None and int(license_used) >= license_warn:
status = State.WARN
else:
status = State.OK

if license_crit is not None:
infotext += ' (warn/crit at %d/%d)' % (license_warn, license_crit)

yield Result(state=status, summary=infotext)


register.check_plugin(
name = 'veeam_o365licenses',
license_used = int(license_used)
license_total = int(license_total)

match params.get('licenses', None):
case ('always_ok', None):
levels = ('no_levels', None)
case ('absolute', {'warn': warn, 'crit': crit}):
levels = ('fixed', (license_total - warn, license_total - crit))
case ('percentage', {'warn': warn, 'crit': crit}):
levels = ('fixed', (int(license_total * (100 - warn) / 100), int(license_total * (100 - crit) / 100)))
case _:
levels = ('fixed', (int(license_total), int(license_total)))

yield from check_levels(license_used,
label="used",
render_func=lambda v: f"{v:d}",
levels_upper=levels,
metric_name='licenses',
boundaries=(0, int(license_total)))


check_plugin_veeam_o365jobs = CheckPlugin(
name='veeam_o365licenses',
service_name = 'VEEAM O365 License %s',
discovery_function = discovery_veeam_o365licenses,
check_function = check_veeam_o365licenses,
discovery_function=discovery_veeam_o365licenses,
check_function=check_veeam_o365licenses,
check_ruleset_name='veeam_o365licenses',
check_default_parameters={},
)
Loading

0 comments on commit 9530db8

Please sign in to comment.