import os
if os.name=='nt':
STPROFILE='c:/etc/syscheck.pf'
WEBPATH='c:\\etc\\web\\'
else:
STPROFILE='/etc/syscheck.pf'
WEBPATH='\\etc\\web\\'
import time
import smlib
import string
import srpc
import win32api
import thread
import traceback
from netman import *
syscheck_version='1.1'
threaddoing=[] #用于存放现在执行中的线程描述
file_lock=thread.allocate_lock()
threaddoing_lock=thread.allocate_lock()
sysname=smlib.getConfigStr(STPROFILE,'esys','name','landpage') #共用参数
# Logging support
# ===============
logfile = "" # Filename to log to, if not empty
logfp = None # File object to log to, if not None
def initlog(*allargs):
"""Write a log message, if there is a log file.
Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).
The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.
If the global logfp is not None, it should be a file object to
which log data is written.
If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode. This file should be
world writable!!! If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).
"""
global logfp, log
if logfile and not logfp:
try:
logfp = open(logfile, "a")
except IOError:
pass
if not logfp:
log = nolog
else:
log = dolog
apply(log, allargs)
def dolog(fmt, *args):
"""Write a log message to the log file. See initlog() for docs."""
global logfp
while 1:
if (file_lock.acquire(0)):
logfp.write(fmt%args + "\n")
logfp.close()
print '日志已记录,日志文件关闭!'
if logfile:
try:
logfp = open(logfile, "a")
print '日志文件打开!'
except IOError:
print '日志文件打开失败!'
pass
file_lock.release()
break
else:
time.sleep(1)
def nolog(*allargs):
"""Dummy function, assigned to log when logging is disabled."""
print '无日志记录!'
pass
log = initlog # The current logging function
def getcompproc():
## print 'getcompproc now!'
res=Getcompproc()
return res[0][1:],res[1][1:],res[2][1:],res[3][1:],res[4][1:]
def getdiskfree(path):
## print '---------------------------------'
## print '分区:',path,'磁盘空间情况!'
## print '---------------------------------'
res=Getdiskfree(path)
if res==None:
log( 'Args send wrong!--%s\r\n',getNowTime())
## if res[4]:
## logwrongmsg(res[4])
## return -1,-1
total=(res[0]*res[1]*res[3])/(1024*1024)
free=(res[0]*res[1]*res[2])/(1024*1024)
## print '每扇区段数:',res[0]
## print '每段字节数:',res[1]
## print '空扇区数:',res[2]
## print '扇区总数:',res[3]
## print '返回值:',res[4]
## print '\r\n'
print path
print '可用字节数:',res[0]*res[1]*res[2]
print '字节总数:',res[0]*res[1]*res[3]
return total,free
def add_stringname(name,add):
if name=='':
name+=add
else:
name+=','+add
return name
14 个解决方案
#1
def modifythreaddoing(flag,content):
global threaddoing
while 1:
if (threaddoing_lock.acquire(0)):
if flag==1:
threaddoing.append(content)
else:
threaddoing.remove(content)
threaddoing_lock.release()
break
else:
time.sleep(1)
def usingCheck(timewait):
modifythreaddoing(1,'CPU、内存检测程序')
try:
while 1:
timeoutcheck=smlib.getConfigInt(STPROFILE,'time','outcheck',30)
timeoutcount=smlib.getConfigInt(STPROFILE,'time','outcount',1)
count=0
while count<timeoutcount:
phymemolimit=smlib.getConfigInt(STPROFILE,'phymemo','limit',95) #使用率参数
virmemolimit=smlib.getConfigInt(STPROFILE,'virmemo','limit',95)
cpuusedlimit=smlib.getConfigInt(STPROFILE,'cpuused','limit',95)
proccheck=smlib.getConfigStr(STPROFILE,'proc','check','')
proclimit=smlib.getConfigStr(STPROFILE,'proc','limit','')
ret=GlobalMemoryStatus()
totalphymemo=ret[0]
freephymemo=ret[1]
totalvirmemo=ret[2]
freevirmemo=ret[3]
proccheckli=proccheck.split(',')
proclimitli=proclimit.split(',')
if proccheck=='':
proccheckli=[]
res=getcompproc()
procname=res[0].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
i+=1
numli=[]
i=0
while i<len(proccheckli):
numli.append(0)
j=0
while j<len(procname):
if procname[j].capitalize()==proccheckli[i].capitalize():
numli[i]+=1
j+=1
i+=1
usedphymemo=totalphymemo-freephymemo
usedvirmemo=totalvirmemo-freevirmemo
prate=usedphymemo/float(totalphymemo)*100
vrate=usedvirmemo/float(totalvirmemo)*100
cpuused=GetCpuState()
sprate=getAfter(prate)
svrate=getAfter(vrate)
outflag=0
i=0
while i<len(numli):
if numli[i]>=int(proclimitli[i]):
outflag=1
proctuple=(int(proclimitli[i]),proccheckli[i])
break
i+=1
if prate>phymemolimit or vrate>virmemolimit or cpuused>cpuusedlimit or outflag:
if prate>phymemolimit:
print 'Check physical memory using rate again!'
elif vrate>virmemolimit:
print 'Check virtual memory using rate again!'
elif cpuused>cpuusedlimit:
print 'Check cup time using rate again!'
elif outflag:
print 'Count process %s numbers again!'%proctuple[1]
count+=1
time.sleep(timeoutcheck)
else:
break
if prate>phymemolimit:
sendcycle('phymemo',phymemolimit)
if vrate>virmemolimit:
sendcycle('virmemo',virmemolimit)
if cpuused>cpuusedlimit:
sendcycle('cpuused',cpuusedlimit)
if outflag:
sendcycle('procnum',proctuple[0],proctuple[1])
print 'The using rate of physical memory is %s'%sprate+'%'
print 'The using rate of virtual memory is %s'%svrate+'%'
print 'The using rate of cpu is %d'%cpuused+'%'
i=0
while i<len(proccheckli):
print 'The number of process %s is %s!'%(proccheckli[i],numli[i])
i+=1
print '\r\n'
time.sleep(timewait)
except:
msg='CPU、内存检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'CPU、内存检测程序')
global threaddoing
while 1:
if (threaddoing_lock.acquire(0)):
if flag==1:
threaddoing.append(content)
else:
threaddoing.remove(content)
threaddoing_lock.release()
break
else:
time.sleep(1)
def usingCheck(timewait):
modifythreaddoing(1,'CPU、内存检测程序')
try:
while 1:
timeoutcheck=smlib.getConfigInt(STPROFILE,'time','outcheck',30)
timeoutcount=smlib.getConfigInt(STPROFILE,'time','outcount',1)
count=0
while count<timeoutcount:
phymemolimit=smlib.getConfigInt(STPROFILE,'phymemo','limit',95) #使用率参数
virmemolimit=smlib.getConfigInt(STPROFILE,'virmemo','limit',95)
cpuusedlimit=smlib.getConfigInt(STPROFILE,'cpuused','limit',95)
proccheck=smlib.getConfigStr(STPROFILE,'proc','check','')
proclimit=smlib.getConfigStr(STPROFILE,'proc','limit','')
ret=GlobalMemoryStatus()
totalphymemo=ret[0]
freephymemo=ret[1]
totalvirmemo=ret[2]
freevirmemo=ret[3]
proccheckli=proccheck.split(',')
proclimitli=proclimit.split(',')
if proccheck=='':
proccheckli=[]
res=getcompproc()
procname=res[0].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
i+=1
numli=[]
i=0
while i<len(proccheckli):
numli.append(0)
j=0
while j<len(procname):
if procname[j].capitalize()==proccheckli[i].capitalize():
numli[i]+=1
j+=1
i+=1
usedphymemo=totalphymemo-freephymemo
usedvirmemo=totalvirmemo-freevirmemo
prate=usedphymemo/float(totalphymemo)*100
vrate=usedvirmemo/float(totalvirmemo)*100
cpuused=GetCpuState()
sprate=getAfter(prate)
svrate=getAfter(vrate)
outflag=0
i=0
while i<len(numli):
if numli[i]>=int(proclimitli[i]):
outflag=1
proctuple=(int(proclimitli[i]),proccheckli[i])
break
i+=1
if prate>phymemolimit or vrate>virmemolimit or cpuused>cpuusedlimit or outflag:
if prate>phymemolimit:
print 'Check physical memory using rate again!'
elif vrate>virmemolimit:
print 'Check virtual memory using rate again!'
elif cpuused>cpuusedlimit:
print 'Check cup time using rate again!'
elif outflag:
print 'Count process %s numbers again!'%proctuple[1]
count+=1
time.sleep(timeoutcheck)
else:
break
if prate>phymemolimit:
sendcycle('phymemo',phymemolimit)
if vrate>virmemolimit:
sendcycle('virmemo',virmemolimit)
if cpuused>cpuusedlimit:
sendcycle('cpuused',cpuusedlimit)
if outflag:
sendcycle('procnum',proctuple[0],proctuple[1])
print 'The using rate of physical memory is %s'%sprate+'%'
print 'The using rate of virtual memory is %s'%svrate+'%'
print 'The using rate of cpu is %d'%cpuused+'%'
i=0
while i<len(proccheckli):
print 'The number of process %s is %s!'%(proccheckli[i],numli[i])
i+=1
print '\r\n'
time.sleep(timewait)
except:
msg='CPU、内存检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'CPU、内存检测程序')
#2
def sendcycle(name,arga,argb=''):
otime=smlib.getConfigInt(STPROFILE,'time','outset',300)
nowtime=getNowTime()
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
addrli=addr.split(',')
if addrli[0]=='':
return ''
i=0
logflag=0
while i<len(addrli):
mob='mob'+str(i)
mobname=smlib.getConfigStr(STPROFILE,mob,'name','')
namelist=string.split(mobname,',')
mobnum=smlib.getConfigStr(STPROFILE,mob,'number','')
numlist=string.split(mobnum,',')
if name=='phymemo':
print 'The physical memory will be exhausted!'
msg=sysname+'服务器物理内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器物理内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='virmemo':
print 'The virtual memory will be exhausted!'
msg=sysname+'服务器虚拟内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器虚拟内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='cpuused':
print 'The cup time will be exhausted!'
msg=sysname+'服务器CPU使用率超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器CPU使用率超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='procnum':
msg=sysname+'服务器进程%s'%argb+'个数超过%s'%arga+'--%s'%nowtime
if not logflag:
log( sysname+'服务器进程%s个数超过%s'+'--%s\n',argb,arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='pmail':
print 'The pmail.status file check not passed!'
msg=sysname
failflag=arga
pmailflag=0
if failflag in [1,3,5,7,9,11,13,15]:
msg+=' mail for delete'
pmailflag=1
if failflag in [2,3,6,7,10,11,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='mail for move'
if failflag in [4,5,6,7,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='event que'
if failflag in [8,9,10,11,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='client'
msg+='超出上限'
if not logflag:
log(msg+'--%s\n',nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='disk':
print 'The free disk space is under the limit!'
msg=sysname+' '+arga+'--%s'%nowtime
if not logflag:
log(msg)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
logflag=1
i+=1
def emanateMsg(i,msg,otime,namelist,numlist,nowtime):
wtime=smlib.getConfigInt(STPROFILE,'time','wrongset',5)*60
sms_clnt=checkSendSM(i,wtime)
if not isinstance(sms_clnt,srpc.SRPCClient):
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'短信发送失败!Cant connect to %s send sms server! --%s \r\n',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'短信发送失败!Cant connect to send sms server! --%s \r\n',nowtime)
return
i=0
while i<len(numlist):
mobile=numlist[i]
alias=mobile
if i<len(namelist):
alias=namelist[i]
if not isinstance(sms_clnt,srpc.SRPCClient):
retcode=[0]
else:
retcode=[1]
retcode=smlib.SendSingle(sms_clnt,mobile,msg)
if len(retcode)<1 or retcode[0]!=1:
print 'send short message to %s failed!'%alias
log( 'send short message to %s failed, return %s !\r\n', alias,retcode)
i+=1
time.sleep(otime)
def checkSendSM(i,wtime):
sms_clnt=connectSendSMServer(i)
tt=time.localtime(time.time())
nowtime=str(tt[0])+'-'+str(tt[1])+'-'+str(tt[2])+' '+str(tt[3])+':'+str(tt[4])+':'+str(tt[5])
if not isinstance(sms_clnt,srpc.SRPCClient) and sms_clnt!='':
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'邮件系统告急!Cant connect to %s send sms server! --%s ',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'邮件系统告急!Cant connect to send sms server! --%s ',nowtime)
print "Can't send short messages!\r\n"
time.sleep(wtime)
return sms_clnt
def connectSendSMServer(i):
addr=getSendSMServer(i)
if addr[0]=='':
return ''
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return addr[0]
return cli
def getSendSMServer(i):
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
port=smlib.getConfigStr(STPROFILE,'sendsm','port','')
addrli=addr.split(',')
portli=port.split(',')
return (addrli[i],int(portli[i]))
def getAfter(v,after=4):#取小数点后after-1位数
snum=str(v)
index=snum.find('.')
if index==-1:
return snum
nindex=index+after
if nindex>len(snum)-1:
nindex=len(snum)-1
if int(snum[nindex])>4 and snum[nindex-1]!='9':
try:
val=int(snum[nindex-1])+1
except:
return snum
snum=snum[:nindex-1]+str(val)
else:
snum=snum[:nindex]
return snum
def getNowTime():
nowtime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
return nowtime
otime=smlib.getConfigInt(STPROFILE,'time','outset',300)
nowtime=getNowTime()
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
addrli=addr.split(',')
if addrli[0]=='':
return ''
i=0
logflag=0
while i<len(addrli):
mob='mob'+str(i)
mobname=smlib.getConfigStr(STPROFILE,mob,'name','')
namelist=string.split(mobname,',')
mobnum=smlib.getConfigStr(STPROFILE,mob,'number','')
numlist=string.split(mobnum,',')
if name=='phymemo':
print 'The physical memory will be exhausted!'
msg=sysname+'服务器物理内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器物理内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='virmemo':
print 'The virtual memory will be exhausted!'
msg=sysname+'服务器虚拟内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器虚拟内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='cpuused':
print 'The cup time will be exhausted!'
msg=sysname+'服务器CPU使用率超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器CPU使用率超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='procnum':
msg=sysname+'服务器进程%s'%argb+'个数超过%s'%arga+'--%s'%nowtime
if not logflag:
log( sysname+'服务器进程%s个数超过%s'+'--%s\n',argb,arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='pmail':
print 'The pmail.status file check not passed!'
msg=sysname
failflag=arga
pmailflag=0
if failflag in [1,3,5,7,9,11,13,15]:
msg+=' mail for delete'
pmailflag=1
if failflag in [2,3,6,7,10,11,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='mail for move'
if failflag in [4,5,6,7,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='event que'
if failflag in [8,9,10,11,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='client'
msg+='超出上限'
if not logflag:
log(msg+'--%s\n',nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='disk':
print 'The free disk space is under the limit!'
msg=sysname+' '+arga+'--%s'%nowtime
if not logflag:
log(msg)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
logflag=1
i+=1
def emanateMsg(i,msg,otime,namelist,numlist,nowtime):
wtime=smlib.getConfigInt(STPROFILE,'time','wrongset',5)*60
sms_clnt=checkSendSM(i,wtime)
if not isinstance(sms_clnt,srpc.SRPCClient):
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'短信发送失败!Cant connect to %s send sms server! --%s \r\n',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'短信发送失败!Cant connect to send sms server! --%s \r\n',nowtime)
return
i=0
while i<len(numlist):
mobile=numlist[i]
alias=mobile
if i<len(namelist):
alias=namelist[i]
if not isinstance(sms_clnt,srpc.SRPCClient):
retcode=[0]
else:
retcode=[1]
retcode=smlib.SendSingle(sms_clnt,mobile,msg)
if len(retcode)<1 or retcode[0]!=1:
print 'send short message to %s failed!'%alias
log( 'send short message to %s failed, return %s !\r\n', alias,retcode)
i+=1
time.sleep(otime)
def checkSendSM(i,wtime):
sms_clnt=connectSendSMServer(i)
tt=time.localtime(time.time())
nowtime=str(tt[0])+'-'+str(tt[1])+'-'+str(tt[2])+' '+str(tt[3])+':'+str(tt[4])+':'+str(tt[5])
if not isinstance(sms_clnt,srpc.SRPCClient) and sms_clnt!='':
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'邮件系统告急!Cant connect to %s send sms server! --%s ',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'邮件系统告急!Cant connect to send sms server! --%s ',nowtime)
print "Can't send short messages!\r\n"
time.sleep(wtime)
return sms_clnt
def connectSendSMServer(i):
addr=getSendSMServer(i)
if addr[0]=='':
return ''
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return addr[0]
return cli
def getSendSMServer(i):
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
port=smlib.getConfigStr(STPROFILE,'sendsm','port','')
addrli=addr.split(',')
portli=port.split(',')
return (addrli[i],int(portli[i]))
def getAfter(v,after=4):#取小数点后after-1位数
snum=str(v)
index=snum.find('.')
if index==-1:
return snum
nindex=index+after
if nindex>len(snum)-1:
nindex=len(snum)-1
if int(snum[nindex])>4 and snum[nindex-1]!='9':
try:
val=int(snum[nindex-1])+1
except:
return snum
snum=snum[:nindex-1]+str(val)
else:
snum=snum[:nindex]
return snum
def getNowTime():
nowtime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
return nowtime
#3
def pmailstatusCheck(ptimewait):
modifythreaddoing(1,'pmail.status文件检测检测程序')
try:
while 1:
ptimeoutcheck=smlib.getConfigInt(STPROFILE,'ptime','outcheck',3)
ptimeoutcount=smlib.getConfigInt(STPROFILE,'ptime','outcount',3)
checkfile=smlib.getConfigStr(STPROFILE,'pmail','status','')
count=0
while count<ptimeoutcount:
deletelimit=smlib.getConfigInt(STPROFILE,'delete','limit',100000)
movelimit=smlib.getConfigInt(STPROFILE,'move','limit',200)
quelimit=smlib.getConfigInt(STPROFILE,'que','limit',5)
clientlimit=smlib.getConfigInt(STPROFILE,'client','limit',200)
try:
pmailfile=open(checkfile,'r')
except:
print '文件%s不存在,请检查配置设置!'
time.sleep(60)
continue
lines=pmailfile.readlines()[:4]
pmailfile.close()
i=0
failflag=0
while i<len(lines):
thislineli=lines[i].split(':')
if len(thislineli)<2:
i+=1
continue
elif thislineli[0]=='mail for delete' and int(thislineli[1])>deletelimit:
failflag+=1
elif thislineli[0]=='mail for move' and int(thislineli[1])>movelimit:
failflag+=2
elif thislineli[0]=='event que' and int(thislineli[1].split(',')[0])>quelimit:
failflag+=4
elif thislineli[0]=='client' and int(thislineli[1])>clientlimit:
failflag+=8
else:
pass
i+=1
if not failflag:
break
else:
print 'check pmail.status again!'
time.sleep(ptimeoutcheck)
count+=1
if failflag:
logmsg=''
for x in lines:
logmsg+=x
log(logmsg.strip())
sendcycle('pmail',failflag)
time.sleep(ptimewait)
except:
msg='pmail.status文件检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'pmail.status文件检测检测程序')
def diskcheck(diskwait):
modifythreaddoing(1,'磁盘空间检测程序')
try:
volumepath=smlib.getConfigStr(STPROFILE,'volume','path','')
volumelimit=smlib.getConfigStr(STPROFILE,'volume','limit','')
diskname=smlib.getConfigStr(STPROFILE,'disk','name','')
disklimit=smlib.getConfigStr(STPROFILE,'disk','limit','')
diskoutcheck=smlib.getConfigInt(STPROFILE,'disk','outcheck',3)
diskoutcount=smlib.getConfigInt(STPROFILE,'disk','outcount',3)
if volumepath=='':
vplist=[]
vllist=[]
else:
vplist=volumepath.split(',')
vllist=volumelimit.split(',')
if diskname=='':
dnlist=[]
dllist=[]
else:
dnlist=diskname.split(',')
dllist=disklimit.split(',')
if (len(vplist)==0 and len(dnlist)==0) or len(vplist)!=len(vllist) or len(dnlist)!=len(dllist):
msg='磁盘检测线程退出,请检查文件配置!--%s'%getNowTime()
print msg
log(msg)
modifythreaddoing(-1,'磁盘空间检测程序')
return
wrongcount=0
while 1:
msg=''
i=0
while i<len(vplist):
ret=Getvolumefree(vplist[i])
if ret[0]<int(vllist[i]):
msg+='卷'+vplist[i]+'剩余空间低于下限%sM'%vllist[i]
break
i+=1
j=0
while j<len(dnlist):
ret=Getvolumefree(dnlist[j]+':')
if ret[0]<int(dllist[j]):
if msg!='':
break
msg+='磁盘'+dnlist[j]+'剩余空间低于下限%sM'%dllist[j]
break
j+=1
if msg!='':
wrongcount+=1
print msg+' 再次检测!'
time.sleep(diskoutcheck)
else:
wrongcount=0
if wrongcount>=diskoutcount:
wrongcount=0
sendcycle('disk',msg)
time.sleep(diskwait)
except:
msg='磁盘空间检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'磁盘空间检测程序')
def getlate(timea,timeb):
timelatecount=0
ta=timea[3]*3600+timea[4]*60+timea[5]
tb=timeb[3]*3600+timeb[4]*60+timeb[5]
if timeb[3]<timea[3]:
timelatecount=86400-ta+tb
else:
timelatecount=tb-ta
return timelatecount
def stopprocess(procid):
print 'stopprocess now!'
res=StopProcess(procid)
print 'return code:',res
if res==1:
return 1
else:
return 0
modifythreaddoing(1,'pmail.status文件检测检测程序')
try:
while 1:
ptimeoutcheck=smlib.getConfigInt(STPROFILE,'ptime','outcheck',3)
ptimeoutcount=smlib.getConfigInt(STPROFILE,'ptime','outcount',3)
checkfile=smlib.getConfigStr(STPROFILE,'pmail','status','')
count=0
while count<ptimeoutcount:
deletelimit=smlib.getConfigInt(STPROFILE,'delete','limit',100000)
movelimit=smlib.getConfigInt(STPROFILE,'move','limit',200)
quelimit=smlib.getConfigInt(STPROFILE,'que','limit',5)
clientlimit=smlib.getConfigInt(STPROFILE,'client','limit',200)
try:
pmailfile=open(checkfile,'r')
except:
print '文件%s不存在,请检查配置设置!'
time.sleep(60)
continue
lines=pmailfile.readlines()[:4]
pmailfile.close()
i=0
failflag=0
while i<len(lines):
thislineli=lines[i].split(':')
if len(thislineli)<2:
i+=1
continue
elif thislineli[0]=='mail for delete' and int(thislineli[1])>deletelimit:
failflag+=1
elif thislineli[0]=='mail for move' and int(thislineli[1])>movelimit:
failflag+=2
elif thislineli[0]=='event que' and int(thislineli[1].split(',')[0])>quelimit:
failflag+=4
elif thislineli[0]=='client' and int(thislineli[1])>clientlimit:
failflag+=8
else:
pass
i+=1
if not failflag:
break
else:
print 'check pmail.status again!'
time.sleep(ptimeoutcheck)
count+=1
if failflag:
logmsg=''
for x in lines:
logmsg+=x
log(logmsg.strip())
sendcycle('pmail',failflag)
time.sleep(ptimewait)
except:
msg='pmail.status文件检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'pmail.status文件检测检测程序')
def diskcheck(diskwait):
modifythreaddoing(1,'磁盘空间检测程序')
try:
volumepath=smlib.getConfigStr(STPROFILE,'volume','path','')
volumelimit=smlib.getConfigStr(STPROFILE,'volume','limit','')
diskname=smlib.getConfigStr(STPROFILE,'disk','name','')
disklimit=smlib.getConfigStr(STPROFILE,'disk','limit','')
diskoutcheck=smlib.getConfigInt(STPROFILE,'disk','outcheck',3)
diskoutcount=smlib.getConfigInt(STPROFILE,'disk','outcount',3)
if volumepath=='':
vplist=[]
vllist=[]
else:
vplist=volumepath.split(',')
vllist=volumelimit.split(',')
if diskname=='':
dnlist=[]
dllist=[]
else:
dnlist=diskname.split(',')
dllist=disklimit.split(',')
if (len(vplist)==0 and len(dnlist)==0) or len(vplist)!=len(vllist) or len(dnlist)!=len(dllist):
msg='磁盘检测线程退出,请检查文件配置!--%s'%getNowTime()
print msg
log(msg)
modifythreaddoing(-1,'磁盘空间检测程序')
return
wrongcount=0
while 1:
msg=''
i=0
while i<len(vplist):
ret=Getvolumefree(vplist[i])
if ret[0]<int(vllist[i]):
msg+='卷'+vplist[i]+'剩余空间低于下限%sM'%vllist[i]
break
i+=1
j=0
while j<len(dnlist):
ret=Getvolumefree(dnlist[j]+':')
if ret[0]<int(dllist[j]):
if msg!='':
break
msg+='磁盘'+dnlist[j]+'剩余空间低于下限%sM'%dllist[j]
break
j+=1
if msg!='':
wrongcount+=1
print msg+' 再次检测!'
time.sleep(diskoutcheck)
else:
wrongcount=0
if wrongcount>=diskoutcount:
wrongcount=0
sendcycle('disk',msg)
time.sleep(diskwait)
except:
msg='磁盘空间检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'磁盘空间检测程序')
def getlate(timea,timeb):
timelatecount=0
ta=timea[3]*3600+timea[4]*60+timea[5]
tb=timeb[3]*3600+timeb[4]*60+timeb[5]
if timeb[3]<timea[3]:
timelatecount=86400-ta+tb
else:
timelatecount=tb-ta
return timelatecount
def stopprocess(procid):
print 'stopprocess now!'
res=StopProcess(procid)
print 'return code:',res
if res==1:
return 1
else:
return 0
#4
收藏ing~~~
#5
def kill(killwait):
modifythreaddoing(1,'杀进程程序')
try:
cpualllimit=smlib.getConfigInt(STPROFILE,'cpuall','limit',90) #总的CPU使用率上限
cpulimit=smlib.getConfigInt(STPROFILE,'cpu','limit',45) #单个进程cpu使用率上限
memolimit=smlib.getConfigInt(STPROFILE,'memo','limit',500) #物理内存使用上限,单位(M)
fmemolimit=smlib.getConfigInt(STPROFILE,'fmemo','limit',500) #虚拟内存使用上限,单位(M)
proccyc=smlib.getConfigInt(STPROFILE,'proc','cyc',3) #对该进程连续检测几次后还有问题则杀掉
stopproc=smlib.getConfigStr(STPROFILE,'stop','proc','pythonw.exe') #要停止的进程名
li=[]
num=[]
while 1:
ret=getcompproc()
cpuused=GetCpuState()
procname=ret[0].split(',')
procid=ret[1].split(',')
memoli=ret[2].split(',')
fmemoli=ret[3].split(',')
cpuli=ret[4].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
procid.remove(procid[i])
i+=1
print 'li:',li
print 'num:',num
print '\n'
i=0
while i<len(memoli):
j=0
while j<len(li):
if li[j] not in procid:
li.remove(li[j])
num.remove(num[j])
j+=1
## if procname[i].capitalize()==stopproc.capitalize() and int(cpuused)>cpualllimit and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024) and int(cpuli[i])>cpulimit:
if procname[i].capitalize()==stopproc.capitalize() and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024):
if procid[i] not in li:
li.append(procid[i])
num.append([procid[i],0])
else:
index=li.index(procid[i])
if num[index][1]>proccyc:
ret=stopprocess(int(procid[i]))
if ret:
msg='成功停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
else:
msg='失败停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
log(msg)
li.remove(procid[i])
num.remove(num[index])
else:
num[index][1]=num[index][1]+1
elif procname[i].capitalize()==stopproc.capitalize() and procid[i] in li:
index=li.index(procid[i])
li.remove(procid[i])
num.remove(num[index])
i+=1
time.sleep(killwait)
except:
## traceback.print_exc()
msg='杀进程线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'杀进程程序')
def main():
global logfile, logfp
print 'syscheck version:',syscheck_version
timecyc=smlib.getConfigStr(STPROFILE,'time','cyc','3')
timewait=smlib.getConfigInt(STPROFILE,'time','wait',120)
ptimewait=smlib.getConfigInt(STPROFILE,'ptime','wait',120)
diskwait=smlib.getConfigInt(STPROFILE,'disk','wait',5)*60
killwait=smlib.getConfigInt(STPROFILE,'kill','wait',5)
sysflag=smlib.getConfigInt(STPROFILE,'sys','flag',1)
diskflag=smlib.getConfigInt(STPROFILE,'disk','flag',1)
pmailflag=smlib.getConfigInt(STPROFILE,'pmail','flag',1)
killflag=smlib.getConfigInt(STPROFILE,'kill','flag',1)
logfile='sysstatus.log'
timecycf=float(timecyc)*3600
timescyc=int(timecycf/10)
timecount=0
try:
if sysflag:
thread.start_new_thread(usingCheck,(timewait,))
if diskflag:
thread.start_new_thread(diskcheck,(diskwait,))
if pmailflag:
thread.start_new_thread(pmailstatusCheck,(ptimewait,))
if killflag:
thread.start_new_thread(kill,(killwait,))
except:
msg='主线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
while (1):
threadnum=len(threaddoing)
if threadnum:
print '当前运行中的线程:',
else:
print '当前运行中的线程:'
try:
i=0
while i<threadnum:
if i==threadnum-1:
print threaddoing[i]
else:
print threaddoing[i],
i+=1
except:
pass
timecount+=1
if timecount==timescyc:
return
time.sleep(10)
if __name__=='__main__':main()
if logfp:
logfp.close()
(完)
modifythreaddoing(1,'杀进程程序')
try:
cpualllimit=smlib.getConfigInt(STPROFILE,'cpuall','limit',90) #总的CPU使用率上限
cpulimit=smlib.getConfigInt(STPROFILE,'cpu','limit',45) #单个进程cpu使用率上限
memolimit=smlib.getConfigInt(STPROFILE,'memo','limit',500) #物理内存使用上限,单位(M)
fmemolimit=smlib.getConfigInt(STPROFILE,'fmemo','limit',500) #虚拟内存使用上限,单位(M)
proccyc=smlib.getConfigInt(STPROFILE,'proc','cyc',3) #对该进程连续检测几次后还有问题则杀掉
stopproc=smlib.getConfigStr(STPROFILE,'stop','proc','pythonw.exe') #要停止的进程名
li=[]
num=[]
while 1:
ret=getcompproc()
cpuused=GetCpuState()
procname=ret[0].split(',')
procid=ret[1].split(',')
memoli=ret[2].split(',')
fmemoli=ret[3].split(',')
cpuli=ret[4].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
procid.remove(procid[i])
i+=1
print 'li:',li
print 'num:',num
print '\n'
i=0
while i<len(memoli):
j=0
while j<len(li):
if li[j] not in procid:
li.remove(li[j])
num.remove(num[j])
j+=1
## if procname[i].capitalize()==stopproc.capitalize() and int(cpuused)>cpualllimit and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024) and int(cpuli[i])>cpulimit:
if procname[i].capitalize()==stopproc.capitalize() and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024):
if procid[i] not in li:
li.append(procid[i])
num.append([procid[i],0])
else:
index=li.index(procid[i])
if num[index][1]>proccyc:
ret=stopprocess(int(procid[i]))
if ret:
msg='成功停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
else:
msg='失败停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
log(msg)
li.remove(procid[i])
num.remove(num[index])
else:
num[index][1]=num[index][1]+1
elif procname[i].capitalize()==stopproc.capitalize() and procid[i] in li:
index=li.index(procid[i])
li.remove(procid[i])
num.remove(num[index])
i+=1
time.sleep(killwait)
except:
## traceback.print_exc()
msg='杀进程线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'杀进程程序')
def main():
global logfile, logfp
print 'syscheck version:',syscheck_version
timecyc=smlib.getConfigStr(STPROFILE,'time','cyc','3')
timewait=smlib.getConfigInt(STPROFILE,'time','wait',120)
ptimewait=smlib.getConfigInt(STPROFILE,'ptime','wait',120)
diskwait=smlib.getConfigInt(STPROFILE,'disk','wait',5)*60
killwait=smlib.getConfigInt(STPROFILE,'kill','wait',5)
sysflag=smlib.getConfigInt(STPROFILE,'sys','flag',1)
diskflag=smlib.getConfigInt(STPROFILE,'disk','flag',1)
pmailflag=smlib.getConfigInt(STPROFILE,'pmail','flag',1)
killflag=smlib.getConfigInt(STPROFILE,'kill','flag',1)
logfile='sysstatus.log'
timecycf=float(timecyc)*3600
timescyc=int(timecycf/10)
timecount=0
try:
if sysflag:
thread.start_new_thread(usingCheck,(timewait,))
if diskflag:
thread.start_new_thread(diskcheck,(diskwait,))
if pmailflag:
thread.start_new_thread(pmailstatusCheck,(ptimewait,))
if killflag:
thread.start_new_thread(kill,(killwait,))
except:
msg='主线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
while (1):
threadnum=len(threaddoing)
if threadnum:
print '当前运行中的线程:',
else:
print '当前运行中的线程:'
try:
i=0
while i<threadnum:
if i==threadnum-1:
print threaddoing[i]
else:
print threaddoing[i],
i+=1
except:
pass
timecount+=1
if timecount==timescyc:
return
time.sleep(10)
if __name__=='__main__':main()
if logfp:
logfp.close()
(完)
#6
好东东....
#7
这个程序是从哪儿找到的?里面用到了好几个模块,比如smlib,netman等等,但是在这里面却又没有这些模块的代码,我想要知道这几个模块的代码在哪儿,谢谢
#8
呵呵,谢了
#9
嘿嘿,关于上面的模块,你可以到mail.trasin.net/soft看看
那里面还有很多别的PYTHON程序哦
那里面还有很多别的PYTHON程序哦
#10
找了一圈没有找到.看到有个python的目录,可进去了也没有找到那上面的几个模块,倒是python的其他的相关软件都有
#11
都是老的版本的,没什么用
#12
这CSDN什么时候能对代码格式支持一下?还好意思说是中国最大的软件论坛呢.
#13
回复人: wfh_178() ( )
都是老的版本的,没什么
老版本好啊!要不你把新版本放上来好了
都是老的版本的,没什么
老版本好啊!要不你把新版本放上来好了
#14
mark
#1
def modifythreaddoing(flag,content):
global threaddoing
while 1:
if (threaddoing_lock.acquire(0)):
if flag==1:
threaddoing.append(content)
else:
threaddoing.remove(content)
threaddoing_lock.release()
break
else:
time.sleep(1)
def usingCheck(timewait):
modifythreaddoing(1,'CPU、内存检测程序')
try:
while 1:
timeoutcheck=smlib.getConfigInt(STPROFILE,'time','outcheck',30)
timeoutcount=smlib.getConfigInt(STPROFILE,'time','outcount',1)
count=0
while count<timeoutcount:
phymemolimit=smlib.getConfigInt(STPROFILE,'phymemo','limit',95) #使用率参数
virmemolimit=smlib.getConfigInt(STPROFILE,'virmemo','limit',95)
cpuusedlimit=smlib.getConfigInt(STPROFILE,'cpuused','limit',95)
proccheck=smlib.getConfigStr(STPROFILE,'proc','check','')
proclimit=smlib.getConfigStr(STPROFILE,'proc','limit','')
ret=GlobalMemoryStatus()
totalphymemo=ret[0]
freephymemo=ret[1]
totalvirmemo=ret[2]
freevirmemo=ret[3]
proccheckli=proccheck.split(',')
proclimitli=proclimit.split(',')
if proccheck=='':
proccheckli=[]
res=getcompproc()
procname=res[0].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
i+=1
numli=[]
i=0
while i<len(proccheckli):
numli.append(0)
j=0
while j<len(procname):
if procname[j].capitalize()==proccheckli[i].capitalize():
numli[i]+=1
j+=1
i+=1
usedphymemo=totalphymemo-freephymemo
usedvirmemo=totalvirmemo-freevirmemo
prate=usedphymemo/float(totalphymemo)*100
vrate=usedvirmemo/float(totalvirmemo)*100
cpuused=GetCpuState()
sprate=getAfter(prate)
svrate=getAfter(vrate)
outflag=0
i=0
while i<len(numli):
if numli[i]>=int(proclimitli[i]):
outflag=1
proctuple=(int(proclimitli[i]),proccheckli[i])
break
i+=1
if prate>phymemolimit or vrate>virmemolimit or cpuused>cpuusedlimit or outflag:
if prate>phymemolimit:
print 'Check physical memory using rate again!'
elif vrate>virmemolimit:
print 'Check virtual memory using rate again!'
elif cpuused>cpuusedlimit:
print 'Check cup time using rate again!'
elif outflag:
print 'Count process %s numbers again!'%proctuple[1]
count+=1
time.sleep(timeoutcheck)
else:
break
if prate>phymemolimit:
sendcycle('phymemo',phymemolimit)
if vrate>virmemolimit:
sendcycle('virmemo',virmemolimit)
if cpuused>cpuusedlimit:
sendcycle('cpuused',cpuusedlimit)
if outflag:
sendcycle('procnum',proctuple[0],proctuple[1])
print 'The using rate of physical memory is %s'%sprate+'%'
print 'The using rate of virtual memory is %s'%svrate+'%'
print 'The using rate of cpu is %d'%cpuused+'%'
i=0
while i<len(proccheckli):
print 'The number of process %s is %s!'%(proccheckli[i],numli[i])
i+=1
print '\r\n'
time.sleep(timewait)
except:
msg='CPU、内存检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'CPU、内存检测程序')
global threaddoing
while 1:
if (threaddoing_lock.acquire(0)):
if flag==1:
threaddoing.append(content)
else:
threaddoing.remove(content)
threaddoing_lock.release()
break
else:
time.sleep(1)
def usingCheck(timewait):
modifythreaddoing(1,'CPU、内存检测程序')
try:
while 1:
timeoutcheck=smlib.getConfigInt(STPROFILE,'time','outcheck',30)
timeoutcount=smlib.getConfigInt(STPROFILE,'time','outcount',1)
count=0
while count<timeoutcount:
phymemolimit=smlib.getConfigInt(STPROFILE,'phymemo','limit',95) #使用率参数
virmemolimit=smlib.getConfigInt(STPROFILE,'virmemo','limit',95)
cpuusedlimit=smlib.getConfigInt(STPROFILE,'cpuused','limit',95)
proccheck=smlib.getConfigStr(STPROFILE,'proc','check','')
proclimit=smlib.getConfigStr(STPROFILE,'proc','limit','')
ret=GlobalMemoryStatus()
totalphymemo=ret[0]
freephymemo=ret[1]
totalvirmemo=ret[2]
freevirmemo=ret[3]
proccheckli=proccheck.split(',')
proclimitli=proclimit.split(',')
if proccheck=='':
proccheckli=[]
res=getcompproc()
procname=res[0].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
i+=1
numli=[]
i=0
while i<len(proccheckli):
numli.append(0)
j=0
while j<len(procname):
if procname[j].capitalize()==proccheckli[i].capitalize():
numli[i]+=1
j+=1
i+=1
usedphymemo=totalphymemo-freephymemo
usedvirmemo=totalvirmemo-freevirmemo
prate=usedphymemo/float(totalphymemo)*100
vrate=usedvirmemo/float(totalvirmemo)*100
cpuused=GetCpuState()
sprate=getAfter(prate)
svrate=getAfter(vrate)
outflag=0
i=0
while i<len(numli):
if numli[i]>=int(proclimitli[i]):
outflag=1
proctuple=(int(proclimitli[i]),proccheckli[i])
break
i+=1
if prate>phymemolimit or vrate>virmemolimit or cpuused>cpuusedlimit or outflag:
if prate>phymemolimit:
print 'Check physical memory using rate again!'
elif vrate>virmemolimit:
print 'Check virtual memory using rate again!'
elif cpuused>cpuusedlimit:
print 'Check cup time using rate again!'
elif outflag:
print 'Count process %s numbers again!'%proctuple[1]
count+=1
time.sleep(timeoutcheck)
else:
break
if prate>phymemolimit:
sendcycle('phymemo',phymemolimit)
if vrate>virmemolimit:
sendcycle('virmemo',virmemolimit)
if cpuused>cpuusedlimit:
sendcycle('cpuused',cpuusedlimit)
if outflag:
sendcycle('procnum',proctuple[0],proctuple[1])
print 'The using rate of physical memory is %s'%sprate+'%'
print 'The using rate of virtual memory is %s'%svrate+'%'
print 'The using rate of cpu is %d'%cpuused+'%'
i=0
while i<len(proccheckli):
print 'The number of process %s is %s!'%(proccheckli[i],numli[i])
i+=1
print '\r\n'
time.sleep(timewait)
except:
msg='CPU、内存检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'CPU、内存检测程序')
#2
def sendcycle(name,arga,argb=''):
otime=smlib.getConfigInt(STPROFILE,'time','outset',300)
nowtime=getNowTime()
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
addrli=addr.split(',')
if addrli[0]=='':
return ''
i=0
logflag=0
while i<len(addrli):
mob='mob'+str(i)
mobname=smlib.getConfigStr(STPROFILE,mob,'name','')
namelist=string.split(mobname,',')
mobnum=smlib.getConfigStr(STPROFILE,mob,'number','')
numlist=string.split(mobnum,',')
if name=='phymemo':
print 'The physical memory will be exhausted!'
msg=sysname+'服务器物理内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器物理内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='virmemo':
print 'The virtual memory will be exhausted!'
msg=sysname+'服务器虚拟内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器虚拟内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='cpuused':
print 'The cup time will be exhausted!'
msg=sysname+'服务器CPU使用率超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器CPU使用率超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='procnum':
msg=sysname+'服务器进程%s'%argb+'个数超过%s'%arga+'--%s'%nowtime
if not logflag:
log( sysname+'服务器进程%s个数超过%s'+'--%s\n',argb,arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='pmail':
print 'The pmail.status file check not passed!'
msg=sysname
failflag=arga
pmailflag=0
if failflag in [1,3,5,7,9,11,13,15]:
msg+=' mail for delete'
pmailflag=1
if failflag in [2,3,6,7,10,11,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='mail for move'
if failflag in [4,5,6,7,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='event que'
if failflag in [8,9,10,11,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='client'
msg+='超出上限'
if not logflag:
log(msg+'--%s\n',nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='disk':
print 'The free disk space is under the limit!'
msg=sysname+' '+arga+'--%s'%nowtime
if not logflag:
log(msg)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
logflag=1
i+=1
def emanateMsg(i,msg,otime,namelist,numlist,nowtime):
wtime=smlib.getConfigInt(STPROFILE,'time','wrongset',5)*60
sms_clnt=checkSendSM(i,wtime)
if not isinstance(sms_clnt,srpc.SRPCClient):
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'短信发送失败!Cant connect to %s send sms server! --%s \r\n',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'短信发送失败!Cant connect to send sms server! --%s \r\n',nowtime)
return
i=0
while i<len(numlist):
mobile=numlist[i]
alias=mobile
if i<len(namelist):
alias=namelist[i]
if not isinstance(sms_clnt,srpc.SRPCClient):
retcode=[0]
else:
retcode=[1]
retcode=smlib.SendSingle(sms_clnt,mobile,msg)
if len(retcode)<1 or retcode[0]!=1:
print 'send short message to %s failed!'%alias
log( 'send short message to %s failed, return %s !\r\n', alias,retcode)
i+=1
time.sleep(otime)
def checkSendSM(i,wtime):
sms_clnt=connectSendSMServer(i)
tt=time.localtime(time.time())
nowtime=str(tt[0])+'-'+str(tt[1])+'-'+str(tt[2])+' '+str(tt[3])+':'+str(tt[4])+':'+str(tt[5])
if not isinstance(sms_clnt,srpc.SRPCClient) and sms_clnt!='':
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'邮件系统告急!Cant connect to %s send sms server! --%s ',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'邮件系统告急!Cant connect to send sms server! --%s ',nowtime)
print "Can't send short messages!\r\n"
time.sleep(wtime)
return sms_clnt
def connectSendSMServer(i):
addr=getSendSMServer(i)
if addr[0]=='':
return ''
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return addr[0]
return cli
def getSendSMServer(i):
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
port=smlib.getConfigStr(STPROFILE,'sendsm','port','')
addrli=addr.split(',')
portli=port.split(',')
return (addrli[i],int(portli[i]))
def getAfter(v,after=4):#取小数点后after-1位数
snum=str(v)
index=snum.find('.')
if index==-1:
return snum
nindex=index+after
if nindex>len(snum)-1:
nindex=len(snum)-1
if int(snum[nindex])>4 and snum[nindex-1]!='9':
try:
val=int(snum[nindex-1])+1
except:
return snum
snum=snum[:nindex-1]+str(val)
else:
snum=snum[:nindex]
return snum
def getNowTime():
nowtime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
return nowtime
otime=smlib.getConfigInt(STPROFILE,'time','outset',300)
nowtime=getNowTime()
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
addrli=addr.split(',')
if addrli[0]=='':
return ''
i=0
logflag=0
while i<len(addrli):
mob='mob'+str(i)
mobname=smlib.getConfigStr(STPROFILE,mob,'name','')
namelist=string.split(mobname,',')
mobnum=smlib.getConfigStr(STPROFILE,mob,'number','')
numlist=string.split(mobnum,',')
if name=='phymemo':
print 'The physical memory will be exhausted!'
msg=sysname+'服务器物理内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器物理内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='virmemo':
print 'The virtual memory will be exhausted!'
msg=sysname+'服务器虚拟内存使用超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器虚拟内存使用超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='cpuused':
print 'The cup time will be exhausted!'
msg=sysname+'服务器CPU使用率超过%s'%arga+'%'+'--%s'%nowtime
if not logflag:
log( sysname+'服务器CPU使用率超过%s'+'%%'+'--%s\n',arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='procnum':
msg=sysname+'服务器进程%s'%argb+'个数超过%s'%arga+'--%s'%nowtime
if not logflag:
log( sysname+'服务器进程%s个数超过%s'+'--%s\n',argb,arga,nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='pmail':
print 'The pmail.status file check not passed!'
msg=sysname
failflag=arga
pmailflag=0
if failflag in [1,3,5,7,9,11,13,15]:
msg+=' mail for delete'
pmailflag=1
if failflag in [2,3,6,7,10,11,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='mail for move'
if failflag in [4,5,6,7,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='event que'
if failflag in [8,9,10,11,12,13,14,15]:
if pmailflag:
msg+=','
else:
msg+=' '
pmailflag=1
msg+='client'
msg+='超出上限'
if not logflag:
log(msg+'--%s\n',nowtime)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
elif name=='disk':
print 'The free disk space is under the limit!'
msg=sysname+' '+arga+'--%s'%nowtime
if not logflag:
log(msg)
retcode=emanateMsg(i,msg,otime,namelist,numlist,nowtime)
logflag=1
i+=1
def emanateMsg(i,msg,otime,namelist,numlist,nowtime):
wtime=smlib.getConfigInt(STPROFILE,'time','wrongset',5)*60
sms_clnt=checkSendSM(i,wtime)
if not isinstance(sms_clnt,srpc.SRPCClient):
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'短信发送失败!Cant connect to %s send sms server! --%s \r\n',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'短信发送失败!Cant connect to send sms server! --%s \r\n',nowtime)
return
i=0
while i<len(numlist):
mobile=numlist[i]
alias=mobile
if i<len(namelist):
alias=namelist[i]
if not isinstance(sms_clnt,srpc.SRPCClient):
retcode=[0]
else:
retcode=[1]
retcode=smlib.SendSingle(sms_clnt,mobile,msg)
if len(retcode)<1 or retcode[0]!=1:
print 'send short message to %s failed!'%alias
log( 'send short message to %s failed, return %s !\r\n', alias,retcode)
i+=1
time.sleep(otime)
def checkSendSM(i,wtime):
sms_clnt=connectSendSMServer(i)
tt=time.localtime(time.time())
nowtime=str(tt[0])+'-'+str(tt[1])+'-'+str(tt[2])+' '+str(tt[3])+':'+str(tt[4])+':'+str(tt[5])
if not isinstance(sms_clnt,srpc.SRPCClient) and sms_clnt!='':
if type(sms_clnt)==type(''):
print "Can't connect to %s send sms server!--%s\r\n" % (sms_clnt,nowtime)
log( sysname+'邮件系统告急!Cant connect to %s send sms server! --%s ',sms_clnt,nowtime)
else:
print "Can't connect to send sms server!--%s\r\n" % nowtime
log( sysname+'邮件系统告急!Cant connect to send sms server! --%s ',nowtime)
print "Can't send short messages!\r\n"
time.sleep(wtime)
return sms_clnt
def connectSendSMServer(i):
addr=getSendSMServer(i)
if addr[0]=='':
return ''
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return addr[0]
return cli
def getSendSMServer(i):
addr=smlib.getConfigStr(STPROFILE,'sendsm','server','')
port=smlib.getConfigStr(STPROFILE,'sendsm','port','')
addrli=addr.split(',')
portli=port.split(',')
return (addrli[i],int(portli[i]))
def getAfter(v,after=4):#取小数点后after-1位数
snum=str(v)
index=snum.find('.')
if index==-1:
return snum
nindex=index+after
if nindex>len(snum)-1:
nindex=len(snum)-1
if int(snum[nindex])>4 and snum[nindex-1]!='9':
try:
val=int(snum[nindex-1])+1
except:
return snum
snum=snum[:nindex-1]+str(val)
else:
snum=snum[:nindex]
return snum
def getNowTime():
nowtime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
return nowtime
#3
def pmailstatusCheck(ptimewait):
modifythreaddoing(1,'pmail.status文件检测检测程序')
try:
while 1:
ptimeoutcheck=smlib.getConfigInt(STPROFILE,'ptime','outcheck',3)
ptimeoutcount=smlib.getConfigInt(STPROFILE,'ptime','outcount',3)
checkfile=smlib.getConfigStr(STPROFILE,'pmail','status','')
count=0
while count<ptimeoutcount:
deletelimit=smlib.getConfigInt(STPROFILE,'delete','limit',100000)
movelimit=smlib.getConfigInt(STPROFILE,'move','limit',200)
quelimit=smlib.getConfigInt(STPROFILE,'que','limit',5)
clientlimit=smlib.getConfigInt(STPROFILE,'client','limit',200)
try:
pmailfile=open(checkfile,'r')
except:
print '文件%s不存在,请检查配置设置!'
time.sleep(60)
continue
lines=pmailfile.readlines()[:4]
pmailfile.close()
i=0
failflag=0
while i<len(lines):
thislineli=lines[i].split(':')
if len(thislineli)<2:
i+=1
continue
elif thislineli[0]=='mail for delete' and int(thislineli[1])>deletelimit:
failflag+=1
elif thislineli[0]=='mail for move' and int(thislineli[1])>movelimit:
failflag+=2
elif thislineli[0]=='event que' and int(thislineli[1].split(',')[0])>quelimit:
failflag+=4
elif thislineli[0]=='client' and int(thislineli[1])>clientlimit:
failflag+=8
else:
pass
i+=1
if not failflag:
break
else:
print 'check pmail.status again!'
time.sleep(ptimeoutcheck)
count+=1
if failflag:
logmsg=''
for x in lines:
logmsg+=x
log(logmsg.strip())
sendcycle('pmail',failflag)
time.sleep(ptimewait)
except:
msg='pmail.status文件检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'pmail.status文件检测检测程序')
def diskcheck(diskwait):
modifythreaddoing(1,'磁盘空间检测程序')
try:
volumepath=smlib.getConfigStr(STPROFILE,'volume','path','')
volumelimit=smlib.getConfigStr(STPROFILE,'volume','limit','')
diskname=smlib.getConfigStr(STPROFILE,'disk','name','')
disklimit=smlib.getConfigStr(STPROFILE,'disk','limit','')
diskoutcheck=smlib.getConfigInt(STPROFILE,'disk','outcheck',3)
diskoutcount=smlib.getConfigInt(STPROFILE,'disk','outcount',3)
if volumepath=='':
vplist=[]
vllist=[]
else:
vplist=volumepath.split(',')
vllist=volumelimit.split(',')
if diskname=='':
dnlist=[]
dllist=[]
else:
dnlist=diskname.split(',')
dllist=disklimit.split(',')
if (len(vplist)==0 and len(dnlist)==0) or len(vplist)!=len(vllist) or len(dnlist)!=len(dllist):
msg='磁盘检测线程退出,请检查文件配置!--%s'%getNowTime()
print msg
log(msg)
modifythreaddoing(-1,'磁盘空间检测程序')
return
wrongcount=0
while 1:
msg=''
i=0
while i<len(vplist):
ret=Getvolumefree(vplist[i])
if ret[0]<int(vllist[i]):
msg+='卷'+vplist[i]+'剩余空间低于下限%sM'%vllist[i]
break
i+=1
j=0
while j<len(dnlist):
ret=Getvolumefree(dnlist[j]+':')
if ret[0]<int(dllist[j]):
if msg!='':
break
msg+='磁盘'+dnlist[j]+'剩余空间低于下限%sM'%dllist[j]
break
j+=1
if msg!='':
wrongcount+=1
print msg+' 再次检测!'
time.sleep(diskoutcheck)
else:
wrongcount=0
if wrongcount>=diskoutcount:
wrongcount=0
sendcycle('disk',msg)
time.sleep(diskwait)
except:
msg='磁盘空间检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'磁盘空间检测程序')
def getlate(timea,timeb):
timelatecount=0
ta=timea[3]*3600+timea[4]*60+timea[5]
tb=timeb[3]*3600+timeb[4]*60+timeb[5]
if timeb[3]<timea[3]:
timelatecount=86400-ta+tb
else:
timelatecount=tb-ta
return timelatecount
def stopprocess(procid):
print 'stopprocess now!'
res=StopProcess(procid)
print 'return code:',res
if res==1:
return 1
else:
return 0
modifythreaddoing(1,'pmail.status文件检测检测程序')
try:
while 1:
ptimeoutcheck=smlib.getConfigInt(STPROFILE,'ptime','outcheck',3)
ptimeoutcount=smlib.getConfigInt(STPROFILE,'ptime','outcount',3)
checkfile=smlib.getConfigStr(STPROFILE,'pmail','status','')
count=0
while count<ptimeoutcount:
deletelimit=smlib.getConfigInt(STPROFILE,'delete','limit',100000)
movelimit=smlib.getConfigInt(STPROFILE,'move','limit',200)
quelimit=smlib.getConfigInt(STPROFILE,'que','limit',5)
clientlimit=smlib.getConfigInt(STPROFILE,'client','limit',200)
try:
pmailfile=open(checkfile,'r')
except:
print '文件%s不存在,请检查配置设置!'
time.sleep(60)
continue
lines=pmailfile.readlines()[:4]
pmailfile.close()
i=0
failflag=0
while i<len(lines):
thislineli=lines[i].split(':')
if len(thislineli)<2:
i+=1
continue
elif thislineli[0]=='mail for delete' and int(thislineli[1])>deletelimit:
failflag+=1
elif thislineli[0]=='mail for move' and int(thislineli[1])>movelimit:
failflag+=2
elif thislineli[0]=='event que' and int(thislineli[1].split(',')[0])>quelimit:
failflag+=4
elif thislineli[0]=='client' and int(thislineli[1])>clientlimit:
failflag+=8
else:
pass
i+=1
if not failflag:
break
else:
print 'check pmail.status again!'
time.sleep(ptimeoutcheck)
count+=1
if failflag:
logmsg=''
for x in lines:
logmsg+=x
log(logmsg.strip())
sendcycle('pmail',failflag)
time.sleep(ptimewait)
except:
msg='pmail.status文件检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'pmail.status文件检测检测程序')
def diskcheck(diskwait):
modifythreaddoing(1,'磁盘空间检测程序')
try:
volumepath=smlib.getConfigStr(STPROFILE,'volume','path','')
volumelimit=smlib.getConfigStr(STPROFILE,'volume','limit','')
diskname=smlib.getConfigStr(STPROFILE,'disk','name','')
disklimit=smlib.getConfigStr(STPROFILE,'disk','limit','')
diskoutcheck=smlib.getConfigInt(STPROFILE,'disk','outcheck',3)
diskoutcount=smlib.getConfigInt(STPROFILE,'disk','outcount',3)
if volumepath=='':
vplist=[]
vllist=[]
else:
vplist=volumepath.split(',')
vllist=volumelimit.split(',')
if diskname=='':
dnlist=[]
dllist=[]
else:
dnlist=diskname.split(',')
dllist=disklimit.split(',')
if (len(vplist)==0 and len(dnlist)==0) or len(vplist)!=len(vllist) or len(dnlist)!=len(dllist):
msg='磁盘检测线程退出,请检查文件配置!--%s'%getNowTime()
print msg
log(msg)
modifythreaddoing(-1,'磁盘空间检测程序')
return
wrongcount=0
while 1:
msg=''
i=0
while i<len(vplist):
ret=Getvolumefree(vplist[i])
if ret[0]<int(vllist[i]):
msg+='卷'+vplist[i]+'剩余空间低于下限%sM'%vllist[i]
break
i+=1
j=0
while j<len(dnlist):
ret=Getvolumefree(dnlist[j]+':')
if ret[0]<int(dllist[j]):
if msg!='':
break
msg+='磁盘'+dnlist[j]+'剩余空间低于下限%sM'%dllist[j]
break
j+=1
if msg!='':
wrongcount+=1
print msg+' 再次检测!'
time.sleep(diskoutcheck)
else:
wrongcount=0
if wrongcount>=diskoutcount:
wrongcount=0
sendcycle('disk',msg)
time.sleep(diskwait)
except:
msg='磁盘空间检测线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'磁盘空间检测程序')
def getlate(timea,timeb):
timelatecount=0
ta=timea[3]*3600+timea[4]*60+timea[5]
tb=timeb[3]*3600+timeb[4]*60+timeb[5]
if timeb[3]<timea[3]:
timelatecount=86400-ta+tb
else:
timelatecount=tb-ta
return timelatecount
def stopprocess(procid):
print 'stopprocess now!'
res=StopProcess(procid)
print 'return code:',res
if res==1:
return 1
else:
return 0
#4
收藏ing~~~
#5
def kill(killwait):
modifythreaddoing(1,'杀进程程序')
try:
cpualllimit=smlib.getConfigInt(STPROFILE,'cpuall','limit',90) #总的CPU使用率上限
cpulimit=smlib.getConfigInt(STPROFILE,'cpu','limit',45) #单个进程cpu使用率上限
memolimit=smlib.getConfigInt(STPROFILE,'memo','limit',500) #物理内存使用上限,单位(M)
fmemolimit=smlib.getConfigInt(STPROFILE,'fmemo','limit',500) #虚拟内存使用上限,单位(M)
proccyc=smlib.getConfigInt(STPROFILE,'proc','cyc',3) #对该进程连续检测几次后还有问题则杀掉
stopproc=smlib.getConfigStr(STPROFILE,'stop','proc','pythonw.exe') #要停止的进程名
li=[]
num=[]
while 1:
ret=getcompproc()
cpuused=GetCpuState()
procname=ret[0].split(',')
procid=ret[1].split(',')
memoli=ret[2].split(',')
fmemoli=ret[3].split(',')
cpuli=ret[4].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
procid.remove(procid[i])
i+=1
print 'li:',li
print 'num:',num
print '\n'
i=0
while i<len(memoli):
j=0
while j<len(li):
if li[j] not in procid:
li.remove(li[j])
num.remove(num[j])
j+=1
## if procname[i].capitalize()==stopproc.capitalize() and int(cpuused)>cpualllimit and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024) and int(cpuli[i])>cpulimit:
if procname[i].capitalize()==stopproc.capitalize() and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024):
if procid[i] not in li:
li.append(procid[i])
num.append([procid[i],0])
else:
index=li.index(procid[i])
if num[index][1]>proccyc:
ret=stopprocess(int(procid[i]))
if ret:
msg='成功停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
else:
msg='失败停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
log(msg)
li.remove(procid[i])
num.remove(num[index])
else:
num[index][1]=num[index][1]+1
elif procname[i].capitalize()==stopproc.capitalize() and procid[i] in li:
index=li.index(procid[i])
li.remove(procid[i])
num.remove(num[index])
i+=1
time.sleep(killwait)
except:
## traceback.print_exc()
msg='杀进程线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'杀进程程序')
def main():
global logfile, logfp
print 'syscheck version:',syscheck_version
timecyc=smlib.getConfigStr(STPROFILE,'time','cyc','3')
timewait=smlib.getConfigInt(STPROFILE,'time','wait',120)
ptimewait=smlib.getConfigInt(STPROFILE,'ptime','wait',120)
diskwait=smlib.getConfigInt(STPROFILE,'disk','wait',5)*60
killwait=smlib.getConfigInt(STPROFILE,'kill','wait',5)
sysflag=smlib.getConfigInt(STPROFILE,'sys','flag',1)
diskflag=smlib.getConfigInt(STPROFILE,'disk','flag',1)
pmailflag=smlib.getConfigInt(STPROFILE,'pmail','flag',1)
killflag=smlib.getConfigInt(STPROFILE,'kill','flag',1)
logfile='sysstatus.log'
timecycf=float(timecyc)*3600
timescyc=int(timecycf/10)
timecount=0
try:
if sysflag:
thread.start_new_thread(usingCheck,(timewait,))
if diskflag:
thread.start_new_thread(diskcheck,(diskwait,))
if pmailflag:
thread.start_new_thread(pmailstatusCheck,(ptimewait,))
if killflag:
thread.start_new_thread(kill,(killwait,))
except:
msg='主线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
while (1):
threadnum=len(threaddoing)
if threadnum:
print '当前运行中的线程:',
else:
print '当前运行中的线程:'
try:
i=0
while i<threadnum:
if i==threadnum-1:
print threaddoing[i]
else:
print threaddoing[i],
i+=1
except:
pass
timecount+=1
if timecount==timescyc:
return
time.sleep(10)
if __name__=='__main__':main()
if logfp:
logfp.close()
(完)
modifythreaddoing(1,'杀进程程序')
try:
cpualllimit=smlib.getConfigInt(STPROFILE,'cpuall','limit',90) #总的CPU使用率上限
cpulimit=smlib.getConfigInt(STPROFILE,'cpu','limit',45) #单个进程cpu使用率上限
memolimit=smlib.getConfigInt(STPROFILE,'memo','limit',500) #物理内存使用上限,单位(M)
fmemolimit=smlib.getConfigInt(STPROFILE,'fmemo','limit',500) #虚拟内存使用上限,单位(M)
proccyc=smlib.getConfigInt(STPROFILE,'proc','cyc',3) #对该进程连续检测几次后还有问题则杀掉
stopproc=smlib.getConfigStr(STPROFILE,'stop','proc','pythonw.exe') #要停止的进程名
li=[]
num=[]
while 1:
ret=getcompproc()
cpuused=GetCpuState()
procname=ret[0].split(',')
procid=ret[1].split(',')
memoli=ret[2].split(',')
fmemoli=ret[3].split(',')
cpuli=ret[4].split(',')
i=0
while i<len(procname):
if procname[i][-1]=='!':
procname.remove(procname[i])
procid.remove(procid[i])
i+=1
print 'li:',li
print 'num:',num
print '\n'
i=0
while i<len(memoli):
j=0
while j<len(li):
if li[j] not in procid:
li.remove(li[j])
num.remove(num[j])
j+=1
## if procname[i].capitalize()==stopproc.capitalize() and int(cpuused)>cpualllimit and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024) and int(cpuli[i])>cpulimit:
if procname[i].capitalize()==stopproc.capitalize() and (int(memoli[i])>memolimit*1024 or int(fmemoli[i])>fmemolimit*1024):
if procid[i] not in li:
li.append(procid[i])
num.append([procid[i],0])
else:
index=li.index(procid[i])
if num[index][1]>proccyc:
ret=stopprocess(int(procid[i]))
if ret:
msg='成功停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
else:
msg='失败停止进程%s,系统cpu%s,cpu占用%s,物理内存占用%sK,虚拟内存占用%sK--%s'%(procname[i],cpuused,cpuli[i],memoli[i],fmemoli[i],getNowTime())
log(msg)
li.remove(procid[i])
num.remove(num[index])
else:
num[index][1]=num[index][1]+1
elif procname[i].capitalize()==stopproc.capitalize() and procid[i] in li:
index=li.index(procid[i])
li.remove(procid[i])
num.remove(num[index])
i+=1
time.sleep(killwait)
except:
## traceback.print_exc()
msg='杀进程线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
modifythreaddoing(-1,'杀进程程序')
def main():
global logfile, logfp
print 'syscheck version:',syscheck_version
timecyc=smlib.getConfigStr(STPROFILE,'time','cyc','3')
timewait=smlib.getConfigInt(STPROFILE,'time','wait',120)
ptimewait=smlib.getConfigInt(STPROFILE,'ptime','wait',120)
diskwait=smlib.getConfigInt(STPROFILE,'disk','wait',5)*60
killwait=smlib.getConfigInt(STPROFILE,'kill','wait',5)
sysflag=smlib.getConfigInt(STPROFILE,'sys','flag',1)
diskflag=smlib.getConfigInt(STPROFILE,'disk','flag',1)
pmailflag=smlib.getConfigInt(STPROFILE,'pmail','flag',1)
killflag=smlib.getConfigInt(STPROFILE,'kill','flag',1)
logfile='sysstatus.log'
timecycf=float(timecyc)*3600
timescyc=int(timecycf/10)
timecount=0
try:
if sysflag:
thread.start_new_thread(usingCheck,(timewait,))
if diskflag:
thread.start_new_thread(diskcheck,(diskwait,))
if pmailflag:
thread.start_new_thread(pmailstatusCheck,(ptimewait,))
if killflag:
thread.start_new_thread(kill,(killwait,))
except:
msg='主线程产生一异常!--%s'%getNowTime()
print msg
log(msg)
while 1:
if (file_lock.acquire(0)):
traceback.print_exc(file=open('sysstatus.log','a'))
file_lock.release()
break
else:
time.sleep(1)
while (1):
threadnum=len(threaddoing)
if threadnum:
print '当前运行中的线程:',
else:
print '当前运行中的线程:'
try:
i=0
while i<threadnum:
if i==threadnum-1:
print threaddoing[i]
else:
print threaddoing[i],
i+=1
except:
pass
timecount+=1
if timecount==timescyc:
return
time.sleep(10)
if __name__=='__main__':main()
if logfp:
logfp.close()
(完)
#6
好东东....
#7
这个程序是从哪儿找到的?里面用到了好几个模块,比如smlib,netman等等,但是在这里面却又没有这些模块的代码,我想要知道这几个模块的代码在哪儿,谢谢
#8
呵呵,谢了
#9
嘿嘿,关于上面的模块,你可以到mail.trasin.net/soft看看
那里面还有很多别的PYTHON程序哦
那里面还有很多别的PYTHON程序哦
#10
找了一圈没有找到.看到有个python的目录,可进去了也没有找到那上面的几个模块,倒是python的其他的相关软件都有
#11
都是老的版本的,没什么用
#12
这CSDN什么时候能对代码格式支持一下?还好意思说是中国最大的软件论坛呢.
#13
回复人: wfh_178() ( )
都是老的版本的,没什么
老版本好啊!要不你把新版本放上来好了
都是老的版本的,没什么
老版本好啊!要不你把新版本放上来好了
#14
mark