Projet

Général

Profil

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

root / plugins / jvm / jstat__heap @ f0548a37

Historique | Voir | Annoter | Télécharger (5,11 ko)

1
#!/bin/sh
2
#
3
# Plugin for monitor JVM activity - Heap Usage -
4
#
5
# Usage:
6
#
7
#       Symlink into /etc/munin/plugins/ and add the monitored
8
#	alias name like :
9
#
10
#       ln -s /usr/share/munin/plugins/jstat__heap \
11
#	  /etc/munin/plugins/jstat_<jvm alias>_heap
12
#       This should, however, be given through autoconf and suggest.
13
#
14
# Requirements:
15
#
16
#	You need to execute your Java program under jsvc provided by
17
#	  http://jakarta.apache.org/commons/daemon/
18
#	which enables you to run your Java program with specified
19
#	pid file with -pidfile option.
20
#       A Brief setup documentation is also available at
21
#         http://tomcat.apache.org/tomcat-5.5-doc/setup.html
22
#
23
# Target:
24
#
25
#       Target Java Virtual Machine to monitor are:
26
#         Sun JDK 5.0 (http://java.sun.com/javase/)
27
#         Sun JDK 8.0 (http://java.sun.com/javase/)
28
#         OpenJDK 1.7 .. 11 (https://openjdk.java.net/)
29
#         BEA JRockit 5.0 (http://dev2dev.bea.com/jrockit/)
30
#
31
# Parameters:
32
#
33
#       config   (required)
34
#
35
# Config variables:
36
#
37
#       pidfilepath  - Which file path use. Defaults to '/var/run/jsvc.pid'
38
#       javahome     - override automatic detection of JRE directory
39
#       graphtitle   - Title of the graph (defaults to PID file location)
40
#
41

    
42
default_java_home=/usr/lib/jvm/default-java
43
[ -e "$default_java_home" ] || default_java_home=/usr/local/java/jdk
44

    
45
pidfilepath=${pidfilepath:-/var/run/jsvc.pid}
46
graphtitle=${graphtitle:-$pidfilepath}
47
JAVA_HOME=${javahome:-$default_java_home}
48

    
49
export JAVA_HOME
50

    
51

    
52
get_jdk_type() {
53
    local version
54
    if "${JAVA_HOME}/bin/java" -version 2>&1 | grep -qi 'jrockit'; then
55
        echo "bea"
56
    else
57
        echo "sun"
58
  fi
59
}
60

    
61

    
62
print_config() {
63
    echo "graph_title Heap Usage $graphtitle"
64
    echo "graph_args --base 1024 -l 0"
65
    echo "graph_vlabel Heap Usage(Bytes)"
66
    echo "graph_info Heap Usage"
67
    echo "graph_category virtualization"
68

    
69
    if [ "${JDK_TYPE}" = "bea" ]; then
70
        echo "NurserySize.label NurserySize"
71
        echo "HeapSize.label HeapSize"
72
        echo "UsedHeapSize.label UsedHeapSize"
73
        echo "NurserySize.draw AREA"
74
        echo "HeapSize.draw STACK"
75
        echo "UsedHeapSize.draw STACK"
76
    else
77
        echo "Eden_Used.label Eden_Used"
78
        echo "Eden_Free.label Eden_Free"
79
        echo "Survivor0_Used.label Survivor0_Used"
80
        echo "Survivor0_Free.label Survivor0_Free"
81
        echo "Survivor1_Used.label Survivor1_Used"
82
        echo "Survivor1_Free.label Survivor1_Free"
83
        echo "Old_Used.label Old_Used"
84
        echo "Old_Free.label Old_Free"
85
        echo "Permanent_Used.label Permanent_Used"
86
        echo "Permanent_Free.label Permanent_Free"
87
        echo "Eden_Used.draw AREA"
88
        echo "Eden_Free.draw STACK"
89
        echo "Survivor0_Used.draw STACK"
90
        echo "Survivor0_Free.draw STACK"
91
        echo "Survivor1_Used.draw STACK"
92
        echo "Survivor1_Free.draw STACK"
93
        echo "Old_Used.draw STACK"
94
        echo "Old_Free.draw STACK"
95
        echo "Permanent_Used.draw STACK"
96
        echo "Permanent_Free.draw STACK"
97
    fi
98
}
99

    
100

    
101
print_stats() {
102
    local pid_num="$1"
103
    local awk_script
104
    if [ "${JDK_TYPE}" = "bea" ]; then
105
        # shellcheck disable=SC2016
106
        awk_script='{
107
            HeapSize = $1;
108
            NurserySize = $2;
109
            UsedHeapSize = $3;
110
            print "NurserySize.value " NurserySize * 1024;
111
            print "HeapSize.value " HeapSize * 1024;
112
            print "UsedHeapSize.value " UsedHeapSize * 1024; }'
113
    else
114
        # List & Order of columns of jstat changes with java versions
115
        # idx["YGC"] is index of YGC column in output (i.e. 13)
116
        # $idx["YGC"] then accesses the value at this position (taken from 2nd line of the output)
117
        # shellcheck disable=SC2016
118
        awk_script='
119
	    NR==1 { 
120
                for (i=1;i<=NF;i++) idx[$i]=i
121
            }
122
            NR==2 {
123
                S0F = $idx["S0C"] - $idx["S0U"];
124
                S1F = $idx["S1C"] - $idx["S1U"];
125
                EF  = $idx["EC"]  - $idx["EU"];
126
                OF  = $idx["OC"]  - $idx["OU"];
127
                MF  = $idx["MC"]  - $idx["MU"];
128
                print "Eden_Used.value " $idx["EU"] * 1024;
129
                print "Eden_Free.value " EF * 1024;
130
                print "Survivor0_Used.value " $idx["S0U"] * 1024;
131
                print "Survivor0_Free.value " S0F * 1024;
132
                print "Survivor1_Used.value " $idx["S1U"] * 1024;
133
                print "Survivor1_Free.value " S1F * 1024;
134
                print "Old_Used.value " $idx["OU"] * 1024;
135
                print "Old_Free.value " OF * 1024;
136
                print "Permanent_Used.value " $idx["MU"] * 1024;
137
                print "Permanent_Free.value " MF * 1024;
138
            }'
139
    fi
140
    "${JAVA_HOME}/bin/jstat" -gc "$pid_num" | awk "$awk_script"
141
}
142

    
143

    
144
#
145
# autoconf
146
#
147
if [ "$1" = "autoconf" ]; then
148

    
149
  if [ ! -x "${JAVA_HOME}/bin/jstat" ]; then
150
    echo "no (No jstat found in ${JAVA_HOME}/bin)"
151
  elif [ ! -f "$pidfilepath" ]; then
152
    echo "no (missing file $pidfilepath)"
153
  elif [ ! -r "$pidfilepath" ]; then
154
    echo "no (cannot read $pidfilepath)"
155
  else
156
    echo "yes"
157
  fi
158
  exit 0
159
fi
160

    
161

    
162
JDK_TYPE=$(get_jdk_type)
163

    
164

    
165
if [ "$1" = "config" ]; then
166
    print_config
167
fi
168

    
169
print_stats "$(cat "$pidfilepath")"