1#!@BASH_SHELL@
2#
3#
4#	Virtual Machine and Container Registration Service OCF Resource Agent
5#	It records (in the CIB) various attributes of a node
6#
7# Copyright (c) 2017 Mathieu Grzybek
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# Parameter defaults
36
37OCF_RESKEY_pidfile_default="$HA_RSCTMP/MachineInfo-${OCF_RESOURCE_INSTANCE}"
38OCF_RESKEY_delay_default="0s"
39OCF_RESKEY_clone_default="0"
40
41: ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}}
42: ${OCF_RESKEY_delay=${OCF_RESKEY_delay_default}}
43: ${OCF_RESKEY_clone=${OCF_RESKEY_clone_default}}
44
45#######################################################################
46
47meta_data() {
48	cat <<END
49<?xml version="1.0"?>
50<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
51<resource-agent name="machine-info">
52<version>1.0</version>
53
54<longdesc lang="en">
55This is a Virtual Machine and Container Registration Service Resource Agent.
56It records (in the CIB) attributes about the number of running virtual machines
57and containers running on the node.
58It uses systemd machinectl.
59Sample output:
60   machines:   5
61</longdesc>
62<shortdesc lang="en">Records various node attributes in the CIB</shortdesc>
63
64<parameters>
65<parameter name="pidfile" unique="0">
66<longdesc lang="en">PID file</longdesc>
67<shortdesc lang="en">PID file</shortdesc>
68<content type="string" default="${OCF_RESKEY_pidfile_default}" />
69</parameter>
70<parameter name="delay" unique="0">
71<longdesc lang="en">Interval to allow values to stabilize</longdesc>
72<shortdesc lang="en">Dampening Delay</shortdesc>
73<content type="string" default="${OCF_RESKEY_delay_default}" />
74</parameter>
75</parameters>
76
77<actions>
78<action name="start"   timeout="20s" />
79<action name="stop"    timeout="20s" />
80<action name="monitor" timeout="20s" interval="60s"/>
81<action name="meta-data"  timeout="5s" />
82<action name="validate-all"  timeout="20s" />
83</actions>
84</resource-agent>
85END
86}
87
88#######################################################################
89
90MachineInfoStats() {
91	value=$(machinectl|awk '/machines listed/ {print $1}')
92	echo -e "machines:\t$value"
93	${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -S status -n machines -v $value
94}
95
96MachineInfo_usage() {
97	cat <<END
98usage: $0 {start|stop|monitor|validate-all|meta-data}
99
100Expects to have a fully populated OCF RA-compliant environment set.
101END
102}
103
104MachineInfo_start() {
105	echo $OCF_RESKEY_clone > $OCF_RESKEY_pidfile
106	MachineInfoStats
107	exit $OCF_SUCCESS
108}
109
110MachineInfo_stop() {
111	rm -f $OCF_RESKEY_pidfile
112	${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -D -S state -n machines
113	exit $OCF_SUCCESS
114}
115
116MachineInfo_monitor() {
117	if [ -f "$OCF_RESKEY_pidfile" ] ; then
118		MachineInfoStats
119		exit $OCF_RUNNING
120	fi
121	exit $OCF_NOT_RUNNING
122}
123
124MachineInfo_validate() {
125	return $OCF_SUCCESS
126}
127
128if [ $# -ne 1 ]; then
129    MachineInfo_usage
130    exit $OCF_ERR_ARGS
131fi
132
133if [ x != x${OCF_RESKEY_delay} ]; then
134	OCF_RESKEY_delay="-d ${OCF_RESKEY_delay}"
135fi
136
137case $__OCF_ACTION in
138meta-data)	meta_data
139		exit $OCF_SUCCESS
140		;;
141start)		MachineInfo_start
142		;;
143stop)		MachineInfo_stop
144		;;
145monitor)	MachineInfo_monitor
146		;;
147validate-all)	MachineInfo_validate
148		;;
149usage|help)	MachineInfo_usage
150		exit $OCF_SUCCESS
151		;;
152*)		MachineInfo_usage
153		exit $OCF_ERR_UNIMPLEMENTED
154		;;
155esac
156
157exit $?
158