from OFS.SimpleItem import SimpleItem
from Globals import package_home
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
import os
import os.path
import time
module_list=['33335','22222','ff']
grepString="DragThing"
def zptFile(self, path, orphaned=False):
"""returns a page template file from the product"""
if orphaned:
# unusual case
pt=PageTemplateFile(os.path.join(package_home(globals()), path))
else:
pt=PageTemplateFile(os.path.join(package_home(globals()), path)).__of__(self)
return pt
def getLastLine(lines):
"""letzte"""
length=len(lines)
if length>0:
return lines[length-1]
else:
return None
def checkFormat(line,formats):
"""checke formats"""
splitted=line.split("GET")
splitted=splitted[1].split()
splitted2=splitted[0].split("/")
if splitted2[4][0:3] in formats:
return True
else:
return False
def cmpLine(line):
"""gibt minuten differenz der letzen beiden eintraege"""
tmp=line.split("[")
tmp=tmp[1].split("]")
timestr=tmp[0]
split=timestr.split(":")
time1=int(split[1])*60+int(split[2])
localtime=time.asctime(time.localtime())
split=localtime.split(":")
time2=int(split[0].split()[3])*60+int(split[1])
return time2-time1
class checkStation(SimpleItem):
"""test ob station lebt"""
meta_type="check Media Stations"
def asctime(self):
return time.asctime(tm)
def __init__(self, id, path):
"""init"""
self.path=path #pfad fuer logfile
self.id=id
self.modulOverview={}
def checkStation(self,nr,time=5,formats=["VID"]):
"""check station with nr nr"""
txt=[]
txtFh = os.popen("grep /%s/ %s | tail "%(nr,self.path))
for line in txtFh.readlines():
txt.append(line)
line=getLastLine(txt)
if not line:
return True,"",0
cmpT=cmpLine(line)
if not checkFormat(line,formats):
# teste ob letzte Zeile vom format in formats
return True,line,cmpT
if cmpT > time:
return False,line,cmpT
else:
return True,line,cmpT
def getModuls(self):
"""getModuls"""
return module_list
def overviewStations(self,time=5):
"""overview"""
self.REQUEST.SESSION['outTime']=time
zp=zptFile(self,"zpt/overview.zpt")
return zp()
ips={"22222":"127.0.0.1","33335":"127.0.0.2"}
def rebootStation(self,modul):
"""neustart"""
ip=self.ips[modul]
str="""ssh root@%s "ps -xa | grep %s" """
rets=os.popen(str%(ip,grepString)).readlines()
for ret in rets:
if ret.find("grep") <= 0:
process=ret.split()[0]
str="""ssh root@%s kill -9 %s """
rets=os.popen(str%(ip,process)).readlines()
print "WW",str%(ip,process)
def checkRebootDead(self,timer=5):
"""checks and reboots non responding firefox"""
self.lastCheck=time.localtime()
for modul in self.getModuls():
checked=self.checkStation(modul,time=timer,formats=["VID"])
if not checked[0]:
self.rebootStation(modul)
if not hasattr(self,'modulOverview'):
setattr(self,'modulOverview',{})
self.modulOverview[modul]=time.localtime()
def rebootOverview(self):
"""give Overview"""
pt=zptFile(self,'zpt/rebootOverview.zpt')
return pt()
def changeCheckStationForm(self):
"""form for change"""
pt=zptFile(self, 'zpt/ChangeCheckStation.zpt')
return pt()
def changeCheckStation(self,path,RESPONSE=None):
"""change"""
self.path=path
if RESPONSE:
RESPONSE.redirect("manage_main")
def manage_addCheckStationForm(self):
"""add"""
pt=zptFile(self,'zpt/AddCheckStation')
return pt()
def manage_addCheckStation(self,id,path,RESPONSE=None):
"""manage checkstation"""
newObj=checkStation(id,path)
self._setObject(id,newObj)
if RESPONSE is not None:
RESPONSE.redirect('manage_main')
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>