~ubuntu-branches/debian/stretch/resource-agents/stretch

« back to all changes in this revision

Viewing changes to heartbeat/SysInfo

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2011-06-10 16:26:35 UTC
  • Revision ID: james.westby@ubuntu.com-20110610162635-yiy0vfopqw4trzgx
Tags: upstream-3.9.0
Import upstream version 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
#
 
4
#       SysInfo OCF Resource Agent
 
5
#       It records (in the CIB) various attributes of a node
 
6
#
 
7
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Br�e
 
8
#                    All Rights Reserved.
 
9
#
 
10
# This program is free software; you can redistribute it and/or modify
 
11
# it under the terms of version 2 of the GNU General Public License as
 
12
# published by the Free Software Foundation.
 
13
#
 
14
# This program is distributed in the hope that it would be useful, but
 
15
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
17
#
 
18
# Further, this software is distributed without any warranty that it is
 
19
# free of the rightful claim of any third person regarding infringement
 
20
# or the like.  Any license provided herein, whether implied or
 
21
# otherwise, applies only to this software file.  Patent licenses, if
 
22
# any, provided herein do not apply to combinations of this program with
 
23
# other software, or any other product whatsoever.
 
24
#
 
25
# You should have received a copy of the GNU General Public License
 
26
# along with this program; if not, write the Free Software Foundation,
 
27
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 
28
#
 
29
#######################################################################
 
30
# Initialization:
 
31
 
 
32
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
 
33
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
 
34
 
 
35
#######################################################################
 
36
 
 
37
meta_data() {
 
38
        cat <<END
 
39
<?xml version="1.0"?>
 
40
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
 
41
<resource-agent name="SysInfo">
 
42
<version>1.0</version>
 
43
 
 
44
<longdesc lang="en">
 
45
This is a SysInfo Resource Agent.
 
46
It records (in the CIB) various attributes of a node
 
47
Sample Linux output:
 
48
   arch:   i686
 
49
   os:     Linux-2.4.26-gentoo-r14
 
50
   free_swap:      1999
 
51
   cpu_info:       Intel(R) Celeron(R) CPU 2.40GHz
 
52
   cpu_speed:      4771.02
 
53
   cpu_cores:      1
 
54
   cpu_load:       0.00
 
55
   ram_total:      513
 
56
   ram_free:       117
 
57
   root_free:      2.4
 
58
 
 
59
Sample Darwin output:
 
60
   arch:   i386
 
61
   os:     Darwin-8.6.2
 
62
   cpu_info:       Intel Core Duo
 
63
   cpu_speed:      2.16
 
64
   cpu_cores:      2
 
65
   cpu_load:       0.18
 
66
   ram_total:      2016
 
67
   ram_free:       787
 
68
   root_free:      13
 
69
 
 
70
Units:
 
71
   free_swap: Mb
 
72
   ram_*:     Mb
 
73
   root_free: Gb
 
74
   cpu_speed (Linux): bogomips
 
75
   cpu_speed (Darwin): Ghz
 
76
 
 
77
</longdesc>
 
78
<shortdesc lang="en">Records various node attributes in the CIB</shortdesc>
 
79
 
 
80
<parameters>
 
81
 
 
82
<parameter name="pidfile" unique="0">
 
83
<longdesc lang="en">PID file</longdesc>
 
84
<shortdesc lang="en">PID file</shortdesc>
 
85
<content type="string" default="$OCF_RESKEY_pidfile" />
 
86
</parameter>
 
87
 
 
88
<parameter name="delay" unique="0">
 
89
<longdesc lang="en">Interval to allow values to stabilize</longdesc>
 
90
<shortdesc lang="en">Dampening Delay</shortdesc>
 
91
<content type="string" default="0s" />
 
92
</parameter>
 
93
 
 
94
</parameters>
 
95
<actions>
 
96
<action name="start"   timeout="20s" />
 
97
<action name="stop"    timeout="20s" />
 
98
<action name="monitor" timeout="20s" interval="60s"/>
 
99
<action name="meta-data"  timeout="5" />
 
100
<action name="validate-all"  timeout="20s" />
 
101
</actions>
 
102
</resource-agent>
 
103
END
 
104
}
 
105
 
 
106
#######################################################################
 
107
 
 
108
UpdateStat() {
 
109
    name=$1; shift
 
110
    value="$*"
 
111
    echo -e "$name:\t$value"
 
112
    ${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -S status -n $name -v "$value"
 
113
}
 
114
 
 
115
SysInfoStats() {
 
116
 
 
117
    UpdateStat arch "`uname -m`"
 
118
    UpdateStat os "`uname -s`-`uname -r`"
 
119
 
 
120
    case `uname -s` in
 
121
        "Darwin")
 
122
            mem=`top -l 1 | grep Mem: | awk '{print $10}'`
 
123
            mem_used=`top -l 1 | grep Mem: | awk '{print $8}'`
 
124
            mem=`SysInfo_mem_units $mem`
 
125
            mem_used=`SysInfo_mem_units $mem_used`
 
126
            mem_total=`expr $mem_used + $mem`
 
127
            cpu_type=`system_profiler SPHardwareDataType | grep "CPU Type:"`
 
128
            cpu_type=${cpu_type/*: /}
 
129
            cpu_speed=`system_profiler SPHardwareDataType | grep "CPU Speed:" | awk '{print $3}'`
 
130
            cpu_cores=`system_profiler SPHardwareDataType | grep "Number Of"`
 
131
            cpu_cores=${cpu_cores/*: /}
 
132
        ;;
 
133
        "Linux")
 
134
            if [ -f /proc/cpuinfo ]; then
 
135
                cpu_type=`grep "model name" /proc/cpuinfo | head -n 1`
 
136
                cpu_type=${cpu_type/*: /}
 
137
                cpu_speed=`grep "bogomips" /proc/cpuinfo | head -n 1`
 
138
                cpu_speed=${cpu_speed/*: /}
 
139
                cpu_cores=`grep "^processor" /proc/cpuinfo | wc -l`
 
140
            fi
 
141
 
 
142
            if [ -f /proc/meminfo ]; then
 
143
                # meminfo results are in kB
 
144
                mem=`grep "SwapFree" /proc/meminfo | awk '{print $2"k"}'`
 
145
                if [ ! -z $mem ]; then
 
146
                    UpdateStat free_swap `SysInfo_mem_units $mem`
 
147
                fi
 
148
                mem=`grep "Inactive" /proc/meminfo | awk '{print $2"k"}'`
 
149
                mem_total=`grep "MemTotal" /proc/meminfo | awk '{print $2"k"}'`
 
150
            else
 
151
                mem=`top -n 1 | grep Mem: | awk '{print $7}'`
 
152
            fi
 
153
            ;;
 
154
        *)
 
155
    esac
 
156
 
 
157
    if [ x != x"$cpu_type" ]; then
 
158
        UpdateStat cpu_info "$cpu_type"
 
159
    fi
 
160
 
 
161
    if [ x != x"$cpu_speed" ]; then
 
162
        UpdateStat cpu_speed "$cpu_speed"
 
163
    fi
 
164
 
 
165
    if [ x != x"$cpu_cores" ]; then
 
166
        UpdateStat cpu_cores "$cpu_cores"
 
167
    fi
 
168
 
 
169
    loads=`uptime`
 
170
    load15=`echo ${loads} | awk '{print $10}'`
 
171
    UpdateStat cpu_load $load15
 
172
 
 
173
    if [ ! -z "$mem" ]; then
 
174
        # Massage the memory values
 
175
        UpdateStat ram_total `SysInfo_mem_units $mem_total`
 
176
        UpdateStat ram_free `SysInfo_mem_units $mem`
 
177
    fi
 
178
 
 
179
    # Portability notes:
 
180
    #   o df:   -h flag not available on Solaris 8.  (OK on 9, 10, ...) #FIXME#
 
181
    #   o tail: explicit "-n" not available in Solaris; instead simplify
 
182
    #     'tail -n <c>' to the equivalent 'tail -<c>'.
 
183
    disk=`df -h / | tail -1 | awk '{print $4}'`
 
184
    if [ x != x"$disk" ]; then
 
185
        UpdateStat root_free `SysInfo_hdd_units $disk`
 
186
    fi
 
187
}
 
188
 
 
189
SysInfo_mem_units() {
 
190
    mem=$1
 
191
 
 
192
    if [ -z $1 ]; then 
 
193
        return
 
194
    fi
 
195
 
 
196
    memlen=`expr ${#mem} - 1`
 
197
    memlen_alt=`expr ${#mem} - 2`    
 
198
    if [ ${mem:$memlen:1} = "G" ]; then
 
199
        mem="${mem:0:$memlen}"
 
200
        if [ $mem != ${mem/./} ]; then
 
201
            mem_before=${mem/.*/}
 
202
            mem_after=${mem/*./}
 
203
            mem=$[mem_before*1024]
 
204
            if [ ${#mem_after} = 0 ]; then
 
205
                :
 
206
            elif [ ${#mem_after} = 1 ]; then
 
207
                mem=$[mem+100*$mem_after]
 
208
            elif [ ${#mem_after} = 2 ]; then
 
209
                mem=$[mem+10*$mem_after]
 
210
            elif [ ${#mem_after} = 3 ]; then
 
211
                mem=$[mem+$mem_after]
 
212
            else 
 
213
                mem_after=${mem_after:0:3}
 
214
                mem=$[mem+$mem_after]
 
215
            fi
 
216
        fi
 
217
    elif [ ${mem:$memlen:1} = "M" ]; then
 
218
        mem=${mem/.*/}
 
219
        mem="${mem:0:$memlen}"
 
220
    elif [ ${mem:$memlen:1} = "k" ]; then
 
221
        mem="${mem:0:$memlen}"
 
222
        mem=${mem/.*/}
 
223
        mem=`expr $mem / 1024`
 
224
    elif [ ${mem:$memlen_alt:2} = "kB" ]; then
 
225
        mem="${mem:0:$memlen_alt}"
 
226
        mem=${mem/.*/}
 
227
        mem=`expr $mem / 1024`
 
228
    elif [ ${mem:$memlen_alt:2} = "Mb" ]; then
 
229
        mem="${mem:0:$memlen_alt}"
 
230
        mem=${mem/.*/}
 
231
    elif [ ${mem:$memlen_alt:2} = "MB" ]; then
 
232
        mem="${mem:0:$memlen_alt}"
 
233
        mem=${mem/.*/}
 
234
    fi
 
235
 
 
236
    # Round to the next multiple of 50
 
237
    memlen=`expr ${#mem} - 2`
 
238
    mem_round="${mem:$memlen:2}"
 
239
    if [ x$mem_round = x ]; then 
 
240
        :
 
241
    elif [ $mem_round = "00" ]; then 
 
242
        :
 
243
    else
 
244
        mem_round=`echo $mem_round | sed 's/^0//'`
 
245
        if [ $mem_round -lt "50" ]; then 
 
246
            mem=$[mem+50]
 
247
            mem=$[mem-$mem_round]
 
248
 
 
249
        else 
 
250
            mem=$[mem+100]
 
251
            mem=$[mem-$mem_round]
 
252
        fi
 
253
    fi
 
254
    echo $mem    
 
255
}
 
256
 
 
257
SysInfo_hdd_units() {
 
258
    disk=$1
 
259
    disklen=`expr ${#disk} - 1`
 
260
    disklen_alt=`expr ${#disk} - 2`
 
261
    if [ ${disk:$disklen:1} = "G" ]; then
 
262
        disk="${disk:0:$disklen}"
 
263
    elif [ ${disk:$disklen:1} = "M" ]; then
 
264
        disk="${disk:0:$disklen}"
 
265
        disk=${disk/.*/}
 
266
        disk=`expr $disk / 1024`
 
267
    elif [ ${disk:$disklen:1} = "k" ]; then
 
268
        disk="${disk:0:$disklen}"
 
269
        disk=${disk/.*/}
 
270
        disk=`expr $disk / 1048576`
 
271
    elif [ ${disk:$disklen_alt:2} = "kB" ]; then
 
272
        disk="${disk:0:$disklen_alt}"
 
273
        disk=${disk/.*/}
 
274
        disk=`expr $disk / 1048576`
 
275
    elif [ ${disk:$disklen_alt:2} = "Mb" ]; then
 
276
        disk="${disk:0:$disklen_alt}"
 
277
        disk=${disk/.*/}
 
278
        disk=`expr $disk / 1024`
 
279
    elif [ ${disk:$disklen_alt:2} = "MB" ]; then
 
280
        disk="${disk:0:$disklen_alt}"
 
281
        disk=${disk/.*/}
 
282
        disk=`expr $disk / 1024`
 
283
    fi
 
284
    echo $disk    
 
285
}
 
286
 
 
287
SysInfo_usage() {
 
288
        cat <<END
 
289
usage: $0 {start|stop|monitor|validate-all|meta-data}
 
290
 
 
291
Expects to have a fully populated OCF RA-compliant environment set.
 
292
END
 
293
}
 
294
 
 
295
SysInfo_start() {
 
296
    echo $OCF_RESKEY_clone > $OCF_RESKEY_pidfile
 
297
    SysInfoStats
 
298
    exit $OCF_SUCCESS
 
299
}
 
300
 
 
301
SysInfo_stop() {
 
302
    rm $OCF_RESKEY_pidfile
 
303
    exit $OCF_SUCCESS
 
304
}
 
305
 
 
306
SysInfo_monitor() {
 
307
    if [ -f $OCF_RESKEY_pidfile ]; then
 
308
        clone=`cat $OCF_RESKEY_pidfile`
 
309
    fi
 
310
 
 
311
    if [ x$clone = x ]; then
 
312
        rm $OCF_RESKEY_pidfile
 
313
        exit $OCF_NOT_RUNNING
 
314
 
 
315
    elif [ $clone = $OCF_RESKEY_clone ]; then
 
316
        SysInfoStats
 
317
        exit $OCF_SUCCESS
 
318
 
 
319
    elif [ x$OCF_RESKEY_CRM_meta_globally_unique = xtrue  
 
320
            -o x$OCF_RESKEY_CRM_meta_globally_unique = xTrue
 
321
            -o x$OCF_RESKEY_CRM_meta_globally_unique = xyes
 
322
            -o x$OCF_RESKEY_CRM_meta_globally_unique = xYes
 
323
        ]; then
 
324
        SysInfoStats
 
325
        exit $OCF_SUCCESS
 
326
    fi
 
327
    exit $OCF_NOT_RUNNING
 
328
}
 
329
 
 
330
SysInfo_validate() {
 
331
    return $OCF_SUCCESS
 
332
}
 
333
 
 
334
if [ $# -ne 1 ]; then
 
335
    SysInfo_usage
 
336
    exit $OCF_ERR_ARGS
 
337
fi
 
338
 
 
339
: ${OCF_RESKEY_pidfile:="$HA_RSCTMP/SysInfo-${OCF_RESOURCE_INSTANCE}"}
 
340
: ${OCF_RESKEY_clone:="0"}
 
341
if [ x != x${OCF_RESKEY_delay} ]; then
 
342
    OCF_RESKEY_delay="-d ${OCF_RESKEY_delay}"
 
343
fi
 
344
 
 
345
case $__OCF_ACTION in
 
346
meta-data)      meta_data
 
347
                exit $OCF_SUCCESS
 
348
                ;;
 
349
start)          SysInfo_start
 
350
                ;;
 
351
stop)           SysInfo_stop
 
352
                ;;
 
353
monitor)        SysInfo_monitor
 
354
                ;;
 
355
validate-all)   SysInfo_validate
 
356
                ;;
 
357
usage|help)     SysInfo_usage
 
358
                exit $OCF_SUCCESS
 
359
                ;;
 
360
*)              SysInfo_usage
 
361
                exit $OCF_ERR_UNIMPLEMENTED
 
362
                ;;
 
363
esac
 
364
 
 
365
exit $?