Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / user / procbyuser @ 743395a6

Historique | Voir | Annoter | Télécharger (2,26 ko)

1
#!/bin/sh
2
# -*- sh -*-
3

    
4
: << =cut
5

    
6
=head1 NAME
7

    
8
procbyuser - Plugin to monitor the amount of processes owned by a user
9

    
10
=head1 INSTALLATION
11

    
12
Place in /etc/munin/plugins/ (or link it there using ln -s)
13

    
14
=head1 CONFIGURATION
15

    
16
Add this to your /etc/munin/plugin-conf.d/munin-node:
17

    
18
 [procbyuser]
19
 user root               # required if /proc can't be read from by any user!
20
 env.USERS root yann     # defaults to ALL, i.e. display all users. 'root' is counted as one of the 'others'
21
 env.OTHER_FIELD others  # enable 'others'-list, set the label/field name
22

    
23
=head1 AUTHORS
24

    
25
Copyright (C) 2019 pcy <pcy@ulyssis.org>
26

    
27
Based on the plugin "cpubyuser".
28

    
29
=head1 MAGIC MARKERS
30

    
31
 #%# family=auto
32
 #%# capabilities=autoconf
33

    
34
=cut
35

    
36
. "$MUNIN_LIBDIR/plugins/plugin.sh"
37

    
38
[ -z "$USERS" ] && USERS=ALL
39
if [ "$USERS" = "ALL" ]; then
40
	USERS="$(ps ax --format uname | tail +2 | sort -u | grep -v -e '^root$')"
41
fi
42

    
43
if [ "$1" = "autoconf" ]; then
44
	echo "yes"
45
fi
46

    
47
if [ "$1" = "config" ]; then
48
	echo "graph_args -r --lower-limit 0"
49
	echo "graph_title Processes per user"
50
	echo "graph_category processes"
51
	echo "graph_info This graph shows the amount of processes owned by a user."
52
	echo "graph_vlabel processes"
53
	echo "graph_scale no"
54
	echo "graph_period second"
55
	user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
56
	echo "graph_order $user_fields $(clean_fieldname "$OTHER_FIELD")"
57
	for user in $USERS "$OTHER_FIELD"; do
58
		if [ -n "$user" ]; then
59
			user_field="$(clean_fieldname "$user")"
60
			echo "${user_field}.label $user"
61
			echo "${user_field}.info processes of user $user"
62
			echo "${user_field}.type GAUGE"
63
			echo "${user_field}.draw AREASTACK"
64
		fi
65
	done
66
	exit
67
fi
68

    
69
OTHER_PRINT=""
70
[ -z "$OTHER_FIELD" ] || OTHER_PRINT="print \"$(clean_fieldname "$OTHER_FIELD")\", others_sum;"
71

    
72
ps ax -o user:42= -o cmd= | \
73
	awk -v USERS="$USERS" '
74
		!($2 ~ /^\[/) { NUSER[$1]=NUSER[$1]+1 } # filter away kernel threads
75
		END {
76
			others_sum = 0
77
			split(USERS, user_array)
78
			for (user in NUSER) {
79
				m = match(USERS,user)
80
				if (m != 0) {
81
					_user=user
82
					gsub(/[-.]/,"_",_user);
83
					print _user, (NUSER[user])
84
				} else
85
					others_sum += NUSER[user]
86
			}
87
			'"$OTHER_PRINT"'
88
		}' | while read -r user count; do
89
			# apply fieldname cleanup
90
			echo "$(clean_fieldname "$user").value $count"
91
		done