Projet

Général

Profil

Révision 0b7a005e

ID0b7a005eb26ba95b65527d8fbe65c00ae7a88ef5
Parent 0d13497e
Enfant ed61eab1

Ajouté par Sam Darwin il y a presque 5 ans

cpu and memory plugins for OSX (#1173)

Voir les différences:

plugins/cpu/cpu_osx
1
#!/bin/sh
2
# -*- sh -*-
3

  
4
# shellcheck disable=SC2046
5

  
6
: << =cut
7

  
8
=head1 NAME
9

  
10
cpu - Plugin to measure cpu on osx.
11

  
12
=head1 NOTES
13

  
14
This plugin runs the top command once per interval, to discover the current value of CPU usage on OSX.
15
The result is scaled to # of CPU's, so a 4 core machine will reach 400% utilization (unless $scaleto100 is set to "yes", in which case the maximum will be 100%).
16

  
17
Contributions are welcome to convert the plugin to use a long running counter such as /proc/stat in Linux.
18

  
19
=head1 LICENSE
20

  
21
GPLv2
22

  
23
=head1 MAGIC MARKERS
24

  
25
 #%# family=auto
26
 #%# capabilities=autoconf
27

  
28
=cut
29

  
30
if [ "$1" = "autoconf" ]; then
31
    if [ "$(uname)" = "Darwin" ]; then
32
        echo yes
33
        exit 0
34
    else
35
        echo "no (uname does not report 'Darwin')"
36
        exit 0
37
    fi
38
fi
39

  
40
scaleto100=${scaleto100:-}
41

  
42
if [ "$scaleto100" = "yes" ]; then
43
    NCPU="1"
44
else
45
    NCPU=$(sysctl hw.ncpu | cut -d" " -f2)
46
fi
47

  
48
if [ "$1" = "config" ]; then
49
        graphlimit=$(( NCPU*100 ))
50
        echo 'graph_title CPU usage'
51
        echo "graph_order system user idle"
52
        echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit"
53
        echo 'graph_scale no'
54
        echo 'graph_vlabel %'
55
        echo 'graph_category system'
56
        echo 'system.label system'
57
        echo 'system.draw AREA'
58
        echo 'system.min 0'
59
        echo "system.info CPU time spent by the kernel in system activities"
60
        echo 'user.label user'
61
        echo 'user.draw STACK'
62
        echo 'user.min 0'
63
        echo 'user.info CPU time spent by normal programs and daemons'
64
        echo 'idle.label idle'
65
        echo 'idle.draw STACK'
66
        echo 'idle.min 0'
67
        echo 'idle.info Idle CPU time'
68

  
69
        exit 0
70
fi
71

  
72
# The second cpu reading is more accurate than the first, so "-l 2":
73
TOPINFO=$(top -l 2 | grep "CPU usage: " | tail -n 1)
74

  
75
CPU_USER=$(echo "$TOPINFO" | awk '/CPU usage: / { print substr($3, 1, length($3)-1) }')
76
CPU_SYS=$(echo "$TOPINFO" | awk '/CPU usage: / { print substr($5, 1, length($5)-1) }') 
77
CPU_IDLE=$(echo "$TOPINFO" | awk '/CPU usage: / { print substr($7, 1, length($7)-1) }') 
78
echo "system.value" $(echo "$NCPU" "$CPU_SYS" | awk '{print($1 * $2)}')
79
echo "user.value" $(echo "$NCPU" "$CPU_USER" | awk '{print($1 * $2)}')
80
echo "idle.value" $(echo "$NCPU" "$CPU_IDLE" | awk '{print($1 * $2)}')
plugins/memory/memory_osx
1
#!/bin/sh
2
# -*- sh -*-
3

  
4
# shellcheck disable=SC2046
5

  
6
: << =cut
7

  
8
=head1 NAME
9

  
10
memory - Plugin to measure memory on osx.
11

  
12
=head1 NOTES
13

  
14
This plugin runs the top command once per interval, to discover memory usage on OSX.
15
Contributions are welcome to collect additional memory regions, if possible, such as buffers and caches.
16

  
17
=head1 LICENSE
18

  
19
GPLv2
20

  
21
=head1 MAGIC MARKERS
22

  
23
 #%# family=auto
24
 #%# capabilities=autoconf
25

  
26
=cut
27

  
28
if [ "$1" = "autoconf" ]; then
29
    if [ "$(uname)" = "Darwin" ]; then
30
        echo yes
31
        exit 0
32
    else
33
        echo "no (uname does not report 'Darwin')"
34
        exit 0
35
    fi
36
fi
37

  
38
TOTALMEM=$(sysctl hw.memsize | cut -d" " -f2)
39
graphlimit=$TOTALMEM
40

  
41
dehumanise() {
42
    echo "$1" | sed -e "s/K/*1024/g;s/M/*1024*1024/;s/G/*1024*1024*1024/;s/T/*1024*1024*1024*1024/"
43
}
44

  
45
if [ "$1" = "config" ]; then
46
        echo 'graph_title Memory'
47
        echo "graph_order used wired unused"
48
        echo "graph_args --base 1024 -r --lower-limit 0 --upper-limit $graphlimit"
49
        echo 'graph_vlabel Bytes'
50
        echo 'graph_category system'
51
        echo 'used.label used (not incl. wired)'
52
        echo 'used.draw AREA'
53
        echo 'used.min 0'
54
        echo "used.info Used memory, not including wired"
55
        echo 'wired.label wired'
56
        echo 'wired.draw STACK'
57
        echo 'wired.min 0'
58
        echo 'wired.info Wired memory'
59

  
60
        exit 0
61
fi
62

  
63
TOPINFO=$(top -l 1 | grep "PhysMem: ")
64
MEM_USED=$(echo "$TOPINFO" | awk '/PhysMem: / { print substr($2, 1, length($2)) }')
65
MEM_WIRED=$(echo "$TOPINFO" | awk '/PhysMem: / { print substr($4, 2, length($4)-1) }') 
66
echo "used.value" $(( $(dehumanise "$MEM_USED") - $(dehumanise "$MEM_WIRED") ))
67
echo "wired.value" $(( $(dehumanise "$MEM_WIRED") ))

Formats disponibles : Unified diff