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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
icinga-checks
Merge requests
!55
Add check requirement
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Add check requirement
add-check-requirement
into
master
Overview
0
Commits
5
Pipelines
0
Changes
1
Merged
Add check requirement
Hahn Axel (hahn)
requested to merge
add-check-requirement
into
master
Feb 13, 2023
Overview
0
Commits
5
Pipelines
0
Changes
1
... and some shell fixes
0
0
Merge request reports
Viewing commit
2af97e2a
Prev
Next
Show latest version
1 file
+
170
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
2af97e2a
add check_requirements 1.0
· 2af97e2a
Hahn Axel (hahn)
authored
Feb 13, 2023
check_requirements
0 → 100755
+
170
−
0
View file @ 2af97e2a
Edit in single-file editor
Open in Web IDE
#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: check requirements
#
# Check if a list requirements regarding processes and network
# connections do exist
# ----------------------------------------------------------------------
#
# ah=axel.hahn@unibe.ch
#
# 2023-02-13 v1.0 ah
# ======================================================================
.
"
$(
dirname
$0
)
/inc_pluginfunctions"
self_APPNAME
=
$(
basename
$0
|
tr
[
:lower:]
[
:upper:]
)
self_APPVERSION
=
1.0
self
=
$(
basename
$0
)
typeset
-i
VERBOSE
=
0
OUT
=
typeset
-i
COUNT
=
0
typeset
-i
COUNTOK
=
0
typeset
-i
COUNTERR
=
0
PROCESSES
=
# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------
# show help
function
showHelp
(){
cat
<<
EOF
______________________________________________________________________
$self_APPNAME
v
$self_APPVERSION
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
______________________________________________________________________
Check if a list requirements regarding processes and network
connections do exist.
The check returns OK if all given requirements match.
SYNTAX:
$(
basename
$0
)
[-h] [PARAMETERS]
OPTIONS:
-h or --help show this help.
-v or --verbose show more data: processes and port numbers from
/etc/services; add it as first param
PARAMETERS:
-c|--connect TYPE TARGET PORT
check if connect to a server to a given port
is reachable
TYPE string one of tcp|udp
TARGET string target host
PORT int port number
-p|--process STRING check if a process with given regex exists
-t|--tcp PORT check if connect on local tcp port is reachable;
This is a shortcut for -c tcp localhost [port]
The parameters can be repeated multiple times. Checks will be executed in
the given order.
EXAMPLE
$(
basename
$0
)
-p httpd -p mysqld -t 22 -t 80 -t 443 -t 3306
Check if
- a process httpd and a process mysqld exist
- localhost listens to ports 22, 80, 443 and 3306
EOF
}
# helper function: add new line in $OUT
function
newline
(){
OUT+
=
"
"
}
# check in process list
# param string regex to search for in output of "ps x"
function
chkProcess
(){
local
label
=
"
$1
"
test
-z
"
$PROCESSES
"
&&
PROCESSES
=
$(
ps x
)
COUNT+
=
1
local
found
=
$(
echo
"
$PROCESSES
"
|
grep
-v
"grep"
|
grep
-v
"
$self
"
|
grep
-E
"
$label
"
)
if
[
-z
"
$found
"
]
;
then
COUNTERR+
=
1
ph.setStatus critical
OUT+
=
"ERROR process
$label
was not found"
else
COUNTOK+
=
1
OUT+
=
"OK process
$label
(
$(
echo
"""
$found
"""
|
wc
-l
)
x)"
test
$VERBOSE
-gt
0
&&
newline
test
$VERBOSE
-gt
0
&&
OUT+
=
"
$(
echo
"""
$found
"""
|
sed
's#^# #g'
)
"
test
$VERBOSE
-gt
0
&&
newline
fi
newline
}
# check connection
# param string type: one of tcp|udp
# param string target: a servername eg. localhost or www.example.com
# param integer port number to connect
function
chkConnection
(){
local type
=
"
$1
"
# tcp | udp
local
target
=
"
$2
"
# servername
local
port
=
"
$3
"
# port number
COUNT+
=
1
if
!
echo
"
$type
/
$target
/
$port
"
|
grep
-E
"^(tcp|udp)/[a-z][a-z0-9
\.\-
]*/[1-9][0-9]*$"
>
/dev/null 2>&1
;
then
COUNTERR+
=
1
OUT+
=
"ERROR syntax error detected - verify params for port check; type=
$type
(one of tcp|udp) target=
$target
(servername to connect) port=
$port
"
else
if
(>
/dev/
$type
/
$target
/
$port
)
2>/dev/null
;
then
COUNTOK+
=
1
OUT+
=
"OK connection
$type
to
$target
on port
$port
"
else
COUNTERR+
=
1
ph.setStatus critical
OUT+
=
"ERROR connection
$type
to
$target
on port
$port
FAILED"
fi
test
$VERBOSE
-gt
0
&&
newline
test
$VERBOSE
-gt
0
&&
OUT+
=
"
$(
grep
"""
$port
/
$type
"""
/etc/services |
sed
's#^# #g'
)
"
test
$VERBOSE
-gt
0
&&
newline
fi
newline
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
if
[
"$#"
-eq
0
]
;
then
showHelp
;
exit
1
fi
# parse params
while
[[
"$#"
-gt
0
]]
;
do case
$1
in
-c
|
--connect
)
chkConnection
"
$2
"
"
$3
"
"
$4
"
;
shift
;
shift
;
shift
;
shift
;;
-p
|
--process
)
chkProcess
"
$2
"
;
shift
2
;;
-h
|
--help
)
showHelp
;
exit
0
;;
-t
|
--tcp
)
chkConnection tcp localhost
"
$2
"
;
shift
;
shift
;;
-v
|
--verbose
)
VERBOSE
=
1
;
shift
;;
*
)
echo
"ERROR: Unknown parameter:
$1
"
;
showHelp
;
exit
1
;
esac
;
done
ph.status
"
$COUNT
Requirement checks - errors:
$COUNTERR
"
echo
echo
"STATUS TYPE RESULT"
echo
"
$OUT
"
ph.exit
# ----------------------------------------------------------------------
Loading