Projet

Général

Profil

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

root / plugins / cpu / cpu_osx @ 0b7a005e

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

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)}')