停电自动关机

简介:

机房停电自动关机脚本, 机房有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,如需转载请自行联系原作者
相关文章
|
Java 关系型数据库 数据库连接
【SpringBoot】入门到精通 这一篇就够了
【SpringBoot】入门到精通 这一篇就够了
300 1
|
Kubernetes Cloud Native 容器
完全免费的K8S学习平台:在线集群环境助力你的云原生之路!
完全免费的K8S学习平台:在线集群环境助力你的云原生之路!
2733 1
|
消息中间件 SQL Kafka
离线数仓(四)【数仓数据同步策略】(1)
离线数仓(四)【数仓数据同步策略】
构造命题公式的真值表
构造命题公式的真值表
256 0
|
SQL NoSQL 关系型数据库
Mysql Innodb死锁情况分析与归纳
Mysql Innodb死锁情况分析与归纳
146 0
|
Web App开发 监控 前端开发
SpringBoot2.x系列教程05--SpringBoot花样配置之实现热部署配置及原理
前言 上一章节中,壹哥 带各位学会了配置自定义的Web端口与项目根目录,今天我们再来看看其他的一些小花样。 在开发阶段,我们编写完代码后,就得需要点击启动按钮,重启项目后才能看到更新后的内容,每次都这么操作,就会就得有点麻烦,那能不能让项目随着代码的更新而自动重启呢? 既然有了这样的需求,SpringBoot就很暖心的给我们提供了这样的功能。今天我们就来看看SpringBoot中的另一个小花样配置---实现SpringBoot项目的热加载。 注意 网上也有不少教程把该功能称为热部署,我觉得叫做热加载更准确点! 一. SpringBoot热加载简介 在SpringBoot中实现热加载相对来说
502 0
|
数据可视化 vr&ar 图形学
Unity可视化编程XDreamer插件导入
前言 XDreamer是一款基于Unity平台开发的,可在Unity(包括编辑器与运行时)中使用的可扩展的中文交互编辑软件,可进行2D、3D、VR、AR、MR开发。 本期博客为XDreamer的官方讲解的学习记录。可以理解为UE4中的蓝图效果。是从事美术人员的福音,美术人员也可不用编写程序进行游戏的制作。 一、下载XDreamer官方插件包 XDreamer中文交互编辑器http://www.xdreamer.com.cn/请在官网进行下载,得到如下的文件。 二、插件加载 目前我导入到URP
867 0
Unity可视化编程XDreamer插件导入
|
JavaScript 数据可视化
Echarts动态数据可视化学习(3):图表中函数的使用场景及方法js快捷函数
Echarts动态数据可视化学习(3):图表中函数的使用场景及方法js快捷函数
261 0
|
Java API
Java开发——17.常用类(System、Math、Arrays、Date、Calendar、DateFormat及实现类)
17和18以及后面将要学习的Java中的常用类,这里都是仅做了解,具体使用还是要对照API文档;所以这是挑重点方法进行学习。
Java开发——17.常用类(System、Math、Arrays、Date、Calendar、DateFormat及实现类)