]> git.somenet.org - root/pub/somesible.git/blob - roles/base/munin-node/files/default/plugins.somesible/threads
[roles/server/dhcpd] install dhcpd
[root/pub/somesible.git] / roles / base / munin-node / files / default / plugins.somesible / threads
1 #!/bin/sh
2 # -*- sh -*-
3 #
4 ################################################
5 ### Managed by someone's ansible provisioner ###
6 ################################################
7 # Part of: https://git.somenet.org/root/pub/somesible.git
8 # 2017-2024 by someone <someone@somenet.org>
9 #
10
11 set -e
12
13 : << =cut
14
15 =head1 NAME
16
17 threads - Plugin to monitor threads and thread states. - copy of threads, with another ps option. commented out all but linux.
18
19 =head1 ABOUT
20
21 This plugin requires munin-server version 1.2.5 or 1.3.3 (or higher).
22
23 This plugin is backwards compatible with the old threads-plugins found on
24 SunOS, Linux and *BSD (i.e. the history is preserved).
25
26 All fields have colours associated with them which reflect the type of process
27 (sleeping/idle = blue, running = green, stopped/zombie/dead = red, etc.)
28
29 =head1 CONFIGURATION
30
31 No configuration for this plugin.
32
33 =head1 AUTHOR
34
35 Copyright (C) 2006 Lars Strand
36
37 =head1 LICENSE
38
39 GNU General Public License, version 2
40
41 =begin comment
42
43 This file is part of Munin.
44
45 This program is free software; you can redistribute it and/or modify it under
46 the terms of the GNU General Public License as published by the Free Software
47 Foundation; version 2 dated June, 1991.
48
49 This program is distributed in the hope that it will be useful, but WITHOUT ANY
50 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
51 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
52
53 You should have received a copy of the GNU General Public License along with
54 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
55 Street, Fifth Floor, Boston, MA 02110-1301 USA.
56
57 =end comment
58
59 =head1 MAGIC MARKERS
60
61 =begin comment
62
63 These magic markers are used by munin-node-configure when installing
64 munin-node.
65
66 =end comment
67
68  #%# family=auto
69  #%# capabilities=autoconf
70
71 =cut
72
73 # Search for program in $PATH unless predefined.
74 awk=${awk:-awk}
75 ps=${ps:-ps}
76
77 # Find operating system
78 OPERSYS=${OPERSYS:-$(uname | cut -f 1 -d _)}
79 [ -z "$OPERSYS" ] && echo >&2 "Failed to detect environment via uname" && exit 1
80
81 if [ "$1" = "autoconf" ]; then
82     case "$OPERSYS" in
83         Linux) #|SunOS|FreeBSD|OpenBSD|NetBSD|Darwin|CYGWIN) - some edit: disable all but linux.
84             if ! "$ps" >/dev/null 2>/dev/null; then
85                 echo "no (ps=$ps failed)"
86             elif ! echo | "$awk" '{ print "Hei" }' >/dev/null 2>/dev/null; then
87                 echo "no (awk=$awk failed)"
88             else
89                 echo yes
90             fi
91             exit 0
92             ;;
93         *)
94             echo "no (unknown OS)"
95             exit 0
96             ;;
97     esac
98 fi
99
100 . "$MUNIN_LIBDIR/plugins/plugin.sh"
101
102 # Define colours
103 RUNNABLE='22ff22'         # Green
104 SLEEPING='0022ff'         # Blue
105 STOPPED='cc0000'          # Darker red
106 ZOMBIE='990000'           # Darkest red
107 UNINTERRUPTIBLE='ffa500'  # Orange
108 IDLE='4169e1'             # Royal blue
109 PAGING='00aaaa'           # Darker turquoise
110 INTERRUPT='ff00ff'        # Fuchsia
111 LOCK='ff3333'             # Lighter red
112 RUNNING='00ff7f'          # Spring green
113 DEAD='ff0000'             # Red
114 SUSPENDED='ff1493'        # Deep pink
115 TOTAL='c0c0c0'            # Silver
116
117 # Taken from ps(1)
118 # R - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX, HP-UX      (runable)
119 # S - Linux, SunOS, FreeBSD*, OpenBSD*, NetBSD*, OSX*, HP-UX  (sleeping)
120 # T - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX, HP-UX      (stopped)
121 # Z - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX, HP-UX      (zombie/terminated)
122 # D - Linux, FreeBSD, OpenBSD, NetBSD                         (uninterruptible)
123 # I - FreeBSD, OpenBSD, NetBSD, OSX, HP-UX                    (idle/intermediate)
124 # W - Linux*, FreeBSD*, HP-UX                                 (paging/interrupt/waiting)
125 # L - FreeBSD                                                 (lock)
126 # O - SunOS                                                   (running)
127 # X - Linux, HP-UX*                                           (dead)
128 # U - OSX, NetBSD*                                            (uninterruptible/suspended)
129 # 0 - HP-UX                                                   (nonexistent)
130 # *) Differ meaning
131
132 if [ "$1" = "config" ]; then
133     echo "graph_title Threads"
134     echo "graph_info This graph shows the number of threads"
135     echo "graph_category system"
136     echo "graph_args --base 1000 -l 0"
137     echo "graph_vlabel Number of threads"
138
139     # OS specific flags
140     if [ "$OPERSYS" = "Linux" ]; then
141         echo "graph_order sleeping idle stopped zombie dead paging uninterruptible runnable threads"
142         echo "dead.label dead"
143         echo "dead.draw STACK"
144         echo "dead.colour $DEAD"
145         echo "dead.info The number of dead threads."
146         print_warning dead
147         print_critical dead
148         echo "paging.label paging"
149         echo "paging.draw STACK"
150         echo "paging.colour $PAGING"
151         echo "paging.info The number of paging threads (<2.6 kernels only)."
152         print_warning paging
153         print_critical paging
154
155     elif [ "$OPERSYS" = "SunOS" ]; then
156         echo "graph_order sleeping stopped zombie runnable running total"
157         echo "running.label running"
158         echo "running.draw STACK"
159         echo "running.colour $RUNNING"
160         echo "running.info The number of threads that are running on a processor."
161         print_warning running
162         print_critical running
163         # Be backwards compatible.
164         echo "total.label total"
165         echo "total.draw LINE1"
166         echo "total.colour $TOTAL"
167         echo "total.info The total number of threads."
168         print_warning total
169         print_critical total
170
171     elif [ "$OPERSYS" = "FreeBSD" ]; then
172         echo "graph_order sleeping idle stopped zombie lock uninterruptible interrupt runnable threads"
173         echo "lock.label lock"
174         echo "lock.draw STACK"
175         echo "lock.colour $LOCK"
176         echo "lock.info The number of threads that are waiting to acquire a lock."
177         print_warning lock
178         print_critical lock
179         echo "interrupt.label interrupt"
180         echo "interrupt.draw STACK"
181         echo "interrupt.colour $INTERRUPT"
182         echo "interrupt.info The number of idle interrupt threads."
183         print_warning interrupt
184         print_critical interrupt
185
186     elif [ "$OPERSYS" = "OpenBSD" ]; then
187         echo "graph_order sleeping idle stopped zombie uninterruptible runnable threads"
188
189     elif [ "$OPERSYS" = "NetBSD" ]; then
190         echo "graph_order sleeping idle stopped zombie uninterruptible suspended runnable threads"
191         echo "suspended.label suspended"
192         echo "suspended.draw STACK"
193         echo "suspended.colour $SUSPENDED"
194         echo "suspended.info The number of threads that are suspended."
195         print_warning suspended
196         print_critical suspended
197
198     elif [ "$OPERSYS" = "Darwin" ]; then
199         echo "graph_order sleeping idle stopped zombie uninterruptible running threads"
200         echo "uninterruptible.label uninterruptible"
201         echo "uninterruptible.draw STACK"
202         echo "uninterruptible.colour $UNINTERRUPTIBLE"
203         echo "uninterruptible.info The number of uninterruptible threads (usually IO)."
204         print_warning uninterruptible
205         print_critical uninterruptible
206     elif [ "$OPERSYS" = "HP-UX" ]; then
207         echo "graph_order sleeping intermediate stopped terminated waiting growing nonexistent runnable threads"
208         echo "waiting.label waiting"
209         echo "waiting.draw STACK"
210         echo "waiting.colour $INTERRUPT"
211         echo "waiting.info The number of waiting threads."
212         print_warning waiting
213         print_critical waiting
214         echo "terminated.label terminated"
215         echo "terminated.draw STACK"
216         echo "terminated.colour $ZOMBIE"
217         echo "terminated.info The number of threads that are terminated."
218         print_warning terminated
219         print_critical terminated
220         echo "growing.label growing"
221         echo "growing.draw STACK"
222         echo "growing.colour $RUNNING"
223         echo "growing.info The number of growing threads."
224         print_warning growing
225         print_critical growing
226         echo "intermediate.label intermediate"
227         echo "intermediate.draw STACK"
228         echo "intermediate.colour $IDLE"
229         echo "intermediate.info The number of intermediate threads."
230         print_warning intermediate
231         print_critical intermediate
232         echo "nonexistent.label nonexistent"
233         echo "nonexistent.draw STACK"
234         echo "nonexistent.colour $LOCK"
235         echo "nonexistent.info The number of nonexistent threads."
236         print_warning nonexistent
237         print_critical nonexistent
238     fi
239
240     # Common flags for some OS
241     if [ "$OPERSYS" = "FreeBSD" ] || [ "$OPERSYS" = "OpenBSD" ] ||
242     [ "$OPERSYS" = "NetBSD" ] || [ "$OPERSYS" = "Darwin" ]; then
243         echo "idle.label idle"
244         echo "idle.draw STACK"
245         echo "idle.colour $IDLE"
246         echo "idle.info The number of threads that are idle (sleeping for longer than about 20 seconds)."
247         print_warning idle
248         print_critical idle
249         echo "sleeping.label sleeping"
250         echo "sleeping.draw AREA"
251         echo "sleeping.colour $SLEEPING"
252         echo "sleeping.info The number of threads that are sleeping for less than about 20 seconds."
253         print_warning sleeping
254         print_critical sleeping
255     elif [ "$OPERSYS" = "Linux" ]; then
256         echo "idle.label idle"
257         echo "idle.draw STACK"
258         echo "idle.colour $IDLE"
259         echo "idle.info The number of idle kernel threads (>= 4.2 kernels only)."
260         print_warning idle
261         print_critical idle
262         echo "sleeping.label sleeping"
263         echo "sleeping.draw AREA"
264         echo "sleeping.colour $SLEEPING"
265         echo "sleeping.info The number of sleeping threads."
266         print_warning sleeping
267         print_critical sleeping
268     elif [ "$OPERSYS" = "SunOS" ] || [ "$OPERSYS" = "HP-UX" ]; then
269         echo "sleeping.label sleeping"
270         echo "sleeping.draw AREA"
271         echo "sleeping.colour $SLEEPING"
272         echo "sleeping.info The number of sleeping threads."
273         print_warning sleeping
274         print_critical sleeping
275     fi
276
277     if [ "$OPERSYS" = "Linux" ] || [ "$OPERSYS" = "FreeBSD" ] ||
278     [ "$OPERSYS" = "OpenBSD" ] || [ "$OPERSYS" = "NetBSD" ]; then
279         echo "uninterruptible.label uninterruptible"
280         echo "uninterruptible.draw STACK"
281         echo "uninterruptible.colour $UNINTERRUPTIBLE"
282         echo "uninterruptible.info The number of uninterruptible threads (usually IO)."
283         print_warning uninterruptible
284         print_critical uninterruptible
285     fi
286
287     # Common (non-cygwin) flags
288     if [ "$OPERSYS" != "CYGWIN" ]; then
289         echo "stopped.label stopped"
290         echo "stopped.draw STACK"
291         echo "stopped.colour $STOPPED"
292         echo "stopped.info The number of stopped or traced threads."
293         print_warning stopped
294         print_critical stopped
295
296         echo "runnable.label runnable"
297         echo "runnable.draw STACK"
298         echo "runnable.colour $RUNNABLE"
299         echo "runnable.info The number of runnable threads (on the run queue)."
300         print_warning runnable
301         print_critical runnable
302     fi
303
304     if [ "$OPERSYS" != "CYGWIN" ] && [ "$OPERSYS" != "HP-UX" ]; then
305         echo "zombie.label zombie"
306         echo "zombie.draw STACK"
307         echo "zombie.colour $ZOMBIE"
308         echo "zombie.info The number of defunct ('zombie') threads (process terminated and parent not waiting)."
309         print_warning zombie
310         print_critical zombie
311     fi
312
313     if [ "$OPERSYS" != "SunOS" ]; then
314     # Not using 'graph_total' due to backwards compability. SunOS uses 'total'.
315         #echo 'graph_total total'
316         echo "threads.label total"
317         echo "threads.draw LINE1"
318         echo "threads.colour $TOTAL"
319         echo "threads.info The total number of threads."
320     print_warning threads
321     print_critical threads
322     fi
323
324     exit 0
325 fi
326
327 if [ "$OPERSYS" = "Linux" ]; then
328     # shellcheck disable=SC2016
329     "$ps" --no-header -eTo s | "$awk" '
330 { threads++; stat[$1]++ }
331 END {
332 print "threads.value "        0+threads;
333 print "uninterruptible.value "  0+stat["D"];
334 print "runnable.value "         0+stat["R"];
335 print "sleeping.value "         0+stat["S"];
336 print "idle.value "             0+stat["I"];
337 print "stopped.value "          0+stat["T"];
338 print "paging.value "           0+stat["W"];
339 print "dead.value "             0+stat["X"];
340 print "zombie.value "           0+stat["Z"];
341 }'
342
343 #elif [ "$OPERSYS" = "SunOS" ]; then
344 #    # shellcheck disable=SC2016
345 #    "$ps" -e -o s | "$awk" '
346 #{ total++; stat[$1]++ }
347 #END {
348 #print "total.value "    0+total;
349 #print "running.value "  0+stat["O"];
350 #print "sleeping.value " 0+stat["S"];
351 #print "runnable.value " 0+stat["R"];
352 #print "stopped.value "  0+stat["T"];
353 #print "zombie.value "   0+stat["Z"];
354 #}'
355 #elif [ "$OPERSYS" = "FreeBSD" ]; then
356 #    # shellcheck disable=SC2016
357 #    "$ps" -axo state= | sed -e 's/^\(.\).*/\1/' | "$awk" '
358 #{ threads++; stat[$1]++ }
359 #END {
360 #print "threads.value "        0+threads;
361 #print "uninterruptible.value "  0+stat["D"];
362 #print "idle.value "             0+stat["I"];
363 #print "lock.value "             0+stat["G"];
364 #print "runnable.value "         0+stat["R"];
365 #print "sleeping.value "         0+stat["S"];
366 #print "stopped.value "          0+stat["T"];
367 #print "interrupt.value "        0+stat["W"];
368 #print "zombie.value "           0+stat["Z"];
369 #}'
370 #elif [ "$OPERSYS" = "OpenBSD" ]; then
371 #    # First line is header. Remove it.
372 #    # shellcheck disable=SC2016
373 #    "$ps" -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | "$awk" '
374 #{ threads++; stat[$1]++ }
375 #END {
376 #print "threads.value "        0+threads;
377 #print "uninterruptible.value "  0+stat["D"];
378 #print "idle.value "             0+stat["I"];
379 #print "runnable.value "         0+stat["R"];
380 #print "sleeping.value "         0+stat["S"];
381 #print "stopped.value "          0+stat["T"];
382 #print "zombie.value "           0+stat["Z"];
383 #}'
384 #elif [ "$OPERSYS" = "NetBSD" ]; then
385 #    # First line is header. Remove it.
386 #    # shellcheck disable=SC2016
387 #    "$ps" -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | "$awk" '
388 #{ threads++; stat[$1]++ }
389 #END {
390 #print "threads.value "        0+threads;
391 #print "uninterruptible.value "  0+stat["D"];
392 #print "idle.value "             0+stat["I"];
393 #print "suspended.value "        0+stat["U"];
394 #print "runnable.value "         0+stat["R"];
395 #print "sleeping.value "         0+stat["S"];
396 #print "stopped.value "          0+stat["T"];
397 #print "zombie.value "           0+stat["Z"];
398 #}'
399 #
400 #elif [ "$OPERSYS" = "Darwin" ]; then
401 #    # First line is header. Remove it.
402 #    # shellcheck disable=SC2016
403 #    "$ps" -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | "$awk" '
404 #{ threads++; stat[$1]++ }
405 #END {
406 #print "threads.value "        0+threads;
407 #print "uninterruptible.value "  0+stat["U"];
408 #print "idle.value "             0+stat["I"];
409 #print "runnable.value "         0+stat["R"];
410 #print "sleeping.value "         0+stat["S"];
411 #print "stopped.value "          0+stat["T"];
412 #print "zombie.value "           0+stat["Z"];
413 #}'
414 #
415 #elif [ "$OPERSYS" = "CYGWIN" ]; then
416 #    # First line is header. Remove it. Also remove WINPID duplicates.
417 #    # shellcheck disable=SC2016
418 #    "$ps" -aW | sed '1d' | cut -c 30-36 | sort -u | "$awk" '
419 #{ threads++; }
420 #END {
421 #print "threads.value "        0+threads;
422 #}'
423 #
424 #elif [ "$OPERSYS" = "HP-UX" ]; then
425 #    # First line is header. Remove it.
426 #    # shellcheck disable=SC2016
427 #    "$ps" -el | sed '1d' | "$awk" '{print $2}' | "$awk" '
428 #{ threads++; stat[$1]++ }
429 #END {
430 #print "threads.value "        0+threads;
431 #print "nonexistent.value "      0+stat["0"];
432 #print "sleeping.value "         0+stat["S"];
433 #print "waiting.value "          0+stat["W"];
434 #print "runnable.value "         0+stat["R"];
435 #print "intermediate.value "     0+stat["I"];
436 #print "terminated.value "       0+stat["Z"];
437 #print "stopped.value "          0+stat["T"];
438 #print "growing.value "          0+stat["X"];
439 #}'
440
441 fi