####################################################################################
#coding =utf-8
#脚本用途: OpenWeb Service Automation check
#编写时间: July 11, 2016
#脚本乐橙运维组
####################################################################################
#!/usr/bin/env python
#载入模块
Import commands
Import OS
Import socket, fcntl, struct
#制定各标准
is_pro_success = 0
is_crontab_success = 0
#获取当前执行脚本的时间
Time = Commands.getoutput (' Date ')
print ' \033[1;31;40m '
print ' This script is executed at ' + Time '
#获取当前服务器的IP地址和hostname
def check_local_ip (ifname = ' eth0 '):
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
inet = Fcntl.ioctl (S.fileno (), 0x8915, Struct.pack (' 256s ', ifname[:15]))
ret = Socket.inet_ntoa (inet[20:24])
return ret
print ' \033[1;32;40m '
print ' script-executed server intranet IP is ' + check_local_ip ()
#获取程序后台进程数量
Def checkprocess ():
Global is_pro_success
print ' \033[1;33;1m '
Boot = commands.getoutput ("Ps-ef | grep java|grep-v grep|wc-l ")
if boot = = ' 1 ':
is_pro_success = 1
Print (' ~~~~openweb has been started ~ ~ ~ ~ ~ ~ ')
Else
Print (' ~~~~openweb does not start properly, the script will automatically help you start the startup.sh script ~ ~ ~ ~ ')
Os.system (' sh/mnt/hswx/tomcat/bin/startup.sh ')
#检测守护进程是否存在
Def checkcrontab ():
Global is_crontab_success
Crontab_content = Commands.getoutput (' crontab-l ')
Crontab_number = Commands.getoutput (' crontab-l|wc-l ')
if (int (crontab_number) = = 3 and "* * * * * * cd/mnt/hswx/util/; /monitor_hswx_websvr.sh "in crontab_content):
is_crontab_success = 1
elif int (crontab_number) = = 0:
print ' \033[1;34;1m '
PRINT ('!!!!! Watchdog program does not exist, number of processes: 0\n ')
Print (' Now open the watchdog program for you, start monitor_hswx_websvr.sh ')
Os.system (' cd/mnt/hswx/util/;. /monitor_hswx_websvr.sh ')
Else
print ' \033[1;34;1m '
PRINT ('!!!!! watchdog more than 1, please restart the program \ n ')
#判断所有检测是否都成功
Def checksuccess ():
if (is_pro_success = = 1 and is_crontab_success = = 1):
Print (' ==========ok!!! SUCCESS!!! ===========\n ')
if __name__ = = ' __main__ ':
Checkprocess ()
Checkcrontab ()
Checksuccess ()
CHECK_LOCAL_IP ()
print ' \033[0m '
Print (' The whole process is over, thank you for your use. ‘)
Use the following, start without opening the OpenWeb server, and then start the script. The machine itself does not have watchdog software, so startup watchdog is a failure, if prompted no crontab for Root, then in any window #crontab-e, and then do not write, directly: Wq exit, so there will be no hint.
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/84/0B/wKiom1eEZMWCqFX4AAF91tadfaw748.png-wh_500x0-wm_3 -wmp_4-s_2182813352.png "title=" 1.png "alt=" Wkiom1eezmwcqfx4aaf91tadfaw748.png-wh_50 "/>
---------------------------------------------Split Line-------------------------------
OpenWeb is what to do, I do not know, anyway development so write, I do so. As for the inside path is the server developed.
There are many kinds of syntax for executing other program commands in Python, The company before this man with is Os.popen, I use is commands, with commands, basic a Well Python script can be written shell, Shell How to write, Python is outside add a commands.getoutput this shell, also can The command is executed successfully through the GETSTAT function.
Note Here a detail, boot = commands.getoutput ("Ps-ef | grep java|grep-v grep|wc-l "), where the boot is not an int, but a str, that is, with the", so the sentence can not directly write the number, but ' 1 '.
If you change the color, the company before the script heroes with the from Printcolour import printcolour, I do not use this module, after all, there is no guarantee that each machine is installed printcolour This module, so I simply use the "format: \ 033[display mode; background color M " this routine, very simple also very useful but only in the use of Linux, the other place is not good, is the last time to remember the font initialization.
This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1825624
A script that the company uses to check whether the OpenWeb service is started