机房停电自动关机脚本, 机房有UPS,但只能撑30分钟吧,
脚本指定时间去ping两台机器, 如果都超时,就开始关机
脚本放在 100.33 电脑中的VM里面
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
|
#!/usr/bin/ruby
require
'net/ssh'
require
'net/telnet'
ip = [
"192.168.100.20"
,
"192.168.100.21"
]
list_srv =
'/root/machine.txt'
$hash
= {}
$log
=
''
def
mailer(subject,main=subject)
msgstr = <<
END_OF_MESSAGE
From:
SVN
<svn
@163
.com>
To: Bian <bian
@163
.com>
Subject:
#{subject}
#{main}
END_OF_MESSAGE
acct =
'svn@163.com'
domain =
"163.com"
pass =
'x3s2x2'
begin
Net::
SMTP
.start(
'smtp.163.com'
,
25
, domain, acct, pass,
:login
) { |smtp|
smtp.send_message msgstr,
'svn@163.com'
,
'bian@163.com'
}
rescue
end
end
def
check_alive(ip)
result = []
ip.
each
{|x| result << `ping
#{x} -c 4 | grep ttl | wc -l`.to_i }
return
result
end
def
close_win(whost,wuser,wpass)
whost ==
'192.168.100.33'
? time =
240
: time =
3
begin
`net rpc shutdown -s -f -t
#{time} -I #{whost} -U #{wuser}%#{wpass}`
rescue
end
end
def
close_ubuntu(host,user,pass)
begin
Net::
SSH
.start(host, user,
:password
=> pass) { |session|
session.open_channel { |channel|
channel.request_pty
channel.exec(
"sync; sync; sync; sudo -p 'Password' init 0"
) { |ch, success|
abort
"could not execute command"
unless
success
channel.on_data { |ch, data|
#result
if
data =~ /Password/
channel.send_data(pass +
"\n"
)
end
}
}
}
#session.loop
}
rescue
end
end
def
close_centos(ip,user,pass)
begin
Net::
SSH
.start(ip, user,
:password
=> pass) {|ssh|
ssh.exec!(
"sync; sync; sync; init 0"
)
}
rescue
end
end
def
close_vm(ip,user,pass,type,vm_name)
if
type ==
"vm"
cmd =
"sync; sync; sync; halt"
else
cmd = "esxcli vm process kill --type=soft --world-id=`esxcli vm process list |
grep -
A
1
-
E
'^#{vm_name}'
| tail -
1
| awk
'{print $3}'
`"
end
begin
Net::
SSH
.start(ip, user,
:password
=> pass) {|ssh|
ssh.exec!(
"#{cmd}"
)
}
rescue
end
end
def
drbd(ip,user,pass)
#check DRBD Pri or Sec
begin
Net::
SSH
.start(ip, user,
:password
=> pass ) { |ssh|
result = ssh.exec!(
"ip a | grep '192.168.100.50' | wc -l"
)
return
result.to_i
}
rescue
end
end
def
close(type)
$hash
.
each
{ |x,y|
if
y[
3
].strip == type
case
y[
2
].strip
when
'win'
close_win(x.strip, y[
0
].strip, y[
1
].strip)
when
'centos'
close_centos(x.strip, y[
0
].strip, y[
1
].strip)
when
'ubuntu'
if
type ==
'c'
if
drbd(x.strip, y[
0
].strip, y[
1
].strip) ==
1
#DRBD is Pri
redo
else
close_ubuntu(x.strip, y[
0
].strip, y[
1
].strip)
end
else
close_ubuntu(x.strip, y[
0
].strip, y[
1
].strip)
end
when
'vm'
if
type ==
'a'
close_vm(x.strip, y[
0
].strip, y[
1
].strip,
"srv"
,
"BeiJingHWA"
)
elsif
type ==
'b'
close_vm(x.strip, y[
0
].strip, y[
1
].strip,
"srv"
,
"Panabit"
)
else
close_vm(x.strip, y[
0
].strip, y[
1
].strip,
"vm"
,
""
)
end
end
sleep
3
end
}
end
def
check(type)
$hash
.
each
{|x,y|
if
y[
3
].strip == type
begin
Net::Telnet:
:new
(
"Host"
=> x.strip,
"Port"
=> y[
4
].strip.to_i,
"Telnetmode"
=>
false
)
$log
=
$log
+
"#{x.strip} is PowerON "
+
Time
.now.strftime(
"%Y-%m-%d %k:%M"
)
rescue
$log
=
$log
+
"#{x.strip} is PowerOFF "
+
Time
.now.strftime(
"%Y-%m-%d %k:%M"
)
end
end
}
end
def
log(str)
aFile =
File
.
new
(
Dir
.pwd +
Time
.now.strftime(
"%Y-%m-%d"
) +
".log"
,
"w"
)
aFile.puts str
aFile.close
end
#============== start close server
File
.open(list_srv,
'r'
).
each
{|x|
$hash
[x.split(
','
)[
0
]] = [x.split(
','
)[
1
], x.split(
','
)[
2
], x.split(
','
)[
3
], x.split(
','
)[
4
], x.split(
','
)[
5
]]
#ip,user,pass,os_type,close_type,check_port
}
loop
do
result = check_alive(ip)
if
(result[
0
] ==
0
) && (result[
1
] ==
0
)
mailer(
"Power Notice"
,
"Abnormal power"
)
break
elsif
(result[
0
] ==
0
) || (result[
1
] ==
0
)
mailer(
"Device Err"
,
"One Device Maybe Down"
)
Process.exit(
0
)
end
sleep
40
end
#first
close(
'a'
)
sleep
120
check(
'a'
)
#second
close(
'b'
)
sleep
30
check(
'b'
)
#third
close(
'c'
)
sleep
180
check(
'c'
)
#vm
close(
'd'
)
sleep
60
check(
'd'
)
log(
$log
)
sleep
5
#close vcenter
close_win(
"192.168.100.33"
,
$hash
[
'192.168.100.33'
][
0
],
$hash
[
'192.168.100.33'
][
1
])
exec(
'sync; sync; sync; init 0'
)
|
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1344957,如需转载请自行联系原作者