Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
icinga-checks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
icinga-checks
Commits
b40c4838
Commit
b40c4838
authored
1 year ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
detect DOCKER; use sudo; add debug
parent
c2e18fe9
No related branches found
No related tags found
1 merge request
!230
Docker checks
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
check_docker_info
+37
-24
37 additions, 24 deletions
check_docker_info
inc_dockerfunctions.sh
+69
-0
69 additions, 0 deletions
inc_dockerfunctions.sh
with
106 additions
and
24 deletions
check_docker_info
+
37
−
24
View file @
b40c4838
...
@@ -5,17 +5,21 @@
...
@@ -5,17 +5,21 @@
#
#
# requirements:
# requirements:
# - docker
# - docker
# - sudo permissions on docker command
#
#
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Cli docs:
# Cli docs:
# https://docs.docker.com/engine/reference/commandline/docker/
# https://docs.docker.com/engine/reference/commandline/docker/
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# 2024-01-18 v1.0 <axel.hahn@unibe.ch> init
# 2024-01-18 v1.0 <axel.hahn@unibe.ch> init
# 2024-01-22 v1.1 <axel.hahn@unibe.ch> detect DOCKER; use sudo; add debug
# ======================================================================
# ======================================================================
.
$(
dirname
$0
)
/inc_pluginfunctions
.
$(
dirname
$0
)
/inc_pluginfunctions
self_APPVERSION
=
1.0
.
$(
dirname
$0
)
/inc_dockerfunctions.sh
self_APPVERSION
=
1.1
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# FUNCTIONS
# FUNCTIONS
...
@@ -30,38 +34,39 @@ $( ph.showImlHelpHeader )
...
@@ -30,38 +34,39 @@ $( ph.showImlHelpHeader )
Show docker version and count of containers total and by its status.
Show docker version and count of containers total and by its status.
It returns
CRITICAL if a created container is not running.
OK if no container was created yet or all conmtainers are running
UNKNOWM if
- docker or jq were not found
- docker data were not fetched
- docker cannot be connected
This check provides performance data.
USAGE:
USAGE:
$_self
[OPTIONS]
$_self
[OPTIONS]
OPTIONS:
OPTIONS:
-h this help
-h this help
-d Debug; Flag: show all docker system infos
EXAMPLES:
EXAMPLES:
$_self
$_self
Show Status of all containers
EOH
$_self
-d
}
Show Status of all containers and json with all docker system infos
# filter json data with jq
EOH
# param string json data
# param string jq filter
function
_filterJson
(){
echo
"
$1
"
| jq
"
$2
"
}
# filter json data with jq (by expecting a single result) and remove quotes
# param string json data
# param string jq filter
function
_getString
(){
_filterJson
"
$1
"
"
$2
"
|
tr
-d
'"'
}
}
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# MAIN
# MAIN
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
ph.hasParamoption
"h"
"
$@
"
;
bOptHelp
=
$?
ph.hasParamoption
"h"
"
$@
"
;
bOptHelp
=
$?
ph.hasParamoption
"d"
"
$@
"
;
bOptDebug
=
$?
if
[
$bOptHelp
-eq
0
]
;
then
if
[
$bOptHelp
-eq
0
]
;
then
_usage
_usage
...
@@ -71,10 +76,12 @@ fi
...
@@ -71,10 +76,12 @@ fi
ph.require
"docker"
ph.require
"docker"
ph.require
"jq"
ph.require
"jq"
_detectDockerenv
# --- get data
# --- get data
data
=
$(
docker system info
--format
"{{ json . }}"
)
data
=
$(
sudo
-n
--preserve-env
docker system info
--format
'{{ json . }}'
)
_detectDockererror
"
$data
"
typeset
-i
iCTotal
;
typeset
-i
iCTotal
;
iCTotal
=
$(
_getString
"
$data
"
".Containers"
)
iCTotal
=
$(
_getString
"
$data
"
".Containers"
)
...
@@ -87,15 +94,19 @@ sLicense=$( _getString "$data" ".ProductLicense" )
...
@@ -87,15 +94,19 @@ sLicense=$( _getString "$data" ".ProductLicense" )
# --- generate result
# --- generate result
if
[
"
$iCRunning
"
-eq
"0"
]
;
then
if
[
"
$iCTotal
"
-eq
"0"
]
;
then
ph.setStatus critical
out+
=
"No container was created yet."
out+
=
"No container is running"
else
else
if
[
"
$iCRunning
"
-
ne
"
$iCTotal
"
]
;
then
if
[
"
$iCRunning
"
-
eq
"0
"
]
;
then
ph.setStatus
warning
ph.setStatus
critical
out+
=
"No
t all
container
s are
running"
out+
=
"No container
is
running"
else
else
out+
=
"All containers are running"
if
[
"
$iCRunning
"
-ne
"
$iCTotal
"
]
;
then
ph.setStatus critical
out+
=
"Not all containers are running"
else
out+
=
"All containers are running"
fi
fi
fi
fi
fi
...
@@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages"
...
@@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages"
ph.status
"Docker
$sVersion
(
$sLicense
) .. containers:
$iCTotal
running:
$iCRunning
paused:
$iCPaused
stopped:
$iCStopped
.. images:
$iImages
"
ph.status
"Docker
$sVersion
(
$sLicense
) .. containers:
$iCTotal
running:
$iCRunning
paused:
$iCPaused
stopped:
$iCStopped
.. images:
$iImages
"
echo
"
$out
"
echo
"
$out
"
# if -d was given then show debug infos too
test
$bOptDebug
-eq
0
&&
(
echo
;
echo
"DEBUG: full docker system infos as json"
;
echo
"
$data
"
| jq
)
ph.exit
ph.exit
...
...
This diff is collapsed.
Click to expand it.
inc_dockerfunctions.sh
0 → 100644
+
69
−
0
View file @
b40c4838
#!/bin/bash
# ======================================================================
#
# Shared functions for DOCKER checks
#
# requirements:
# - docker
# - jq
#
# ----------------------------------------------------------------------
# Cli docs:
# https://docs.docker.com/engine/reference/commandline/docker/
# ----------------------------------------------------------------------
# 2024-01-19 v1.0 <axel.hahn@unibe.ch> init
# ======================================================================
.
$(
dirname
$0
)
/inc_pluginfunctions
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# filter json data with jq
# param string json data
# param string jq filter
function
_filterJson
(){
echo
"
$1
"
| jq
"
$2
"
}
# filter json data with jq (by expecting a single result) and remove quotes
# param string json data
# param string jq filter
function
_getString
(){
_filterJson
"
$1
"
"
$2
"
|
tr
-d
'"'
}
# if DOCKER_HOST is not set we try to detect the user running "containerd"
# create an env var DOCKER_HOST="unix:///run/user/<ID>/docker.sock"
function
_detectDockerenv
(){
if
[
-z
"
$DOCKER_HOST
"
]
;
then
dockeruid
=
$(
ps
-ef
|
grep
containerd |
grep
-Eo
"/run/user/([0-9]*)/"
|
head
-1
|
cut
-f
4
-d
'/'
)
test
-n
"
$dockeruid
"
&&
export
DOCKER_HOST
=
"unix:///run/user/
$dockeruid
/docker.sock"
# Don't abort - it is allowed that the variable DOCKER_HOST is missing
fi
}
# detect error after dicker command. It stops if
# - no content was fetched
# - content contains key "ServerErrors"
function
_detectDockererror
(){
local
data
=
"
$1
"
if
[
-z
"
$data
"
]
;
then
ph.setStatus unknown
ph.status
"No data. Unable to fetch Docker information."
ph.exit
fi
sSrvErrror
=
$(
_filterJson
"
$data
"
".ServerErrors"
)
if
[
"
$sSrvErrror
"
!=
"null"
]
;
then
ph.setStatus unknown
ph.status
"Unable to connect to Docker:"
echo
"
$sSrvErrror
"
|
grep
"^ "
ph.exit
fi
}
# ----------------------------------------------------------------------
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment