Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nagios-plugin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Le Filament
nagios-plugin
Commits
460bbf01
Commit
460bbf01
authored
3 years ago
by
Théo - Le Filament
Browse files
Options
Downloads
Patches
Plain Diff
add check_docker shell script
parent
03246197
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
check_docker.sh
+103
-0
103 additions, 0 deletions
check_docker.sh
with
103 additions
and
0 deletions
check_docker.sh
0 → 100755
+
103
−
0
View file @
460bbf01
#!/bin/bash
# Default threshold values.
cpu_threshold_warning
=
25
cpu_threshold_critical
=
50
mem_threshold_warning
=
50
mem_threshold_critical
=
75
filter
=
''
print_help
()
{
echo
"usage:
${
0
}
[OPTIONS]"
echo
"options:"
echo
-e
" -c, --cpu <warning:critical>
\t\t
threshold values for cpu usage"
echo
-e
" -f, --filter <regex>
\t\t\t
containers to check"
echo
-e
" -h, --help
\t\t\t\t
print this help"
echo
-e
" -m, --memory <warning:critical>
\t
threshold values for memory usage"
}
while
[[
$#
-gt
0
]]
;
do
case
"
$1
"
in
-c
|
--cpu
)
cpu_threshold_warning
=
`
awk
-F
':'
'{print +$1}'
<
(
echo
$2
)
`
cpu_threshold_critical
=
`
awk
-F
':'
'{print +$2}'
<
(
echo
$2
)
`
shift
2
;;
-f
|
--filter
)
filter
=
$2
shift
2
;;
-m
|
--memory
)
mem_threshold_warning
=
`
awk
-F
':'
'{print +$1}'
<
(
echo
$2
)
`
mem_threshold_critical
=
`
awk
-F
':'
'{print +$2}'
<
(
echo
$2
)
`
shift
2
;;
--help
|
-h
)
print_help
exit
0
;;
*
)
echo
"Error while parsing argument '
$1
'."
exit
1
;;
esac
done
# Gathering facts.
all_containers
=
`
sudo
docker ps
--format
'{{.Names}}'
--filter
"name=
${
filter
}
"
--no-trunc
--quiet
--all
`
running_containers
=
`
sudo
docker ps
--format
'{{.Names}}'
--filter
"name=
${
filter
}
"
--no-trunc
--quiet
`
running_containers_stats
=
`
sudo
docker stats
--format
'{{.Name}}:{{.CPUPerc}}:{{.MemPerc}}'
--no-stream
`
not_running_containers
=
`
comm
-23
<
(
echo
$all_containers
)
<
(
echo
$running_containers
)
`
# Format data.
output
=
''
perf
=
''
cpu_scale
=
";
${
cpu_threshold_warning
}
;
${
cpu_threshold_critical
}
;0;100"
mem_scale
=
";
${
mem_threshold_warning
}
;
${
mem_threshold_critical
}
;0;100"
# Running containers.
for
container
in
${
running_containers
}
;
do
output+
=
" OK:
${
container
}
status is running"
done
# Not running containers.
for
container
in
${
not_running_containers
}
;
do
output+
=
" NOK:
${
container
}
status is not running"
done
# CPU and memory statistics for running containers.
for
container_stats
in
${
running_containers_stats
}
;
do
name
=
`
awk
-F
':'
'{print $1}'
<
(
echo
$container_stats
)
`
[[
!
$name
=
~
${
filter
}
]]
&&
continue
cpu
=
`
awk
-F
':'
'{print +$2}'
<
(
echo
$container_stats
)
`
mem
=
`
awk
-F
':'
'{print +$3}'
<
(
echo
$container_stats
)
`
status
=
'OK'
[[
${
cpu
%.*
}
-ge
$cpu_threshold_warning
]]
&&
status
=
'WARNING'
[[
${
cpu
%.*
}
-ge
$cpu_threshold_critical
]]
&&
status
=
'CRITICAL'
output+
=
"
${
status
}
:
${
name
}
cpu is
${
cpu
}
%"
status
=
'OK'
[[
${
mem
%.*
}
-ge
$mem_threshold_warning
]]
&&
status
=
'WARNING'
[[
${
mem
%.*
}
-ge
$mem_threshold_critical
]]
&&
status
=
'CRITICAL'
output+
=
"
${
status
}
:
${
name
}
memory is
${
mem
}
%"
perf+
=
"
${
name
}
_cpu=
${
cpu
}
%
${
cpu_scale
}
"
perf+
=
"
${
name
}
_mem=
${
mem
}
%
${
mem_scale
}
"
done
# Nagios OK status.
exit_code
=
0
# Nagios WARNING status.
grep
--quiet
'WARNING:'
<
(
echo
$output
)
&&
exit_code
=
1
# Nagios CRITICAL status.
grep
--quiet
'NOK:\|CRITICAL:'
<
(
echo
$output
)
&&
exit_code
=
2
# Plugin output.
echo
"
${
output
#
' '
}
|
${
perf
#
' '
}
"
exit
$exit_code
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