summaryrefslogtreecommitdiffstats
path: root/agents/ocf/SysInfo.in
blob: 5c2c7c7047a4325d57f698d43f38597f8132ae5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!@BASH_PATH@
#
# ocf:pacemaker:SysInfo resource agent
#
# Original copyright 2004 SUSE LINUX AG, Lars Marowsky-Br<E9>e
# Later changes copyright 2008-2023 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# (GPLv2) WITHOUT ANY WARRANTY.
#

#
# This agent records (in the CIB) various attributes of a node
#
#######################################################################
# Initialization:

: ${OCF_FUNCTIONS:="${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs"}
. "${OCF_FUNCTIONS}"
: ${__OCF_ACTION:="$1"}

# Explicitly list all environment variables used, to make static analysis happy
: ${OCF_RESKEY_CRM_meta_globally_unique:="false"}
: ${OCF_RESKEY_pidfile:="${HA_VARRUN%%/}/SysInfo-${OCF_RESOURCE_INSTANCE}"}
: ${OCF_RESKEY_disk_unit:="G"}
: ${OCF_RESKEY_clone:="0"}
: ${OCF_RESKEY_disks:=""}
: ${OCF_RESKEY_delay:=""}
: ${OCF_RESKEY_min_disk_free:=""}

#######################################################################

meta_data() {
    cat <<END
<?xml version="1.0"?>
<resource-agent name="SysInfo" version="@VERSION@">
<version>1.1</version>

<longdesc lang="en">
This is a SysInfo Resource Agent.
It records (in the CIB) various attributes of a node
Sample Linux output:
   arch:   i686
   os:     Linux-2.4.26-gentoo-r14
   free_swap:      1999
   cpu_info:       Intel(R) Celeron(R) CPU 2.40GHz
   cpu_speed:      4771.02
   cpu_cores:      1
   cpu_load:       0.00
   ram_total:      513
   ram_free:       117
   root_free:      2.4
   #health_disk:   red

Sample Darwin output:
   arch:   i386
   os:     Darwin-8.6.2
   cpu_info:       Intel Core Duo
   cpu_speed:      2.16
   cpu_cores:      2
   cpu_load:       0.18
   ram_total:      2016
   ram_free:       787
   root_free:      13
   #health_disk:   green

Units:
   free_swap: MB
   ram_*:     MB
   cpu_speed (Linux): bogomips
   cpu_speed (Darwin): GHz
   *_free:    GB (or user-defined: disk_unit)

</longdesc>
<shortdesc lang="en">SysInfo resource agent</shortdesc>

<parameters>

<parameter name="pidfile" unique-group="pidfile">
<longdesc lang="en">PID file</longdesc>
<shortdesc lang="en">PID file</shortdesc>
<content type="string" default="$OCF_RESKEY_pidfile" />
</parameter>

<parameter name="delay">
<longdesc lang="en">Interval to allow values to stabilize</longdesc>
<shortdesc lang="en">Dampening Delay</shortdesc>
<content type="string" default="0s" />
</parameter>

<parameter name="disks" reloadable="1">
<longdesc lang="en">
Filesystems or Paths to be queried for free disk space as a SPACE
separated list - e.g "/dev/sda1 /tmp".
Results will be written to an attribute with leading slashes
removed, and other slashes replaced with underscore, and the word
'free' appended - e.g for /dev/sda1 it would be 'dev_sda1_free'.
Note: The root filesystem '/' is always queried to an attribute
named 'root_free'
</longdesc>
<shortdesc lang="en">List of Filesytems/Paths to query for free disk space</shortdesc>
<content type="string" />
</parameter>

<parameter name="disk_unit" reloadable="1">
<longdesc lang="en">
Unit to report disk free space in.
Can be one of: B, K, M, G, T, P (case-insensitive)
</longdesc>
<shortdesc lang="en">Unit to report disk free space in</shortdesc>
<content type="select" default="G">
  <option value="B" />
  <option value="b" />
  <option value="K" />
  <option value="k" />
  <option value="M" />  
  <option value="m" />
  <option value="G" />
  <option value="g" />
  <option value="T" />
  <option value="t" />
  <option value="P" />
  <option value="p" />
</content>
</parameter>

<parameter name="min_disk_free" reloadable="1">
<longdesc lang="en">
The amount of free space required in monitored disks. If any
of the monitored disks has less than this amount of free space,
, with the node attribute "#health_disk" changing to "red",
all resources will move away from the node. Set the node-health-strategy
property appropriately for this to take effect.
If the unit is not specified, it defaults to disk_unit.
</longdesc>
<shortdesc lang="en">minimum disk free space required</shortdesc>
<content type="string" default=""/>
</parameter>


</parameters>
<actions>
<action name="start"   timeout="20s" />
<action name="stop"    timeout="20s" />
<action name="monitor" timeout="20s" interval="60s"/>
<action name="reload-agent" timeout="20s" />
<action name="meta-data"  timeout="5s" />
<action name="validate-all"  timeout="30s" depth="0" />
</actions>
</resource-agent>
END
}

#######################################################################

UpdateStat() {
    name="$1"; shift
    value="$*"
    printf "%s:\t%s\n" "$name" "$value"
    if [ "$__OCF_ACTION" = "start" ] ; then
        "${HA_SBIN_DIR}/attrd_updater" ${OCF_RESKEY_delay} -S status -n $name -B "$value"
    else
        "${HA_SBIN_DIR}/attrd_updater" ${OCF_RESKEY_delay} -S status -n $name -v "$value"
    fi
}

SysInfoStats() {
    local DISK_STATUS="green"

    UpdateStat arch "$(uname -m)"
    UpdateStat os "$(uname -s)-$(uname -r)"

    case $(uname -s) in
        "Darwin")
            mem=$(top -l 1 | grep Mem: | awk '{print $10}')
            mem_used=$(top -l 1 | grep Mem: | awk '{print $8}')
            mem=$(SysInfo_mem_units "$mem")
            mem_used=$(SysInfo_mem_units "$mem_used")
            mem_total=$(expr $mem_used + $mem)
            cpu_type=$(system_profiler SPHardwareDataType | awk -F': ' '/^CPU Type/ {print $2; exit}')
            cpu_speed=$(system_profiler SPHardwareDataType | awk -F': ' '/^CPU Speed/ {print $2; exit}')
            cpu_cores=$(system_profiler SPHardwareDataType | awk -F': ' '/^Number Of/ {print $2; exit}')
            cpu_load=$(uptime | awk -F 'load average: ' '{ print $2 }' | awk -F ', ' '{ print $2 }')
        ;;
        "FreeBSD")
            cpu_type=$(sysctl -in hw.model)
            cpu_speed=$(sysctl -in dev.cpu.0.freq)
            cpu_cores=$(sysctl -in hw.ncpu)
            cpu_load=$(sysctl -in vm.loadavg | awk '{ print $4 }')

            free_pages=$(sysctl -in vm.stats.vm.v_free_count)
            page_count=$(sysctl -in vm.stats.vm.v_page_count)
            page_size=$(sysctl -in vm.stats.vm.v_page_size)

            mem=$(expr $free_pages \* $page_size / 1024 / 1024)M
            mem_total=$(expr $page_count \* $page_size / 1024 / 1024)M
        ;;
        "Linux")
            if [ -f /proc/cpuinfo ]; then
                cpu_type=$(awk -F': ' '/model name/ {print $2; exit}' /proc/cpuinfo)
                cpu_speed=$(awk -F': ' '/bogomips/ {print $2; exit}' /proc/cpuinfo)
                cpu_cores=$(grep "^processor" /proc/cpuinfo | wc -l)
            fi
            cpu_load=$(uptime | awk -F 'load average: ' '{ print $2 }' | awk -F ', ' '{ print $2 }')

            if [ -f /proc/meminfo ]; then
                # meminfo results are in kB
                mem=$(grep "SwapFree" /proc/meminfo | awk '{print $2"k"}')
                if [ -n "$mem" ]; then
                    UpdateStat free_swap "$(SysInfo_mem_units "$mem")"
                fi
                mem=$(grep "Inactive" /proc/meminfo | awk '{print $2"k"}')
                mem_total=$(grep "MemTotal" /proc/meminfo | awk '{print $2"k"}')
            else
                mem=$(top -n 1 | grep Mem: | awk '{print $7}')
            fi
            ;;
        *)
    esac

    if [ -n "$cpu_type" ]; then
        UpdateStat cpu_info "$cpu_type"
    fi

    if [ -n "$cpu_speed" ]; then
        UpdateStat cpu_speed "$cpu_speed"
    fi

    if [ -n "$cpu_cores" ]; then
        UpdateStat cpu_cores "$cpu_cores"
    fi

    if [ -n "$cpu_load" ]; then
        UpdateStat cpu_load "$cpu_load"
    fi

    if [ -n "$mem" ]; then
        # Massage the memory values
        UpdateStat ram_total "$(SysInfo_mem_units "$mem_total")"
        UpdateStat ram_free "$(SysInfo_mem_units "$mem")"
    fi

    # Portability notes:
    #   o tail: explicit "-n" not available in Solaris; instead simplify
    #     'tail -n <c>' to the equivalent 'tail -<c>'.
    for disk in "/" ${OCF_RESKEY_disks}; do
        unset disk_free disk_label
        disk_free=$(df -h "${disk}" | tail -1 | awk '{print $4}')
        if [ -n "$disk_free" ]; then
            disk_label=$(echo $disk | sed -e 's#^/$#root#;s#^/*##;s#/#_#g')
            disk_free=$(SysInfo_hdd_units "$disk_free")
            UpdateStat "${disk_label}_free" $disk_free
            if [ -n "$MIN_FREE" ] && [ $disk_free -le $MIN_FREE ]; then
                DISK_STATUS="red"
            fi
        fi
    done
    UpdateStat "#health_disk" "$DISK_STATUS"
}

SysInfo_megabytes() {
    # Size in megabytes
    echo $1 | awk '{ n = $0;
        sub( /[0-9]+(.[0-9]+)?/, "" );
        if ( $0 == "" ) { $0 = "G" }; # Do not change previous behavior `if ($0 == "G" || $0 == "") { n *= 1024 };`
        split( n, a, $0 );
        n = a[1];
        if ( /^[pP]i?[bB]?/ ) { n *= 1024 * 1024 * 1024 };
        if ( /^[tT]i?[bB]?/ ) { n *= 1024 * 1024 };
        if ( /^[gG]i?[bB]?/ ) { n *= 1024 };
        if ( /^[mM]i?[bB]?/ ) { n *= 1 };
        if ( /^[kK]i?[bB]?/ ) { n /= 1024 };
        if ( /^[bB]i?/ )      { n /= 1024 * 1024 };
        printf "%d\n", n }' # Intentionally round to an integer
}

SysInfo_mem_units() {
    mem="$1"

    if [ -z "$1" ]; then
        return
    fi

    mem=$(SysInfo_megabytes "$1")
    # Round to the next multiple of 50
    r=$(($mem % 50))
    if [ $r -ne 0 ]; then
        mem=$(($mem + 50 - $r))
    fi

    echo $mem
}

SysInfo_hdd_units() {
    # Defauts to size in gigabytes

    case "$OCF_RESKEY_disk_unit" in
        [Pp]) echo $(($(SysInfo_megabytes "$1") / 1024 / 1024 / 1024));;
        [Tt]) echo $(($(SysInfo_megabytes "$1") / 1024 / 1024));;
        [Gg]) echo $(($(SysInfo_megabytes "$1") / 1024));;
        [Mm]) echo "$(SysInfo_megabytes "$1")" ;;
        [Kk]) echo $(($(SysInfo_megabytes "$1") * 1024));;
        [Bb]) echo $(($(SysInfo_megabytes "$1") * 1024 * 1024));;
        *)
            ocf_log err "Invalid value for disk_unit: $OCF_RESKEY_disk_unit"
            echo $(($(SysInfo_megabytes "$1") / 1024));;
    esac
}

SysInfo_usage() {
    cat <<END
usage: $0 {start|stop|monitor|reload-agent|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

SysInfo_start() {
    echo $OCF_RESKEY_clone > "$OCF_RESKEY_pidfile"
    SysInfoStats
    exit $OCF_SUCCESS
}

SysInfo_stop() {
    rm "$OCF_RESKEY_pidfile"
    exit $OCF_SUCCESS
}

SysInfo_monitor() {
    if [ -f "$OCF_RESKEY_pidfile" ]; then
        clone=$(cat "$OCF_RESKEY_pidfile")
    fi

    if [ -z "$clone" ]; then
        rm "$OCF_RESKEY_pidfile"
        exit $OCF_NOT_RUNNING

    elif [ "$clone" = "$OCF_RESKEY_clone" ]; then
        SysInfoStats
        exit $OCF_SUCCESS

    elif ocf_is_true "$OCF_RESKEY_CRM_meta_globally_unique"; then
        SysInfoStats
        exit $OCF_SUCCESS
    fi
    exit $OCF_NOT_RUNNING
}

SysInfo_reload_agent() {
    # No action required
    :;
}

SysInfo_validate() {
    # If specified, is min_disk_free a non-negative integer followed by a disk unit?

    pat="[1-9][0-9]*[KkBbMmGgTtPp]?"
    if [[ ! $OCF_RESKEY_min_disk_free =~ $pat ]]; then
        return $OCF_ERR_CONFIGURED
    fi

}

if [ $# -ne 1 ]; then
    SysInfo_usage
    exit $OCF_ERR_ARGS
fi

if [ -n "${OCF_RESKEY_delay}" ]; then
    OCF_RESKEY_delay="-d ${OCF_RESKEY_delay}"
else
    OCF_RESKEY_delay="-d 0"
fi
MIN_FREE=""
if [ -n "$OCF_RESKEY_min_disk_free" ]; then
    ocf_is_decimal "$OCF_RESKEY_min_disk_free" &&
        OCF_RESKEY_min_disk_free="$OCF_RESKEY_min_disk_free$OCF_RESKEY_disk_unit"
    MIN_FREE=$(SysInfo_hdd_units $OCF_RESKEY_min_disk_free)
fi

case "$__OCF_ACTION" in
meta-data)      meta_data
                exit $OCF_SUCCESS
                ;;
start)          SysInfo_start
                ;;
stop)           SysInfo_stop
                ;;
monitor)        SysInfo_monitor
                ;;
reload-agent)   SysInfo_reload_agent
                ;;
validate-all)   SysInfo_validate
                ;;
usage|help)     SysInfo_usage
                exit $OCF_SUCCESS
                ;;
*)              SysInfo_usage
                exit $OCF_ERR_UNIMPLEMENTED
                ;;
esac

exit $?

# vim: set filetype=sh expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80: