-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_monitoring
More file actions
executable file
·247 lines (221 loc) · 7.51 KB
/
process_monitoring
File metadata and controls
executable file
·247 lines (221 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/sh
########################################################################
# process_monitoring: Munin Plugin for Monitoring Required Processes
#
# This plugin belongs to the 'contrib' family, indicating it is community-maintained.
#%# family=contrib
# This plugin supports Munin's autoconf feature for automatic configuration checks.
#%# capabilities=autoconf
#
# Description:
# This plugin monitors the presence and count of essential system
# processes and configuration rules. Designed for use with Munin,
# it reports the number of running instances or matching configurations,
# such as iptables rules, ntpd, memcached, postgres (or postmaster),
# apache2, and MySQL-compatible servers (mysqld / mariadbd).
#
# It helps system administrators ensure that critical services are
# running as expected. The plugin outputs a graph in Munin showing
# the state of required processes.
#
# Toggle targets:
# Set the following global variables to 1 or 0 to enable or disable monitoring.
# Default is 1 (enabled). You can edit them here or pass via environment
# (e.g., in /etc/munin/plugin-conf.d using env.iptables=0).
# iptables = 1 # Monitor iptables rule presence
# ntpd = 1 # Monitor ntpd process
# memcached = 1 # Monitor memcached process
# postgres = 1 # Monitor postgres/postmaster process
# mysql = 1 # Monitor mysqld/mariadbd process
# apache2 = 1 # Monitor apache2 process
#
# Example: to disable iptables monitoring, set: iptables=0
#
# You can also override these values via Munin plugin configuration:
# /etc/munin/plugin-conf.d/local
# [process_monitoring]
# env.iptables=0
# env.mysql=0
#
# This allows you to manage settings without editing the plugin itself.
# If both are defined, the environment variable (plugin-conf.d setting) takes precedence.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/munin-plugins
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# sudo ln -s /usr/local/share/munin/plugins/process_monitoring /etc/munin/plugins/process_monitoring
#
# Then reload munin-node:
# sudo systemctl restart munin-node
#
# Example:
# ./process_monitoring autoconf
# ./process_monitoring config
# ./process_monitoring
#
# Note:
# If your system requires privileges for listing firewall rules,
# allow the munin user via sudoers, e.g.:
# munin ALL=(root) NOPASSWD:/sbin/iptables
#
# Version History:
# v2.0 2025-09-23
# Add simple per-target toggle via global variables with defaults.
# Make autoconf conditional on toggles and suppress disabled fields in config/fetch.
# v1.3 2025-09-15
# Added GAUGE type definitions, raised graph ceiling to 50, and ensured PostgreSQL
# compatibility by counting both postgres and legacy postmaster processes.
# v1.2 2025-09-09
# POSIX safe process counting via ps comm and simplified autoconf dependencies.
# v1.1 2025-08-29
# Add MySQL (mysqld / mariadbd) process monitoring (label: mysql).
# v1.0 2025-03-26
# Refactored for POSIX compliance. Added full header documentation,
# improved readability and maintainability with English comments.
# v0.1 2018-09-18
# Initial version.
#
########################################################################
# --- Target toggles with sane defaults ---
# Allow override via environment (Munin plugin-conf env.*) or inline edits below.
: "${iptables:=1}"
: "${ntpd:=1}"
: "${memcached:=1}"
: "${postgres:=1}"
: "${mysql:=1}"
: "${apache2:=1}"
# Display full script header information extracted from the top comment block
usage() {
awk '
BEGIN { in_header = 0 }
/^#{10,}$/ { if (!in_header) { in_header = 1; next } else exit }
in_header && /^# ?/ {
if ($0 ~ /^#%#/) next
print substr($0, 3)
}
' "$0"
return 0
}
# --- Autoconf check ---
autoconf() {
# Exit code flag for autoconf checks
CONF_ERR=0
# Check for required commands
# Note: Do not require service binaries themselves (memcached, etc.)
for cmd in ps grep awk; do
command -v "$cmd" >/dev/null 2>&1 || CONF_ERR=1
done
# Only require iptables and sudo if iptables monitoring is enabled
if [ "${iptables:-1}" -eq 1 ]; then
command -v iptables >/dev/null 2>&1 || CONF_ERR=1
# sudo may be required to list rules depending on policy
command -v sudo >/dev/null 2>&1 || CONF_ERR=1
fi
if [ "$CONF_ERR" -eq 0 ]; then
echo "yes"
else
echo "no"
fi
return 0
}
# --- Munin graph configuration output ---
config() {
echo "graph_category processes"
echo "graph_title Required processes"
echo "graph_vlabel Number of required processes"
echo "graph_args --lower-limit 0 --upper-limit 20"
# Define label and graph style/type for each monitored service (conditionally)
if [ "$iptables" -eq 1 ]; then
echo "iptables.label iptables"
echo "iptables.type GAUGE"
echo "iptables.draw LINE2"
echo "iptables.warning 1:"
echo "iptables.critical 1:"
fi
if [ "$ntpd" -eq 1 ]; then
echo "ntpd.label ntp"
echo "ntpd.type GAUGE"
echo "ntpd.draw LINE2"
echo "ntpd.warning 1:"
echo "ntpd.critical 1:"
fi
if [ "$memcached" -eq 1 ]; then
echo "memcached.label memcached"
echo "memcached.type GAUGE"
echo "memcached.draw LINE2"
echo "memcached.warning 1:"
echo "memcached.critical 1:"
fi
if [ "$postgres" -eq 1 ]; then
echo "postgres.label postgres"
echo "postgres.type GAUGE"
echo "postgres.draw LINE2"
echo "postgres.warning 1:"
echo "postgres.critical 1:"
fi
if [ "$mysql" -eq 1 ]; then
echo "mysql.label mysql"
echo "mysql.type GAUGE"
echo "mysql.draw LINE2"
echo "mysql.warning 1:"
echo "mysql.critical 1:"
fi
if [ "$apache2" -eq 1 ]; then
echo "apache2.label apache2"
echo "apache2.type GAUGE"
echo "apache2.draw LINE2"
echo "apache2.warning 1:"
echo "apache2.critical 1:"
fi
return 0
}
# --- Data collection helpers ---
# Count processes by exact command name using POSIX-safe ps
count_comm() {
# Prints the count of processes whose command name exactly matches $1
ps -e -o comm= | grep -c "^$1$"
}
# --- Data collection phase ---
fetch() {
# Count iptables rules matching specific description
if [ "$iptables" -eq 1 ]; then
CMD=$(sudo iptables -vn -L 2>/dev/null | grep -c "SET name: SSH side: source")
echo "iptables.value $CMD"
fi
if [ "$ntpd" -eq 1 ]; then
CMD=$(count_comm ntpd)
echo "ntpd.value $CMD"
fi
if [ "$memcached" -eq 1 ]; then
CMD=$(count_comm memcached)
echo "memcached.value $CMD"
fi
if [ "$postgres" -eq 1 ]; then
CMD=$(ps -e -o comm= | grep -E -c "^(postgres|postmaster)$")
echo "postgres.value $CMD"
fi
if [ "$mysql" -eq 1 ]; then
CMD=$(ps -e -o comm= | grep -E -c "^(mysqld|mariadbd)$")
echo "mysql.value $CMD"
fi
if [ "$apache2" -eq 1 ]; then
CMD=$(count_comm apache2)
echo "apache2.value $CMD"
fi
return 0
}
# Main entry point of the script
main() {
case "$1" in
-h|--help|-v|--version) usage ;;
autoconf) autoconf ;;
config) config ;;
*) fetch ;;
esac
return $?
}
# Execute main function
main "$@"