File: WinPopup

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (231 lines) | stat: -rwxr-xr-x 5,033 bytes parent folder | download | duplicates (4)
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
#!/bin/sh
#
# Resource script for sending WinPopups using smbclient
# derived from Alan Robertson's MailTo script
#
# Author: Sandro Poppi <spoppi@gmx.de>
#
# Description: sends WinPopups to a sysadmin's workstation
#	       whenever a takeover occurs.
#
#	  OCF parameters are as below:
#		OCF_RESKEY_hostfile
#		
#	where "hostfile" is a file containing the IPs/Workstation names
#	one by line to be sent WinPopups
#
# License:  GNU General Public License (GPL)

WINPOPUPFILE=${HA_VARRUN}/WinPopup
#######################################################################
# Initialization:

# Source function library.
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

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

us=`uname -n`

usage() {
  echo "Usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="WinPopup">
<version>1.0</version>

<longdesc lang="en">
Resource script for WinPopup. It sends WinPopups message to a 
sysadmin's workstation whenever a takeover occurs.
</longdesc>
<shortdesc lang="en">Sends an SMB notification message to selected hosts</shortdesc>

<parameters>
<parameter name="hostfile" unique="0" required="1">
<longdesc lang="en">
The file containing the hosts to send WinPopup messages to.
</longdesc>
<shortdesc lang="en">Host file</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="30" />
<action name="stop" timeout="30" />
<action name="status" depth="0" timeout="10" interval="10" />
<action name="monitor" depth="0" timeout="10" interval="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}

sendWinPopup() {
  # if workstation file exists and is not zero
  if [ -s "$hostfile" ] ; then
    subject=$1
    shift

    for i in `cat $hostfile` ; do
      echo "$subject $*" | smbclient -M $i >/dev/null 2>&1
    done
  else
    ocf_log err "Workstation file $hostfile missing or corrupt!"
    return $OCF_ERR_ARGS
  fi

  return $?
}

SubjectLine() {
  case $1 in
    ??*)	echo $1;;
    *)		echo "Resource Group";;
  esac
}


WinPopupStart() {

	Subject="`SubjectLine $2` Takeover in progress on $us"
 
	if sendWinPopup "$Subject" $1; then
		touch $WINPOPUPFILE
		return $?
	else
		return $?
	fi

}

WinPopupStop () {
	Subject="`SubjectLine $2` Reestablishing original master connection in progress on $us"

	if sendWinPopup "$Subject" $1; then
		rm -f $WINPOPUPFILE
		return $?
	else
		return $?
	fi

}

WinPopupStatus () {
	ocf_log warn "Don't stat/monitor me! WinPopup is a pseudo resource agent, so the status reported may be incorrect"
	if [ -f $WINPOPUPFILE ]; then
		echo "running"
		return $OCF_SUCCESS
	else
		echo "stopped"
		return $OCF_NOT_RUNNING
	fi
}

# A not reliable IP address checking function, which only picks up those _obvious_ violations...
#
# It accepts IPv4 address in dotted quad notation, for example "192.168.1.1"
#
# 100% confidence whenever it reports "negative", 
# but may get false "positive" answer. 
# 
CheckIP() {
  ip="$1"
  case $ip in
    *[!0-9.]*) #got invalid char
	false;;
    .*|*.) #begin or end by ".", which is invalid
	false;;
    *..*) #consecutive ".", which is invalid
	false;;
    *.*.*.*.*) #four decimal dots, which is too many
	false;;
    *.*.*.*) #exactly three decimal dots, candidate, evaluate each field
	local IFS=.
	set -- $ip
	if
	    ( [ $1 -le 254 ] && [ $2 -le 254 ] && [ $3 -le 254 ] && [ $4 -le 254 ] )
	then
	    true
	fi	   
	;;
    *) #less than three decimal dots
	false;;
  esac
  return $? # This return is unnecessary, this comment too :)
}

WinPopupValidateAll () {
  if [ ! -s "$hostfile" ] ; then
    ocf_log err "Workstation file $hostfile missing or corrupt!"
    return $OCF_ERR_ARGS
  fi

  # What kind of hostfiles are valid? 
  # We stick to the definition that, a hostfile is valid if and only if it 
  # contains at least one valid host to send WinPopup message to.

#  have_valid_host=no
  for host in `cat $hostfile`; do
	nmblookup $host 2>&1 | grep -q "failed to find name $host\>"
	if [ $? -ne 0 ]; then
#	  have_valid_host=yes
	  return $OCF_SUCCESS
	fi
	# $host is not a netbios name, an IP address maybe?
	if CheckIP "$host"; then
#	  have_valid_host=yes
	  return $OCF_SUCCESS
	fi
  done

  ocf_log err "Workstation file $hostfile contains no valid host!"
  return $OCF_ERR_CONFIGURED
}

if
  ( [ $# -ne 1 ] )
then
  usage
  exit $OCF_ERR_ARGS
fi

# See how the environment virables were set.
hostfile=${OCF_RESKEY_hostfile:-hosts}

case "$1" in
  meta-data)
	meta_data
	exit $OCF_SUCCESS
	;;
  start)
	WinPopupStart
	;;
  stop)
	WinPopupStop
	;;

	#	Not quite sure what to do with this one...
  status|monitor)
	WinPopupStatus
	;;
  validate-all)
	WinPopupValidateAll
	;;
  usage)
	usage
	exit $OCF_SUCCESS
	;;
  *)
        usage
	exit $OCF_ERR_UNIMPLEMENTED
	;;
esac

exit $?